xref: /freebsd/usr.sbin/rwhod/rwhod.c (revision b601c69bdbe8755d26570261d7fd4c02ee4eff74)
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   "$FreeBSD$";
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			quiet_mode;
120 int			iff_flag = IFF_POINTOPOINT;
121 int			multicast_mode  = NO_MULTICAST;
122 int			multicast_scope;
123 struct sockaddr_in	multicast_addr  = { sizeof multicast_addr, AF_INET };
124 
125 /*
126  * Alarm interval. Don't forget to change the down time check in ruptime
127  * if this is changed.
128  */
129 #define AL_INTERVAL (3 * 60)
130 
131 char	myname[MAXHOSTNAMELEN];
132 
133 /*
134  * We communicate with each neighbor in a list constructed at the time we're
135  * started up.  Neighbors are currently directly connected via a hardware
136  * interface.
137  */
138 struct	neighbor {
139 	struct	neighbor *n_next;
140 	char	*n_name;		/* interface name */
141 	struct	sockaddr *n_addr;		/* who to send to */
142 	int	n_addrlen;		/* size of address */
143 	int	n_flags;		/* should forward?, interface flags */
144 };
145 
146 struct	neighbor *neighbors;
147 struct	whod mywd;
148 struct	servent *sp;
149 int	s, utmpf;
150 
151 #define	WHDRSIZE	(sizeof(mywd) - sizeof(mywd.wd_we))
152 
153 void	 run_as __P((uid_t *, gid_t *));
154 int	 configure __P((int));
155 void	 getboottime __P((int));
156 void	 onalrm __P((int));
157 void	 quit __P((char *));
158 void	 rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
159 int	 verify __P((char *, int));
160 static void usage __P((void));
161 #ifdef DEBUG
162 char	*interval __P((int, char *));
163 void	 Sendto __P((int, const void *, size_t, int,
164 		     const struct sockaddr *, int));
165 #define	 sendto Sendto
166 #endif
167 
168 int
169 main(argc, argv)
170 	int argc;
171 	char *argv[];
172 {
173 	struct sockaddr_in from;
174 	struct stat st;
175 	char path[64];
176 	int on = 1;
177 	char *cp;
178 	struct sockaddr_in sin;
179 	uid_t unpriv_uid;
180 	gid_t unpriv_gid;
181 
182 	if (getuid())
183 		errx(1, "not super user");
184 
185 	run_as(&unpriv_uid, &unpriv_gid);
186 
187 	argv++; argc--;
188 	while (argc > 0 && *argv[0] == '-') {
189 		if (strcmp(*argv, "-m") == 0) {
190 			if (argc > 1 && isdigit(*(argv + 1)[0])) {
191 				argv++, argc--;
192 				multicast_mode  = SCOPED_MULTICAST;
193 				multicast_scope = atoi(*argv);
194 				if (multicast_scope > MAX_MULTICAST_SCOPE)
195 					errx(1, "ttl must not exceed %u",
196 					MAX_MULTICAST_SCOPE);
197 			}
198 			else multicast_mode = PER_INTERFACE_MULTICAST;
199 		}
200 		else if (strcmp(*argv, "-i") == 0)
201 			insecure_mode = 1;
202 		else if (strcmp(*argv, "-l") == 0)
203 			quiet_mode = 1;
204 		else if (strcmp(*argv, "-p") == 0)
205 			iff_flag = 0;
206 		else
207 			usage();
208 		argv++, argc--;
209 	}
210 	if (argc > 0)
211 		usage();
212 #ifndef DEBUG
213 	daemon(1, 0);
214 #endif
215 	(void) signal(SIGHUP, getboottime);
216 	openlog("rwhod", LOG_PID, LOG_DAEMON);
217 	sp = getservbyname("who", "udp");
218 	if (sp == NULL) {
219 		syslog(LOG_ERR, "udp/who: unknown service");
220 		exit(1);
221 	}
222 	if (chdir(_PATH_RWHODIR) < 0) {
223 		syslog(LOG_ERR, "%s: %m", _PATH_RWHODIR);
224 		exit(1);
225 	}
226 	/*
227 	 * Establish host name as returned by system.
228 	 */
229 	if (gethostname(myname, sizeof(myname) - 1) < 0) {
230 		syslog(LOG_ERR, "gethostname: %m");
231 		exit(1);
232 	}
233 	if ((cp = index(myname, '.')) != NULL)
234 		*cp = '\0';
235 	strncpy(mywd.wd_hostname, myname, sizeof(mywd.wd_hostname) - 1);
236 	mywd.wd_hostname[sizeof(mywd.wd_hostname) - 1] = '\0';
237 	utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
238 	if (utmpf < 0) {
239 		syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
240 		exit(1);
241 	}
242 	getboottime(0);
243 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
244 		syslog(LOG_ERR, "socket: %m");
245 		exit(1);
246 	}
247 	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
248 		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
249 		exit(1);
250 	}
251 	memset(&sin, 0, sizeof(sin));
252 	sin.sin_len = sizeof(sin);
253 	sin.sin_family = AF_INET;
254 	sin.sin_port = sp->s_port;
255 	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
256 		syslog(LOG_ERR, "bind: %m");
257 		exit(1);
258 	}
259 	setgid(unpriv_gid);
260 	setgroups(1, &unpriv_gid);	/* XXX BOGUS groups[0] = egid */
261 	setuid(unpriv_uid);
262 	if (!configure(s))
263 		exit(1);
264 	if (!quiet_mode) {
265 		signal(SIGALRM, onalrm);
266 		onalrm(0);
267 	}
268 	for (;;) {
269 		struct whod wd;
270 		int cc, whod, len = sizeof(from);
271 
272 		cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0,
273 			(struct sockaddr *)&from, &len);
274 		if (cc <= 0) {
275 			if (cc < 0 && errno != EINTR)
276 				syslog(LOG_WARNING, "recv: %m");
277 			continue;
278 		}
279 		if (from.sin_port != sp->s_port && !insecure_mode) {
280 			syslog(LOG_WARNING, "%d: bad from port",
281 				ntohs(from.sin_port));
282 			continue;
283 		}
284 		if (wd.wd_vers != WHODVERSION)
285 			continue;
286 		if (wd.wd_type != WHODTYPE_STATUS)
287 			continue;
288 		if (!verify(wd.wd_hostname, sizeof wd.wd_hostname)) {
289 			syslog(LOG_WARNING, "malformed host name from %x",
290 				from.sin_addr);
291 			continue;
292 		}
293 		(void) snprintf(path, sizeof path, "whod.%s", wd.wd_hostname);
294 		/*
295 		 * Rather than truncating and growing the file each time,
296 		 * use ftruncate if size is less than previous size.
297 		 */
298 		whod = open(path, O_WRONLY | O_CREAT, 0644);
299 		if (whod < 0) {
300 			syslog(LOG_WARNING, "%s: %m", path);
301 			continue;
302 		}
303 #if ENDIAN != BIG_ENDIAN
304 		{
305 			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
306 			struct whoent *we;
307 
308 			/* undo header byte swapping before writing to file */
309 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
310 			for (i = 0; i < 3; i++)
311 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
312 			wd.wd_boottime = ntohl(wd.wd_boottime);
313 			we = wd.wd_we;
314 			for (i = 0; i < n; i++) {
315 				we->we_idle = ntohl(we->we_idle);
316 				we->we_utmp.out_time =
317 				    ntohl(we->we_utmp.out_time);
318 				we++;
319 			}
320 		}
321 #endif
322 		(void) time((time_t *)&wd.wd_recvtime);
323 		(void) write(whod, (char *)&wd, cc);
324 		if (fstat(whod, &st) < 0 || st.st_size > cc)
325 			ftruncate(whod, cc);
326 		(void) close(whod);
327 	}
328 }
329 
330 static void
331 usage()
332 {
333 	fprintf(stderr, "usage: rwhod [-i] [-p] [-l] [-m [ttl]]\n");
334 	exit(1);
335 }
336 
337 void
338 run_as(uid, gid)
339 	uid_t *uid;
340 	gid_t *gid;
341 {
342 	struct passwd *pw;
343 	struct group *gr;
344 
345 	pw = getpwnam(UNPRIV_USER);
346 	if (!pw) {
347 		syslog(LOG_ERR, "getpwnam(%s): %m", UNPRIV_USER);
348 		exit(1);
349 	}
350 	*uid = pw->pw_uid;
351 
352 	gr = getgrnam(UNPRIV_GROUP);
353 	if (!gr) {
354 		syslog(LOG_ERR, "getgrnam(%s): %m", UNPRIV_GROUP);
355 		exit(1);
356 	}
357 	*gid = gr->gr_gid;
358 }
359 
360 /*
361  * Check out host name for unprintables
362  * and other funnies before allowing a file
363  * to be created.  Sorry, but blanks aren't allowed.
364  */
365 int
366 verify(name, maxlen)
367 	register char *name;
368 	register int   maxlen;
369 {
370 	register int size = 0;
371 
372 	while (*name && size < maxlen - 1) {
373 		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
374 			return (0);
375 		name++, size++;
376 	}
377 	*name = '\0';
378 	return (size > 0);
379 }
380 
381 int	utmptime;
382 int	utmpent;
383 int	utmpsize = 0;
384 struct	utmp *utmp;
385 int	alarmcount;
386 
387 void
388 onalrm(signo)
389 	int signo;
390 {
391 	register struct neighbor *np;
392 	register struct whoent *we = mywd.wd_we, *wlast;
393 	register int i;
394 	struct stat stb;
395 	double avenrun[3];
396 	time_t now;
397 	int cc;
398 
399 	now = time(NULL);
400 	if (alarmcount % 10 == 0)
401 		getboottime(0);
402 	alarmcount++;
403 	(void) fstat(utmpf, &stb);
404 	if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
405 		utmptime = stb.st_mtime;
406 		if (stb.st_size > utmpsize) {
407 			utmpsize = stb.st_size + 10 * sizeof(struct utmp);
408 			if (utmp)
409 				utmp = (struct utmp *)realloc(utmp, utmpsize);
410 			else
411 				utmp = (struct utmp *)malloc(utmpsize);
412 			if (! utmp) {
413 				syslog(LOG_WARNING, "malloc failed");
414 				utmpsize = 0;
415 				goto done;
416 			}
417 		}
418 		(void) lseek(utmpf, (off_t)0, L_SET);
419 		cc = read(utmpf, (char *)utmp, stb.st_size);
420 		if (cc < 0) {
421 			syslog(LOG_ERR, "read(%s): %m", _PATH_UTMP);
422 			goto done;
423 		}
424 		wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1];
425 		utmpent = cc / sizeof(struct utmp);
426 		for (i = 0; i < utmpent; i++)
427 			if (utmp[i].ut_name[0]) {
428 				memcpy(we->we_utmp.out_line, utmp[i].ut_line,
429 				   sizeof(utmp[i].ut_line));
430 				memcpy(we->we_utmp.out_name, utmp[i].ut_name,
431 				   sizeof(utmp[i].ut_name));
432 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
433 				if (we >= wlast)
434 					break;
435 				we++;
436 			}
437 		utmpent = we - mywd.wd_we;
438 	}
439 
440 	/*
441 	 * The test on utmpent looks silly---after all, if no one is
442 	 * logged on, why worry about efficiency?---but is useful on
443 	 * (e.g.) compute servers.
444 	 */
445 	if (utmpent && chdir(_PATH_DEV)) {
446 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
447 		exit(1);
448 	}
449 	we = mywd.wd_we;
450 	for (i = 0; i < utmpent; i++) {
451 		if (stat(we->we_utmp.out_line, &stb) >= 0)
452 			we->we_idle = htonl(now - stb.st_atime);
453 		we++;
454 	}
455 	(void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
456 	for (i = 0; i < 3; i++)
457 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
458 	cc = (char *)we - (char *)&mywd;
459 	mywd.wd_sendtime = htonl(time(0));
460 	mywd.wd_vers = WHODVERSION;
461 	mywd.wd_type = WHODTYPE_STATUS;
462 	if (multicast_mode == SCOPED_MULTICAST) {
463 		(void) sendto(s, (char *)&mywd, cc, 0,
464 				(struct sockaddr *)&multicast_addr,
465 				sizeof(multicast_addr));
466 	}
467 	else for (np = neighbors; np != NULL; np = np->n_next) {
468 		if (multicast_mode == PER_INTERFACE_MULTICAST &&
469 		    np->n_flags & IFF_MULTICAST) {
470 			/*
471 			 * Select the outgoing interface for the multicast.
472 			 */
473 			if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
474 			    &(((struct sockaddr_in *)np->n_addr)->sin_addr),
475 			    sizeof(struct in_addr)) < 0) {
476 				syslog(LOG_ERR,
477 					"setsockopt IP_MULTICAST_IF: %m");
478 				exit(1);
479 			}
480 			(void) sendto(s, (char *)&mywd, cc, 0,
481 				(struct sockaddr *)&multicast_addr,
482 				sizeof(multicast_addr));
483 		} else (void) sendto(s, (char *)&mywd, cc, 0,
484 					np->n_addr, np->n_addrlen);
485 	}
486 	if (utmpent && chdir(_PATH_RWHODIR)) {
487 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
488 		exit(1);
489 	}
490 done:
491 	(void) alarm(AL_INTERVAL);
492 }
493 
494 void
495 getboottime(signo)
496 	int signo;
497 {
498 	int mib[2];
499 	size_t size;
500 	struct timeval tm;
501 
502 	mib[0] = CTL_KERN;
503 	mib[1] = KERN_BOOTTIME;
504 	size = sizeof(tm);
505 	if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) {
506 		syslog(LOG_ERR, "cannot get boottime: %m");
507 		exit(1);
508 	}
509 	mywd.wd_boottime = htonl(tm.tv_sec);
510 }
511 
512 void
513 quit(msg)
514 	char *msg;
515 {
516 	syslog(LOG_ERR, "%s", msg);
517 	exit(1);
518 }
519 
520 #define ROUNDUP(a) \
521 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
522 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
523 
524 void
525 rt_xaddrs(cp, cplim, rtinfo)
526 	register caddr_t cp, cplim;
527 	register struct rt_addrinfo *rtinfo;
528 {
529 	register struct sockaddr *sa;
530 	register int i;
531 
532 	memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
533 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
534 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
535 			continue;
536 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
537 		ADVANCE(cp, sa);
538 	}
539 }
540 
541 /*
542  * Figure out device configuration and select
543  * networks which deserve status information.
544  */
545 int
546 configure(s)
547 	int s;
548 {
549 	register struct neighbor *np;
550 	register struct if_msghdr *ifm;
551 	register struct ifa_msghdr *ifam;
552 	struct sockaddr_dl *sdl;
553 	size_t needed;
554 	int mib[6], flags = 0, len;
555 	char *buf, *lim, *next;
556 	struct rt_addrinfo info;
557 
558 	if (multicast_mode != NO_MULTICAST) {
559 		multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP);
560 		multicast_addr.sin_port = sp->s_port;
561 	}
562 
563 	if (multicast_mode == SCOPED_MULTICAST) {
564 		struct ip_mreq mreq;
565 		unsigned char ttl;
566 
567 		mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
568 		mreq.imr_interface.s_addr = htonl(INADDR_ANY);
569 		if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
570 					&mreq, sizeof(mreq)) < 0) {
571 			syslog(LOG_ERR,
572 				"setsockopt IP_ADD_MEMBERSHIP: %m");
573 			return(0);
574 		}
575 		ttl = multicast_scope;
576 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
577 					&ttl, sizeof(ttl)) < 0) {
578 			syslog(LOG_ERR,
579 				"setsockopt IP_MULTICAST_TTL: %m");
580 			return(0);
581 		}
582 		return(1);
583 	}
584 
585 	mib[0] = CTL_NET;
586 	mib[1] = PF_ROUTE;
587 	mib[2] = 0;
588 	mib[3] = AF_INET;
589 	mib[4] = NET_RT_IFLIST;
590 	mib[5] = 0;
591 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
592 		quit("route-sysctl-estimate");
593 	if ((buf = malloc(needed)) == NULL)
594 		quit("malloc");
595 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
596 		quit("actual retrieval of interface table");
597 	lim = buf + needed;
598 
599 	sdl = NULL;		/* XXX just to keep gcc -Wall happy */
600 	for (next = buf; next < lim; next += ifm->ifm_msglen) {
601 		ifm = (struct if_msghdr *)next;
602 		if (ifm->ifm_type == RTM_IFINFO) {
603 			sdl = (struct sockaddr_dl *)(ifm + 1);
604 			flags = ifm->ifm_flags;
605 			continue;
606 		}
607 		if ((flags & IFF_UP) == 0 ||
608 		    (flags & (((multicast_mode == PER_INTERFACE_MULTICAST) ?
609 				IFF_MULTICAST : 0) |
610 				IFF_BROADCAST|iff_flag)) == 0)
611 			continue;
612 		if (ifm->ifm_type != RTM_NEWADDR)
613 			quit("out of sync parsing NET_RT_IFLIST");
614 		ifam = (struct ifa_msghdr *)ifm;
615 		info.rti_addrs = ifam->ifam_addrs;
616 		rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
617 			&info);
618 		/* gag, wish we could get rid of Internet dependencies */
619 #define dstaddr	info.rti_info[RTAX_BRD]
620 #define ifaddr info.rti_info[RTAX_IFA]
621 #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
622 #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
623 		if (dstaddr == 0 || dstaddr->sa_family != AF_INET)
624 			continue;
625 		PORT_SA(dstaddr) = sp->s_port;
626 		for (np = neighbors; np != NULL; np = np->n_next)
627 			if (memcmp(sdl->sdl_data, np->n_name,
628 				   sdl->sdl_nlen) == 0 &&
629 			    IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr))
630 				break;
631 		if (np != NULL)
632 			continue;
633 		len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1;
634 		np = (struct neighbor *)malloc(len);
635 		if (np == NULL)
636 			quit("malloc of neighbor structure");
637 		memset(np, 0, len);
638 		np->n_flags = flags;
639 		np->n_addr = (struct sockaddr *)(np + 1);
640 		np->n_addrlen = dstaddr->sa_len;
641 		np->n_name = np->n_addrlen + (char *)np->n_addr;
642 		memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen);
643 		memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen);
644 		if (multicast_mode == PER_INTERFACE_MULTICAST &&
645 		    (flags & IFF_MULTICAST) &&
646 		   !(flags & IFF_LOOPBACK)) {
647 			struct ip_mreq mreq;
648 
649 			memcpy((char *)np->n_addr, (char *)ifaddr,
650 				np->n_addrlen);
651 			mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
652 			mreq.imr_interface.s_addr =
653 			  ((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr;
654 			if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
655 						&mreq, sizeof(mreq)) < 0) {
656 				syslog(LOG_ERR,
657 				    "setsockopt IP_ADD_MEMBERSHIP: %m");
658 #if 0
659 				/* Fall back to broadcast on this if. */
660 				np->n_flags &= ~IFF_MULTICAST;
661 #else
662 				free((char *)np);
663 				continue;
664 #endif
665 			}
666 		}
667 		np->n_next = neighbors;
668 		neighbors = np;
669 	}
670 	free(buf);
671 	return (1);
672 }
673 
674 #ifdef DEBUG
675 void
676 Sendto(s, buf, cc, flags, to, tolen)
677 	int s;
678 	const void *buf;
679 	size_t cc;
680 	int flags;
681 	const struct sockaddr *to;
682 	int tolen;
683 {
684 	register struct whod *w = (struct whod *)buf;
685 	register struct whoent *we;
686 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
687 
688 	printf("sendto %x.%d\n", ntohl(sin->sin_addr.s_addr),
689 				 ntohs(sin->sin_port));
690 	printf("hostname %s %s\n", w->wd_hostname,
691 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
692 	printf("load %4.2f, %4.2f, %4.2f\n",
693 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
694 	    ntohl(w->wd_loadav[2]) / 100.0);
695 	cc -= WHDRSIZE;
696 	for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) {
697 		time_t t = ntohl(we->we_utmp.out_time);
698 		printf("%-8.8s %s:%s %.12s",
699 			we->we_utmp.out_name,
700 			w->wd_hostname, we->we_utmp.out_line,
701 			ctime(&t)+4);
702 		we->we_idle = ntohl(we->we_idle) / 60;
703 		if (we->we_idle) {
704 			if (we->we_idle >= 100*60)
705 				we->we_idle = 100*60 - 1;
706 			if (we->we_idle >= 60)
707 				printf(" %2d", we->we_idle / 60);
708 			else
709 				printf("   ");
710 			printf(":%02d", we->we_idle % 60);
711 		}
712 		printf("\n");
713 	}
714 }
715 
716 char *
717 interval(time, updown)
718 	int time;
719 	char *updown;
720 {
721 	static char resbuf[32];
722 	int days, hours, minutes;
723 
724 	if (time < 0 || time > 3*30*24*60*60) {
725 		(void) sprintf(resbuf, "   %s ??:??", updown);
726 		return (resbuf);
727 	}
728 	minutes = (time + 59) / 60;		/* round to minutes */
729 	hours = minutes / 60; minutes %= 60;
730 	days = hours / 24; hours %= 24;
731 	if (days)
732 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
733 		    updown, days, hours, minutes);
734 	else
735 		(void) sprintf(resbuf, "%s    %2d:%02d",
736 		    updown, hours, minutes);
737 	return (resbuf);
738 }
739 #endif
740