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