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