Home | History | Annotate | Line # | Download | only in timedc
cmds.c revision 1.8
      1 /*	$NetBSD: cmds.c,v 1.8 1997/10/18 07:13:33 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1985, 1993 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)cmds.c	8.2 (Berkeley) 3/26/95";
     40 #else
     41 __RCSID("$NetBSD: cmds.c,v 1.8 1997/10/18 07:13:33 lukem Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #ifdef sgi
     46 #ident "$Revision: 1.8 $"
     47 #endif
     48 
     49 #include "timedc.h"
     50 #include <sys/file.h>
     51 
     52 #include <netinet/in_systm.h>
     53 #include <netinet/ip.h>
     54 #include <netinet/ip_icmp.h>
     55 
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <unistd.h>
     59 
     60 #define TSPTYPES
     61 #include <protocols/timed.h>
     62 
     63 #ifdef sgi
     64 #include <bstring.h>
     65 #include <sys/clock.h>
     66 #else
     67 #define	SECHR	(60*60)
     68 #define	SECDAY	(24*SECHR)
     69 #endif /* sgi */
     70 
     71 # define DATE_PROTO "udp"
     72 # define DATE_PORT "time"
     73 
     74 
     75 int sock;
     76 int sock_raw;
     77 char myname[MAXHOSTNAMELEN];
     78 struct hostent *hp;
     79 struct sockaddr_in server;
     80 struct sockaddr_in dayaddr;
     81 extern int measure_delta;
     82 
     83 void bytenetorder(struct tsp *);
     84 void bytehostorder(struct tsp *);
     85 
     86 
     87 #define BU ((unsigned long)2208988800U)	/* seconds before UNIX epoch */
     88 
     89 
     90 /* compute the difference between our date and another machine
     91  */
     92 static int				/* difference in days from our time */
     93 daydiff(char *hostname)
     94 {
     95 	int i;
     96 	int trials;
     97 	struct timeval tout, now;
     98 	fd_set ready;
     99 	struct sockaddr from;
    100 	int fromlen;
    101 	unsigned long sec;
    102 
    103 
    104 	/* wait 2 seconds between 10 tries */
    105 	tout.tv_sec = 2;
    106 	tout.tv_usec = 0;
    107 	for (trials = 0; trials < 10; trials++) {
    108 		/* ask for the time */
    109 		sec = 0;
    110 		if (sendto(sock, &sec, sizeof(sec), 0,
    111 			   (struct sockaddr*)&dayaddr, sizeof(dayaddr)) < 0) {
    112 			perror("sendto(sock)");
    113 			return 0;
    114 		}
    115 
    116 		for (;;) {
    117 			FD_ZERO(&ready);
    118 			FD_SET(sock, &ready);
    119 			i = select(sock+1, &ready, (fd_set *)0,
    120 				   (fd_set *)0, &tout);
    121 			if (i < 0) {
    122 				if (errno == EINTR)
    123 					continue;
    124 				perror("select(date read)");
    125 				return 0;
    126 			}
    127 			if (0 == i)
    128 				break;
    129 
    130 			fromlen = sizeof(from);
    131 			if (recvfrom(sock,&sec,sizeof(sec),0,
    132 				     &from,&fromlen) < 0) {
    133 				perror("recvfrom(date read)");
    134 				return 0;
    135 			}
    136 
    137 			sec = ntohl(sec);
    138 			if (sec < BU) {
    139 				fprintf(stderr,
    140 					"%s says it is before 1970: %lu",
    141 					hostname, sec);
    142 				return 0;
    143 			}
    144 			sec -= BU;
    145 
    146 			(void)gettimeofday(&now, (struct timezone*)0);
    147 			return (sec - now.tv_sec);
    148 		}
    149 	}
    150 
    151 	/* if we get here, we tried too many times */
    152 	fprintf(stderr,"%s will not tell us the date\n", hostname);
    153 	return 0;
    154 }
    155 
    156 
    157 /*
    158  * Clockdiff computes the difference between the time of the machine on
    159  * which it is called and the time of the machines given as argument.
    160  * The time differences measured by clockdiff are obtained using a sequence
    161  * of ICMP TSTAMP messages which are returned to the sender by the IP module
    162  * in the remote machine.
    163  * In order to compare clocks of machines in different time zones, the time
    164  * is transmitted (as a 32-bit value) in milliseconds since midnight UT.
    165  * If a hosts uses a different time format, it should set the high order
    166  * bit of the 32-bit quantity it transmits.
    167  * However, VMS apparently transmits the time in milliseconds since midnight
    168  * local time (rather than GMT) without setting the high order bit.
    169  * Furthermore, it does not understand daylight-saving time.  This makes
    170  * clockdiff behaving inconsistently with hosts running VMS.
    171  *
    172  * In order to reduce the sensitivity to the variance of message transmission
    173  * time, clockdiff sends a sequence of messages.  Yet, measures between
    174  * two `distant' hosts can be affected by a small error. The error can,
    175  * however, be reduced by increasing the number of messages sent in each
    176  * measurement.
    177  */
    178 void
    179 clockdiff(argc, argv)
    180 	int argc;
    181 	char *argv[];
    182 {
    183 	int measure_status;
    184 	extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
    185 	register int avg_cnt;
    186 	register long avg;
    187 	struct servent *sp;
    188 
    189 	if (argc < 2)  {
    190 		printf("Usage: clockdiff host ... \n");
    191 		return;
    192 	}
    193 
    194 	(void)gethostname(myname,sizeof(myname));
    195 
    196 	/* get the address for the date ready */
    197 	sp = getservbyname(DATE_PORT, DATE_PROTO);
    198 	if (!sp) {
    199 		(void)fprintf(stderr, "%s/%s is an unknown service\n",
    200 			      DATE_PORT, DATE_PROTO);
    201 		dayaddr.sin_port = 0;
    202 	} else {
    203 		dayaddr.sin_port = sp->s_port;
    204 	}
    205 
    206 	while (argc > 1) {
    207 		argc--; argv++;
    208 		hp = gethostbyname(*argv);
    209 		if (hp == NULL) {
    210 			fprintf(stderr, "timedc: %s: ", *argv);
    211 			herror(0);
    212 			continue;
    213 		}
    214 
    215 		server.sin_family = hp->h_addrtype;
    216 		bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
    217 		for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
    218 			measure_status = measure(10000,100, *argv, &server, 1);
    219 			if (measure_status != GOOD)
    220 				break;
    221 			avg += measure_delta;
    222 		}
    223 		if (measure_status == GOOD)
    224 			measure_delta = avg/avg_cnt;
    225 
    226 		switch (measure_status) {
    227 		case HOSTDOWN:
    228 			printf("%s is down\n", hp->h_name);
    229 			continue;
    230 		case NONSTDTIME:
    231 			printf("%s transmitts a non-standard time format\n",
    232 			       hp->h_name);
    233 			continue;
    234 		case UNREACHABLE:
    235 			printf("%s is unreachable\n", hp->h_name);
    236 			continue;
    237 		}
    238 
    239 		/*
    240 		 * Try to get the date only after using ICMP timestamps to
    241 		 * get the time.  This is because the date protocol
    242 		 * is optional.
    243 		 */
    244 		if (dayaddr.sin_port != 0) {
    245 			dayaddr.sin_family = hp->h_addrtype;
    246 			bcopy(hp->h_addr, &dayaddr.sin_addr.s_addr,
    247 			      hp->h_length);
    248 			avg = daydiff(*argv);
    249 			if (avg > SECDAY) {
    250 				printf("time on %s is %ld days ahead %s\n",
    251 				       hp->h_name, avg/SECDAY, myname);
    252 				continue;
    253 			} else if (avg < -SECDAY) {
    254 				printf("time on %s is %ld days behind %s\n",
    255 				       hp->h_name, -avg/SECDAY, myname);
    256 				continue;
    257 			}
    258 		}
    259 
    260 		if (measure_delta > 0) {
    261 			printf("time on %s is %d ms. ahead of time on %s\n",
    262 			       hp->h_name, measure_delta, myname);
    263 		} else if (measure_delta == 0) {
    264 			printf("%s and %s have the same time\n",
    265 			       hp->h_name, myname);
    266 		} else {
    267 			printf("time on %s is %d ms. behind time on %s\n",
    268 			       hp->h_name, -measure_delta, myname);
    269 		}
    270 	}
    271 	return;
    272 }
    273 
    274 
    275 /*
    276  * finds location of master timedaemon
    277  */
    278 void
    279 msite(int argc, char *argv[])
    280 {
    281 	int cc;
    282 	fd_set ready;
    283 	struct sockaddr_in dest;
    284 	int i, length;
    285 	struct sockaddr from;
    286 	struct timeval tout;
    287 	struct tsp msg;
    288 	struct servent *srvp;
    289 	char *tgtname;
    290 
    291 	if (argc < 1) {
    292 		printf("Usage: msite [hostname]\n");
    293 		return;
    294 	}
    295 
    296 	srvp = getservbyname("timed", "udp");
    297 	if (srvp == 0) {
    298 		fprintf(stderr, "udp/timed: unknown service\n");
    299 		return;
    300 	}
    301 	dest.sin_port = srvp->s_port;
    302 	dest.sin_family = AF_INET;
    303 
    304 	(void)gethostname(myname, sizeof(myname));
    305 	i = 1;
    306 	do {
    307 		tgtname = (i >= argc) ? myname : argv[i];
    308 		hp = gethostbyname(tgtname);
    309 		if (hp == 0) {
    310 			fprintf(stderr, "timedc: %s: ", tgtname);
    311 			herror(0);
    312 			continue;
    313 		}
    314 		bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
    315 
    316 		(void)strncpy(msg.tsp_name, myname, sizeof(msg.tsp_name) - 1);
    317 		msg.tsp_type = TSP_MSITE;
    318 		msg.tsp_vers = TSPVERSION;
    319 		bytenetorder(&msg);
    320 		if (sendto(sock, &msg, sizeof(struct tsp), 0,
    321 			   (struct sockaddr*)&dest,
    322 			   sizeof(struct sockaddr)) < 0) {
    323 			perror("sendto");
    324 			continue;
    325 		}
    326 
    327 		tout.tv_sec = 15;
    328 		tout.tv_usec = 0;
    329 		FD_ZERO(&ready);
    330 		FD_SET(sock, &ready);
    331 		if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0,
    332 			   &tout)) {
    333 			length = sizeof(struct sockaddr);
    334 			cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
    335 				      &from, &length);
    336 			if (cc < 0) {
    337 				perror("recvfrom");
    338 				continue;
    339 			}
    340 			bytehostorder(&msg);
    341 			if (msg.tsp_type == TSP_ACK) {
    342 				printf("master timedaemon at %s is %s\n",
    343 				       tgtname, msg.tsp_name);
    344 			} else {
    345 				printf("received wrong ack: %s\n",
    346 				       tsptype[msg.tsp_type]);
    347 			}
    348 		} else {
    349 			printf("communication error with %s\n", tgtname);
    350 		}
    351 	} while (++i < argc);
    352 }
    353 
    354 /*
    355  * quits timedc
    356  */
    357 void
    358 quit(int argc, char *argv[])
    359 {
    360 	exit(0);
    361 }
    362 
    363 
    364 /*
    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.
    368  */
    369 void
    370 testing(int argc, char *argv[])
    371 {
    372 	struct servent *srvp;
    373 	struct sockaddr_in sin;
    374 	struct tsp msg;
    375 
    376 	if (argc < 2)  {
    377 		printf("Usage: election host1 [host2 ...]\n");
    378 		return;
    379 	}
    380 
    381 	srvp = getservbyname("timed", "udp");
    382 	if (srvp == 0) {
    383 		fprintf(stderr, "udp/timed: unknown service\n");
    384 		return;
    385 	}
    386 
    387 	while (argc > 1) {
    388 		argc--; argv++;
    389 		hp = gethostbyname(*argv);
    390 		if (hp == NULL) {
    391 			fprintf(stderr, "timedc: %s: ", *argv);
    392 			herror(0);
    393 			argc--; argv++;
    394 			continue;
    395 		}
    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);
    399 
    400 		msg.tsp_type = TSP_TEST;
    401 		msg.tsp_vers = TSPVERSION;
    402 		(void)gethostname(myname, sizeof(myname));
    403 		(void)strncpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
    404 		bytenetorder(&msg);
    405 		if (sendto(sock, &msg, sizeof(struct tsp), 0,
    406 			   (struct sockaddr*)&sin,
    407 			   sizeof(struct sockaddr)) < 0) {
    408 			perror("sendto");
    409 		}
    410 	}
    411 }
    412 
    413 
    414 /*
    415  * Enables or disables tracing on local timedaemon
    416  */
    417 void
    418 tracing(int argc, char *argv[])
    419 {
    420 	int onflag;
    421 	int length;
    422 	int cc;
    423 	fd_set ready;
    424 	struct sockaddr_in dest;
    425 	struct sockaddr from;
    426 	struct timeval tout;
    427 	struct tsp msg;
    428 	struct servent *srvp;
    429 
    430 	if (argc != 2) {
    431 		printf("Usage: tracing { on | off }\n");
    432 		return;
    433 	}
    434 
    435 	srvp = getservbyname("timed", "udp");
    436 	if (srvp == 0) {
    437 		fprintf(stderr, "udp/timed: unknown service\n");
    438 		return;
    439 	}
    440 	dest.sin_port = srvp->s_port;
    441 	dest.sin_family = AF_INET;
    442 
    443 	(void)gethostname(myname,sizeof(myname));
    444 	hp = gethostbyname(myname);
    445 	bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
    446 
    447 	if (strcmp(argv[1], "on") == 0) {
    448 		msg.tsp_type = TSP_TRACEON;
    449 		onflag = ON;
    450 	} else {
    451 		msg.tsp_type = TSP_TRACEOFF;
    452 		onflag = OFF;
    453 	}
    454 
    455 	(void)strncpy(msg.tsp_name, myname, sizeof(msg.tsp_name) - 1);
    456 	msg.tsp_vers = TSPVERSION;
    457 	bytenetorder(&msg);
    458 	if (sendto(sock, &msg, sizeof(struct tsp), 0,
    459 		   (struct sockaddr*)&dest, sizeof(struct sockaddr)) < 0) {
    460 		perror("sendto");
    461 		return;
    462 	}
    463 
    464 	tout.tv_sec = 5;
    465 	tout.tv_usec = 0;
    466 	FD_ZERO(&ready);
    467 	FD_SET(sock, &ready);
    468 	if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout)) {
    469 		length = sizeof(struct sockaddr);
    470 		cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
    471 			      &from, &length);
    472 		if (cc < 0) {
    473 			perror("recvfrom");
    474 			return;
    475 		}
    476 		bytehostorder(&msg);
    477 		if (msg.tsp_type == TSP_ACK)
    478 			if (onflag)
    479 				printf("timed tracing enabled\n");
    480 			else
    481 				printf("timed tracing disabled\n");
    482 		else
    483 			printf("wrong ack received: %s\n",
    484 						tsptype[msg.tsp_type]);
    485 	} else
    486 		printf("communication error\n");
    487 }
    488 
    489 int
    490 priv_resources()
    491 {
    492 	int port;
    493 	struct sockaddr_in sin;
    494 
    495 	sock = socket(AF_INET, SOCK_DGRAM, 0);
    496 	if (sock < 0) {
    497 		perror("opening socket");
    498 		return(-1);
    499 	}
    500 
    501 	sin.sin_family = AF_INET;
    502 	sin.sin_addr.s_addr = 0;
    503 	for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
    504 		sin.sin_port = htons((u_short)port);
    505 		if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0)
    506 			break;
    507 		if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
    508 			perror("bind");
    509 			(void) close(sock);
    510 			return(-1);
    511 		}
    512 	}
    513 	if (port == IPPORT_RESERVED / 2) {
    514 		fprintf(stderr, "all reserved ports in use\n");
    515 		(void) close(sock);
    516 		return(-1);
    517 	}
    518 
    519 	sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
    520 	if (sock_raw < 0)  {
    521 		perror("opening raw socket");
    522 		(void) close(sock);
    523 		return(-1);
    524 	}
    525 	return(1);
    526 }
    527