xref: /netbsd/usr.sbin/rwhod/rwhod.c (revision b55bc39b)
1e541169cScgd /*
2e541169cScgd  * Copyright (c) 1983 The Regents of the University of California.
3e541169cScgd  * All rights reserved.
4e541169cScgd  *
5e541169cScgd  * Redistribution and use in source and binary forms, with or without
6e541169cScgd  * modification, are permitted provided that the following conditions
7e541169cScgd  * are met:
8e541169cScgd  * 1. Redistributions of source code must retain the above copyright
9e541169cScgd  *    notice, this list of conditions and the following disclaimer.
10e541169cScgd  * 2. Redistributions in binary form must reproduce the above copyright
11e541169cScgd  *    notice, this list of conditions and the following disclaimer in the
12e541169cScgd  *    documentation and/or other materials provided with the distribution.
13e541169cScgd  * 3. All advertising materials mentioning features or use of this software
14e541169cScgd  *    must display the following acknowledgement:
15e541169cScgd  *	This product includes software developed by the University of
16e541169cScgd  *	California, Berkeley and its contributors.
17e541169cScgd  * 4. Neither the name of the University nor the names of its contributors
18e541169cScgd  *    may be used to endorse or promote products derived from this software
19e541169cScgd  *    without specific prior written permission.
20e541169cScgd  *
21e541169cScgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22e541169cScgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23e541169cScgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24e541169cScgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25e541169cScgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26e541169cScgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27e541169cScgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28e541169cScgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29e541169cScgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30e541169cScgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31e541169cScgd  * SUCH DAMAGE.
32e541169cScgd  */
33e541169cScgd 
34e541169cScgd #ifndef lint
35e541169cScgd char copyright[] =
36e541169cScgd "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
37e541169cScgd  All rights reserved.\n";
38e541169cScgd #endif /* not lint */
39e541169cScgd 
40e541169cScgd #ifndef lint
41e9d867efSmycroft /*static char sccsid[] = "from: @(#)rwhod.c	5.20 (Berkeley) 3/2/91";*/
42*b55bc39bSandrew static char rcsid[] = "$Id: rwhod.c,v 1.7 1994/04/06 03:01:48 andrew Exp $";
43e541169cScgd #endif /* not lint */
44e541169cScgd 
45e541169cScgd #include <sys/param.h>
46e541169cScgd #include <sys/socket.h>
47e541169cScgd #include <sys/stat.h>
48e541169cScgd #include <sys/signal.h>
49e541169cScgd #include <sys/ioctl.h>
50e541169cScgd #include <sys/file.h>
51e541169cScgd 
52e541169cScgd #include <net/if.h>
53e541169cScgd #include <netinet/in.h>
54e541169cScgd 
55e541169cScgd #include <nlist.h>
56e541169cScgd #include <errno.h>
57e541169cScgd #include <utmp.h>
58e541169cScgd #include <ctype.h>
59e541169cScgd #include <netdb.h>
60e541169cScgd #include <syslog.h>
61e541169cScgd #include <protocols/rwhod.h>
62e541169cScgd #include <stdio.h>
63e541169cScgd #include <paths.h>
64e541169cScgd 
65e541169cScgd /*
66e541169cScgd  * Alarm interval. Don't forget to change the down time check in ruptime
67e541169cScgd  * if this is changed.
68e541169cScgd  */
69e541169cScgd #define AL_INTERVAL (3 * 60)
70e541169cScgd 
715a78b453Smycroft struct	sockaddr_in s_in;
72e541169cScgd 
73e541169cScgd char	myname[MAXHOSTNAMELEN];
74e541169cScgd 
75e541169cScgd struct	nlist nl[] = {
76e541169cScgd #define	NL_BOOTTIME	0
77e541169cScgd 	{ "_boottime" },
78e541169cScgd 	0
79e541169cScgd };
80e541169cScgd 
81e541169cScgd /*
82e541169cScgd  * We communicate with each neighbor in
83e541169cScgd  * a list constructed at the time we're
84e541169cScgd  * started up.  Neighbors are currently
85e541169cScgd  * directly connected via a hardware interface.
86e541169cScgd  */
87e541169cScgd struct	neighbor {
88e541169cScgd 	struct	neighbor *n_next;
89e541169cScgd 	char	*n_name;		/* interface name */
90e541169cScgd 	char	*n_addr;		/* who to send to */
91e541169cScgd 	int	n_addrlen;		/* size of address */
92e541169cScgd 	int	n_flags;		/* should forward?, interface flags */
93e541169cScgd };
94e541169cScgd 
95e541169cScgd struct	neighbor *neighbors;
96e541169cScgd struct	whod mywd;
97e541169cScgd struct	servent *sp;
98*b55bc39bSandrew int	s, utmpf;
99e541169cScgd 
100e541169cScgd #define	WHDRSIZE	(sizeof (mywd) - sizeof (mywd.wd_we))
101e541169cScgd 
102e541169cScgd extern int errno;
103e541169cScgd char	*strcpy(), *malloc();
104e541169cScgd void	getkmem(), onalrm();
105e541169cScgd struct	in_addr inet_makeaddr();
106e541169cScgd 
107e541169cScgd main()
108e541169cScgd {
109e541169cScgd 	struct sockaddr_in from;
110e541169cScgd 	struct stat st;
111e541169cScgd 	char path[64];
112e541169cScgd 	int on = 1;
113e541169cScgd 	char *cp, *index(), *strerror();
114e541169cScgd 
115e541169cScgd 	if (getuid()) {
116e541169cScgd 		fprintf(stderr, "rwhod: not super user\n");
117e541169cScgd 		exit(1);
118e541169cScgd 	}
119e541169cScgd 	sp = getservbyname("who", "udp");
120e541169cScgd 	if (sp == 0) {
121e541169cScgd 		fprintf(stderr, "rwhod: udp/who: unknown service\n");
122e541169cScgd 		exit(1);
123e541169cScgd 	}
124e541169cScgd #ifndef DEBUG
125e541169cScgd 	daemon(1, 0);
126e541169cScgd #endif
127e541169cScgd 	if (chdir(_PATH_RWHODIR) < 0) {
128e541169cScgd 		(void)fprintf(stderr, "rwhod: %s: %s\n",
129e541169cScgd 		    _PATH_RWHODIR, strerror(errno));
130e541169cScgd 		exit(1);
131e541169cScgd 	}
132e541169cScgd 	(void) signal(SIGHUP, getkmem);
133e541169cScgd 	openlog("rwhod", LOG_PID, LOG_DAEMON);
134e541169cScgd 	/*
135e541169cScgd 	 * Establish host name as returned by system.
136e541169cScgd 	 */
137e541169cScgd 	if (gethostname(myname, sizeof (myname) - 1) < 0) {
138e541169cScgd 		syslog(LOG_ERR, "gethostname: %m");
139e541169cScgd 		exit(1);
140e541169cScgd 	}
141e541169cScgd 	if ((cp = index(myname, '.')) != NULL)
142e541169cScgd 		*cp = '\0';
143e541169cScgd 	strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
144e541169cScgd 	utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
145e541169cScgd 	if (utmpf < 0) {
146e541169cScgd 		syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
147e541169cScgd 		exit(1);
148e541169cScgd 	}
149e541169cScgd 	getkmem();
150e541169cScgd 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
151e541169cScgd 		syslog(LOG_ERR, "socket: %m");
152e541169cScgd 		exit(1);
153e541169cScgd 	}
154e541169cScgd 	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
155e541169cScgd 		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
156e541169cScgd 		exit(1);
157e541169cScgd 	}
1585a78b453Smycroft 	s_in.sin_family = AF_INET;
1595a78b453Smycroft 	s_in.sin_port = sp->s_port;
1605a78b453Smycroft 	if (bind(s, (struct sockaddr *)&s_in, sizeof (s_in)) < 0) {
161e541169cScgd 		syslog(LOG_ERR, "bind: %m");
162e541169cScgd 		exit(1);
163e541169cScgd 	}
164e541169cScgd 	if (!configure(s))
165e541169cScgd 		exit(1);
166e541169cScgd 	signal(SIGALRM, onalrm);
167e541169cScgd 	onalrm();
168e541169cScgd 	for (;;) {
169e541169cScgd 		struct whod wd;
170e541169cScgd 		int cc, whod, len = sizeof (from);
171e541169cScgd 
172e541169cScgd 		cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
173e541169cScgd 			(struct sockaddr *)&from, &len);
174e541169cScgd 		if (cc <= 0) {
175e541169cScgd 			if (cc < 0 && errno != EINTR)
176e541169cScgd 				syslog(LOG_WARNING, "recv: %m");
177e541169cScgd 			continue;
178e541169cScgd 		}
179e541169cScgd 		if (from.sin_port != sp->s_port) {
180e541169cScgd 			syslog(LOG_WARNING, "%d: bad from port",
181e541169cScgd 				ntohs(from.sin_port));
182e541169cScgd 			continue;
183e541169cScgd 		}
184e541169cScgd 		if (wd.wd_vers != WHODVERSION)
185e541169cScgd 			continue;
186e541169cScgd 		if (wd.wd_type != WHODTYPE_STATUS)
187e541169cScgd 			continue;
188e541169cScgd 		if (!verify(wd.wd_hostname)) {
189e541169cScgd 			syslog(LOG_WARNING, "malformed host name from %x",
190e541169cScgd 				from.sin_addr);
191e541169cScgd 			continue;
192e541169cScgd 		}
193e541169cScgd 		(void) sprintf(path, "whod.%s", wd.wd_hostname);
194e541169cScgd 		/*
195e541169cScgd 		 * Rather than truncating and growing the file each time,
196e541169cScgd 		 * use ftruncate if size is less than previous size.
197e541169cScgd 		 */
198e541169cScgd 		whod = open(path, O_WRONLY | O_CREAT, 0644);
199e541169cScgd 		if (whod < 0) {
200e541169cScgd 			syslog(LOG_WARNING, "%s: %m", path);
201e541169cScgd 			continue;
202e541169cScgd 		}
203e541169cScgd #if ENDIAN != BIG_ENDIAN
204e541169cScgd 		{
205e541169cScgd 			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
206e541169cScgd 			struct whoent *we;
207e541169cScgd 
208e541169cScgd 			/* undo header byte swapping before writing to file */
209e541169cScgd 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
210e541169cScgd 			for (i = 0; i < 3; i++)
211e541169cScgd 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
212e541169cScgd 			wd.wd_boottime = ntohl(wd.wd_boottime);
213e541169cScgd 			we = wd.wd_we;
214e541169cScgd 			for (i = 0; i < n; i++) {
215e541169cScgd 				we->we_idle = ntohl(we->we_idle);
216e541169cScgd 				we->we_utmp.out_time =
217e541169cScgd 				    ntohl(we->we_utmp.out_time);
218e541169cScgd 				we++;
219e541169cScgd 			}
220e541169cScgd 		}
221e541169cScgd #endif
222e541169cScgd 		(void) time((time_t *)&wd.wd_recvtime);
223e541169cScgd 		(void) write(whod, (char *)&wd, cc);
224e541169cScgd 		if (fstat(whod, &st) < 0 || st.st_size > cc)
225e541169cScgd 			ftruncate(whod, cc);
226e541169cScgd 		(void) close(whod);
227e541169cScgd 	}
228e541169cScgd }
229e541169cScgd 
230e541169cScgd /*
231e541169cScgd  * Check out host name for unprintables
232e541169cScgd  * and other funnies before allowing a file
233e541169cScgd  * to be created.  Sorry, but blanks aren't allowed.
234e541169cScgd  */
235e541169cScgd verify(name)
236e541169cScgd 	register char *name;
237e541169cScgd {
238e541169cScgd 	register int size = 0;
239e541169cScgd 
240e541169cScgd 	while (*name) {
241e541169cScgd 		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
242e541169cScgd 			return (0);
243e541169cScgd 		name++, size++;
244e541169cScgd 	}
245e541169cScgd 	return (size > 0);
246e541169cScgd }
247e541169cScgd 
248e541169cScgd int	utmptime;
249e541169cScgd int	utmpent;
250e541169cScgd int	utmpsize = 0;
251e541169cScgd struct	utmp *utmp;
252e541169cScgd int	alarmcount;
253e541169cScgd 
254e541169cScgd void
255e541169cScgd onalrm()
256e541169cScgd {
257e541169cScgd 	register struct neighbor *np;
258e541169cScgd 	register struct whoent *we = mywd.wd_we, *wlast;
259e541169cScgd 	register int i;
260e541169cScgd 	struct stat stb;
261e541169cScgd 	int cc;
262e541169cScgd 	double avenrun[3];
263e541169cScgd 	time_t now = time((time_t *)NULL);
264e541169cScgd 	char *strerror();
265e541169cScgd 
266e541169cScgd 	if (alarmcount % 10 == 0)
267e541169cScgd 		getkmem();
268e541169cScgd 	alarmcount++;
269e541169cScgd 	(void) fstat(utmpf, &stb);
270e541169cScgd 	if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
271e541169cScgd 		utmptime = stb.st_mtime;
272e541169cScgd 		if (stb.st_size > utmpsize) {
273e541169cScgd 			utmpsize = stb.st_size + 10 * sizeof(struct utmp);
274e541169cScgd 			if (utmp)
275e541169cScgd 				utmp = (struct utmp *)realloc(utmp, utmpsize);
276e541169cScgd 			else
277e541169cScgd 				utmp = (struct utmp *)malloc(utmpsize);
278e541169cScgd 			if (! utmp) {
279e541169cScgd 				fprintf(stderr, "rwhod: malloc failed\n");
280e541169cScgd 				utmpsize = 0;
281e541169cScgd 				goto done;
282e541169cScgd 			}
283e541169cScgd 		}
2847a6e8896Scgd 		(void) lseek(utmpf, 0, L_SET);
285e541169cScgd 		cc = read(utmpf, (char *)utmp, stb.st_size);
286e541169cScgd 		if (cc < 0) {
287e541169cScgd 			fprintf(stderr, "rwhod: %s: %s\n",
288e541169cScgd 			    _PATH_UTMP, strerror(errno));
289e541169cScgd 			goto done;
290e541169cScgd 		}
291e541169cScgd 		wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
292e541169cScgd 		utmpent = cc / sizeof (struct utmp);
293e541169cScgd 		for (i = 0; i < utmpent; i++)
294e541169cScgd 			if (utmp[i].ut_name[0]) {
295e541169cScgd 				bcopy(utmp[i].ut_line, we->we_utmp.out_line,
296e541169cScgd 				   sizeof (utmp[i].ut_line));
297e541169cScgd 				bcopy(utmp[i].ut_name, we->we_utmp.out_name,
298e541169cScgd 				   sizeof (utmp[i].ut_name));
299e541169cScgd 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
300e541169cScgd 				if (we >= wlast)
301e541169cScgd 					break;
302e541169cScgd 				we++;
303e541169cScgd 			}
304e541169cScgd 		utmpent = we - mywd.wd_we;
305e541169cScgd 	}
306e541169cScgd 
307e541169cScgd 	/*
308e541169cScgd 	 * The test on utmpent looks silly---after all, if no one is
309e541169cScgd 	 * logged on, why worry about efficiency?---but is useful on
310e541169cScgd 	 * (e.g.) compute servers.
311e541169cScgd 	 */
312e541169cScgd 	if (utmpent && chdir(_PATH_DEV)) {
313e541169cScgd 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
314e541169cScgd 		exit(1);
315e541169cScgd 	}
316e541169cScgd 	we = mywd.wd_we;
317e541169cScgd 	for (i = 0; i < utmpent; i++) {
318e541169cScgd 		if (stat(we->we_utmp.out_line, &stb) >= 0)
319e541169cScgd 			we->we_idle = htonl(now - stb.st_atime);
320e541169cScgd 		we++;
321e541169cScgd 	}
322e541169cScgd 	(void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
323e541169cScgd 	for (i = 0; i < 3; i++)
324e541169cScgd 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
325e541169cScgd 	cc = (char *)we - (char *)&mywd;
326e541169cScgd 	mywd.wd_sendtime = htonl(time(0));
327e541169cScgd 	mywd.wd_vers = WHODVERSION;
328e541169cScgd 	mywd.wd_type = WHODTYPE_STATUS;
329e541169cScgd 	for (np = neighbors; np != NULL; np = np->n_next)
330e541169cScgd 		(void) sendto(s, (char *)&mywd, cc, 0,
331e541169cScgd 			(struct sockaddr *)np->n_addr, np->n_addrlen);
332e541169cScgd 	if (utmpent && chdir(_PATH_RWHODIR)) {
333e541169cScgd 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
334e541169cScgd 		exit(1);
335e541169cScgd 	}
336e541169cScgd done:
337e541169cScgd 	(void) alarm(AL_INTERVAL);
338e541169cScgd }
339e541169cScgd 
340e541169cScgd void
341e541169cScgd getkmem()
342e541169cScgd {
343e541169cScgd 	static ino_t vmunixino;
344e541169cScgd 	static time_t vmunixctime;
345e541169cScgd 	struct stat sb;
346e541169cScgd 
347e541169cScgd 	if (stat(_PATH_UNIX, &sb) < 0) {
348e541169cScgd 		if (vmunixctime)
349e541169cScgd 			return;
350e541169cScgd 	} else {
351e541169cScgd 		if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
352e541169cScgd 			return;
353e541169cScgd 		vmunixctime = sb.st_ctime;
354e541169cScgd 		vmunixino= sb.st_ino;
355e541169cScgd 	}
356*b55bc39bSandrew 
357*b55bc39bSandrew 	if (kvm_nlist(nl)) {
358e541169cScgd 		syslog(LOG_WARNING, "%s: namelist botch", _PATH_UNIX);
359*b55bc39bSandrew 		mywd.wd_boottime = 0;
360*b55bc39bSandrew 		return;
361e541169cScgd 	}
362*b55bc39bSandrew 	(void) kvm_read((void *)nl[NL_BOOTTIME].n_value, &mywd.wd_boottime,
363e541169cScgd 	    sizeof (mywd.wd_boottime));
364e541169cScgd 	mywd.wd_boottime = htonl(mywd.wd_boottime);
365e541169cScgd }
366e541169cScgd 
367e541169cScgd /*
368e541169cScgd  * Figure out device configuration and select
369e541169cScgd  * networks which deserve status information.
370e541169cScgd  */
371e541169cScgd configure(s)
372e541169cScgd 	int s;
373e541169cScgd {
374e541169cScgd 	char buf[BUFSIZ], *cp, *cplim;
375e541169cScgd 	struct ifconf ifc;
376e541169cScgd 	struct ifreq ifreq, *ifr;
3775a78b453Smycroft 	struct sockaddr_in *s_in;
378e541169cScgd 	register struct neighbor *np;
379e541169cScgd 
380e541169cScgd 	ifc.ifc_len = sizeof (buf);
381e541169cScgd 	ifc.ifc_buf = buf;
382e541169cScgd 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
383e541169cScgd 		syslog(LOG_ERR, "ioctl (get interface configuration)");
384e541169cScgd 		return (0);
385e541169cScgd 	}
386e541169cScgd 	ifr = ifc.ifc_req;
387e541169cScgd #ifdef AF_LINK
388e541169cScgd #define max(a, b) (a > b ? a : b)
389e541169cScgd #define size(p)	max((p).sa_len, sizeof(p))
390e541169cScgd #else
391e541169cScgd #define size(p) (sizeof (p))
392e541169cScgd #endif
393e541169cScgd 	cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
394e541169cScgd 	for (cp = buf; cp < cplim;
395e541169cScgd 			cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
396e541169cScgd 		ifr = (struct ifreq *)cp;
397e541169cScgd 		for (np = neighbors; np != NULL; np = np->n_next)
398e541169cScgd 			if (np->n_name &&
399e541169cScgd 			    strcmp(ifr->ifr_name, np->n_name) == 0)
400e541169cScgd 				break;
401e541169cScgd 		if (np != NULL)
402e541169cScgd 			continue;
403e541169cScgd 		ifreq = *ifr;
404e541169cScgd 		np = (struct neighbor *)malloc(sizeof (*np));
405e541169cScgd 		if (np == NULL)
406e541169cScgd 			continue;
407e541169cScgd 		np->n_name = malloc(strlen(ifr->ifr_name) + 1);
408e541169cScgd 		if (np->n_name == NULL) {
409e541169cScgd 			free((char *)np);
410e541169cScgd 			continue;
411e541169cScgd 		}
412e541169cScgd 		strcpy(np->n_name, ifr->ifr_name);
413e541169cScgd 		np->n_addrlen = sizeof (ifr->ifr_addr);
414e541169cScgd 		np->n_addr = malloc(np->n_addrlen);
415e541169cScgd 		if (np->n_addr == NULL) {
416e541169cScgd 			free(np->n_name);
417e541169cScgd 			free((char *)np);
418e541169cScgd 			continue;
419e541169cScgd 		}
420e541169cScgd 		bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
421e541169cScgd 		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
422e541169cScgd 			syslog(LOG_ERR, "ioctl (get interface flags)");
423aa14f04fSmycroft 			free(np->n_addr);
424aa14f04fSmycroft 			free(np->n_name);
425e541169cScgd 			free((char *)np);
426e541169cScgd 			continue;
427e541169cScgd 		}
428e541169cScgd 		if ((ifreq.ifr_flags & IFF_UP) == 0 ||
429e541169cScgd 		    (ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
430aa14f04fSmycroft 			free(np->n_addr);
431aa14f04fSmycroft 			free(np->n_name);
432e541169cScgd 			free((char *)np);
433e541169cScgd 			continue;
434e541169cScgd 		}
435e541169cScgd 		np->n_flags = ifreq.ifr_flags;
436e541169cScgd 		if (np->n_flags & IFF_POINTOPOINT) {
437e541169cScgd 			if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
438e541169cScgd 				syslog(LOG_ERR, "ioctl (get dstaddr)");
439aa14f04fSmycroft 				free(np->n_addr);
440aa14f04fSmycroft 				free(np->n_name);
441e541169cScgd 				free((char *)np);
442e541169cScgd 				continue;
443e541169cScgd 			}
444e541169cScgd 			/* we assume addresses are all the same size */
445e541169cScgd 			bcopy((char *)&ifreq.ifr_dstaddr,
446e541169cScgd 			  np->n_addr, np->n_addrlen);
447e541169cScgd 		}
448e541169cScgd 		if (np->n_flags & IFF_BROADCAST) {
449e541169cScgd 			if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
450e541169cScgd 				syslog(LOG_ERR, "ioctl (get broadaddr)");
451aa14f04fSmycroft 				free(np->n_addr);
452aa14f04fSmycroft 				free(np->n_name);
453e541169cScgd 				free((char *)np);
454e541169cScgd 				continue;
455e541169cScgd 			}
456e541169cScgd 			/* we assume addresses are all the same size */
457e541169cScgd 			bcopy((char *)&ifreq.ifr_broadaddr,
458e541169cScgd 			  np->n_addr, np->n_addrlen);
459e541169cScgd 		}
460e541169cScgd 		/* gag, wish we could get rid of Internet dependencies */
4615a78b453Smycroft 		s_in = (struct sockaddr_in *)np->n_addr;
4625a78b453Smycroft 		s_in->sin_port = sp->s_port;
463e541169cScgd 		np->n_next = neighbors;
464e541169cScgd 		neighbors = np;
465e541169cScgd 	}
466e541169cScgd 	return (1);
467e541169cScgd }
468e541169cScgd 
469e541169cScgd #ifdef DEBUG
470e541169cScgd sendto(s, buf, cc, flags, to, tolen)
471e541169cScgd 	int s;
472e541169cScgd 	char *buf;
473e541169cScgd 	int cc, flags;
474e541169cScgd 	char *to;
475e541169cScgd 	int tolen;
476e541169cScgd {
477e541169cScgd 	register struct whod *w = (struct whod *)buf;
478e541169cScgd 	register struct whoent *we;
4795a78b453Smycroft 	struct sockaddr_in *s_in = (struct sockaddr_in *)to;
480e541169cScgd 	char *interval();
481e541169cScgd 
4825a78b453Smycroft 	printf("sendto %x.%d\n", ntohl(s_in->sin_addr), ntohs(s_in->sin_port));
483e541169cScgd 	printf("hostname %s %s\n", w->wd_hostname,
484e541169cScgd 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
485e541169cScgd 	printf("load %4.2f, %4.2f, %4.2f\n",
486e541169cScgd 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
487e541169cScgd 	    ntohl(w->wd_loadav[2]) / 100.0);
488e541169cScgd 	cc -= WHDRSIZE;
489e541169cScgd 	for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
490e541169cScgd 		time_t t = ntohl(we->we_utmp.out_time);
491e541169cScgd 		printf("%-8.8s %s:%s %.12s",
492e541169cScgd 			we->we_utmp.out_name,
493e541169cScgd 			w->wd_hostname, we->we_utmp.out_line,
494e541169cScgd 			ctime(&t)+4);
495e541169cScgd 		we->we_idle = ntohl(we->we_idle) / 60;
496e541169cScgd 		if (we->we_idle) {
497e541169cScgd 			if (we->we_idle >= 100*60)
498e541169cScgd 				we->we_idle = 100*60 - 1;
499e541169cScgd 			if (we->we_idle >= 60)
500e541169cScgd 				printf(" %2d", we->we_idle / 60);
501e541169cScgd 			else
502e541169cScgd 				printf("   ");
503e541169cScgd 			printf(":%02d", we->we_idle % 60);
504e541169cScgd 		}
505e541169cScgd 		printf("\n");
506e541169cScgd 	}
507e541169cScgd }
508e541169cScgd 
509e541169cScgd char *
510e541169cScgd interval(time, updown)
511e541169cScgd 	int time;
512e541169cScgd 	char *updown;
513e541169cScgd {
514e541169cScgd 	static char resbuf[32];
515e541169cScgd 	int days, hours, minutes;
516e541169cScgd 
517e541169cScgd 	if (time < 0 || time > 3*30*24*60*60) {
518e541169cScgd 		(void) sprintf(resbuf, "   %s ??:??", updown);
519e541169cScgd 		return (resbuf);
520e541169cScgd 	}
521e541169cScgd 	minutes = (time + 59) / 60;		/* round to minutes */
522e541169cScgd 	hours = minutes / 60; minutes %= 60;
523e541169cScgd 	days = hours / 24; hours %= 24;
524e541169cScgd 	if (days)
525e541169cScgd 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
526e541169cScgd 		    updown, days, hours, minutes);
527e541169cScgd 	else
528e541169cScgd 		(void) sprintf(resbuf, "%s    %2d:%02d",
529e541169cScgd 		    updown, hours, minutes);
530e541169cScgd 	return (resbuf);
531e541169cScgd }
532e541169cScgd #endif
533