xref: /freebsd/usr.sbin/rwhod/rwhod.c (revision 2ad872c5794e4c26fdf6ed219ad3f09ca0d5304a)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)rwhod.c	8.1 (Berkeley) 6/6/93";
43 #endif
44 static const char rcsid[] =
45 	"$Id: rwhod.c,v 1.7 1997/10/13 11:27:55 charnier Exp $";
46 #endif /* not lint */
47 
48 #include <sys/param.h>
49 #include <sys/socket.h>
50 #include <sys/stat.h>
51 #include <sys/signal.h>
52 #include <sys/ioctl.h>
53 #include <sys/sysctl.h>
54 
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <protocols/rwhod.h>
60 
61 #include <ctype.h>
62 #include <err.h>
63 #include <errno.h>
64 #include <fcntl.h>
65 #include <netdb.h>
66 #include <paths.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <syslog.h>
71 #include <unistd.h>
72 #include <utmp.h>
73 #include <pwd.h>
74 #include <grp.h>
75 
76 /*
77  * This version of Berkeley's rwhod has been modified to use IP multicast
78  * datagrams, under control of a new command-line option:
79  *
80  *	rwhod -m	causes rwhod to use IP multicast (instead of
81  *			broadcast or unicast) on all interfaces that have
82  *			the IFF_MULTICAST flag set in their "ifnet" structs
83  *			(excluding the loopback interface).  The multicast
84  *			reports are sent with a time-to-live of 1, to prevent
85  *			forwarding beyond the directly-connected subnet(s).
86  *
87  *	rwhod -m <ttl>	causes rwhod to send IP multicast datagrams with a
88  *			time-to-live of <ttl>, via a SINGLE interface rather
89  *			than all interfaces.  <ttl> must be between 0 and
90  *			MAX_MULTICAST_SCOPE, defined below.  Note that "-m 1"
91  *			is different than "-m", in that "-m 1" specifies
92  *			transmission on one interface only.
93  *
94  * When "-m" is used without a <ttl> argument, the program accepts multicast
95  * rwhod reports from all multicast-capable interfaces.  If a <ttl> argument
96  * is given, it accepts multicast reports from only one interface, the one
97  * on which reports are sent (which may be controlled via the host's routing
98  * table).  Regardless of the "-m" option, the program accepts broadcast or
99  * unicast reports from all interfaces.  Thus, this program will hear the
100  * reports of old, non-multicasting rwhods, but, if multicasting is used,
101  * those old rwhods won't hear the reports generated by this program.
102  *
103  *                  -- Steve Deering, Stanford University, February 1989
104  */
105 
106 #define	UNPRIV_USER		"daemon"
107 #define	UNPRIV_GROUP		"daemon"
108 
109 #define NO_MULTICAST		0	  /* multicast modes */
110 #define PER_INTERFACE_MULTICAST	1
111 #define SCOPED_MULTICAST	2
112 
113 #define MAX_MULTICAST_SCOPE	32	  /* "site-wide", by convention */
114 
115 #define INADDR_WHOD_GROUP (u_long)0xe0000103      /* 224.0.1.3 */
116 					  /* (belongs in protocols/rwhod.h) */
117 
118 int			insecure_mode;
119 int			multicast_mode  = NO_MULTICAST;
120 int			multicast_scope;
121 struct sockaddr_in	multicast_addr  = { sizeof multicast_addr, AF_INET };
122 
123 /*
124  * Alarm interval. Don't forget to change the down time check in ruptime
125  * if this is changed.
126  */
127 #define AL_INTERVAL (3 * 60)
128 
129 char	myname[MAXHOSTNAMELEN];
130 
131 /*
132  * We communicate with each neighbor in a list constructed at the time we're
133  * started up.  Neighbors are currently directly connected via a hardware
134  * interface.
135  */
136 struct	neighbor {
137 	struct	neighbor *n_next;
138 	char	*n_name;		/* interface name */
139 	struct	sockaddr *n_addr;		/* who to send to */
140 	int	n_addrlen;		/* size of address */
141 	int	n_flags;		/* should forward?, interface flags */
142 };
143 
144 struct	neighbor *neighbors;
145 struct	whod mywd;
146 struct	servent *sp;
147 int	s, utmpf;
148 
149 #define	WHDRSIZE	(sizeof(mywd) - sizeof(mywd.wd_we))
150 
151 void	 run_as __P((uid_t *, gid_t *));
152 int	 configure __P((int));
153 void	 getboottime __P((int));
154 void	 onalrm __P((int));
155 void	 quit __P((char *));
156 void	 rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
157 int	 verify __P((char *, int));
158 static void usage __P((void));
159 #ifdef DEBUG
160 char	*interval __P((int, char *));
161 void	 Sendto __P((int, const void *, size_t, int,
162 		     const struct sockaddr *, int));
163 #define	 sendto Sendto
164 #endif
165 
166 int
167 main(argc, argv)
168 	int argc;
169 	char *argv[];
170 {
171 	struct sockaddr_in from;
172 	struct stat st;
173 	char path[64];
174 	int on = 1;
175 	char *cp;
176 	struct sockaddr_in sin;
177 	uid_t unpriv_uid;
178 	gid_t unpriv_gid;
179 
180 	if (getuid())
181 		errx(1, "not super user");
182 
183 	run_as(&unpriv_uid, &unpriv_gid);
184 
185 	argv++; argc--;
186 	while (argc > 0 && *argv[0] == '-') {
187 		if (strcmp(*argv, "-m") == 0) {
188 			if (argc > 1 && isdigit(*(argv + 1)[0])) {
189 				argv++, argc--;
190 				multicast_mode  = SCOPED_MULTICAST;
191 				multicast_scope = atoi(*argv);
192 				if (multicast_scope > MAX_MULTICAST_SCOPE)
193 					errx(1, "ttl must not exceed %u",
194 					MAX_MULTICAST_SCOPE);
195 			}
196 			else multicast_mode = PER_INTERFACE_MULTICAST;
197 		}
198 		else if (strcmp(*argv, "-i") == 0)
199 		    insecure_mode = 1;
200 		else
201 			usage();
202 		argv++, argc--;
203 	}
204 	if (argc > 0)
205 		usage();
206 #ifndef DEBUG
207 	daemon(1, 0);
208 #endif
209 	(void) signal(SIGHUP, getboottime);
210 	openlog("rwhod", LOG_PID, LOG_DAEMON);
211 	sp = getservbyname("who", "udp");
212 	if (sp == NULL) {
213 		syslog(LOG_ERR, "rwhod: udp/who: unknown service\n");
214 		exit(1);
215 	}
216 	if (chdir(_PATH_RWHODIR) < 0) {
217 		syslog(LOG_ERR, "rwhod: %s: %m\n", _PATH_RWHODIR);
218 		exit(1);
219 	}
220 	/*
221 	 * Establish host name as returned by system.
222 	 */
223 	if (gethostname(myname, sizeof(myname) - 1) < 0) {
224 		syslog(LOG_ERR, "gethostname: %m");
225 		exit(1);
226 	}
227 	if ((cp = index(myname, '.')) != NULL)
228 		*cp = '\0';
229 	strncpy(mywd.wd_hostname, myname, sizeof(mywd.wd_hostname) - 1);
230 	mywd.wd_hostname[sizeof(mywd.wd_hostname) - 1] = '\0';
231 	utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
232 	if (utmpf < 0) {
233 		syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
234 		exit(1);
235 	}
236 	getboottime(0);
237 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
238 		syslog(LOG_ERR, "socket: %m");
239 		exit(1);
240 	}
241 	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
242 		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
243 		exit(1);
244 	}
245 	memset(&sin, 0, sizeof(sin));
246 	sin.sin_len = sizeof(sin);
247 	sin.sin_family = AF_INET;
248 	sin.sin_port = sp->s_port;
249 	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
250 		syslog(LOG_ERR, "bind: %m");
251 		exit(1);
252 	}
253 	setgid(unpriv_gid);
254 	setgroups(1, &unpriv_gid);	/* XXX BOGUS groups[0] = egid */
255 	setuid(unpriv_uid);
256 	if (!configure(s))
257 		exit(1);
258 	signal(SIGALRM, onalrm);
259 	onalrm(0);
260 	for (;;) {
261 		struct whod wd;
262 		int cc, whod, len = sizeof(from);
263 
264 		cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0,
265 			(struct sockaddr *)&from, &len);
266 		if (cc <= 0) {
267 			if (cc < 0 && errno != EINTR)
268 				syslog(LOG_WARNING, "recv: %m");
269 			continue;
270 		}
271 		if (from.sin_port != sp->s_port && !insecure_mode) {
272 			syslog(LOG_WARNING, "%d: bad from port",
273 				ntohs(from.sin_port));
274 			continue;
275 		}
276 		if (wd.wd_vers != WHODVERSION)
277 			continue;
278 		if (wd.wd_type != WHODTYPE_STATUS)
279 			continue;
280 		if (!verify(wd.wd_hostname, sizeof wd.wd_hostname)) {
281 			syslog(LOG_WARNING, "malformed host name from %x",
282 				from.sin_addr);
283 			continue;
284 		}
285 		(void) snprintf(path, sizeof path, "whod.%s", wd.wd_hostname);
286 		/*
287 		 * Rather than truncating and growing the file each time,
288 		 * use ftruncate if size is less than previous size.
289 		 */
290 		whod = open(path, O_WRONLY | O_CREAT, 0644);
291 		if (whod < 0) {
292 			syslog(LOG_WARNING, "%s: %m", path);
293 			continue;
294 		}
295 #if ENDIAN != BIG_ENDIAN
296 		{
297 			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
298 			struct whoent *we;
299 
300 			/* undo header byte swapping before writing to file */
301 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
302 			for (i = 0; i < 3; i++)
303 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
304 			wd.wd_boottime = ntohl(wd.wd_boottime);
305 			we = wd.wd_we;
306 			for (i = 0; i < n; i++) {
307 				we->we_idle = ntohl(we->we_idle);
308 				we->we_utmp.out_time =
309 				    ntohl(we->we_utmp.out_time);
310 				we++;
311 			}
312 		}
313 #endif
314 		(void) time((time_t *)&wd.wd_recvtime);
315 		(void) write(whod, (char *)&wd, cc);
316 		if (fstat(whod, &st) < 0 || st.st_size > cc)
317 			ftruncate(whod, cc);
318 		(void) close(whod);
319 	}
320 }
321 
322 static void
323 usage()
324 {
325 	fprintf(stderr, "usage: rwhod [-i] [-m [ttl]]\n");
326 	exit(1);
327 }
328 
329 void
330 run_as(uid, gid)
331 	uid_t *uid;
332 	gid_t *gid;
333 {
334 	struct passwd *pw;
335 	struct group *gr;
336 
337 	pw = getpwnam(UNPRIV_USER);
338 	if (!pw) {
339 		syslog(LOG_ERR, "getpwnam(%s): %m", UNPRIV_USER);
340 		exit(1);
341 	}
342 	*uid = pw->pw_uid;
343 
344 	gr = getgrnam(UNPRIV_GROUP);
345 	if (!gr) {
346 		syslog(LOG_ERR, "getgrnam(%s): %m", UNPRIV_GROUP);
347 		exit(1);
348 	}
349 	*gid = gr->gr_gid;
350 }
351 
352 /*
353  * Check out host name for unprintables
354  * and other funnies before allowing a file
355  * to be created.  Sorry, but blanks aren't allowed.
356  */
357 int
358 verify(name, maxlen)
359 	register char *name;
360 	register int   maxlen;
361 {
362 	register int size = 0;
363 
364 	while (*name && size < maxlen - 1) {
365 		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
366 			return (0);
367 		name++, size++;
368 	}
369 	*name = '\0';
370 	return (size > 0);
371 }
372 
373 int	utmptime;
374 int	utmpent;
375 int	utmpsize = 0;
376 struct	utmp *utmp;
377 int	alarmcount;
378 
379 void
380 onalrm(signo)
381 	int signo;
382 {
383 	register struct neighbor *np;
384 	register struct whoent *we = mywd.wd_we, *wlast;
385 	register int i;
386 	struct stat stb;
387 	double avenrun[3];
388 	time_t now;
389 	int cc;
390 
391 	now = time(NULL);
392 	if (alarmcount % 10 == 0)
393 		getboottime(0);
394 	alarmcount++;
395 	(void) fstat(utmpf, &stb);
396 	if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
397 		utmptime = stb.st_mtime;
398 		if (stb.st_size > utmpsize) {
399 			utmpsize = stb.st_size + 10 * sizeof(struct utmp);
400 			if (utmp)
401 				utmp = (struct utmp *)realloc(utmp, utmpsize);
402 			else
403 				utmp = (struct utmp *)malloc(utmpsize);
404 			if (! utmp) {
405 				syslog(LOG_WARNING, "malloc failed");
406 				utmpsize = 0;
407 				goto done;
408 			}
409 		}
410 		(void) lseek(utmpf, (off_t)0, L_SET);
411 		cc = read(utmpf, (char *)utmp, stb.st_size);
412 		if (cc < 0) {
413 			syslog(LOG_ERR, "read(%s): %m", _PATH_UTMP);
414 			goto done;
415 		}
416 		wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1];
417 		utmpent = cc / sizeof(struct utmp);
418 		for (i = 0; i < utmpent; i++)
419 			if (utmp[i].ut_name[0]) {
420 				memcpy(we->we_utmp.out_line, utmp[i].ut_line,
421 				   sizeof(utmp[i].ut_line));
422 				memcpy(we->we_utmp.out_name, utmp[i].ut_name,
423 				   sizeof(utmp[i].ut_name));
424 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
425 				if (we >= wlast)
426 					break;
427 				we++;
428 			}
429 		utmpent = we - mywd.wd_we;
430 	}
431 
432 	/*
433 	 * The test on utmpent looks silly---after all, if no one is
434 	 * logged on, why worry about efficiency?---but is useful on
435 	 * (e.g.) compute servers.
436 	 */
437 	if (utmpent && chdir(_PATH_DEV)) {
438 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
439 		exit(1);
440 	}
441 	we = mywd.wd_we;
442 	for (i = 0; i < utmpent; i++) {
443 		if (stat(we->we_utmp.out_line, &stb) >= 0)
444 			we->we_idle = htonl(now - stb.st_atime);
445 		we++;
446 	}
447 	(void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
448 	for (i = 0; i < 3; i++)
449 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
450 	cc = (char *)we - (char *)&mywd;
451 	mywd.wd_sendtime = htonl(time(0));
452 	mywd.wd_vers = WHODVERSION;
453 	mywd.wd_type = WHODTYPE_STATUS;
454 	if (multicast_mode == SCOPED_MULTICAST) {
455 		(void) sendto(s, (char *)&mywd, cc, 0,
456 				(struct sockaddr *)&multicast_addr,
457 				sizeof(multicast_addr));
458 	}
459 	else for (np = neighbors; np != NULL; np = np->n_next) {
460 		if (multicast_mode == PER_INTERFACE_MULTICAST &&
461 		    np->n_flags & IFF_MULTICAST) {
462 			/*
463 			 * Select the outgoing interface for the multicast.
464 			 */
465 			if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
466 			    &(((struct sockaddr_in *)np->n_addr)->sin_addr),
467 			    sizeof(struct in_addr)) < 0) {
468 				syslog(LOG_ERR,
469 					"setsockopt IP_MULTICAST_IF: %m");
470 				exit(1);
471 			}
472 			(void) sendto(s, (char *)&mywd, cc, 0,
473 				(struct sockaddr *)&multicast_addr,
474 				sizeof(multicast_addr));
475 		} else (void) sendto(s, (char *)&mywd, cc, 0,
476 					np->n_addr, np->n_addrlen);
477 	}
478 	if (utmpent && chdir(_PATH_RWHODIR)) {
479 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
480 		exit(1);
481 	}
482 done:
483 	(void) alarm(AL_INTERVAL);
484 }
485 
486 void
487 getboottime(signo)
488 	int signo;
489 {
490 	int mib[2];
491 	size_t size;
492 	struct timeval tm;
493 
494 	mib[0] = CTL_KERN;
495 	mib[1] = KERN_BOOTTIME;
496 	size = sizeof(tm);
497 	if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) {
498 		syslog(LOG_ERR, "cannot get boottime: %m");
499 		exit(1);
500 	}
501 	mywd.wd_boottime = htonl(tm.tv_sec);
502 }
503 
504 void
505 quit(msg)
506 	char *msg;
507 {
508 	syslog(LOG_ERR, msg);
509 	exit(1);
510 }
511 
512 #define ROUNDUP(a) \
513 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
514 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
515 
516 void
517 rt_xaddrs(cp, cplim, rtinfo)
518 	register caddr_t cp, cplim;
519 	register struct rt_addrinfo *rtinfo;
520 {
521 	register struct sockaddr *sa;
522 	register int i;
523 
524 	memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
525 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
526 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
527 			continue;
528 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
529 		ADVANCE(cp, sa);
530 	}
531 }
532 
533 /*
534  * Figure out device configuration and select
535  * networks which deserve status information.
536  */
537 int
538 configure(s)
539 	int s;
540 {
541 	register struct neighbor *np;
542 	register struct if_msghdr *ifm;
543 	register struct ifa_msghdr *ifam;
544 	struct sockaddr_dl *sdl;
545 	size_t needed;
546 	int mib[6], flags = 0, len;
547 	char *buf, *lim, *next;
548 	struct rt_addrinfo info;
549 
550 	if (multicast_mode != NO_MULTICAST) {
551 		multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP);
552 		multicast_addr.sin_port = sp->s_port;
553 	}
554 
555 	if (multicast_mode == SCOPED_MULTICAST) {
556 		struct ip_mreq mreq;
557 		unsigned char ttl;
558 
559 		mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
560 		mreq.imr_interface.s_addr = htonl(INADDR_ANY);
561 		if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
562 					&mreq, sizeof(mreq)) < 0) {
563 			syslog(LOG_ERR,
564 				"setsockopt IP_ADD_MEMBERSHIP: %m");
565 			return(0);
566 		}
567 		ttl = multicast_scope;
568 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
569 					&ttl, sizeof(ttl)) < 0) {
570 			syslog(LOG_ERR,
571 				"setsockopt IP_MULTICAST_TTL: %m");
572 			return(0);
573 		}
574 		return(1);
575 	}
576 
577 	mib[0] = CTL_NET;
578 	mib[1] = PF_ROUTE;
579 	mib[2] = 0;
580 	mib[3] = AF_INET;
581 	mib[4] = NET_RT_IFLIST;
582 	mib[5] = 0;
583 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
584 		quit("route-sysctl-estimate");
585 	if ((buf = malloc(needed)) == NULL)
586 		quit("malloc");
587 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
588 		quit("actual retrieval of interface table");
589 	lim = buf + needed;
590 
591 	sdl = NULL;		/* XXX just to keep gcc -Wall happy */
592 	for (next = buf; next < lim; next += ifm->ifm_msglen) {
593 		ifm = (struct if_msghdr *)next;
594 		if (ifm->ifm_type == RTM_IFINFO) {
595 			sdl = (struct sockaddr_dl *)(ifm + 1);
596 			flags = ifm->ifm_flags;
597 			continue;
598 		}
599 		if ((flags & IFF_UP) == 0 ||
600 		    (flags & (((multicast_mode == PER_INTERFACE_MULTICAST) ?
601 				IFF_MULTICAST : 0) |
602 				IFF_BROADCAST|IFF_POINTOPOINT)) == 0)
603 			continue;
604 		if (ifm->ifm_type != RTM_NEWADDR)
605 			quit("out of sync parsing NET_RT_IFLIST");
606 		ifam = (struct ifa_msghdr *)ifm;
607 		info.rti_addrs = ifam->ifam_addrs;
608 		rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
609 			&info);
610 		/* gag, wish we could get rid of Internet dependencies */
611 #define dstaddr	info.rti_info[RTAX_BRD]
612 #define ifaddr info.rti_info[RTAX_IFA]
613 #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
614 #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
615 		if (dstaddr == 0 || dstaddr->sa_family != AF_INET)
616 			continue;
617 		PORT_SA(dstaddr) = sp->s_port;
618 		for (np = neighbors; np != NULL; np = np->n_next)
619 			if (memcmp(sdl->sdl_data, np->n_name,
620 				   sdl->sdl_nlen) == 0 &&
621 			    IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr))
622 				break;
623 		if (np != NULL)
624 			continue;
625 		len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1;
626 		np = (struct neighbor *)malloc(len);
627 		if (np == NULL)
628 			quit("malloc of neighbor structure");
629 		memset(np, 0, len);
630 		np->n_flags = flags;
631 		np->n_addr = (struct sockaddr *)(np + 1);
632 		np->n_addrlen = dstaddr->sa_len;
633 		np->n_name = np->n_addrlen + (char *)np->n_addr;
634 		memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen);
635 		memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen);
636 		if (multicast_mode == PER_INTERFACE_MULTICAST &&
637 		    (flags & IFF_MULTICAST) &&
638 		   !(flags & IFF_LOOPBACK)) {
639 			struct ip_mreq mreq;
640 
641 			memcpy((char *)np->n_addr, (char *)ifaddr,
642 				np->n_addrlen);
643 			mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
644 			mreq.imr_interface.s_addr =
645 			  ((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr;
646 			if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
647 						&mreq, sizeof(mreq)) < 0) {
648 				syslog(LOG_ERR,
649 				    "setsockopt IP_ADD_MEMBERSHIP: %m");
650 #if 0
651 				/* Fall back to broadcast on this if. */
652 				np->n_flags &= ~IFF_MULTICAST;
653 #else
654 				free((char *)np);
655 				continue;
656 #endif
657 			}
658 		}
659 		np->n_next = neighbors;
660 		neighbors = np;
661 	}
662 	free(buf);
663 	return (1);
664 }
665 
666 #ifdef DEBUG
667 void
668 Sendto(s, buf, cc, flags, to, tolen)
669 	int s;
670 	const void *buf;
671 	size_t cc;
672 	int flags;
673 	const struct sockaddr *to;
674 	int tolen;
675 {
676 	register struct whod *w = (struct whod *)buf;
677 	register struct whoent *we;
678 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
679 
680 	printf("sendto %x.%d\n", ntohl(sin->sin_addr.s_addr),
681 				 ntohs(sin->sin_port));
682 	printf("hostname %s %s\n", w->wd_hostname,
683 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
684 	printf("load %4.2f, %4.2f, %4.2f\n",
685 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
686 	    ntohl(w->wd_loadav[2]) / 100.0);
687 	cc -= WHDRSIZE;
688 	for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) {
689 		time_t t = ntohl(we->we_utmp.out_time);
690 		printf("%-8.8s %s:%s %.12s",
691 			we->we_utmp.out_name,
692 			w->wd_hostname, we->we_utmp.out_line,
693 			ctime(&t)+4);
694 		we->we_idle = ntohl(we->we_idle) / 60;
695 		if (we->we_idle) {
696 			if (we->we_idle >= 100*60)
697 				we->we_idle = 100*60 - 1;
698 			if (we->we_idle >= 60)
699 				printf(" %2d", we->we_idle / 60);
700 			else
701 				printf("   ");
702 			printf(":%02d", we->we_idle % 60);
703 		}
704 		printf("\n");
705 	}
706 }
707 
708 char *
709 interval(time, updown)
710 	int time;
711 	char *updown;
712 {
713 	static char resbuf[32];
714 	int days, hours, minutes;
715 
716 	if (time < 0 || time > 3*30*24*60*60) {
717 		(void) sprintf(resbuf, "   %s ??:??", updown);
718 		return (resbuf);
719 	}
720 	minutes = (time + 59) / 60;		/* round to minutes */
721 	hours = minutes / 60; minutes %= 60;
722 	days = hours / 24; hours %= 24;
723 	if (days)
724 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
725 		    updown, days, hours, minutes);
726 	else
727 		(void) sprintf(resbuf, "%s    %2d:%02d",
728 		    updown, hours, minutes);
729 	return (resbuf);
730 }
731 #endif
732