xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.sbin/in.rwhod.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
39 #include <sys/stat.h>
40 #include <sys/ioctl.h>
41 #include <sys/file.h>
42 #include <sys/loadavg.h>
43 
44 #include <net/if.h>
45 #include <netinet/in.h>
46 
47 #include <stdio.h>
48 #include <signal.h>
49 #include <errno.h>
50 #include <utmpx.h>
51 #include <ctype.h>
52 #include <netdb.h>
53 #include <syslog.h>
54 #include <fcntl.h>
55 #include <sys/isa_defs.h>	/* for ENDIAN defines */
56 #include <arpa/inet.h>
57 #include <protocols/rwhod.h>
58 
59 #include <strings.h>
60 #include <stdlib.h>
61 #include <unistd.h>
62 
63 /*
64  * This version of Berkeley's rwhod has been modified to use IP multicast
65  * datagrams, under control of a new command-line option:
66  *
67  *	rwhod -m	causes rwhod to use IP multicast (instead of
68  *			broadcast or unicast) on all interfaces that have
69  *			the IFF_MULTICAST flag set in their "ifnet" structs
70  *			(excluding the loopback interface).  The multicast
71  *			reports are sent with a time-to-live of 1, to prevent
72  *			forwarding beyond the directly-connected subnet(s).
73  *
74  *	rwhod -m <ttl>	causes rwhod to send IP multicast datagrams with a
75  *			time-to-live of <ttl>, via a SINGLE interface rather
76  *			than all interfaces.  <ttl> must be between 0 and
77  *			MAX_MULTICAST_SCOPE, defined below.  Note that "-m 1"
78  *			is different than "-m", in that "-m 1" specifies
79  *			transmission on one interface only.
80  *
81  * When "-m" is used without a <ttl> argument, the program accepts multicast
82  * rwhod reports from all multicast-capable interfaces.  If a <ttl> argument
83  * is given, it accepts multicast reports from only one interface, the one
84  * on which reports are sent (which may be controlled via the host's routing
85  * table).  Regardless of the "-m" option, the program accepts broadcast or
86  * unicast reports from all interfaces.  Thus, this program will hear the
87  * reports of old, non-multicasting rwhods, but, if multicasting is used,
88  * those old rwhods won't hear the reports generated by this program.
89  *
90  *                  -- Steve Deering, Stanford University, February 1989
91  */
92 
93 #define	NO_MULTICAST		0	  /* multicast modes */
94 #define	PER_INTERFACE_MULTICAST	1
95 #define	SCOPED_MULTICAST	2
96 
97 #define	MAX_MULTICAST_SCOPE	32	  /* "site-wide", by convention */
98 
99 #define	INADDR_WHOD_GROUP	(ulong_t)0xe0000103	/* 224.0.1.3 */
100 					/* (belongs in protocols/rwhod.h) */
101 
102 static int			multicast_mode  = NO_MULTICAST;
103 static int			multicast_scope;
104 static struct sockaddr_in	multicast_addr  = { AF_INET };
105 
106 
107 /*
108  * Alarm interval. Don't forget to change the down time check in ruptime
109  * if this is changed.
110  */
111 #define	AL_INTERVAL (3 * 60)
112 
113 static struct	sockaddr_in sin = { AF_INET };
114 
115 static char	myname[MAXHOSTNAMELEN];
116 
117 /*
118  * We communicate with each neighbor in
119  * a list constructed at the time we're
120  * started up.  Neighbors are currently
121  * directly connected via a hardware interface.
122  */
123 struct	neighbor {
124 	struct	neighbor *n_next;
125 	char	*n_name;		/* interface name */
126 	char	*n_addr;		/* who to send to */
127 	int	n_addrlen;		/* size of address */
128 	ulong_t	n_subnet;		/* AF_INET subnet */
129 	uint_t	n_flags;		/* should forward?, interface flags */
130 };
131 
132 static struct	neighbor *neighbors;
133 static struct	whod mywd;
134 static struct	servent *sp;
135 static int	s;
136 
137 #define	WHDRSIZE	(sizeof (mywd) - sizeof (mywd.wd_we))
138 #define	RWHODIR		"/var/spool/rwho"
139 
140 static void		onalrm(void);
141 static void		getkmem(void);
142 static boolean_t	configure(int);
143 static int		verify(const struct whod *);
144 
145 int
main(int argc,char * argv[])146 main(int argc, char *argv[])
147 {
148 	struct sockaddr_in from;
149 	struct stat st;
150 	char path[64];
151 	struct hostent *hp;
152 	int on = 1;
153 	char *cp;
154 	struct stat sb;
155 
156 	if (getuid()) {
157 		(void) fprintf(stderr, "in.rwhod: not super user\n");
158 		exit(1);
159 	}
160 	sp = getservbyname("who", "udp");
161 	if (sp == NULL) {
162 		(void) fprintf(stderr, "in.rwhod: udp/who: unknown service\n");
163 		exit(1);
164 	}
165 	argv++;
166 	argc--;
167 	while (argc > 0 && *argv[0] == '-') {
168 		if (strcmp(*argv, "-m") == 0) {
169 			if (argc > 1 && isdigit(*(argv + 1)[0])) {
170 				argv++;
171 				argc--;
172 				multicast_mode  = SCOPED_MULTICAST;
173 				multicast_scope = atoi(*argv);
174 				if (multicast_scope > MAX_MULTICAST_SCOPE) {
175 					(void) fprintf(stderr,
176 					    "in.rwhod: "
177 					    "ttl must not exceed %u\n",
178 					    MAX_MULTICAST_SCOPE);
179 					exit(1);
180 				}
181 			} else {
182 				multicast_mode = PER_INTERFACE_MULTICAST;
183 			}
184 		} else {
185 			goto usage;
186 		}
187 		argv++;
188 		argc--;
189 	}
190 	if (argc > 0)
191 		goto usage;
192 	if (chdir(RWHODIR) < 0) {
193 		perror(RWHODIR);
194 		exit(1);
195 	}
196 #ifndef DEBUG
197 	if (fork())
198 		exit(0);
199 	/* CSTYLED */
200 	{
201 		(void) close(0);
202 		(void) close(1);
203 		(void) close(2);
204 		(void) open("/", 0);
205 		(void) dup2(0, 1);
206 		(void) dup2(0, 2);
207 		(void) setsid();
208 	}
209 #endif
210 	(void) sigset(SIGHUP, (void (*)())getkmem);
211 	openlog("in.rwhod", LOG_PID, LOG_DAEMON);
212 	/*
213 	 * Establish host name as returned by system.
214 	 */
215 	if (gethostname(myname, sizeof (myname) - 1) < 0) {
216 		syslog(LOG_ERR, "main: gethostname: %m");
217 		exit(1);
218 	}
219 	if ((cp = index(myname, '.')) != NULL)
220 		*cp = '\0';
221 	(void) strlcpy(mywd.wd_hostname, myname, sizeof (mywd.wd_hostname));
222 
223 	if (stat(UTMPX_FILE, &sb) < 0) {
224 		syslog(LOG_ERR, "main: stat: %s: %m", UTMPX_FILE);
225 		exit(1);
226 	}
227 	getkmem();
228 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
229 		syslog(LOG_ERR, "main: socket: %m");
230 		exit(1);
231 	}
232 	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
233 		syslog(LOG_ERR, "main: setsockopt SO_BROADCAST: %m");
234 		exit(1);
235 	}
236 	hp = gethostbyname(myname);
237 	if (hp == NULL) {
238 		syslog(LOG_ERR, "main: %s: don't know my own name\n", myname);
239 		exit(1);
240 	}
241 	sin.sin_family = hp->h_addrtype;
242 	sin.sin_port = sp->s_port;
243 	if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
244 		syslog(LOG_ERR, "main: bind: %m");
245 		exit(1);
246 	}
247 	if (!configure(s))
248 		exit(1);
249 	(void) sigset(SIGALRM, (void (*)())onalrm);
250 	onalrm();
251 	for (;;) {
252 		struct whod wd;
253 		int cc, whod;
254 		socklen_t len = sizeof (from);
255 
256 		cc = recvfrom(s, &wd, sizeof (struct whod), 0,
257 		    (struct sockaddr *)&from, &len);
258 		if (cc <= 0) {
259 			if (cc < 0 && errno != EINTR)
260 				syslog(LOG_WARNING, "main: recvfrom: %m");
261 			continue;
262 		}
263 		if (from.sin_port != sp->s_port) {
264 			syslog(LOG_WARNING, "main: %d: bad from port",
265 			    ntohs(from.sin_port));
266 			continue;
267 		}
268 #ifdef notdef
269 		if (gethostbyname(wd.wd_hostname) == 0) {
270 			syslog(LOG_WARNING, "main: %s: unknown host",
271 			    wd.wd_hostname);
272 			continue;
273 		}
274 #endif
275 		if (wd.wd_vers != WHODVERSION)
276 			continue;
277 		if (wd.wd_type != WHODTYPE_STATUS)
278 			continue;
279 		if (!verify(&wd)) {
280 			syslog(LOG_WARNING, "main: malformed host name from %x",
281 			    from.sin_addr.s_addr);
282 			continue;
283 		}
284 		(void) sprintf(path, "whod.%s", wd.wd_hostname);
285 		/*
286 		 * Rather than truncating and growing the file each time,
287 		 * use ftruncate if size is less than previous size.
288 		 */
289 		whod = open(path, O_WRONLY | O_CREAT, 0644);
290 		if (whod < 0) {
291 			syslog(LOG_WARNING, "main: open: %s: %m", path);
292 			continue;
293 		}
294 #if defined(_LITTLE_ENDIAN)
295 		/* CSTYLED */
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, &wd, cc);
316 		if (fstat(whod, &st) < 0 || st.st_size > cc)
317 			(void) ftruncate(whod, cc);
318 		(void) close(whod);
319 	}
320 	/* NOTREACHED */
321 usage:
322 	(void) fprintf(stderr, "usage: in.rwhod [ -m [ ttl ] ]\n");
323 	return (1);
324 }
325 
326 /*
327  * Check out host name for unprintables
328  * and other funnies before allowing a file
329  * to be created.  Sorry, but blanks aren't allowed.
330  */
331 static int
verify(const struct whod * wd)332 verify(const struct whod *wd)
333 {
334 	int size = 0;
335 	const char *name = wd->wd_hostname;
336 
337 	/*
338 	 * We shouldn't assume the name is NUL terminated, so bound the
339 	 * checks at the size of the whod structures wd_hostname field.
340 	 */
341 	while ((size < sizeof (wd->wd_hostname)) &&
342 	    (*name != '\0')) {
343 		if (*name == '/' || !isascii(*name) ||
344 		    !(isalnum(*name) || ispunct(*name)))
345 			return (0);
346 		name++, size++;
347 	}
348 	/*
349 	 * Fail the verification if NULL name or it wasn't NUL terminated.
350 	 */
351 	return ((size > 0) && (size < sizeof (wd->wd_hostname)));
352 }
353 
354 static int	utmpxtime;
355 static int	utmpxent;
356 static int	alarmcount;
357 struct	utmpx *utmpx;
358 
359 static void
onalrm(void)360 onalrm(void)
361 {
362 	int i;
363 	struct stat stb;
364 	int	utmpxsize = 0;
365 	int	entries;
366 	struct	utmpx *utp;
367 	struct	utmpx *utmpxbegin;
368 	struct whoent *we = mywd.wd_we, *wlast;
369 	int cc, cnt;
370 	double avenrun[3];
371 
372 	time_t now = time(0);
373 	struct neighbor *np;
374 
375 	if (alarmcount % 10 == 0)
376 		getkmem();
377 	alarmcount++;
378 	(void) stat(UTMPX_FILE, &stb);
379 	entries = stb.st_size / sizeof (struct futmpx);
380 	if ((stb.st_mtime != utmpxtime) || (entries > utmpxent)) {
381 		utmpxtime = stb.st_mtime;
382 		if (entries > utmpxent) {
383 			utmpxent = entries;
384 			utmpxsize = utmpxent * sizeof (struct utmpx);
385 			utmpx = realloc(utmpx, utmpxsize);
386 			if (utmpx == NULL) {
387 				syslog(LOG_ERR, "onalrm: realloc: %m");
388 				utmpxsize = 0;
389 				goto done;
390 			}
391 		}
392 		utmpxbegin = utmpx;
393 		setutxent();
394 		cnt = 0;
395 		while (cnt++ < utmpxent && (utp = getutxent()) != NULL)
396 			(void) memcpy(utmpxbegin++, utp, sizeof (struct utmpx));
397 		endutxent();
398 		wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
399 		for (i = 0; i < utmpxent; i++) {
400 			if (utmpx[i].ut_name[0] &&
401 			    utmpx[i].ut_type == USER_PROCESS) {
402 				/*
403 				 * XXX - utmpx name and line lengths should
404 				 * be here
405 				 */
406 				bcopy(utmpx[i].ut_line, we->we_utmp.out_line,
407 				    sizeof (we->we_utmp.out_line));
408 				bcopy(utmpx[i].ut_name, we->we_utmp.out_name,
409 				    sizeof (we->we_utmp.out_name));
410 				we->we_utmp.out_time =
411 				    htonl(utmpx[i].ut_xtime);
412 				if (we >= wlast)
413 					break;
414 				we++;
415 			}
416 		}
417 		utmpxent = we - mywd.wd_we;
418 	}
419 
420 	/*
421 	 * The test on utmpxent looks silly---after all, if no one is
422 	 * logged on, why worry about efficiency?---but is useful on
423 	 * (e.g.) compute servers.
424 	 */
425 	if (utmpxent > 0 && chdir("/dev") == -1) {
426 		syslog(LOG_ERR, "onalrm: chdir /dev: %m");
427 		exit(1);
428 	}
429 	we = mywd.wd_we;
430 	for (i = 0; i < utmpxent; i++) {
431 		if (stat(we->we_utmp.out_line, &stb) >= 0)
432 			we->we_idle = htonl(now - stb.st_atime);
433 		we++;
434 	}
435 	if (getloadavg(avenrun, 3) == -1) {
436 		syslog(LOG_ERR, "onalrm: getloadavg: %m");
437 		exit(1);
438 	}
439 
440 	for (i = 0; i < 3; i++)
441 		mywd.wd_loadav[i] = htonl((ulong_t)(avenrun[i] * 100));
442 	cc = (char *)we - (char *)&mywd;
443 	mywd.wd_sendtime = htonl(time(0));
444 	mywd.wd_vers = WHODVERSION;
445 	mywd.wd_type = WHODTYPE_STATUS;
446 	if (multicast_mode == SCOPED_MULTICAST) {
447 		(void) sendto(s, &mywd, cc, 0,
448 		    (struct sockaddr *)&multicast_addr,
449 		    sizeof (multicast_addr));
450 	} else for (np = neighbors; np != NULL; np = np->n_next) {
451 		if (multicast_mode == PER_INTERFACE_MULTICAST &&
452 		    np->n_flags & IFF_MULTICAST) {
453 			/*
454 			 * Select the outgoing interface for the multicast.
455 			 */
456 			if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
457 			    &(((struct sockaddr_in *)np->n_addr)->sin_addr),
458 			    sizeof (struct in_addr)) < 0) {
459 				syslog(LOG_ERR,
460 				    "onalrm: setsockopt IP_MULTICAST_IF: %m");
461 				exit(1);
462 			}
463 			(void) sendto(s, &mywd, cc, 0,
464 			    (struct sockaddr *)&multicast_addr,
465 			    sizeof (multicast_addr));
466 		} else {
467 			(void) sendto(s, &mywd, cc, 0,
468 			    (struct sockaddr *)np->n_addr, np->n_addrlen);
469 		}
470 	}
471 	if (utmpxent > 0 && chdir(RWHODIR) == -1) {
472 		syslog(LOG_ERR, "onalrm: chdir %s: %m", RWHODIR);
473 		exit(1);
474 	}
475 done:
476 	(void) alarm(AL_INTERVAL);
477 }
478 
479 static void
getkmem(void)480 getkmem(void)
481 {
482 	struct utmpx *utmpx, utmpx_id;
483 
484 	utmpx_id.ut_type = BOOT_TIME;
485 	if ((utmpx = getutxid(&utmpx_id)) != NULL)
486 		mywd.wd_boottime = utmpx->ut_xtime;
487 	endutxent();
488 	mywd.wd_boottime = htonl(mywd.wd_boottime);
489 }
490 
491 /*
492  * Figure out device configuration and select
493  * networks which deserve status information.
494  */
495 static boolean_t
configure(int s)496 configure(int s)
497 {
498 	char *buf;
499 	struct ifconf ifc;
500 	struct ifreq ifreq, *ifr;
501 	struct sockaddr_in *sin;
502 	struct neighbor *np;
503 	struct neighbor *np2;
504 	int n;
505 	int numifs;
506 	unsigned bufsize;
507 
508 	if (multicast_mode == SCOPED_MULTICAST) {
509 		struct ip_mreq mreq;
510 		unsigned char ttl;
511 
512 		mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
513 		mreq.imr_interface.s_addr = htonl(INADDR_ANY);
514 		if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
515 		    sizeof (mreq)) < 0) {
516 			syslog(LOG_ERR,
517 			    "configure: setsockopt IP_ADD_MEMBERSHIP: %m");
518 			return (B_FALSE);
519 		}
520 		ttl = multicast_scope;
521 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
522 		    sizeof (ttl)) < 0) {
523 			syslog(LOG_ERR,
524 			    "configure: setsockopt IP_MULTICAST_TTL: %m");
525 			return (B_FALSE);
526 		}
527 		multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP);
528 		multicast_addr.sin_port = sp->s_port;
529 		return (B_TRUE);
530 	}
531 
532 	if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) {
533 		syslog(LOG_ERR, "configure: ioctl SIOCGIFNUM: %m");
534 		return (B_FALSE);
535 	}
536 	bufsize = numifs * sizeof (struct ifreq);
537 	buf = malloc(bufsize);
538 	if (buf == NULL) {
539 		syslog(LOG_ERR, "configure: malloc: %m");
540 		return (B_FALSE);
541 	}
542 	ifc.ifc_len = bufsize;
543 	ifc.ifc_buf = buf;
544 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
545 		syslog(LOG_ERR,
546 		    "configure: ioctl (get interface configuration): %m");
547 		(void) free(buf);
548 		return (B_FALSE);
549 	}
550 	ifr = ifc.ifc_req;
551 	for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
552 		/* Skip all logical interfaces */
553 		if (index(ifr->ifr_name, ':') != NULL)
554 			continue;
555 
556 		for (np = neighbors; np != NULL; np = np->n_next) {
557 			if (np->n_name &&
558 			    strcmp(ifr->ifr_name, np->n_name) == 0)
559 				break;
560 		}
561 		if (np != NULL)
562 			continue;
563 		ifreq = *ifr;
564 		np = (struct neighbor *)malloc(sizeof (*np));
565 		if (np == NULL)
566 			continue;
567 		np->n_name = malloc(strlen(ifr->ifr_name) + 1);
568 		if (np->n_name == NULL) {
569 			free(np);
570 			continue;
571 		}
572 		(void) strcpy(np->n_name, ifr->ifr_name);
573 		np->n_addrlen = sizeof (ifr->ifr_addr);
574 		np->n_addr = malloc(np->n_addrlen);
575 		if (np->n_addr == NULL) {
576 			free(np->n_name);
577 			free(np);
578 			continue;
579 		}
580 		bcopy(&ifr->ifr_addr, np->n_addr, np->n_addrlen);
581 		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
582 			syslog(LOG_ERR,
583 			    "configure: ioctl (get interface flags): %m");
584 			free(np->n_addr);
585 			free(np->n_name);
586 			free(np);
587 			continue;
588 		}
589 		np->n_flags = ifreq.ifr_flags;
590 		if (((struct sockaddr_in *)np->n_addr)->sin_family == AF_INET &&
591 		    ioctl(s, SIOCGIFNETMASK, (char *)&ifreq) >= 0) {
592 			sin = (struct sockaddr_in *)np->n_addr;
593 
594 			np->n_subnet = sin->sin_addr.s_addr &
595 			    ((struct sockaddr_in *)&ifreq.ifr_addr)->
596 			    sin_addr.s_addr;
597 		}
598 		if (multicast_mode == PER_INTERFACE_MULTICAST &&
599 		    (np->n_flags & IFF_UP) &&
600 		    (np->n_flags & IFF_MULTICAST) &&
601 		    !(np->n_flags & IFF_LOOPBACK)) {
602 			struct ip_mreq mreq;
603 
604 			/*
605 			 * Skip interfaces that have matching subnets i.e.
606 			 * (addr & netmask) are identical.
607 			 * Such interfaces are connected to the same
608 			 * physical wire.
609 			 */
610 			for (np2 = neighbors; np2 != NULL; np2 = np2->n_next) {
611 
612 				if (!(np->n_flags & IFF_POINTOPOINT) &&
613 				    !(np2->n_flags & IFF_POINTOPOINT) &&
614 				    (np->n_subnet == np2->n_subnet)) {
615 					free(np->n_addr);
616 					free(np->n_name);
617 					free(np);
618 					break;
619 				}
620 			}
621 			if (np2 != NULL)
622 				continue;
623 
624 			mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
625 			mreq.imr_interface.s_addr =
626 			    ((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr;
627 			if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
628 			    sizeof (mreq)) < 0) {
629 				syslog(LOG_ERR,
630 				    "configure: "
631 				    "setsockopt IP_ADD_MEMBERSHIP: %m");
632 				free(np->n_addr);
633 				free(np->n_name);
634 				free(np);
635 				continue;
636 			}
637 			multicast_addr.sin_addr.s_addr =
638 			    htonl(INADDR_WHOD_GROUP);
639 			multicast_addr.sin_port = sp->s_port;
640 			np->n_next = neighbors;
641 			neighbors = np;
642 			continue;
643 		}
644 		if ((np->n_flags & IFF_UP) == 0 ||
645 		    (np->n_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
646 			free(np->n_addr);
647 			free(np->n_name);
648 			free(np);
649 			continue;
650 		}
651 		if (np->n_flags & IFF_POINTOPOINT) {
652 			if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
653 				syslog(LOG_ERR,
654 				    "configure: ioctl (get dstaddr): %m");
655 				free(np->n_addr);
656 				free(np->n_name);
657 				free(np);
658 				continue;
659 			}
660 			/* we assume addresses are all the same size */
661 			bcopy(&ifreq.ifr_dstaddr, np->n_addr, np->n_addrlen);
662 		}
663 		if (np->n_flags & IFF_BROADCAST) {
664 			if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
665 				syslog(LOG_ERR,
666 				    "configure: ioctl (get broadaddr): %m");
667 				free(np->n_addr);
668 				free(np->n_name);
669 				free(np);
670 				continue;
671 			}
672 			/* we assume addresses are all the same size */
673 			bcopy(&ifreq.ifr_broadaddr, np->n_addr, np->n_addrlen);
674 		}
675 		/* gag, wish we could get rid of Internet dependencies */
676 		sin = (struct sockaddr_in *)np->n_addr;
677 		sin->sin_port = sp->s_port;
678 
679 		/*
680 		 * Avoid adding duplicate broadcast and pt-pt destinations
681 		 * to the list.
682 		 */
683 		for (np2 = neighbors; np2 != NULL; np2 = np2->n_next) {
684 			struct sockaddr_in *sin2;
685 
686 			sin2 = (struct sockaddr_in *)np2->n_addr;
687 			if (sin2->sin_addr.s_addr == sin->sin_addr.s_addr) {
688 				free(np->n_addr);
689 				free(np->n_name);
690 				free(np);
691 				break;
692 			}
693 		}
694 		if (np2 != NULL)
695 			continue;
696 
697 		np->n_next = neighbors;
698 		neighbors = np;
699 	}
700 	(void) free(buf);
701 	return (B_TRUE);
702 }
703 
704 #ifdef DEBUG
705 static char *interval(uint_t, char *);
706 
707 /* ARGSUSED */
708 static ssize_t
sendto(int s,const void * buf,size_t cc,int flags,const struct sockaddr * to,socklen_t tolen)709 sendto(int s, const void *buf, size_t cc, int flags, const struct sockaddr *to,
710     socklen_t tolen)
711 {
712 	struct whod *w = (struct whod *)buf;
713 	struct whoent *we;
714 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
715 	int nsz;
716 
717 	(void) printf("sendto %x.%d\n", ntohl(sin->sin_addr.s_addr),
718 	    ntohs(sin->sin_port));
719 	(void) printf("hostname %s %s\n", w->wd_hostname,
720 	    interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
721 	(void) printf("load %4.2f, %4.2f, %4.2f\n",
722 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
723 	    ntohl(w->wd_loadav[2]) / 100.0);
724 	cc -= WHDRSIZE;
725 	for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
726 		time_t t = ntohl(we->we_utmp.out_time);
727 
728 		nsz = sizeof (we->we_utmp.out_name);
729 		(void) printf("%-*.*s %s:%s %.12s",
730 		    nsz,
731 		    nsz,
732 		    we->we_utmp.out_name,
733 		    w->wd_hostname,
734 		    we->we_utmp.out_line,
735 		    ctime(&t)+4);
736 		we->we_idle = ntohl(we->we_idle) / 60;
737 		if (we->we_idle) {
738 			if (we->we_idle >= 100*60)
739 				we->we_idle = 100*60 - 1;
740 			if (we->we_idle >= 60)
741 				(void) printf(" %2d", we->we_idle / 60);
742 			else
743 				(void) printf("   ");
744 			(void) printf(":%02d", we->we_idle % 60);
745 		}
746 		(void) printf("\n");
747 	}
748 	return (0);
749 }
750 
751 static char *
interval(uint_t time,char * updown)752 interval(uint_t time, char *updown)
753 {
754 	static char resbuf[32];
755 	int days, hours, minutes;
756 
757 	if (time > 3*30*24*60*60) {
758 		(void) sprintf(resbuf, "   %s ??:??", updown);
759 		return (resbuf);
760 	}
761 	minutes = (time + 59) / 60;		/* round to minutes */
762 	hours = minutes / 60;
763 	minutes %= 60;
764 	days = hours / 24;
765 	hours %= 24;
766 	if (days > 0) {
767 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
768 		    updown, days, hours, minutes);
769 	} else {
770 		(void) sprintf(resbuf, "%s    %2d:%02d",
771 		    updown, hours, minutes);
772 	}
773 	return (resbuf);
774 }
775 #endif
776