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