Home | History | Annotate | Line # | Download | only in timedc
cmds.c revision 1.14
      1  1.14      agc /*	$NetBSD: cmds.c,v 1.14 2003/08/07 11:25:47 agc 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.14      agc __RCSID("$NetBSD: cmds.c,v 1.14 2003/08/07 11:25:47 agc 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.3      cgd 
     52   1.1      cgd #define TSPTYPES
     53   1.1      cgd #include <protocols/timed.h>
     54   1.3      cgd 
     55   1.3      cgd #define	SECHR	(60*60)
     56   1.3      cgd #define	SECDAY	(24*SECHR)
     57   1.3      cgd 
     58   1.3      cgd # define DATE_PROTO "udp"
     59   1.3      cgd # define DATE_PORT "time"
     60   1.3      cgd 
     61   1.1      cgd 
     62   1.1      cgd int sock;
     63   1.1      cgd int sock_raw;
     64   1.9      mrg char myname[MAXHOSTNAMELEN + 1];
     65   1.3      cgd struct hostent *hp;
     66   1.1      cgd struct sockaddr_in server;
     67   1.3      cgd struct sockaddr_in dayaddr;
     68   1.1      cgd extern int measure_delta;
     69   1.3      cgd 
     70   1.3      cgd void bytenetorder(struct tsp *);
     71   1.3      cgd void bytehostorder(struct tsp *);
     72   1.3      cgd 
     73   1.3      cgd 
     74   1.4  mycroft #define BU ((unsigned long)2208988800U)	/* seconds before UNIX epoch */
     75   1.3      cgd 
     76   1.3      cgd 
     77   1.3      cgd /* compute the difference between our date and another machine
     78   1.3      cgd  */
     79   1.3      cgd static int				/* difference in days from our time */
     80   1.3      cgd daydiff(char *hostname)
     81   1.3      cgd {
     82   1.3      cgd 	int i;
     83   1.3      cgd 	int trials;
     84  1.12  mycroft 	int tout;
     85  1.12  mycroft 	struct timeval now;
     86  1.12  mycroft 	struct pollfd set[1];
     87   1.3      cgd 	struct sockaddr from;
     88   1.3      cgd 	int fromlen;
     89   1.3      cgd 	unsigned long sec;
     90   1.3      cgd 
     91   1.3      cgd 
     92   1.3      cgd 	/* wait 2 seconds between 10 tries */
     93  1.12  mycroft 	tout = 2000;
     94  1.12  mycroft 	set[0].fd = sock;
     95  1.12  mycroft 	set[0].events = POLLIN;
     96   1.3      cgd 	for (trials = 0; trials < 10; trials++) {
     97   1.3      cgd 		/* ask for the time */
     98   1.3      cgd 		sec = 0;
     99   1.3      cgd 		if (sendto(sock, &sec, sizeof(sec), 0,
    100   1.3      cgd 			   (struct sockaddr*)&dayaddr, sizeof(dayaddr)) < 0) {
    101   1.3      cgd 			perror("sendto(sock)");
    102   1.3      cgd 			return 0;
    103   1.3      cgd 		}
    104   1.3      cgd 
    105   1.3      cgd 		for (;;) {
    106  1.12  mycroft 			i = poll(set, 1, tout);
    107   1.3      cgd 			if (i < 0) {
    108   1.3      cgd 				if (errno == EINTR)
    109   1.3      cgd 					continue;
    110  1.12  mycroft 				perror("poll(date read)");
    111   1.3      cgd 				return 0;
    112   1.3      cgd 			}
    113   1.3      cgd 			if (0 == i)
    114   1.3      cgd 				break;
    115   1.3      cgd 
    116   1.3      cgd 			fromlen = sizeof(from);
    117   1.3      cgd 			if (recvfrom(sock,&sec,sizeof(sec),0,
    118   1.3      cgd 				     &from,&fromlen) < 0) {
    119   1.3      cgd 				perror("recvfrom(date read)");
    120   1.3      cgd 				return 0;
    121   1.3      cgd 			}
    122   1.3      cgd 
    123   1.3      cgd 			sec = ntohl(sec);
    124   1.3      cgd 			if (sec < BU) {
    125   1.3      cgd 				fprintf(stderr,
    126   1.3      cgd 					"%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.3      cgd 	fprintf(stderr,"%s will not tell us the date\n", 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.3      cgd 	register int avg_cnt;
    170   1.3      cgd 	register 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.3      cgd 		(void)fprintf(stderr, "%s/%s is an unknown service\n",
    185   1.3      cgd 			      DATE_PORT, DATE_PROTO);
    186   1.3      cgd 		dayaddr.sin_port = 0;
    187   1.3      cgd 	} else {
    188   1.3      cgd 		dayaddr.sin_port = sp->s_port;
    189   1.3      cgd 	}
    190   1.1      cgd 
    191   1.1      cgd 	while (argc > 1) {
    192   1.1      cgd 		argc--; argv++;
    193   1.1      cgd 		hp = gethostbyname(*argv);
    194   1.1      cgd 		if (hp == NULL) {
    195   1.3      cgd 			fprintf(stderr, "timedc: %s: ", *argv);
    196   1.3      cgd 			herror(0);
    197   1.1      cgd 			continue;
    198   1.1      cgd 		}
    199   1.3      cgd 
    200   1.1      cgd 		server.sin_family = hp->h_addrtype;
    201   1.3      cgd 		bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
    202   1.3      cgd 		for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
    203   1.3      cgd 			measure_status = measure(10000,100, *argv, &server, 1);
    204   1.3      cgd 			if (measure_status != GOOD)
    205   1.3      cgd 				break;
    206   1.3      cgd 			avg += measure_delta;
    207   1.1      cgd 		}
    208   1.3      cgd 		if (measure_status == GOOD)
    209   1.3      cgd 			measure_delta = avg/avg_cnt;
    210   1.3      cgd 
    211   1.1      cgd 		switch (measure_status) {
    212   1.1      cgd 		case HOSTDOWN:
    213   1.1      cgd 			printf("%s is down\n", hp->h_name);
    214   1.1      cgd 			continue;
    215   1.1      cgd 		case NONSTDTIME:
    216   1.3      cgd 			printf("%s transmitts a non-standard time format\n",
    217   1.3      cgd 			       hp->h_name);
    218   1.1      cgd 			continue;
    219   1.1      cgd 		case UNREACHABLE:
    220   1.1      cgd 			printf("%s is unreachable\n", hp->h_name);
    221   1.1      cgd 			continue;
    222   1.3      cgd 		}
    223   1.3      cgd 
    224   1.3      cgd 		/*
    225   1.3      cgd 		 * Try to get the date only after using ICMP timestamps to
    226   1.3      cgd 		 * get the time.  This is because the date protocol
    227   1.3      cgd 		 * is optional.
    228   1.3      cgd 		 */
    229   1.3      cgd 		if (dayaddr.sin_port != 0) {
    230   1.3      cgd 			dayaddr.sin_family = hp->h_addrtype;
    231   1.3      cgd 			bcopy(hp->h_addr, &dayaddr.sin_addr.s_addr,
    232   1.3      cgd 			      hp->h_length);
    233   1.3      cgd 			avg = daydiff(*argv);
    234   1.3      cgd 			if (avg > SECDAY) {
    235   1.3      cgd 				printf("time on %s is %ld days ahead %s\n",
    236   1.3      cgd 				       hp->h_name, avg/SECDAY, myname);
    237   1.3      cgd 				continue;
    238   1.3      cgd 			} else if (avg < -SECDAY) {
    239   1.3      cgd 				printf("time on %s is %ld days behind %s\n",
    240   1.3      cgd 				       hp->h_name, -avg/SECDAY, myname);
    241   1.3      cgd 				continue;
    242   1.3      cgd 			}
    243   1.1      cgd 		}
    244   1.1      cgd 
    245   1.3      cgd 		if (measure_delta > 0) {
    246   1.3      cgd 			printf("time on %s is %d ms. ahead of time on %s\n",
    247   1.3      cgd 			       hp->h_name, measure_delta, myname);
    248   1.3      cgd 		} else if (measure_delta == 0) {
    249   1.3      cgd 			printf("%s and %s have the same time\n",
    250   1.3      cgd 			       hp->h_name, myname);
    251   1.3      cgd 		} else {
    252   1.3      cgd 			printf("time on %s is %d ms. behind time on %s\n",
    253   1.3      cgd 			       hp->h_name, -measure_delta, myname);
    254   1.3      cgd 		}
    255   1.1      cgd 	}
    256   1.1      cgd 	return;
    257   1.1      cgd }
    258   1.3      cgd 
    259   1.3      cgd 
    260   1.1      cgd /*
    261   1.1      cgd  * finds location of master timedaemon
    262   1.1      cgd  */
    263   1.3      cgd void
    264   1.3      cgd msite(int argc, char *argv[])
    265   1.1      cgd {
    266   1.1      cgd 	int cc;
    267  1.12  mycroft 	struct pollfd set[1];
    268   1.1      cgd 	struct sockaddr_in dest;
    269   1.3      cgd 	int i, 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.1      cgd 		fprintf(stderr, "udp/timed: unknown service\n");
    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.3      cgd 			fprintf(stderr, "timedc: %s: ", tgtname);
    299   1.3      cgd 			herror(0);
    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.3      cgd 			perror("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.3      cgd 				perror("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.1      cgd 		fprintf(stderr, "udp/timed: unknown service\n");
    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.3      cgd 			fprintf(stderr, "timedc: %s: ", *argv);
    376   1.3      cgd 			herror(0);
    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.1      cgd 			perror("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.1      cgd 	int 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.1      cgd 		fprintf(stderr, "udp/timed: unknown service\n");
    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.1      cgd 		perror("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.1      cgd 			perror("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 	struct sockaddr_in sin;
    479   1.1      cgd 
    480   1.1      cgd 	sock = socket(AF_INET, SOCK_DGRAM, 0);
    481   1.1      cgd 	if (sock < 0) {
    482   1.1      cgd 		perror("opening socket");
    483   1.1      cgd 		return(-1);
    484   1.1      cgd 	}
    485   1.1      cgd 
    486   1.1      cgd 	sin.sin_family = AF_INET;
    487   1.1      cgd 	sin.sin_addr.s_addr = 0;
    488   1.1      cgd 	for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
    489   1.1      cgd 		sin.sin_port = htons((u_short)port);
    490   1.3      cgd 		if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0)
    491   1.1      cgd 			break;
    492   1.1      cgd 		if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
    493   1.1      cgd 			perror("bind");
    494   1.1      cgd 			(void) close(sock);
    495   1.1      cgd 			return(-1);
    496   1.1      cgd 		}
    497   1.1      cgd 	}
    498   1.1      cgd 	if (port == IPPORT_RESERVED / 2) {
    499   1.1      cgd 		fprintf(stderr, "all reserved ports in use\n");
    500   1.1      cgd 		(void) close(sock);
    501   1.1      cgd 		return(-1);
    502   1.1      cgd 	}
    503   1.1      cgd 
    504   1.3      cgd 	sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
    505   1.1      cgd 	if (sock_raw < 0)  {
    506   1.1      cgd 		perror("opening raw socket");
    507   1.3      cgd 		(void) close(sock);
    508   1.1      cgd 		return(-1);
    509   1.1      cgd 	}
    510   1.1      cgd 	return(1);
    511   1.1      cgd }
    512