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[] =
45 #include <arpa/inet.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/ip.h>
49 #include <netinet/ip_icmp.h>
57 #include <protocols/timed.h>
60 #define SECDAY (24*SECHR)
62 # define DATE_PROTO "udp"
63 # define DATE_PORT "time"
68 char myname[MAXHOSTNAMELEN];
70 struct sockaddr_in server;
71 struct sockaddr_in dayaddr;
72 extern int measure_delta;
74 void bytenetorder(struct tsp *);
75 void bytehostorder(struct tsp *);
78 #define BU (2208988800UL) /* seconds before UNIX epoch */
81 /* compute the difference between our date and another machine
83 static int /* difference in days from our time */
89 struct timeval tout, now;
96 /* wait 2 seconds between 10 tries */
99 for (trials = 0; trials < 10; trials++) {
100 /* ask for the time */
102 if (sendto(sock, &sec, sizeof(sec), 0,
103 (struct sockaddr*)&dayaddr, sizeof(dayaddr)) < 0) {
104 warn("sendto(sock)");
110 FD_SET(sock, &ready);
111 i = select(sock+1, &ready, (fd_set *)0,
116 warn("select(date read)");
122 fromlen = sizeof(from);
123 if (recvfrom(sock,&sec,sizeof(sec),0,
124 &from,&fromlen) < 0) {
125 warn("recvfrom(date read)");
131 warnx("%s says it is before 1970: %lu",
137 (void)gettimeofday(&now, (struct timezone*)0);
138 return (sec - now.tv_sec);
142 /* if we get here, we tried too many times */
143 warnx("%s will not tell us the date", hostname);
149 * Clockdiff computes the difference between the time of the machine on
150 * which it is called and the time of the machines given as argument.
151 * The time differences measured by clockdiff are obtained using a sequence
152 * of ICMP TSTAMP messages which are returned to the sender by the IP module
153 * in the remote machine.
154 * In order to compare clocks of machines in different time zones, the time
155 * is transmitted (as a 32-bit value) in milliseconds since midnight UT.
156 * If a hosts uses a different time format, it should set the high order
157 * bit of the 32-bit quantity it transmits.
158 * However, VMS apparently transmits the time in milliseconds since midnight
159 * local time (rather than GMT) without setting the high order bit.
160 * Furthermore, it does not understand daylight-saving time. This makes
161 * clockdiff behaving inconsistently with hosts running VMS.
163 * In order to reduce the sensitivity to the variance of message transmission
164 * time, clockdiff sends a sequence of messages. Yet, measures between
165 * two `distant' hosts can be affected by a small error. The error can,
166 * however, be reduced by increasing the number of messages sent in each
170 clockdiff(argc, argv)
175 extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
176 register int avg_cnt;
181 printf("usage: timedc clockdiff host ...\n");
185 if (gethostname(myname, sizeof(myname) - 1) < 0)
186 err(1, "gethostname");
188 /* get the address for the date ready */
189 sp = getservbyname(DATE_PORT, DATE_PROTO);
191 warnx("%s/%s is an unknown service", DATE_PORT, DATE_PROTO);
192 dayaddr.sin_port = 0;
194 dayaddr.sin_port = sp->s_port;
199 hp = gethostbyname(*argv);
201 warnx("%s: %s", *argv, hstrerror(h_errno));
205 server.sin_family = hp->h_addrtype;
206 bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
207 for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
208 measure_status = measure(10000,100, *argv, &server, 1);
209 if (measure_status != GOOD)
211 avg += measure_delta;
213 if (measure_status == GOOD)
214 measure_delta = avg/avg_cnt;
216 switch (measure_status) {
218 printf("%s is down\n", hp->h_name);
221 printf("%s transmitts a non-standard time format\n",
225 printf("%s is unreachable\n", hp->h_name);
230 * Try to get the date only after using ICMP timestamps to
231 * get the time. This is because the date protocol
234 if (dayaddr.sin_port != 0) {
235 dayaddr.sin_family = hp->h_addrtype;
236 bcopy(hp->h_addr, &dayaddr.sin_addr.s_addr,
238 avg = daydiff(*argv);
240 printf("time on %s is %ld days ahead %s\n",
241 hp->h_name, avg/SECDAY, myname);
243 } else if (avg < -SECDAY) {
244 printf("time on %s is %ld days behind %s\n",
245 hp->h_name, -avg/SECDAY, myname);
250 if (measure_delta > 0) {
251 printf("time on %s is %d ms. ahead of time on %s\n",
252 hp->h_name, measure_delta, myname);
253 } else if (measure_delta == 0) {
254 printf("%s and %s have the same time\n",
257 printf("time on %s is %d ms. behind time on %s\n",
258 hp->h_name, -measure_delta, myname);
266 * finds location of master timedaemon
275 struct sockaddr_in dest;
277 struct sockaddr_in from;
280 struct servent *srvp;
284 printf("usage: timedc msite [host ...]\n");
288 srvp = getservbyname("timed", "udp");
290 warnx("udp/timed: unknown service");
293 dest.sin_port = srvp->s_port;
294 dest.sin_family = AF_INET;
296 if (gethostname(myname, sizeof(myname) - 1) < 0)
297 err(1, "gethostname");
300 tgtname = (i >= argc) ? myname : argv[i];
301 hp = gethostbyname(tgtname);
303 warnx("%s: %s", tgtname, hstrerror(h_errno));
306 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
308 (void)strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
309 msg.tsp_type = TSP_MSITE;
310 msg.tsp_vers = TSPVERSION;
312 if (sendto(sock, &msg, sizeof(struct tsp), 0,
313 (struct sockaddr*)&dest,
314 sizeof(struct sockaddr)) < 0) {
322 FD_SET(sock, &ready);
323 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0,
325 length = sizeof(from);
326 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
327 (struct sockaddr *)&from, &length);
333 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
334 * this is still OS-dependent. Demand that the packet is at
335 * least long enough to hold a 4.3BSD packet.
337 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
339 "short packet (%u/%u bytes) from %s\n",
340 cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
341 inet_ntoa(from.sin_addr));
345 if (msg.tsp_type == TSP_ACK) {
346 printf("master timedaemon at %s is %s\n",
347 tgtname, msg.tsp_name);
349 if (msg.tsp_type >= TSPTYPENUMBER)
350 printf("unknown ack received: %u\n",
353 printf("wrong ack received: %s\n",
354 tsptype[msg.tsp_type]);
357 printf("communication error with %s\n", tgtname);
359 } while (++i < argc);
373 * Causes the election timer to expire on the selected hosts
374 * It sends just one udp message per machine, relying on
375 * reliability of communication channel.
382 struct servent *srvp;
383 struct sockaddr_in sin;
387 printf("usage: timedc election host1 [host2 ...]\n");
391 srvp = getservbyname("timed", "udp");
393 warnx("udp/timed: unknown service");
399 hp = gethostbyname(*argv);
401 warnx("%s: %s", *argv, hstrerror(h_errno));
405 sin.sin_port = srvp->s_port;
406 sin.sin_family = hp->h_addrtype;
407 bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length);
409 msg.tsp_type = TSP_TEST;
410 msg.tsp_vers = TSPVERSION;
411 if (gethostname(myname, sizeof(myname) - 1) < 0)
412 err(1, "gethostname");
413 (void)strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
415 if (sendto(sock, &msg, sizeof(struct tsp), 0,
416 (struct sockaddr*)&sin,
417 sizeof(struct sockaddr)) < 0) {
425 * Enables or disables tracing on local timedaemon
436 struct sockaddr_in dest;
437 struct sockaddr_in from;
440 struct servent *srvp;
443 printf("usage: timedc trace { on | off }\n");
447 srvp = getservbyname("timed", "udp");
449 warnx("udp/timed: unknown service");
452 dest.sin_port = srvp->s_port;
453 dest.sin_family = AF_INET;
455 if (gethostname(myname, sizeof(myname) - 1) < 0)
456 err(1, "gethostname");
457 hp = gethostbyname(myname);
458 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
460 if (strcmp(argv[1], "on") == 0) {
461 msg.tsp_type = TSP_TRACEON;
464 msg.tsp_type = TSP_TRACEOFF;
468 (void)strcpy(msg.tsp_name, myname);
469 msg.tsp_vers = TSPVERSION;
471 if (sendto(sock, &msg, sizeof(struct tsp), 0,
472 (struct sockaddr*)&dest, sizeof(struct sockaddr)) < 0) {
480 FD_SET(sock, &ready);
481 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout)) {
482 length = sizeof(from);
483 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
484 (struct sockaddr *)&from, &length);
490 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
491 * this is still OS-dependent. Demand that the packet is at
492 * least long enough to hold a 4.3BSD packet.
494 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
495 fprintf(stderr, "short packet (%u/%u bytes) from %s\n",
496 cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
497 inet_ntoa(from.sin_addr));
501 if (msg.tsp_type == TSP_ACK)
503 printf("timed tracing enabled\n");
505 printf("timed tracing disabled\n");
507 if (msg.tsp_type >= TSPTYPENUMBER)
508 printf("unknown ack received: %u\n",
511 printf("wrong ack received: %s\n",
512 tsptype[msg.tsp_type]);
515 printf("communication error\n");
522 struct sockaddr_in sin;
524 sock = socket(AF_INET, SOCK_DGRAM, 0);
526 warn("opening socket");
530 sin.sin_family = AF_INET;
531 sin.sin_addr.s_addr = 0;
532 for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
533 sin.sin_port = htons((u_short)port);
534 if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0)
536 if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
542 if (port == IPPORT_RESERVED / 2) {
543 warnx("all reserved ports in use");
548 sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
550 warn("opening raw socket");