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";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
46 #include <arpa/inet.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/ip.h>
50 #include <netinet/ip_icmp.h>
58 #include <protocols/timed.h>
61 #define SECDAY (24*SECHR)
63 # define DATE_PROTO "udp"
64 # define DATE_PORT "time"
69 char myname[MAXHOSTNAMELEN];
71 struct sockaddr_in server;
72 struct sockaddr_in dayaddr;
73 extern int measure_delta;
75 void bytenetorder(struct tsp *);
76 void bytehostorder(struct tsp *);
79 #define BU (2208988800UL) /* seconds before UNIX epoch */
82 /* compute the difference between our date and another machine
84 static int /* difference in days from our time */
90 struct timeval tout, now;
97 /* wait 2 seconds between 10 tries */
100 for (trials = 0; trials < 10; trials++) {
101 /* ask for the time */
103 if (sendto(sock, &sec, sizeof(sec), 0,
104 (struct sockaddr*)&dayaddr, sizeof(dayaddr)) < 0) {
105 warn("sendto(sock)");
111 FD_SET(sock, &ready);
112 i = select(sock+1, &ready, (fd_set *)0,
117 warn("select(date read)");
123 fromlen = sizeof(from);
124 if (recvfrom(sock,&sec,sizeof(sec),0,
125 &from,&fromlen) < 0) {
126 warn("recvfrom(date read)");
132 warnx("%s says it is before 1970: %lu",
138 (void)gettimeofday(&now, (struct timezone*)0);
139 return (sec - now.tv_sec);
143 /* if we get here, we tried too many times */
144 warnx("%s will not tell us the date", hostname);
150 * Clockdiff computes the difference between the time of the machine on
151 * which it is called and the time of the machines given as argument.
152 * The time differences measured by clockdiff are obtained using a sequence
153 * of ICMP TSTAMP messages which are returned to the sender by the IP module
154 * in the remote machine.
155 * In order to compare clocks of machines in different time zones, the time
156 * is transmitted (as a 32-bit value) in milliseconds since midnight UT.
157 * If a hosts uses a different time format, it should set the high order
158 * bit of the 32-bit quantity it transmits.
159 * However, VMS apparently transmits the time in milliseconds since midnight
160 * local time (rather than GMT) without setting the high order bit.
161 * Furthermore, it does not understand daylight-saving time. This makes
162 * clockdiff behaving inconsistently with hosts running VMS.
164 * In order to reduce the sensitivity to the variance of message transmission
165 * time, clockdiff sends a sequence of messages. Yet, measures between
166 * two `distant' hosts can be affected by a small error. The error can,
167 * however, be reduced by increasing the number of messages sent in each
171 clockdiff(argc, argv)
176 extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
177 register int avg_cnt;
182 printf("usage: timedc clockdiff host ...\n");
186 if (gethostname(myname, sizeof(myname) - 1) < 0)
187 err(1, "gethostname");
189 /* get the address for the date ready */
190 sp = getservbyname(DATE_PORT, DATE_PROTO);
192 warnx("%s/%s: unknown service", DATE_PORT, DATE_PROTO);
193 dayaddr.sin_port = 0;
195 dayaddr.sin_port = sp->s_port;
200 hp = gethostbyname(*argv);
202 warnx("%s: %s", *argv, hstrerror(h_errno));
206 server.sin_family = hp->h_addrtype;
207 bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
208 for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
209 measure_status = measure(10000,100, *argv, &server, 1);
210 if (measure_status != GOOD)
212 avg += measure_delta;
214 if (measure_status == GOOD)
215 measure_delta = avg/avg_cnt;
217 switch (measure_status) {
219 printf("%s is down\n", hp->h_name);
222 printf("%s transmits a non-standard time format\n",
226 printf("%s is unreachable\n", hp->h_name);
231 * Try to get the date only after using ICMP timestamps to
232 * get the time. This is because the date protocol
235 if (dayaddr.sin_port != 0) {
236 dayaddr.sin_family = hp->h_addrtype;
237 bcopy(hp->h_addr, &dayaddr.sin_addr.s_addr,
239 avg = daydiff(*argv);
241 printf("time on %s is %ld days ahead %s\n",
242 hp->h_name, avg/SECDAY, myname);
244 } else if (avg < -SECDAY) {
245 printf("time on %s is %ld days behind %s\n",
246 hp->h_name, -avg/SECDAY, myname);
251 if (measure_delta > 0) {
252 printf("time on %s is %d ms. ahead of time on %s\n",
253 hp->h_name, measure_delta, myname);
254 } else if (measure_delta == 0) {
255 printf("%s and %s have the same time\n",
258 printf("time on %s is %d ms. behind time on %s\n",
259 hp->h_name, -measure_delta, myname);
267 * finds location of master timedaemon
276 struct sockaddr_in dest;
278 struct sockaddr_in from;
281 struct servent *srvp;
285 printf("usage: timedc msite [host ...]\n");
289 srvp = getservbyname("timed", "udp");
291 warnx("timed/udp: unknown service");
294 dest.sin_port = srvp->s_port;
295 dest.sin_family = AF_INET;
297 if (gethostname(myname, sizeof(myname) - 1) < 0)
298 err(1, "gethostname");
301 tgtname = (i >= argc) ? myname : argv[i];
302 hp = gethostbyname(tgtname);
304 warnx("%s: %s", tgtname, hstrerror(h_errno));
307 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
309 (void)strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
310 msg.tsp_type = TSP_MSITE;
311 msg.tsp_vers = TSPVERSION;
313 if (sendto(sock, &msg, sizeof(struct tsp), 0,
314 (struct sockaddr*)&dest,
315 sizeof(struct sockaddr)) < 0) {
323 FD_SET(sock, &ready);
324 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0,
326 length = sizeof(from);
327 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
328 (struct sockaddr *)&from, &length);
334 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
335 * this is still OS-dependent. Demand that the packet is at
336 * least long enough to hold a 4.3BSD packet.
338 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
340 "short packet (%u/%u bytes) from %s\n",
341 cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
342 inet_ntoa(from.sin_addr));
346 if (msg.tsp_type == TSP_ACK) {
347 printf("master timedaemon at %s is %s\n",
348 tgtname, msg.tsp_name);
350 if (msg.tsp_type >= TSPTYPENUMBER)
351 printf("unknown ack received: %u\n",
354 printf("wrong ack received: %s\n",
355 tsptype[msg.tsp_type]);
358 printf("communication error with %s\n", tgtname);
360 } while (++i < argc);
374 * Causes the election timer to expire on the selected hosts
375 * It sends just one udp message per machine, relying on
376 * reliability of communication channel.
383 struct servent *srvp;
384 struct sockaddr_in sin;
388 printf("usage: timedc election host1 [host2 ...]\n");
392 srvp = getservbyname("timed", "udp");
394 warnx("timed/udp: unknown service");
400 hp = gethostbyname(*argv);
402 warnx("%s: %s", *argv, hstrerror(h_errno));
406 sin.sin_port = srvp->s_port;
407 sin.sin_family = hp->h_addrtype;
408 bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length);
410 msg.tsp_type = TSP_TEST;
411 msg.tsp_vers = TSPVERSION;
412 if (gethostname(myname, sizeof(myname) - 1) < 0)
413 err(1, "gethostname");
414 (void)strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
416 if (sendto(sock, &msg, sizeof(struct tsp), 0,
417 (struct sockaddr*)&sin,
418 sizeof(struct sockaddr)) < 0) {
426 * Enables or disables tracing on local timedaemon
437 struct sockaddr_in dest;
438 struct sockaddr_in from;
441 struct servent *srvp;
444 printf("usage: timedc trace { on | off }\n");
448 srvp = getservbyname("timed", "udp");
450 warnx("timed/udp: unknown service");
453 dest.sin_port = srvp->s_port;
454 dest.sin_family = AF_INET;
456 if (gethostname(myname, sizeof(myname) - 1) < 0)
457 err(1, "gethostname");
458 hp = gethostbyname(myname);
459 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
461 if (strcmp(argv[1], "on") == 0) {
462 msg.tsp_type = TSP_TRACEON;
465 msg.tsp_type = TSP_TRACEOFF;
469 (void)strcpy(msg.tsp_name, myname);
470 msg.tsp_vers = TSPVERSION;
472 if (sendto(sock, &msg, sizeof(struct tsp), 0,
473 (struct sockaddr*)&dest, sizeof(struct sockaddr)) < 0) {
481 FD_SET(sock, &ready);
482 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout)) {
483 length = sizeof(from);
484 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
485 (struct sockaddr *)&from, &length);
491 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
492 * this is still OS-dependent. Demand that the packet is at
493 * least long enough to hold a 4.3BSD packet.
495 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
496 fprintf(stderr, "short packet (%u/%u bytes) from %s\n",
497 cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
498 inet_ntoa(from.sin_addr));
502 if (msg.tsp_type == TSP_ACK)
504 printf("timed tracing enabled\n");
506 printf("timed tracing disabled\n");
508 if (msg.tsp_type >= TSPTYPENUMBER)
509 printf("unknown ack received: %u\n",
512 printf("wrong ack received: %s\n",
513 tsptype[msg.tsp_type]);
516 printf("communication error\n");
523 struct sockaddr_in sin;
525 sock = socket(AF_INET, SOCK_DGRAM, 0);
527 warn("opening socket");
531 sin.sin_family = AF_INET;
532 sin.sin_addr.s_addr = 0;
533 for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
534 sin.sin_port = htons((u_short)port);
535 if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0)
537 if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
543 if (port == IPPORT_RESERVED / 2) {
544 warnx("all reserved ports in use");
549 sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
551 warn("opening raw socket");