1 /* $NetBSD: cmds.c,v 1.9 1998/07/06 07:06:14 mrg 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.9 1998/07/06 07:06:14 mrg Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #ifdef sgi 46 #ident "$Revision: 1.9 $" 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 + 1]; 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 myname[sizeof(myname) - 1] = '\0'; 196 197 /* get the address for the date ready */ 198 sp = getservbyname(DATE_PORT, DATE_PROTO); 199 if (!sp) { 200 (void)fprintf(stderr, "%s/%s is an unknown service\n", 201 DATE_PORT, DATE_PROTO); 202 dayaddr.sin_port = 0; 203 } else { 204 dayaddr.sin_port = sp->s_port; 205 } 206 207 while (argc > 1) { 208 argc--; argv++; 209 hp = gethostbyname(*argv); 210 if (hp == NULL) { 211 fprintf(stderr, "timedc: %s: ", *argv); 212 herror(0); 213 continue; 214 } 215 216 server.sin_family = hp->h_addrtype; 217 bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length); 218 for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) { 219 measure_status = measure(10000,100, *argv, &server, 1); 220 if (measure_status != GOOD) 221 break; 222 avg += measure_delta; 223 } 224 if (measure_status == GOOD) 225 measure_delta = avg/avg_cnt; 226 227 switch (measure_status) { 228 case HOSTDOWN: 229 printf("%s is down\n", hp->h_name); 230 continue; 231 case NONSTDTIME: 232 printf("%s transmitts a non-standard time format\n", 233 hp->h_name); 234 continue; 235 case UNREACHABLE: 236 printf("%s is unreachable\n", hp->h_name); 237 continue; 238 } 239 240 /* 241 * Try to get the date only after using ICMP timestamps to 242 * get the time. This is because the date protocol 243 * is optional. 244 */ 245 if (dayaddr.sin_port != 0) { 246 dayaddr.sin_family = hp->h_addrtype; 247 bcopy(hp->h_addr, &dayaddr.sin_addr.s_addr, 248 hp->h_length); 249 avg = daydiff(*argv); 250 if (avg > SECDAY) { 251 printf("time on %s is %ld days ahead %s\n", 252 hp->h_name, avg/SECDAY, myname); 253 continue; 254 } else if (avg < -SECDAY) { 255 printf("time on %s is %ld days behind %s\n", 256 hp->h_name, -avg/SECDAY, myname); 257 continue; 258 } 259 } 260 261 if (measure_delta > 0) { 262 printf("time on %s is %d ms. ahead of time on %s\n", 263 hp->h_name, measure_delta, myname); 264 } else if (measure_delta == 0) { 265 printf("%s and %s have the same time\n", 266 hp->h_name, myname); 267 } else { 268 printf("time on %s is %d ms. behind time on %s\n", 269 hp->h_name, -measure_delta, myname); 270 } 271 } 272 return; 273 } 274 275 276 /* 277 * finds location of master timedaemon 278 */ 279 void 280 msite(int argc, char *argv[]) 281 { 282 int cc; 283 fd_set ready; 284 struct sockaddr_in dest; 285 int i, length; 286 struct sockaddr from; 287 struct timeval tout; 288 struct tsp msg; 289 struct servent *srvp; 290 char *tgtname; 291 292 if (argc < 1) { 293 printf("Usage: msite [hostname]\n"); 294 return; 295 } 296 297 srvp = getservbyname("timed", "udp"); 298 if (srvp == 0) { 299 fprintf(stderr, "udp/timed: unknown service\n"); 300 return; 301 } 302 dest.sin_port = srvp->s_port; 303 dest.sin_family = AF_INET; 304 305 (void)gethostname(myname, sizeof(myname)); 306 i = 1; 307 do { 308 tgtname = (i >= argc) ? myname : argv[i]; 309 hp = gethostbyname(tgtname); 310 if (hp == 0) { 311 fprintf(stderr, "timedc: %s: ", tgtname); 312 herror(0); 313 continue; 314 } 315 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length); 316 317 (void)strncpy(msg.tsp_name, myname, sizeof(msg.tsp_name) - 1); 318 msg.tsp_type = TSP_MSITE; 319 msg.tsp_vers = TSPVERSION; 320 bytenetorder(&msg); 321 if (sendto(sock, &msg, sizeof(struct tsp), 0, 322 (struct sockaddr*)&dest, 323 sizeof(struct sockaddr)) < 0) { 324 perror("sendto"); 325 continue; 326 } 327 328 tout.tv_sec = 15; 329 tout.tv_usec = 0; 330 FD_ZERO(&ready); 331 FD_SET(sock, &ready); 332 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, 333 &tout)) { 334 length = sizeof(struct sockaddr); 335 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0, 336 &from, &length); 337 if (cc < 0) { 338 perror("recvfrom"); 339 continue; 340 } 341 bytehostorder(&msg); 342 if (msg.tsp_type == TSP_ACK) { 343 printf("master timedaemon at %s is %s\n", 344 tgtname, msg.tsp_name); 345 } else { 346 printf("received wrong ack: %s\n", 347 tsptype[msg.tsp_type]); 348 } 349 } else { 350 printf("communication error with %s\n", tgtname); 351 } 352 } while (++i < argc); 353 } 354 355 /* 356 * quits timedc 357 */ 358 void 359 quit(int argc, char *argv[]) 360 { 361 exit(0); 362 } 363 364 365 /* 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. 369 */ 370 void 371 testing(int argc, char *argv[]) 372 { 373 struct servent *srvp; 374 struct sockaddr_in sin; 375 struct tsp msg; 376 377 if (argc < 2) { 378 printf("Usage: election host1 [host2 ...]\n"); 379 return; 380 } 381 382 srvp = getservbyname("timed", "udp"); 383 if (srvp == 0) { 384 fprintf(stderr, "udp/timed: unknown service\n"); 385 return; 386 } 387 388 while (argc > 1) { 389 argc--; argv++; 390 hp = gethostbyname(*argv); 391 if (hp == NULL) { 392 fprintf(stderr, "timedc: %s: ", *argv); 393 herror(0); 394 argc--; argv++; 395 continue; 396 } 397 sin.sin_port = srvp->s_port; 398 sin.sin_family = hp->h_addrtype; 399 bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length); 400 401 msg.tsp_type = TSP_TEST; 402 msg.tsp_vers = TSPVERSION; 403 (void)gethostname(myname, sizeof(myname)); 404 (void)strncpy(msg.tsp_name, myname, sizeof(msg.tsp_name)); 405 bytenetorder(&msg); 406 if (sendto(sock, &msg, sizeof(struct tsp), 0, 407 (struct sockaddr*)&sin, 408 sizeof(struct sockaddr)) < 0) { 409 perror("sendto"); 410 } 411 } 412 } 413 414 415 /* 416 * Enables or disables tracing on local timedaemon 417 */ 418 void 419 tracing(int argc, char *argv[]) 420 { 421 int onflag; 422 int length; 423 int cc; 424 fd_set ready; 425 struct sockaddr_in dest; 426 struct sockaddr from; 427 struct timeval tout; 428 struct tsp msg; 429 struct servent *srvp; 430 431 if (argc != 2) { 432 printf("Usage: tracing { on | off }\n"); 433 return; 434 } 435 436 srvp = getservbyname("timed", "udp"); 437 if (srvp == 0) { 438 fprintf(stderr, "udp/timed: unknown service\n"); 439 return; 440 } 441 dest.sin_port = srvp->s_port; 442 dest.sin_family = AF_INET; 443 444 (void)gethostname(myname,sizeof(myname)); 445 hp = gethostbyname(myname); 446 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length); 447 448 if (strcmp(argv[1], "on") == 0) { 449 msg.tsp_type = TSP_TRACEON; 450 onflag = ON; 451 } else { 452 msg.tsp_type = TSP_TRACEOFF; 453 onflag = OFF; 454 } 455 456 (void)strncpy(msg.tsp_name, myname, sizeof(msg.tsp_name) - 1); 457 msg.tsp_vers = TSPVERSION; 458 bytenetorder(&msg); 459 if (sendto(sock, &msg, sizeof(struct tsp), 0, 460 (struct sockaddr*)&dest, sizeof(struct sockaddr)) < 0) { 461 perror("sendto"); 462 return; 463 } 464 465 tout.tv_sec = 5; 466 tout.tv_usec = 0; 467 FD_ZERO(&ready); 468 FD_SET(sock, &ready); 469 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout)) { 470 length = sizeof(struct sockaddr); 471 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0, 472 &from, &length); 473 if (cc < 0) { 474 perror("recvfrom"); 475 return; 476 } 477 bytehostorder(&msg); 478 if (msg.tsp_type == TSP_ACK) 479 if (onflag) 480 printf("timed tracing enabled\n"); 481 else 482 printf("timed tracing disabled\n"); 483 else 484 printf("wrong ack received: %s\n", 485 tsptype[msg.tsp_type]); 486 } else 487 printf("communication error\n"); 488 } 489 490 int 491 priv_resources() 492 { 493 int port; 494 struct sockaddr_in sin; 495 496 sock = socket(AF_INET, SOCK_DGRAM, 0); 497 if (sock < 0) { 498 perror("opening socket"); 499 return(-1); 500 } 501 502 sin.sin_family = AF_INET; 503 sin.sin_addr.s_addr = 0; 504 for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) { 505 sin.sin_port = htons((u_short)port); 506 if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0) 507 break; 508 if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) { 509 perror("bind"); 510 (void) close(sock); 511 return(-1); 512 } 513 } 514 if (port == IPPORT_RESERVED / 2) { 515 fprintf(stderr, "all reserved ports in use\n"); 516 (void) close(sock); 517 return(-1); 518 } 519 520 sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 521 if (sock_raw < 0) { 522 perror("opening raw socket"); 523 (void) close(sock); 524 return(-1); 525 } 526 return(1); 527 } 528