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
29 * @(#)cmds.c 8.1 (Berkeley) 6/6/93
30 * $FreeBSD: src/usr.sbin/timed/timedc/cmds.c,v 1.6.2.2 2001/08/31 08:02:06 kris Exp $
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/ip_icmp.h>
46 #include <protocols/timed.h>
50 #include <sys/clock.h>
53 #define SECDAY (24*SECHR)
56 # define DATE_PROTO "udp"
57 # define DATE_PORT "time"
62 char myname[MAXHOSTNAMELEN];
64 struct sockaddr_in server;
65 struct sockaddr_in dayaddr;
66 extern int measure_delta;
68 void bytenetorder(struct tsp *);
69 void bytehostorder(struct tsp *);
72 #define BU (2208988800UL) /* seconds before UNIX epoch */
75 /* compute the difference between our date and another machine
77 static int /* difference in days from our time */
78 daydiff(char *hostname)
82 struct timeval tout, now;
88 /* wait 2 seconds between 10 tries */
91 for (trials = 0; trials < 10; trials++) {
92 /* ask for the time */
94 if (sendto(sock, &sec, sizeof(sec), 0,
95 (struct sockaddr*)&dayaddr, sizeof(dayaddr)) < 0) {
102 FD_SET(sock, &ready);
103 i = select(sock+1, &ready, NULL, NULL, &tout);
107 warn("select(date read)");
113 fromlen = sizeof(from);
114 if (recvfrom(sock,&sec,sizeof(sec),0,
115 &from,&fromlen) < 0) {
116 warn("recvfrom(date read)");
122 warnx("%s says it is before 1970: %lu",
128 gettimeofday(&now, NULL);
129 return (sec - now.tv_sec);
133 /* if we get here, we tried too many times */
134 warnx("%s will not tell us the date", hostname);
140 * Clockdiff computes the difference between the time of the machine on
141 * which it is called and the time of the machines given as argument.
142 * The time differences measured by clockdiff are obtained using a sequence
143 * of ICMP TSTAMP messages which are returned to the sender by the IP module
144 * in the remote machine.
145 * In order to compare clocks of machines in different time zones, the time
146 * is transmitted (as a 32-bit value) in milliseconds since midnight UT.
147 * If a hosts uses a different time format, it should set the high order
148 * bit of the 32-bit quantity it transmits.
149 * However, VMS apparently transmits the time in milliseconds since midnight
150 * local time (rather than GMT) without setting the high order bit.
151 * Furthermore, it does not understand daylight-saving time. This makes
152 * clockdiff behaving inconsistently with hosts running VMS.
154 * In order to reduce the sensitivity to the variance of message transmission
155 * time, clockdiff sends a sequence of messages. Yet, measures between
156 * two `distant' hosts can be affected by a small error. The error can,
157 * however, be reduced by increasing the number of messages sent in each
161 clockdiff(int argc, char *argv[])
164 extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
170 printf("usage: timedc clockdiff host ...\n");
174 if (gethostname(myname, sizeof(myname) - 1) < 0)
175 err(1, "gethostname");
177 /* get the address for the date ready */
178 sp = getservbyname(DATE_PORT, DATE_PROTO);
180 warnx("%s/%s is an unknown service", DATE_PORT, DATE_PROTO);
181 dayaddr.sin_port = 0;
183 dayaddr.sin_port = sp->s_port;
188 hp = gethostbyname(*argv);
190 warnx("%s: %s", *argv, hstrerror(h_errno));
194 server.sin_family = hp->h_addrtype;
195 bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
196 for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
197 measure_status = measure(10000,100, *argv, &server, 1);
198 if (measure_status != GOOD)
200 avg += measure_delta;
202 if (measure_status == GOOD)
203 measure_delta = avg/avg_cnt;
205 switch (measure_status) {
207 printf("%s is down\n", hp->h_name);
210 printf("%s transmitts a non-standard time format\n",
214 printf("%s is unreachable\n", hp->h_name);
219 * Try to get the date only after using ICMP timestamps to
220 * get the time. This is because the date protocol
223 if (dayaddr.sin_port != 0) {
224 dayaddr.sin_family = hp->h_addrtype;
225 bcopy(hp->h_addr, &dayaddr.sin_addr.s_addr,
227 avg = daydiff(*argv);
229 printf("time on %s is %ld days ahead %s\n",
230 hp->h_name, avg/SECDAY, myname);
232 } else if (avg < -SECDAY) {
233 printf("time on %s is %ld days behind %s\n",
234 hp->h_name, -avg/SECDAY, myname);
239 if (measure_delta > 0) {
240 printf("time on %s is %d ms. ahead of time on %s\n",
241 hp->h_name, measure_delta, myname);
242 } else if (measure_delta == 0) {
243 printf("%s and %s have the same time\n",
246 printf("time on %s is %d ms. behind time on %s\n",
247 hp->h_name, -measure_delta, myname);
255 * finds location of master timedaemon
258 msite(int argc, char *argv[])
262 struct sockaddr_in dest;
264 struct sockaddr_in from;
267 struct servent *srvp;
271 printf("usage: timedc msite [host ...]\n");
275 srvp = getservbyname("timed", "udp");
277 warnx("udp/timed: unknown service");
280 dest.sin_port = srvp->s_port;
281 dest.sin_family = AF_INET;
283 if (gethostname(myname, sizeof(myname) - 1) < 0)
284 err(1, "gethostname");
287 tgtname = (i >= argc) ? myname : argv[i];
288 hp = gethostbyname(tgtname);
290 warnx("%s: %s", tgtname, hstrerror(h_errno));
293 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
295 strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
296 msg.tsp_type = TSP_MSITE;
297 msg.tsp_vers = TSPVERSION;
299 if (sendto(sock, &msg, sizeof(struct tsp), 0,
300 (struct sockaddr*)&dest,
301 sizeof(struct sockaddr)) < 0) {
309 FD_SET(sock, &ready);
310 if (select(FD_SETSIZE, &ready, NULL, NULL, &tout)) {
311 length = sizeof(from);
312 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
313 (struct sockaddr *)&from, &length);
319 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
320 * this is still OS-dependent. Demand that the packet is at
321 * least long enough to hold a 4.3BSD packet.
323 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
325 "short packet (%d/%zu bytes) from %s\n",
326 cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
327 inet_ntoa(from.sin_addr));
331 if (msg.tsp_type == TSP_ACK) {
332 printf("master timedaemon at %s is %s\n",
333 tgtname, msg.tsp_name);
335 if (msg.tsp_type >= TSPTYPENUMBER)
336 printf("unknown ack received: %u\n",
339 printf("wrong ack received: %s\n",
340 tsptype[msg.tsp_type]);
343 printf("communication error with %s\n", tgtname);
345 } while (++i < argc);
360 * Causes the election timer to expire on the selected hosts
361 * It sends just one udp message per machine, relying on
362 * reliability of communication channel.
365 testing(int argc, char *argv[])
367 struct servent *srvp;
368 struct sockaddr_in sin;
372 printf("usage: timedc election host1 [host2 ...]\n");
376 srvp = getservbyname("timed", "udp");
378 warnx("udp/timed: unknown service");
384 hp = gethostbyname(*argv);
386 warnx("%s: %s", *argv, hstrerror(h_errno));
390 sin.sin_port = srvp->s_port;
391 sin.sin_family = hp->h_addrtype;
392 bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length);
394 msg.tsp_type = TSP_TEST;
395 msg.tsp_vers = TSPVERSION;
396 if (gethostname(myname, sizeof(myname) - 1) < 0)
397 err(1, "gethostname");
398 strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
400 if (sendto(sock, &msg, sizeof(struct tsp), 0,
401 (struct sockaddr*)&sin,
402 sizeof(struct sockaddr)) < 0) {
410 * Enables or disables tracing on local timedaemon
413 tracing(int argc, char *argv[])
419 struct sockaddr_in dest;
420 struct sockaddr_in from;
423 struct servent *srvp;
426 printf("usage: timedc trace { on | off }\n");
430 srvp = getservbyname("timed", "udp");
432 warnx("udp/timed: unknown service");
435 dest.sin_port = srvp->s_port;
436 dest.sin_family = AF_INET;
438 if (gethostname(myname, sizeof(myname) - 1) < 0)
439 err(1, "gethostname");
440 hp = gethostbyname(myname);
441 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
443 if (strcmp(argv[1], "on") == 0) {
444 msg.tsp_type = TSP_TRACEON;
447 msg.tsp_type = TSP_TRACEOFF;
451 strcpy(msg.tsp_name, myname);
452 msg.tsp_vers = TSPVERSION;
454 if (sendto(sock, &msg, sizeof(struct tsp), 0,
455 (struct sockaddr*)&dest, sizeof(struct sockaddr)) < 0) {
463 FD_SET(sock, &ready);
464 if (select(FD_SETSIZE, &ready, NULL, NULL, &tout)) {
465 length = sizeof(from);
466 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
467 (struct sockaddr *)&from, &length);
473 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
474 * this is still OS-dependent. Demand that the packet is at
475 * least long enough to hold a 4.3BSD packet.
477 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
478 fprintf(stderr, "short packet (%d/%zu bytes) from %s\n",
479 cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
480 inet_ntoa(from.sin_addr));
484 if (msg.tsp_type == TSP_ACK)
486 printf("timed tracing enabled\n");
488 printf("timed tracing disabled\n");
490 if (msg.tsp_type >= TSPTYPENUMBER)
491 printf("unknown ack received: %u\n",
494 printf("wrong ack received: %s\n",
495 tsptype[msg.tsp_type]);
498 printf("communication error\n");
505 struct sockaddr_in sin;
507 sock = socket(AF_INET, SOCK_DGRAM, 0);
509 warn("opening socket");
513 sin.sin_family = AF_INET;
514 sin.sin_addr.s_addr = 0;
515 for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
516 sin.sin_port = htons((u_short)port);
517 if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0)
519 if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
525 if (port == IPPORT_RESERVED / 2) {
526 warnx("all reserved ports in use");
531 sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
533 warn("opening raw socket");