2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)cmds.c 8.1 (Berkeley) 6/6/93";
38 static const char rcsid[] =
39 "$FreeBSD: src/usr.sbin/timed/timedc/cmds.c,v 1.6.2.2 2001/08/31 08:02:06 kris Exp $";
45 #include <netinet/in_systm.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip_icmp.h>
55 #include <protocols/timed.h>
59 #include <sys/clock.h>
62 #define SECDAY (24*SECHR)
65 # define DATE_PROTO "udp"
66 # define DATE_PORT "time"
71 char myname[MAXHOSTNAMELEN];
73 struct sockaddr_in server;
74 struct sockaddr_in dayaddr;
75 extern int measure_delta;
77 void bytenetorder(struct tsp *);
78 void bytehostorder(struct tsp *);
81 #define BU (2208988800UL) /* seconds before UNIX epoch */
84 /* compute the difference between our date and another machine
86 static int /* difference in days from our time */
92 struct timeval tout, now;
99 /* wait 2 seconds between 10 tries */
102 for (trials = 0; trials < 10; trials++) {
103 /* ask for the time */
105 if (sendto(sock, &sec, sizeof(sec), 0,
106 (struct sockaddr*)&dayaddr, sizeof(dayaddr)) < 0) {
107 warn("sendto(sock)");
113 FD_SET(sock, &ready);
114 i = select(sock+1, &ready, (fd_set *)0,
119 warn("select(date read)");
125 fromlen = sizeof(from);
126 if (recvfrom(sock,&sec,sizeof(sec),0,
127 &from,&fromlen) < 0) {
128 warn("recvfrom(date read)");
134 warnx("%s says it is before 1970: %lu",
140 (void)gettimeofday(&now, (struct timezone*)0);
141 return (sec - now.tv_sec);
145 /* if we get here, we tried too many times */
146 warnx("%s will not tell us the date", hostname);
152 * Clockdiff computes the difference between the time of the machine on
153 * which it is called and the time of the machines given as argument.
154 * The time differences measured by clockdiff are obtained using a sequence
155 * of ICMP TSTAMP messages which are returned to the sender by the IP module
156 * in the remote machine.
157 * In order to compare clocks of machines in different time zones, the time
158 * is transmitted (as a 32-bit value) in milliseconds since midnight UT.
159 * If a hosts uses a different time format, it should set the high order
160 * bit of the 32-bit quantity it transmits.
161 * However, VMS apparently transmits the time in milliseconds since midnight
162 * local time (rather than GMT) without setting the high order bit.
163 * Furthermore, it does not understand daylight-saving time. This makes
164 * clockdiff behaving inconsistently with hosts running VMS.
166 * In order to reduce the sensitivity to the variance of message transmission
167 * time, clockdiff sends a sequence of messages. Yet, measures between
168 * two `distant' hosts can be affected by a small error. The error can,
169 * however, be reduced by increasing the number of messages sent in each
173 clockdiff(argc, argv)
178 extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
179 register int avg_cnt;
184 printf("usage: timedc clockdiff host ...\n");
188 if (gethostname(myname, sizeof(myname) - 1) < 0)
189 err(1, "gethostname");
191 /* get the address for the date ready */
192 sp = getservbyname(DATE_PORT, DATE_PROTO);
194 warnx("%s/%s is an unknown service", DATE_PORT, DATE_PROTO);
195 dayaddr.sin_port = 0;
197 dayaddr.sin_port = sp->s_port;
202 hp = gethostbyname(*argv);
204 warnx("%s: %s", *argv, hstrerror(h_errno));
208 server.sin_family = hp->h_addrtype;
209 bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
210 for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
211 measure_status = measure(10000,100, *argv, &server, 1);
212 if (measure_status != GOOD)
214 avg += measure_delta;
216 if (measure_status == GOOD)
217 measure_delta = avg/avg_cnt;
219 switch (measure_status) {
221 printf("%s is down\n", hp->h_name);
224 printf("%s transmitts a non-standard time format\n",
228 printf("%s is unreachable\n", hp->h_name);
233 * Try to get the date only after using ICMP timestamps to
234 * get the time. This is because the date protocol
237 if (dayaddr.sin_port != 0) {
238 dayaddr.sin_family = hp->h_addrtype;
239 bcopy(hp->h_addr, &dayaddr.sin_addr.s_addr,
241 avg = daydiff(*argv);
243 printf("time on %s is %ld days ahead %s\n",
244 hp->h_name, avg/SECDAY, myname);
246 } else if (avg < -SECDAY) {
247 printf("time on %s is %ld days behind %s\n",
248 hp->h_name, -avg/SECDAY, myname);
253 if (measure_delta > 0) {
254 printf("time on %s is %d ms. ahead of time on %s\n",
255 hp->h_name, measure_delta, myname);
256 } else if (measure_delta == 0) {
257 printf("%s and %s have the same time\n",
260 printf("time on %s is %d ms. behind time on %s\n",
261 hp->h_name, -measure_delta, myname);
269 * finds location of master timedaemon
278 struct sockaddr_in dest;
280 struct sockaddr_in from;
283 struct servent *srvp;
287 printf("usage: timedc msite [host ...]\n");
291 srvp = getservbyname("timed", "udp");
293 warnx("udp/timed: unknown service");
296 dest.sin_port = srvp->s_port;
297 dest.sin_family = AF_INET;
299 if (gethostname(myname, sizeof(myname) - 1) < 0)
300 err(1, "gethostname");
303 tgtname = (i >= argc) ? myname : argv[i];
304 hp = gethostbyname(tgtname);
306 warnx("%s: %s", tgtname, hstrerror(h_errno));
309 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
311 (void)strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
312 msg.tsp_type = TSP_MSITE;
313 msg.tsp_vers = TSPVERSION;
315 if (sendto(sock, &msg, sizeof(struct tsp), 0,
316 (struct sockaddr*)&dest,
317 sizeof(struct sockaddr)) < 0) {
325 FD_SET(sock, &ready);
326 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0,
328 length = sizeof(from);
329 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
330 (struct sockaddr *)&from, &length);
336 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
337 * this is still OS-dependent. Demand that the packet is at
338 * least long enough to hold a 4.3BSD packet.
340 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
342 "short packet (%u/%u bytes) from %s\n",
343 cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
344 inet_ntoa(from.sin_addr));
348 if (msg.tsp_type == TSP_ACK) {
349 printf("master timedaemon at %s is %s\n",
350 tgtname, msg.tsp_name);
352 if (msg.tsp_type >= TSPTYPENUMBER)
353 printf("unknown ack received: %u\n",
356 printf("wrong ack received: %s\n",
357 tsptype[msg.tsp_type]);
360 printf("communication error with %s\n", tgtname);
362 } while (++i < argc);
376 * Causes the election timer to expire on the selected hosts
377 * It sends just one udp message per machine, relying on
378 * reliability of communication channel.
385 struct servent *srvp;
386 struct sockaddr_in sin;
390 printf("usage: timedc election host1 [host2 ...]\n");
394 srvp = getservbyname("timed", "udp");
396 warnx("udp/timed: unknown service");
402 hp = gethostbyname(*argv);
404 warnx("%s: %s", *argv, hstrerror(h_errno));
408 sin.sin_port = srvp->s_port;
409 sin.sin_family = hp->h_addrtype;
410 bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length);
412 msg.tsp_type = TSP_TEST;
413 msg.tsp_vers = TSPVERSION;
414 if (gethostname(myname, sizeof(myname) - 1) < 0)
415 err(1, "gethostname");
416 (void)strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
418 if (sendto(sock, &msg, sizeof(struct tsp), 0,
419 (struct sockaddr*)&sin,
420 sizeof(struct sockaddr)) < 0) {
428 * Enables or disables tracing on local timedaemon
439 struct sockaddr_in dest;
440 struct sockaddr_in from;
443 struct servent *srvp;
446 printf("usage: timedc trace { on | off }\n");
450 srvp = getservbyname("timed", "udp");
452 warnx("udp/timed: unknown service");
455 dest.sin_port = srvp->s_port;
456 dest.sin_family = AF_INET;
458 if (gethostname(myname, sizeof(myname) - 1) < 0)
459 err(1, "gethostname");
460 hp = gethostbyname(myname);
461 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
463 if (strcmp(argv[1], "on") == 0) {
464 msg.tsp_type = TSP_TRACEON;
467 msg.tsp_type = TSP_TRACEOFF;
471 (void)strcpy(msg.tsp_name, myname);
472 msg.tsp_vers = TSPVERSION;
474 if (sendto(sock, &msg, sizeof(struct tsp), 0,
475 (struct sockaddr*)&dest, sizeof(struct sockaddr)) < 0) {
483 FD_SET(sock, &ready);
484 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout)) {
485 length = sizeof(from);
486 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
487 (struct sockaddr *)&from, &length);
493 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
494 * this is still OS-dependent. Demand that the packet is at
495 * least long enough to hold a 4.3BSD packet.
497 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
498 fprintf(stderr, "short packet (%u/%u bytes) from %s\n",
499 cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
500 inet_ntoa(from.sin_addr));
504 if (msg.tsp_type == TSP_ACK)
506 printf("timed tracing enabled\n");
508 printf("timed tracing disabled\n");
510 if (msg.tsp_type >= TSPTYPENUMBER)
511 printf("unknown ack received: %u\n",
514 printf("wrong ack received: %s\n",
515 tsptype[msg.tsp_type]);
518 printf("communication error\n");
525 struct sockaddr_in sin;
527 sock = socket(AF_INET, SOCK_DGRAM, 0);
529 warn("opening socket");
533 sin.sin_family = AF_INET;
534 sin.sin_addr.s_addr = 0;
535 for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
536 sin.sin_port = htons((u_short)port);
537 if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0)
539 if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
545 if (port == IPPORT_RESERVED / 2) {
546 warnx("all reserved ports in use");
551 sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
553 warn("opening raw socket");