Deleted Added
full compact
cmds.c (1611) cmds.c (30642)
1/*-
2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 18 unchanged lines hidden (view full) ---

27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
1/*-
2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 18 unchanged lines hidden (view full) ---

27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
35static char sccsid[] = "@(#)cmds.c 8.1 (Berkeley) 6/6/93";
36static char sccsid[] = "@(#)cmds.c 8.1 (Berkeley) 6/6/93";
37#endif
38static const char rcsid[] =
39 "$Id$";
36#endif /* not lint */
37
40#endif /* not lint */
41
38#ifdef sgi
39#ident "$Revision: 1.10 $"
40#endif
41
42#include "timedc.h"
43#include <sys/file.h>
44
45#include <netinet/in_systm.h>
46#include <netinet/ip.h>
47#include <netinet/ip_icmp.h>
48
42#include "timedc.h"
43#include <sys/file.h>
44
45#include <netinet/in_systm.h>
46#include <netinet/ip.h>
47#include <netinet/ip_icmp.h>
48
49#include <err.h>
49#include <stdlib.h>
50#include <strings.h>
51#include <unistd.h>
52
53#define TSPTYPES
54#include <protocols/timed.h>
55
56#ifdef sgi

--- 41 unchanged lines hidden (view full) ---

98 /* wait 2 seconds between 10 tries */
99 tout.tv_sec = 2;
100 tout.tv_usec = 0;
101 for (trials = 0; trials < 10; trials++) {
102 /* ask for the time */
103 sec = 0;
104 if (sendto(sock, &sec, sizeof(sec), 0,
105 (struct sockaddr*)&dayaddr, sizeof(dayaddr)) < 0) {
50#include <stdlib.h>
51#include <strings.h>
52#include <unistd.h>
53
54#define TSPTYPES
55#include <protocols/timed.h>
56
57#ifdef sgi

--- 41 unchanged lines hidden (view full) ---

99 /* wait 2 seconds between 10 tries */
100 tout.tv_sec = 2;
101 tout.tv_usec = 0;
102 for (trials = 0; trials < 10; trials++) {
103 /* ask for the time */
104 sec = 0;
105 if (sendto(sock, &sec, sizeof(sec), 0,
106 (struct sockaddr*)&dayaddr, sizeof(dayaddr)) < 0) {
106 perror("sendto(sock)");
107 warn("sendto(sock)");
107 return 0;
108 }
109
110 for (;;) {
111 FD_ZERO(&ready);
112 FD_SET(sock, &ready);
113 i = select(sock+1, &ready, (fd_set *)0,
114 (fd_set *)0, &tout);
115 if (i < 0) {
108 return 0;
109 }
110
111 for (;;) {
112 FD_ZERO(&ready);
113 FD_SET(sock, &ready);
114 i = select(sock+1, &ready, (fd_set *)0,
115 (fd_set *)0, &tout);
116 if (i < 0) {
116 if (errno = EINTR)
117 if (errno == EINTR)
117 continue;
118 continue;
118 perror("select(date read)");
119 warn("select(date read)");
119 return 0;
120 }
121 if (0 == i)
122 break;
123
124 fromlen = sizeof(from);
125 if (recvfrom(sock,&sec,sizeof(sec),0,
126 &from,&fromlen) < 0) {
120 return 0;
121 }
122 if (0 == i)
123 break;
124
125 fromlen = sizeof(from);
126 if (recvfrom(sock,&sec,sizeof(sec),0,
127 &from,&fromlen) < 0) {
127 perror("recvfrom(date read)");
128 warn("recvfrom(date read)");
128 return 0;
129 }
130
131 sec = ntohl(sec);
132 if (sec < BU) {
129 return 0;
130 }
131
132 sec = ntohl(sec);
133 if (sec < BU) {
133 fprintf(stderr,
134 "%s says it is before 1970: %lu",
134 warnx("%s says it is before 1970: %lu",
135 hostname, sec);
136 return 0;
137 }
138 sec -= BU;
139
140 (void)gettimeofday(&now, (struct timezone*)0);
141 return (sec - now.tv_sec);
142 }
143 }
144
145 /* if we get here, we tried too many times */
135 hostname, sec);
136 return 0;
137 }
138 sec -= BU;
139
140 (void)gettimeofday(&now, (struct timezone*)0);
141 return (sec - now.tv_sec);
142 }
143 }
144
145 /* if we get here, we tried too many times */
146 fprintf(stderr,"%s will not tell us the date\n", hostname);
146 warnx("%s will not tell us the date", hostname);
147 return 0;
148}
149
150
151/*
152 * Clockdiff computes the difference between the time of the machine on
153 * which it is called and the time of the machines given as argument.
154 * The time differences measured by clockdiff are obtained using a sequence

--- 21 unchanged lines hidden (view full) ---

176{
177 int measure_status;
178 extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
179 register int avg_cnt;
180 register long avg;
181 struct servent *sp;
182
183 if (argc < 2) {
147 return 0;
148}
149
150
151/*
152 * Clockdiff computes the difference between the time of the machine on
153 * which it is called and the time of the machines given as argument.
154 * The time differences measured by clockdiff are obtained using a sequence

--- 21 unchanged lines hidden (view full) ---

176{
177 int measure_status;
178 extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
179 register int avg_cnt;
180 register long avg;
181 struct servent *sp;
182
183 if (argc < 2) {
184 printf("Usage: clockdiff host ... \n");
184 printf("usage: timedc clockdiff host ...\n");
185 return;
186 }
187
188 (void)gethostname(myname,sizeof(myname));
189
190 /* get the address for the date ready */
191 sp = getservbyname(DATE_PORT, DATE_PROTO);
192 if (!sp) {
185 return;
186 }
187
188 (void)gethostname(myname,sizeof(myname));
189
190 /* get the address for the date ready */
191 sp = getservbyname(DATE_PORT, DATE_PROTO);
192 if (!sp) {
193 (void)fprintf(stderr, "%s/%s is an unknown service\n",
194 DATE_PORT, DATE_PROTO);
193 warnx("%s/%s is an unknown service", DATE_PORT, DATE_PROTO);
195 dayaddr.sin_port = 0;
196 } else {
197 dayaddr.sin_port = sp->s_port;
198 }
199
200 while (argc > 1) {
201 argc--; argv++;
202 hp = gethostbyname(*argv);
203 if (hp == NULL) {
194 dayaddr.sin_port = 0;
195 } else {
196 dayaddr.sin_port = sp->s_port;
197 }
198
199 while (argc > 1) {
200 argc--; argv++;
201 hp = gethostbyname(*argv);
202 if (hp == NULL) {
204 fprintf(stderr, "timedc: %s: ", *argv);
205 herror(0);
203 warnx("%s: %s", *argv, hstrerror(h_errno));
206 continue;
207 }
208
209 server.sin_family = hp->h_addrtype;
210 bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
211 for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
212 measure_status = measure(10000,100, *argv, &server, 1);
213 if (measure_status != GOOD)

--- 66 unchanged lines hidden (view full) ---

280 int i, length;
281 struct sockaddr from;
282 struct timeval tout;
283 struct tsp msg;
284 struct servent *srvp;
285 char *tgtname;
286
287 if (argc < 1) {
204 continue;
205 }
206
207 server.sin_family = hp->h_addrtype;
208 bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
209 for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
210 measure_status = measure(10000,100, *argv, &server, 1);
211 if (measure_status != GOOD)

--- 66 unchanged lines hidden (view full) ---

278 int i, length;
279 struct sockaddr from;
280 struct timeval tout;
281 struct tsp msg;
282 struct servent *srvp;
283 char *tgtname;
284
285 if (argc < 1) {
288 printf("Usage: msite [hostname]\n");
286 printf("usage: timedc msite [host ...]\n");
289 return;
290 }
291
292 srvp = getservbyname("timed", "udp");
293 if (srvp == 0) {
287 return;
288 }
289
290 srvp = getservbyname("timed", "udp");
291 if (srvp == 0) {
294 fprintf(stderr, "udp/timed: unknown service\n");
292 warnx("udp/timed: unknown service");
295 return;
296 }
297 dest.sin_port = srvp->s_port;
298 dest.sin_family = AF_INET;
299
300 (void)gethostname(myname, sizeof(myname));
301 i = 1;
302 do {
303 tgtname = (i >= argc) ? myname : argv[i];
304 hp = gethostbyname(tgtname);
305 if (hp == 0) {
293 return;
294 }
295 dest.sin_port = srvp->s_port;
296 dest.sin_family = AF_INET;
297
298 (void)gethostname(myname, sizeof(myname));
299 i = 1;
300 do {
301 tgtname = (i >= argc) ? myname : argv[i];
302 hp = gethostbyname(tgtname);
303 if (hp == 0) {
306 fprintf(stderr, "timedc: %s: ", tgtname);
307 herror(0);
304 warnx("%s: %s", tgtname, hstrerror(h_errno));
308 continue;
309 }
310 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
311
305 continue;
306 }
307 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
308
312 (void)strcpy(msg.tsp_name, myname);
309 (void)strncpy(msg.tsp_name, myname, sizeof msg.tsp_name-1);
310 msg.tsp_name[sizeof msg.tsp_name-1] = '\0';
313 msg.tsp_type = TSP_MSITE;
314 msg.tsp_vers = TSPVERSION;
315 bytenetorder(&msg);
316 if (sendto(sock, &msg, sizeof(struct tsp), 0,
317 (struct sockaddr*)&dest,
318 sizeof(struct sockaddr)) < 0) {
311 msg.tsp_type = TSP_MSITE;
312 msg.tsp_vers = TSPVERSION;
313 bytenetorder(&msg);
314 if (sendto(sock, &msg, sizeof(struct tsp), 0,
315 (struct sockaddr*)&dest,
316 sizeof(struct sockaddr)) < 0) {
319 perror("sendto");
317 warn("sendto");
320 continue;
321 }
322
323 tout.tv_sec = 15;
324 tout.tv_usec = 0;
325 FD_ZERO(&ready);
326 FD_SET(sock, &ready);
327 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0,
328 &tout)) {
329 length = sizeof(struct sockaddr);
330 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
331 &from, &length);
332 if (cc < 0) {
318 continue;
319 }
320
321 tout.tv_sec = 15;
322 tout.tv_usec = 0;
323 FD_ZERO(&ready);
324 FD_SET(sock, &ready);
325 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0,
326 &tout)) {
327 length = sizeof(struct sockaddr);
328 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
329 &from, &length);
330 if (cc < 0) {
333 perror("recvfrom");
331 warn("recvfrom");
334 continue;
335 }
336 bytehostorder(&msg);
337 if (msg.tsp_type == TSP_ACK) {
338 printf("master timedaemon at %s is %s\n",
339 tgtname, msg.tsp_name);
340 } else {
341 printf("received wrong ack: %s\n",

--- 25 unchanged lines hidden (view full) ---

367 int argc;
368 char *argv[];
369{
370 struct servent *srvp;
371 struct sockaddr_in sin;
372 struct tsp msg;
373
374 if (argc < 2) {
332 continue;
333 }
334 bytehostorder(&msg);
335 if (msg.tsp_type == TSP_ACK) {
336 printf("master timedaemon at %s is %s\n",
337 tgtname, msg.tsp_name);
338 } else {
339 printf("received wrong ack: %s\n",

--- 25 unchanged lines hidden (view full) ---

365 int argc;
366 char *argv[];
367{
368 struct servent *srvp;
369 struct sockaddr_in sin;
370 struct tsp msg;
371
372 if (argc < 2) {
375 printf("Usage: election host1 [host2 ...]\n");
373 printf("usage: timedc election host1 [host2 ...]\n");
376 return;
377 }
378
379 srvp = getservbyname("timed", "udp");
380 if (srvp == 0) {
374 return;
375 }
376
377 srvp = getservbyname("timed", "udp");
378 if (srvp == 0) {
381 fprintf(stderr, "udp/timed: unknown service\n");
379 warnx("udp/timed: unknown service");
382 return;
383 }
384
385 while (argc > 1) {
386 argc--; argv++;
387 hp = gethostbyname(*argv);
388 if (hp == NULL) {
380 return;
381 }
382
383 while (argc > 1) {
384 argc--; argv++;
385 hp = gethostbyname(*argv);
386 if (hp == NULL) {
389 fprintf(stderr, "timedc: %s: ", *argv);
390 herror(0);
387 warnx("%s: %s", *argv, hstrerror(h_errno));
391 argc--; argv++;
392 continue;
393 }
394 sin.sin_port = srvp->s_port;
395 sin.sin_family = hp->h_addrtype;
396 bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length);
397
398 msg.tsp_type = TSP_TEST;
399 msg.tsp_vers = TSPVERSION;
400 (void)gethostname(myname, sizeof(myname));
401 (void)strncpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
402 bytenetorder(&msg);
403 if (sendto(sock, &msg, sizeof(struct tsp), 0,
404 (struct sockaddr*)&sin,
405 sizeof(struct sockaddr)) < 0) {
388 argc--; argv++;
389 continue;
390 }
391 sin.sin_port = srvp->s_port;
392 sin.sin_family = hp->h_addrtype;
393 bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length);
394
395 msg.tsp_type = TSP_TEST;
396 msg.tsp_vers = TSPVERSION;
397 (void)gethostname(myname, sizeof(myname));
398 (void)strncpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
399 bytenetorder(&msg);
400 if (sendto(sock, &msg, sizeof(struct tsp), 0,
401 (struct sockaddr*)&sin,
402 sizeof(struct sockaddr)) < 0) {
406 perror("sendto");
403 warn("sendto");
407 }
408 }
409}
410
411
412/*
413 * Enables or disables tracing on local timedaemon
414 */

--- 8 unchanged lines hidden (view full) ---

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) {
404 }
405 }
406}
407
408
409/*
410 * Enables or disables tracing on local timedaemon
411 */

--- 8 unchanged lines hidden (view full) ---

420 fd_set ready;
421 struct sockaddr_in dest;
422 struct sockaddr from;
423 struct timeval tout;
424 struct tsp msg;
425 struct servent *srvp;
426
427 if (argc != 2) {
431 printf("Usage: tracing { on | off }\n");
428 printf("usage: timedc trace { on | off }\n");
432 return;
433 }
434
435 srvp = getservbyname("timed", "udp");
436 if (srvp == 0) {
429 return;
430 }
431
432 srvp = getservbyname("timed", "udp");
433 if (srvp == 0) {
437 fprintf(stderr, "udp/timed: unknown service\n");
434 warnx("udp/timed: unknown service");
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
435 return;
436 }
437 dest.sin_port = srvp->s_port;
438 dest.sin_family = AF_INET;
439
440 (void)gethostname(myname,sizeof(myname));
441 hp = gethostbyname(myname);
442 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
443
444 if (strcmp(argv[1], "on") == 0) {
445 msg.tsp_type = TSP_TRACEON;
446 onflag = ON;
447 } else {
448 msg.tsp_type = TSP_TRACEOFF;
449 onflag = OFF;
450 }
451
455 (void)strcpy(msg.tsp_name, myname);
452 (void)strncpy(msg.tsp_name, myname, sizeof msg.tsp_name-1);
453 msg.tsp_name[sizeof msg.tsp_name-1] = '\0';
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) {
454 msg.tsp_vers = TSPVERSION;
455 bytenetorder(&msg);
456 if (sendto(sock, &msg, sizeof(struct tsp), 0,
457 (struct sockaddr*)&dest, sizeof(struct sockaddr)) < 0) {
460 perror("sendto");
458 warn("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) {
459 return;
460 }
461
462 tout.tv_sec = 5;
463 tout.tv_usec = 0;
464 FD_ZERO(&ready);
465 FD_SET(sock, &ready);
466 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout)) {
467 length = sizeof(struct sockaddr);
468 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
469 &from, &length);
470 if (cc < 0) {
473 perror("recvfrom");
471 warn("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");

--- 7 unchanged lines hidden (view full) ---

489int
490priv_resources()
491{
492 int port;
493 struct sockaddr_in sin;
494
495 sock = socket(AF_INET, SOCK_DGRAM, 0);
496 if (sock < 0) {
472 return;
473 }
474 bytehostorder(&msg);
475 if (msg.tsp_type == TSP_ACK)
476 if (onflag)
477 printf("timed tracing enabled\n");
478 else
479 printf("timed tracing disabled\n");

--- 7 unchanged lines hidden (view full) ---

487int
488priv_resources()
489{
490 int port;
491 struct sockaddr_in sin;
492
493 sock = socket(AF_INET, SOCK_DGRAM, 0);
494 if (sock < 0) {
497 perror("opening socket");
495 warn("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) {
496 return(-1);
497 }
498
499 sin.sin_family = AF_INET;
500 sin.sin_addr.s_addr = 0;
501 for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
502 sin.sin_port = htons((u_short)port);
503 if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0)
504 break;
505 if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
508 perror("bind");
506 warn("bind");
509 (void) close(sock);
510 return(-1);
511 }
512 }
513 if (port == IPPORT_RESERVED / 2) {
507 (void) close(sock);
508 return(-1);
509 }
510 }
511 if (port == IPPORT_RESERVED / 2) {
514 fprintf(stderr, "all reserved ports in use\n");
512 warnx("all reserved ports in use");
515 (void) close(sock);
516 return(-1);
517 }
518
519 sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
520 if (sock_raw < 0) {
513 (void) close(sock);
514 return(-1);
515 }
516
517 sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
518 if (sock_raw < 0) {
521 perror("opening raw socket");
519 warn("opening raw socket");
522 (void) close(sock);
523 return(-1);
524 }
525 return(1);
526}
520 (void) close(sock);
521 return(-1);
522 }
523 return(1);
524}