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