2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1985, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)cmds.c 8.1 (Berkeley) 6/6/93";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
43 #include <arpa/inet.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip_icmp.h>
55 #include <protocols/timed.h>
58 #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;
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, (fd_set *)0,
113 warn("select(date read)");
119 fromlen = sizeof(from);
120 if (recvfrom(sock,&sec,sizeof(sec),0,
121 &from,&fromlen) < 0) {
122 warn("recvfrom(date read)");
128 warnx("%s says it is before 1970: %lu",
134 (void)gettimeofday(&now, NULL);
135 return (sec - now.tv_sec);
139 /* if we get here, we tried too many times */
140 warnx("%s will not tell us the date", hostname);
146 * Clockdiff computes the difference between the time of the machine on
147 * which it is called and the time of the machines given as argument.
148 * The time differences measured by clockdiff are obtained using a sequence
149 * of ICMP TSTAMP messages which are returned to the sender by the IP module
150 * in the remote machine.
151 * In order to compare clocks of machines in different time zones, the time
152 * is transmitted (as a 32-bit value) in milliseconds since midnight UT.
153 * If a hosts uses a different time format, it should set the high order
154 * bit of the 32-bit quantity it transmits.
155 * However, VMS apparently transmits the time in milliseconds since midnight
156 * local time (rather than GMT) without setting the high order bit.
157 * Furthermore, it does not understand daylight-saving time. This makes
158 * clockdiff behaving inconsistently with hosts running VMS.
160 * In order to reduce the sensitivity to the variance of message transmission
161 * time, clockdiff sends a sequence of messages. Yet, measures between
162 * two `distant' hosts can be affected by a small error. The error can,
163 * however, be reduced by increasing the number of messages sent in each
167 clockdiff(int argc, char *argv[])
170 extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
171 register int avg_cnt;
176 printf("usage: timedc clockdiff host ...\n");
180 if (gethostname(myname, sizeof(myname) - 1) < 0)
181 err(1, "gethostname");
183 /* get the address for the date ready */
184 sp = getservbyname(DATE_PORT, DATE_PROTO);
186 warnx("%s/%s: unknown service", DATE_PORT, DATE_PROTO);
187 dayaddr.sin_port = 0;
189 dayaddr.sin_port = sp->s_port;
194 hp = gethostbyname(*argv);
196 warnx("%s: %s", *argv, hstrerror(h_errno));
200 server.sin_family = hp->h_addrtype;
201 bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
202 for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
203 measure_status = measure(10000,100, *argv, &server, 1);
204 if (measure_status != GOOD)
206 avg += measure_delta;
208 if (measure_status == GOOD)
209 measure_delta = avg/avg_cnt;
211 switch (measure_status) {
213 printf("%s is down\n", hp->h_name);
216 printf("%s transmits a non-standard time format\n",
220 printf("%s is unreachable\n", hp->h_name);
225 * Try to get the date only after using ICMP timestamps to
226 * get the time. This is because the date protocol
229 if (dayaddr.sin_port != 0) {
230 dayaddr.sin_family = hp->h_addrtype;
231 bcopy(hp->h_addr, &dayaddr.sin_addr.s_addr,
233 avg = daydiff(*argv);
235 printf("time on %s is %ld days ahead %s\n",
236 hp->h_name, avg/SECDAY, myname);
238 } else if (avg < -SECDAY) {
239 printf("time on %s is %ld days behind %s\n",
240 hp->h_name, -avg/SECDAY, myname);
245 if (measure_delta > 0) {
246 printf("time on %s is %d ms. ahead of time on %s\n",
247 hp->h_name, measure_delta, myname);
248 } else if (measure_delta == 0) {
249 printf("%s and %s have the same time\n",
252 printf("time on %s is %d ms. behind time on %s\n",
253 hp->h_name, -measure_delta, myname);
261 * finds location of master timedaemon
264 msite(int argc, char *argv[])
268 struct sockaddr_in dest;
270 struct sockaddr_in from;
273 struct servent *srvp;
277 printf("usage: timedc msite [host ...]\n");
281 srvp = getservbyname("timed", "udp");
283 warnx("timed/udp: unknown service");
286 dest.sin_port = srvp->s_port;
287 dest.sin_family = AF_INET;
289 if (gethostname(myname, sizeof(myname) - 1) < 0)
290 err(1, "gethostname");
293 tgtname = (i >= argc) ? myname : argv[i];
294 hp = gethostbyname(tgtname);
296 warnx("%s: %s", tgtname, hstrerror(h_errno));
299 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
301 (void)strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
302 msg.tsp_type = TSP_MSITE;
303 msg.tsp_vers = TSPVERSION;
305 if (sendto(sock, &msg, sizeof(struct tsp), 0,
306 (struct sockaddr*)&dest,
307 sizeof(struct sockaddr)) < 0) {
315 FD_SET(sock, &ready);
316 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0,
318 length = sizeof(from);
319 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
320 (struct sockaddr *)&from, &length);
326 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
327 * this is still OS-dependent. Demand that the packet is at
328 * least long enough to hold a 4.3BSD packet.
330 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
332 "short packet (%zd/%zu bytes) from %s\n",
333 cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
334 inet_ntoa(from.sin_addr));
338 if (msg.tsp_type == TSP_ACK) {
339 printf("master timedaemon at %s is %s\n",
340 tgtname, msg.tsp_name);
342 if (msg.tsp_type >= TSPTYPENUMBER)
343 printf("unknown ack received: %u\n",
346 printf("wrong ack received: %s\n",
347 tsptype[msg.tsp_type]);
350 printf("communication error with %s\n", tgtname);
352 } while (++i < argc);
366 * Causes the election timer to expire on the selected hosts
367 * It sends just one udp message per machine, relying on
368 * reliability of communication channel.
371 testing(int argc, char *argv[])
373 struct servent *srvp;
374 struct sockaddr_in sin;
378 printf("usage: timedc election host1 [host2 ...]\n");
382 srvp = getservbyname("timed", "udp");
384 warnx("timed/udp: unknown service");
390 hp = gethostbyname(*argv);
392 warnx("%s: %s", *argv, hstrerror(h_errno));
396 sin.sin_port = srvp->s_port;
397 sin.sin_family = hp->h_addrtype;
398 bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length);
400 msg.tsp_type = TSP_TEST;
401 msg.tsp_vers = TSPVERSION;
402 if (gethostname(myname, sizeof(myname) - 1) < 0)
403 err(1, "gethostname");
404 (void)strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
406 if (sendto(sock, &msg, sizeof(struct tsp), 0,
407 (struct sockaddr*)&sin,
408 sizeof(struct sockaddr)) < 0) {
416 * Enables or disables tracing on local timedaemon
419 tracing(int argc, char *argv[])
425 struct sockaddr_in dest;
426 struct sockaddr_in from;
429 struct servent *srvp;
432 printf("usage: timedc trace { on | off }\n");
436 srvp = getservbyname("timed", "udp");
438 warnx("timed/udp: unknown service");
441 dest.sin_port = srvp->s_port;
442 dest.sin_family = AF_INET;
444 if (gethostname(myname, sizeof(myname) - 1) < 0)
445 err(1, "gethostname");
446 hp = gethostbyname(myname);
447 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
449 if (strcmp(argv[1], "on") == 0) {
450 msg.tsp_type = TSP_TRACEON;
453 msg.tsp_type = TSP_TRACEOFF;
457 (void)strcpy(msg.tsp_name, myname);
458 msg.tsp_vers = TSPVERSION;
460 if (sendto(sock, &msg, sizeof(struct tsp), 0,
461 (struct sockaddr*)&dest, sizeof(struct sockaddr)) < 0) {
469 FD_SET(sock, &ready);
470 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout)) {
471 length = sizeof(from);
472 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
473 (struct sockaddr *)&from, &length);
479 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
480 * this is still OS-dependent. Demand that the packet is at
481 * least long enough to hold a 4.3BSD packet.
483 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
484 fprintf(stderr, "short packet (%zd/%zu bytes) from %s\n",
485 cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
486 inet_ntoa(from.sin_addr));
490 if (msg.tsp_type == TSP_ACK)
492 printf("timed tracing enabled\n");
494 printf("timed tracing disabled\n");
496 if (msg.tsp_type >= TSPTYPENUMBER)
497 printf("unknown ack received: %u\n",
500 printf("wrong ack received: %s\n",
501 tsptype[msg.tsp_type]);
504 printf("communication error\n");
511 struct sockaddr_in sin;
513 sock = socket(AF_INET, SOCK_DGRAM, 0);
515 warn("opening socket");
519 sin.sin_family = AF_INET;
520 sin.sin_addr.s_addr = 0;
521 for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
522 sin.sin_port = htons((u_short)port);
523 if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0)
525 if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
531 if (port == IPPORT_RESERVED / 2) {
532 warnx("all reserved ports in use");
537 sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
539 warn("opening raw socket");