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
33 * @(#)cmds.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.sbin/timed/timedc/cmds.c,v 1.6.2.2 2001/08/31 08:02:06 kris Exp $
40 #include <netinet/in_systm.h>
41 #include <netinet/ip.h>
42 #include <netinet/ip_icmp.h>
50 #include <protocols/timed.h>
54 #include <sys/clock.h>
57 #define SECDAY (24*SECHR)
60 # define DATE_PROTO "udp"
61 # define DATE_PORT "time"
66 char myname[MAXHOSTNAMELEN];
68 struct sockaddr_in server;
69 struct sockaddr_in dayaddr;
70 extern int measure_delta;
72 void bytenetorder(struct tsp *);
73 void bytehostorder(struct tsp *);
76 #define BU (2208988800UL) /* seconds before UNIX epoch */
79 /* compute the difference between our date and another machine
81 static int /* difference in days from our time */
82 daydiff(char *hostname)
86 struct timeval tout, now;
92 /* wait 2 seconds between 10 tries */
95 for (trials = 0; trials < 10; trials++) {
96 /* ask for the time */
98 if (sendto(sock, &sec, sizeof(sec), 0,
99 (struct sockaddr*)&dayaddr, sizeof(dayaddr)) < 0) {
100 warn("sendto(sock)");
106 FD_SET(sock, &ready);
107 i = select(sock+1, &ready, NULL, NULL, &tout);
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 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);
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 is an 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 transmitts 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("udp/timed: 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 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, NULL, NULL, &tout)) {
315 length = sizeof(from);
316 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
317 (struct sockaddr *)&from, &length);
323 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
324 * this is still OS-dependent. Demand that the packet is at
325 * least long enough to hold a 4.3BSD packet.
327 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
329 "short packet (%d/%zu bytes) from %s\n",
330 cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
331 inet_ntoa(from.sin_addr));
335 if (msg.tsp_type == TSP_ACK) {
336 printf("master timedaemon at %s is %s\n",
337 tgtname, msg.tsp_name);
339 if (msg.tsp_type >= TSPTYPENUMBER)
340 printf("unknown ack received: %u\n",
343 printf("wrong ack received: %s\n",
344 tsptype[msg.tsp_type]);
347 printf("communication error with %s\n", tgtname);
349 } 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("udp/timed: 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 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("udp/timed: 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 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, NULL, NULL, &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 (%d/%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");