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