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 $
35 * $DragonFly: src/usr.sbin/timed/timedc/cmds.c,v 1.4 2004/12/18 22:48:14 swildner Exp $
41 #include <netinet/in_systm.h>
42 #include <netinet/ip.h>
43 #include <netinet/ip_icmp.h>
51 #include <protocols/timed.h>
55 #include <sys/clock.h>
58 #define SECDAY (24*SECHR)
61 # define DATE_PROTO "udp"
62 # define DATE_PORT "time"
67 char myname[MAXHOSTNAMELEN];
69 struct sockaddr_in server;
70 struct sockaddr_in dayaddr;
71 extern int measure_delta;
73 void bytenetorder(struct tsp *);
74 void bytehostorder(struct tsp *);
77 #define BU (2208988800UL) /* seconds before UNIX epoch */
80 /* compute the difference between our date and another machine
82 static int /* difference in days from our time */
83 daydiff(char *hostname)
87 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, NULL, NULL, &tout);
112 warn("select(date read)");
118 fromlen = sizeof(from);
119 if (recvfrom(sock,&sec,sizeof(sec),0,
120 &from,&fromlen) < 0) {
121 warn("recvfrom(date read)");
127 warnx("%s says it is before 1970: %lu",
133 gettimeofday(&now, NULL);
134 return (sec - now.tv_sec);
138 /* if we get here, we tried too many times */
139 warnx("%s will not tell us the date", hostname);
145 * Clockdiff computes the difference between the time of the machine on
146 * which it is called and the time of the machines given as argument.
147 * The time differences measured by clockdiff are obtained using a sequence
148 * of ICMP TSTAMP messages which are returned to the sender by the IP module
149 * in the remote machine.
150 * In order to compare clocks of machines in different time zones, the time
151 * is transmitted (as a 32-bit value) in milliseconds since midnight UT.
152 * If a hosts uses a different time format, it should set the high order
153 * bit of the 32-bit quantity it transmits.
154 * However, VMS apparently transmits the time in milliseconds since midnight
155 * local time (rather than GMT) without setting the high order bit.
156 * Furthermore, it does not understand daylight-saving time. This makes
157 * clockdiff behaving inconsistently with hosts running VMS.
159 * In order to reduce the sensitivity to the variance of message transmission
160 * time, clockdiff sends a sequence of messages. Yet, measures between
161 * two `distant' hosts can be affected by a small error. The error can,
162 * however, be reduced by increasing the number of messages sent in each
166 clockdiff(int argc, char *argv[])
169 extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
175 printf("usage: timedc clockdiff host ...\n");
179 if (gethostname(myname, sizeof(myname) - 1) < 0)
180 err(1, "gethostname");
182 /* get the address for the date ready */
183 sp = getservbyname(DATE_PORT, DATE_PROTO);
185 warnx("%s/%s is an unknown service", DATE_PORT, DATE_PROTO);
186 dayaddr.sin_port = 0;
188 dayaddr.sin_port = sp->s_port;
193 hp = gethostbyname(*argv);
195 warnx("%s: %s", *argv, hstrerror(h_errno));
199 server.sin_family = hp->h_addrtype;
200 bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
201 for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
202 measure_status = measure(10000,100, *argv, &server, 1);
203 if (measure_status != GOOD)
205 avg += measure_delta;
207 if (measure_status == GOOD)
208 measure_delta = avg/avg_cnt;
210 switch (measure_status) {
212 printf("%s is down\n", hp->h_name);
215 printf("%s transmitts a non-standard time format\n",
219 printf("%s is unreachable\n", hp->h_name);
224 * Try to get the date only after using ICMP timestamps to
225 * get the time. This is because the date protocol
228 if (dayaddr.sin_port != 0) {
229 dayaddr.sin_family = hp->h_addrtype;
230 bcopy(hp->h_addr, &dayaddr.sin_addr.s_addr,
232 avg = daydiff(*argv);
234 printf("time on %s is %ld days ahead %s\n",
235 hp->h_name, avg/SECDAY, myname);
237 } else if (avg < -SECDAY) {
238 printf("time on %s is %ld days behind %s\n",
239 hp->h_name, -avg/SECDAY, myname);
244 if (measure_delta > 0) {
245 printf("time on %s is %d ms. ahead of time on %s\n",
246 hp->h_name, measure_delta, myname);
247 } else if (measure_delta == 0) {
248 printf("%s and %s have the same time\n",
251 printf("time on %s is %d ms. behind time on %s\n",
252 hp->h_name, -measure_delta, myname);
260 * finds location of master timedaemon
263 msite(int argc, char *argv[])
267 struct sockaddr_in dest;
269 struct sockaddr_in from;
272 struct servent *srvp;
276 printf("usage: timedc msite [host ...]\n");
280 srvp = getservbyname("timed", "udp");
282 warnx("udp/timed: unknown service");
285 dest.sin_port = srvp->s_port;
286 dest.sin_family = AF_INET;
288 if (gethostname(myname, sizeof(myname) - 1) < 0)
289 err(1, "gethostname");
292 tgtname = (i >= argc) ? myname : argv[i];
293 hp = gethostbyname(tgtname);
295 warnx("%s: %s", tgtname, hstrerror(h_errno));
298 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
300 strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
301 msg.tsp_type = TSP_MSITE;
302 msg.tsp_vers = TSPVERSION;
304 if (sendto(sock, &msg, sizeof(struct tsp), 0,
305 (struct sockaddr*)&dest,
306 sizeof(struct sockaddr)) < 0) {
314 FD_SET(sock, &ready);
315 if (select(FD_SETSIZE, &ready, NULL, NULL, &tout)) {
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 (%u/%u 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);
365 * Causes the election timer to expire on the selected hosts
366 * It sends just one udp message per machine, relying on
367 * reliability of communication channel.
370 testing(int argc, char *argv[])
372 struct servent *srvp;
373 struct sockaddr_in sin;
377 printf("usage: timedc election host1 [host2 ...]\n");
381 srvp = getservbyname("timed", "udp");
383 warnx("udp/timed: unknown service");
389 hp = gethostbyname(*argv);
391 warnx("%s: %s", *argv, hstrerror(h_errno));
395 sin.sin_port = srvp->s_port;
396 sin.sin_family = hp->h_addrtype;
397 bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length);
399 msg.tsp_type = TSP_TEST;
400 msg.tsp_vers = TSPVERSION;
401 if (gethostname(myname, sizeof(myname) - 1) < 0)
402 err(1, "gethostname");
403 strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
405 if (sendto(sock, &msg, sizeof(struct tsp), 0,
406 (struct sockaddr*)&sin,
407 sizeof(struct sockaddr)) < 0) {
415 * Enables or disables tracing on local timedaemon
418 tracing(int argc, char *argv[])
424 struct sockaddr_in dest;
425 struct sockaddr_in from;
428 struct servent *srvp;
431 printf("usage: timedc trace { on | off }\n");
435 srvp = getservbyname("timed", "udp");
437 warnx("udp/timed: unknown service");
440 dest.sin_port = srvp->s_port;
441 dest.sin_family = AF_INET;
443 if (gethostname(myname, sizeof(myname) - 1) < 0)
444 err(1, "gethostname");
445 hp = gethostbyname(myname);
446 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
448 if (strcmp(argv[1], "on") == 0) {
449 msg.tsp_type = TSP_TRACEON;
452 msg.tsp_type = TSP_TRACEOFF;
456 strcpy(msg.tsp_name, myname);
457 msg.tsp_vers = TSPVERSION;
459 if (sendto(sock, &msg, sizeof(struct tsp), 0,
460 (struct sockaddr*)&dest, sizeof(struct sockaddr)) < 0) {
468 FD_SET(sock, &ready);
469 if (select(FD_SETSIZE, &ready, NULL, NULL, &tout)) {
470 length = sizeof(from);
471 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
472 (struct sockaddr *)&from, &length);
478 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
479 * this is still OS-dependent. Demand that the packet is at
480 * least long enough to hold a 4.3BSD packet.
482 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
483 fprintf(stderr, "short packet (%u/%u bytes) from %s\n",
484 cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
485 inet_ntoa(from.sin_addr));
489 if (msg.tsp_type == TSP_ACK)
491 printf("timed tracing enabled\n");
493 printf("timed tracing disabled\n");
495 if (msg.tsp_type >= TSPTYPENUMBER)
496 printf("unknown ack received: %u\n",
499 printf("wrong ack received: %s\n",
500 tsptype[msg.tsp_type]);
503 printf("communication error\n");
510 struct sockaddr_in sin;
512 sock = socket(AF_INET, SOCK_DGRAM, 0);
514 warn("opening socket");
518 sin.sin_family = AF_INET;
519 sin.sin_addr.s_addr = 0;
520 for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
521 sin.sin_port = htons((u_short)port);
522 if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0)
524 if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
530 if (port == IPPORT_RESERVED / 2) {
531 warnx("all reserved ports in use");
536 sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
538 warn("opening raw socket");