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. 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";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
41 #include <arpa/inet.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/ip.h>
45 #include <netinet/ip_icmp.h>
53 #include <protocols/timed.h>
56 #define SECDAY (24*SECHR)
58 # define DATE_PROTO "udp"
59 # define DATE_PORT "time"
64 char myname[MAXHOSTNAMELEN];
66 struct sockaddr_in server;
67 struct sockaddr_in dayaddr;
68 extern int measure_delta;
70 void bytenetorder(struct tsp *);
71 void bytehostorder(struct tsp *);
74 #define BU (2208988800UL) /* seconds before UNIX epoch */
77 /* compute the difference between our date and another machine
79 static int /* difference in days from our time */
80 daydiff(char *hostname)
84 struct timeval tout, now;
91 /* wait 2 seconds between 10 tries */
94 for (trials = 0; trials < 10; trials++) {
95 /* ask for the time */
97 if (sendto(sock, &sec, sizeof(sec), 0,
98 (struct sockaddr*)&dayaddr, sizeof(dayaddr)) < 0) {
105 FD_SET(sock, &ready);
106 i = select(sock+1, &ready, (fd_set *)0,
111 warn("select(date read)");
117 fromlen = sizeof(from);
118 if (recvfrom(sock,&sec,sizeof(sec),0,
119 &from,&fromlen) < 0) {
120 warn("recvfrom(date read)");
126 warnx("%s says it is before 1970: %lu",
132 (void)gettimeofday(&now, NULL);
133 return (sec - now.tv_sec);
137 /* if we get here, we tried too many times */
138 warnx("%s will not tell us the date", hostname);
144 * Clockdiff computes the difference between the time of the machine on
145 * which it is called and the time of the machines given as argument.
146 * The time differences measured by clockdiff are obtained using a sequence
147 * of ICMP TSTAMP messages which are returned to the sender by the IP module
148 * in the remote machine.
149 * In order to compare clocks of machines in different time zones, the time
150 * is transmitted (as a 32-bit value) in milliseconds since midnight UT.
151 * If a hosts uses a different time format, it should set the high order
152 * bit of the 32-bit quantity it transmits.
153 * However, VMS apparently transmits the time in milliseconds since midnight
154 * local time (rather than GMT) without setting the high order bit.
155 * Furthermore, it does not understand daylight-saving time. This makes
156 * clockdiff behaving inconsistently with hosts running VMS.
158 * In order to reduce the sensitivity to the variance of message transmission
159 * time, clockdiff sends a sequence of messages. Yet, measures between
160 * two `distant' hosts can be affected by a small error. The error can,
161 * however, be reduced by increasing the number of messages sent in each
165 clockdiff(int argc, char *argv[])
168 extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
169 register int avg_cnt;
174 printf("usage: timedc clockdiff host ...\n");
178 if (gethostname(myname, sizeof(myname) - 1) < 0)
179 err(1, "gethostname");
181 /* get the address for the date ready */
182 sp = getservbyname(DATE_PORT, DATE_PROTO);
184 warnx("%s/%s: unknown service", DATE_PORT, DATE_PROTO);
185 dayaddr.sin_port = 0;
187 dayaddr.sin_port = sp->s_port;
192 hp = gethostbyname(*argv);
194 warnx("%s: %s", *argv, hstrerror(h_errno));
198 server.sin_family = hp->h_addrtype;
199 bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
200 for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
201 measure_status = measure(10000,100, *argv, &server, 1);
202 if (measure_status != GOOD)
204 avg += measure_delta;
206 if (measure_status == GOOD)
207 measure_delta = avg/avg_cnt;
209 switch (measure_status) {
211 printf("%s is down\n", hp->h_name);
214 printf("%s transmits a non-standard time format\n",
218 printf("%s is unreachable\n", hp->h_name);
223 * Try to get the date only after using ICMP timestamps to
224 * get the time. This is because the date protocol
227 if (dayaddr.sin_port != 0) {
228 dayaddr.sin_family = hp->h_addrtype;
229 bcopy(hp->h_addr, &dayaddr.sin_addr.s_addr,
231 avg = daydiff(*argv);
233 printf("time on %s is %ld days ahead %s\n",
234 hp->h_name, avg/SECDAY, myname);
236 } else if (avg < -SECDAY) {
237 printf("time on %s is %ld days behind %s\n",
238 hp->h_name, -avg/SECDAY, myname);
243 if (measure_delta > 0) {
244 printf("time on %s is %d ms. ahead of time on %s\n",
245 hp->h_name, measure_delta, myname);
246 } else if (measure_delta == 0) {
247 printf("%s and %s have the same time\n",
250 printf("time on %s is %d ms. behind time on %s\n",
251 hp->h_name, -measure_delta, myname);
259 * finds location of master timedaemon
262 msite(int argc, char *argv[])
266 struct sockaddr_in dest;
268 struct sockaddr_in from;
271 struct servent *srvp;
275 printf("usage: timedc msite [host ...]\n");
279 srvp = getservbyname("timed", "udp");
281 warnx("timed/udp: unknown service");
284 dest.sin_port = srvp->s_port;
285 dest.sin_family = AF_INET;
287 if (gethostname(myname, sizeof(myname) - 1) < 0)
288 err(1, "gethostname");
291 tgtname = (i >= argc) ? myname : argv[i];
292 hp = gethostbyname(tgtname);
294 warnx("%s: %s", tgtname, hstrerror(h_errno));
297 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
299 (void)strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
300 msg.tsp_type = TSP_MSITE;
301 msg.tsp_vers = TSPVERSION;
303 if (sendto(sock, &msg, sizeof(struct tsp), 0,
304 (struct sockaddr*)&dest,
305 sizeof(struct sockaddr)) < 0) {
313 FD_SET(sock, &ready);
314 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0,
316 length = sizeof(from);
317 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
318 (struct sockaddr *)&from, &length);
324 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
325 * this is still OS-dependent. Demand that the packet is at
326 * least long enough to hold a 4.3BSD packet.
328 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
330 "short packet (%zd/%zu bytes) from %s\n",
331 cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
332 inet_ntoa(from.sin_addr));
336 if (msg.tsp_type == TSP_ACK) {
337 printf("master timedaemon at %s is %s\n",
338 tgtname, msg.tsp_name);
340 if (msg.tsp_type >= TSPTYPENUMBER)
341 printf("unknown ack received: %u\n",
344 printf("wrong ack received: %s\n",
345 tsptype[msg.tsp_type]);
348 printf("communication error with %s\n", tgtname);
350 } while (++i < argc);
364 * Causes the election timer to expire on the selected hosts
365 * It sends just one udp message per machine, relying on
366 * reliability of communication channel.
369 testing(int argc, char *argv[])
371 struct servent *srvp;
372 struct sockaddr_in sin;
376 printf("usage: timedc election host1 [host2 ...]\n");
380 srvp = getservbyname("timed", "udp");
382 warnx("timed/udp: unknown service");
388 hp = gethostbyname(*argv);
390 warnx("%s: %s", *argv, hstrerror(h_errno));
394 sin.sin_port = srvp->s_port;
395 sin.sin_family = hp->h_addrtype;
396 bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length);
398 msg.tsp_type = TSP_TEST;
399 msg.tsp_vers = TSPVERSION;
400 if (gethostname(myname, sizeof(myname) - 1) < 0)
401 err(1, "gethostname");
402 (void)strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
404 if (sendto(sock, &msg, sizeof(struct tsp), 0,
405 (struct sockaddr*)&sin,
406 sizeof(struct sockaddr)) < 0) {
414 * Enables or disables tracing on local timedaemon
417 tracing(int argc, char *argv[])
423 struct sockaddr_in dest;
424 struct sockaddr_in from;
427 struct servent *srvp;
430 printf("usage: timedc trace { on | off }\n");
434 srvp = getservbyname("timed", "udp");
436 warnx("timed/udp: unknown service");
439 dest.sin_port = srvp->s_port;
440 dest.sin_family = AF_INET;
442 if (gethostname(myname, sizeof(myname) - 1) < 0)
443 err(1, "gethostname");
444 hp = gethostbyname(myname);
445 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
447 if (strcmp(argv[1], "on") == 0) {
448 msg.tsp_type = TSP_TRACEON;
451 msg.tsp_type = TSP_TRACEOFF;
455 (void)strcpy(msg.tsp_name, myname);
456 msg.tsp_vers = TSPVERSION;
458 if (sendto(sock, &msg, sizeof(struct tsp), 0,
459 (struct sockaddr*)&dest, sizeof(struct sockaddr)) < 0) {
467 FD_SET(sock, &ready);
468 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout)) {
469 length = sizeof(from);
470 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
471 (struct sockaddr *)&from, &length);
477 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
478 * this is still OS-dependent. Demand that the packet is at
479 * least long enough to hold a 4.3BSD packet.
481 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
482 fprintf(stderr, "short packet (%zd/%zu bytes) from %s\n",
483 cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
484 inet_ntoa(from.sin_addr));
488 if (msg.tsp_type == TSP_ACK)
490 printf("timed tracing enabled\n");
492 printf("timed tracing disabled\n");
494 if (msg.tsp_type >= TSPTYPENUMBER)
495 printf("unknown ack received: %u\n",
498 printf("wrong ack received: %s\n",
499 tsptype[msg.tsp_type]);
502 printf("communication error\n");
509 struct sockaddr_in sin;
511 sock = socket(AF_INET, SOCK_DGRAM, 0);
513 warn("opening socket");
517 sin.sin_family = AF_INET;
518 sin.sin_addr.s_addr = 0;
519 for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
520 sin.sin_port = htons((u_short)port);
521 if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0)
523 if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
529 if (port == IPPORT_RESERVED / 2) {
530 warnx("all reserved ports in use");
535 sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
537 warn("opening raw socket");