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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)cmds.c 8.1 (Berkeley) 6/6/93";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
42 #include <arpa/inet.h>
44 #include <netinet/in_systm.h>
45 #include <netinet/ip.h>
46 #include <netinet/ip_icmp.h>
54 #include <protocols/timed.h>
57 #define SECDAY (24*SECHR)
59 # define DATE_PROTO "udp"
60 # define DATE_PORT "time"
65 char myname[MAXHOSTNAMELEN];
67 struct sockaddr_in server;
68 struct sockaddr_in dayaddr;
69 extern int measure_delta;
71 void bytenetorder(struct tsp *);
72 void bytehostorder(struct tsp *);
75 #define BU (2208988800UL) /* seconds before UNIX epoch */
78 /* compute the difference between our date and another machine
80 static int /* difference in days from our time */
86 struct timeval tout, now;
93 /* wait 2 seconds between 10 tries */
96 for (trials = 0; trials < 10; trials++) {
97 /* ask for the time */
99 if (sendto(sock, &sec, sizeof(sec), 0,
100 (struct sockaddr*)&dayaddr, sizeof(dayaddr)) < 0) {
101 warn("sendto(sock)");
107 FD_SET(sock, &ready);
108 i = select(sock+1, &ready, (fd_set *)0,
113 warn("select(date read)");
119 fromlen = sizeof(from);
120 if (recvfrom(sock,&sec,sizeof(sec),0,
121 &from,&fromlen) < 0) {
122 warn("recvfrom(date read)");
128 warnx("%s says it is before 1970: %lu",
134 (void)gettimeofday(&now, NULL);
135 return (sec - now.tv_sec);
139 /* if we get here, we tried too many times */
140 warnx("%s will not tell us the date", hostname);
146 * Clockdiff computes the difference between the time of the machine on
147 * which it is called and the time of the machines given as argument.
148 * The time differences measured by clockdiff are obtained using a sequence
149 * of ICMP TSTAMP messages which are returned to the sender by the IP module
150 * in the remote machine.
151 * In order to compare clocks of machines in different time zones, the time
152 * is transmitted (as a 32-bit value) in milliseconds since midnight UT.
153 * If a hosts uses a different time format, it should set the high order
154 * bit of the 32-bit quantity it transmits.
155 * However, VMS apparently transmits the time in milliseconds since midnight
156 * local time (rather than GMT) without setting the high order bit.
157 * Furthermore, it does not understand daylight-saving time. This makes
158 * clockdiff behaving inconsistently with hosts running VMS.
160 * In order to reduce the sensitivity to the variance of message transmission
161 * time, clockdiff sends a sequence of messages. Yet, measures between
162 * two `distant' hosts can be affected by a small error. The error can,
163 * however, be reduced by increasing the number of messages sent in each
167 clockdiff(argc, argv)
172 extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
173 register int avg_cnt;
178 printf("usage: timedc clockdiff host ...\n");
182 if (gethostname(myname, sizeof(myname) - 1) < 0)
183 err(1, "gethostname");
185 /* get the address for the date ready */
186 sp = getservbyname(DATE_PORT, DATE_PROTO);
188 warnx("%s/%s: unknown service", DATE_PORT, DATE_PROTO);
189 dayaddr.sin_port = 0;
191 dayaddr.sin_port = sp->s_port;
196 hp = gethostbyname(*argv);
198 warnx("%s: %s", *argv, hstrerror(h_errno));
202 server.sin_family = hp->h_addrtype;
203 bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
204 for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
205 measure_status = measure(10000,100, *argv, &server, 1);
206 if (measure_status != GOOD)
208 avg += measure_delta;
210 if (measure_status == GOOD)
211 measure_delta = avg/avg_cnt;
213 switch (measure_status) {
215 printf("%s is down\n", hp->h_name);
218 printf("%s transmits a non-standard time format\n",
222 printf("%s is unreachable\n", hp->h_name);
227 * Try to get the date only after using ICMP timestamps to
228 * get the time. This is because the date protocol
231 if (dayaddr.sin_port != 0) {
232 dayaddr.sin_family = hp->h_addrtype;
233 bcopy(hp->h_addr, &dayaddr.sin_addr.s_addr,
235 avg = daydiff(*argv);
237 printf("time on %s is %ld days ahead %s\n",
238 hp->h_name, avg/SECDAY, myname);
240 } else if (avg < -SECDAY) {
241 printf("time on %s is %ld days behind %s\n",
242 hp->h_name, -avg/SECDAY, myname);
247 if (measure_delta > 0) {
248 printf("time on %s is %d ms. ahead of time on %s\n",
249 hp->h_name, measure_delta, myname);
250 } else if (measure_delta == 0) {
251 printf("%s and %s have the same time\n",
254 printf("time on %s is %d ms. behind time on %s\n",
255 hp->h_name, -measure_delta, myname);
263 * finds location of master timedaemon
272 struct sockaddr_in dest;
274 struct sockaddr_in from;
277 struct servent *srvp;
281 printf("usage: timedc msite [host ...]\n");
285 srvp = getservbyname("timed", "udp");
287 warnx("timed/udp: unknown service");
290 dest.sin_port = srvp->s_port;
291 dest.sin_family = AF_INET;
293 if (gethostname(myname, sizeof(myname) - 1) < 0)
294 err(1, "gethostname");
297 tgtname = (i >= argc) ? myname : argv[i];
298 hp = gethostbyname(tgtname);
300 warnx("%s: %s", tgtname, hstrerror(h_errno));
303 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
305 (void)strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
306 msg.tsp_type = TSP_MSITE;
307 msg.tsp_vers = TSPVERSION;
309 if (sendto(sock, &msg, sizeof(struct tsp), 0,
310 (struct sockaddr*)&dest,
311 sizeof(struct sockaddr)) < 0) {
319 FD_SET(sock, &ready);
320 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0,
322 length = sizeof(from);
323 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
324 (struct sockaddr *)&from, &length);
330 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
331 * this is still OS-dependent. Demand that the packet is at
332 * least long enough to hold a 4.3BSD packet.
334 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
336 "short packet (%zd/%zu bytes) from %s\n",
337 cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
338 inet_ntoa(from.sin_addr));
342 if (msg.tsp_type == TSP_ACK) {
343 printf("master timedaemon at %s is %s\n",
344 tgtname, msg.tsp_name);
346 if (msg.tsp_type >= TSPTYPENUMBER)
347 printf("unknown ack received: %u\n",
350 printf("wrong ack received: %s\n",
351 tsptype[msg.tsp_type]);
354 printf("communication error with %s\n", tgtname);
356 } while (++i < argc);
370 * Causes the election timer to expire on the selected hosts
371 * It sends just one udp message per machine, relying on
372 * reliability of communication channel.
379 struct servent *srvp;
380 struct sockaddr_in sin;
384 printf("usage: timedc election host1 [host2 ...]\n");
388 srvp = getservbyname("timed", "udp");
390 warnx("timed/udp: unknown service");
396 hp = gethostbyname(*argv);
398 warnx("%s: %s", *argv, hstrerror(h_errno));
402 sin.sin_port = srvp->s_port;
403 sin.sin_family = hp->h_addrtype;
404 bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length);
406 msg.tsp_type = TSP_TEST;
407 msg.tsp_vers = TSPVERSION;
408 if (gethostname(myname, sizeof(myname) - 1) < 0)
409 err(1, "gethostname");
410 (void)strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
412 if (sendto(sock, &msg, sizeof(struct tsp), 0,
413 (struct sockaddr*)&sin,
414 sizeof(struct sockaddr)) < 0) {
422 * Enables or disables tracing on local timedaemon
433 struct sockaddr_in dest;
434 struct sockaddr_in from;
437 struct servent *srvp;
440 printf("usage: timedc trace { on | off }\n");
444 srvp = getservbyname("timed", "udp");
446 warnx("timed/udp: unknown service");
449 dest.sin_port = srvp->s_port;
450 dest.sin_family = AF_INET;
452 if (gethostname(myname, sizeof(myname) - 1) < 0)
453 err(1, "gethostname");
454 hp = gethostbyname(myname);
455 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
457 if (strcmp(argv[1], "on") == 0) {
458 msg.tsp_type = TSP_TRACEON;
461 msg.tsp_type = TSP_TRACEOFF;
465 (void)strcpy(msg.tsp_name, myname);
466 msg.tsp_vers = TSPVERSION;
468 if (sendto(sock, &msg, sizeof(struct tsp), 0,
469 (struct sockaddr*)&dest, sizeof(struct sockaddr)) < 0) {
477 FD_SET(sock, &ready);
478 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout)) {
479 length = sizeof(from);
480 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
481 (struct sockaddr *)&from, &length);
487 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
488 * this is still OS-dependent. Demand that the packet is at
489 * least long enough to hold a 4.3BSD packet.
491 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
492 fprintf(stderr, "short packet (%zd/%zu bytes) from %s\n",
493 cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
494 inet_ntoa(from.sin_addr));
498 if (msg.tsp_type == TSP_ACK)
500 printf("timed tracing enabled\n");
502 printf("timed tracing disabled\n");
504 if (msg.tsp_type >= TSPTYPENUMBER)
505 printf("unknown ack received: %u\n",
508 printf("wrong ack received: %s\n",
509 tsptype[msg.tsp_type]);
512 printf("communication error\n");
519 struct sockaddr_in sin;
521 sock = socket(AF_INET, SOCK_DGRAM, 0);
523 warn("opening socket");
527 sin.sin_family = AF_INET;
528 sin.sin_addr.s_addr = 0;
529 for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
530 sin.sin_port = htons((u_short)port);
531 if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0)
533 if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
539 if (port == IPPORT_RESERVED / 2) {
540 warnx("all reserved ports in use");
545 sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
547 warn("opening raw socket");