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