xref: /titanic_51/usr/src/cmd/cmd-inet/usr.sbin/in.rwhod.c (revision 740638c8fa5a1c03618a9c75c3161dba57259a17)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*740638c8Sbw  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/param.h>
397c478bd9Sstevel@tonic-gate #include <sys/socket.h>
407c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
437c478bd9Sstevel@tonic-gate #include <sys/file.h>
447c478bd9Sstevel@tonic-gate #include <sys/loadavg.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <net/if.h>
477c478bd9Sstevel@tonic-gate #include <netinet/in.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <stdio.h>
507c478bd9Sstevel@tonic-gate #include <signal.h>
517c478bd9Sstevel@tonic-gate #include <errno.h>
527c478bd9Sstevel@tonic-gate #include <utmpx.h>
537c478bd9Sstevel@tonic-gate #include <ctype.h>
547c478bd9Sstevel@tonic-gate #include <netdb.h>
557c478bd9Sstevel@tonic-gate #include <syslog.h>
567c478bd9Sstevel@tonic-gate #include <fcntl.h>
577c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>	/* for ENDIAN defines */
587c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
597c478bd9Sstevel@tonic-gate #include <protocols/rwhod.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #include <strings.h>
627c478bd9Sstevel@tonic-gate #include <stdlib.h>
637c478bd9Sstevel@tonic-gate #include <unistd.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * This version of Berkeley's rwhod has been modified to use IP multicast
677c478bd9Sstevel@tonic-gate  * datagrams, under control of a new command-line option:
687c478bd9Sstevel@tonic-gate  *
697c478bd9Sstevel@tonic-gate  *	rwhod -m	causes rwhod to use IP multicast (instead of
707c478bd9Sstevel@tonic-gate  *			broadcast or unicast) on all interfaces that have
717c478bd9Sstevel@tonic-gate  *			the IFF_MULTICAST flag set in their "ifnet" structs
727c478bd9Sstevel@tonic-gate  *			(excluding the loopback interface).  The multicast
737c478bd9Sstevel@tonic-gate  *			reports are sent with a time-to-live of 1, to prevent
747c478bd9Sstevel@tonic-gate  *			forwarding beyond the directly-connected subnet(s).
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  *	rwhod -m <ttl>	causes rwhod to send IP multicast datagrams with a
777c478bd9Sstevel@tonic-gate  *			time-to-live of <ttl>, via a SINGLE interface rather
787c478bd9Sstevel@tonic-gate  *			than all interfaces.  <ttl> must be between 0 and
797c478bd9Sstevel@tonic-gate  *			MAX_MULTICAST_SCOPE, defined below.  Note that "-m 1"
807c478bd9Sstevel@tonic-gate  *			is different than "-m", in that "-m 1" specifies
817c478bd9Sstevel@tonic-gate  *			transmission on one interface only.
827c478bd9Sstevel@tonic-gate  *
837c478bd9Sstevel@tonic-gate  * When "-m" is used without a <ttl> argument, the program accepts multicast
847c478bd9Sstevel@tonic-gate  * rwhod reports from all multicast-capable interfaces.  If a <ttl> argument
857c478bd9Sstevel@tonic-gate  * is given, it accepts multicast reports from only one interface, the one
867c478bd9Sstevel@tonic-gate  * on which reports are sent (which may be controlled via the host's routing
877c478bd9Sstevel@tonic-gate  * table).  Regardless of the "-m" option, the program accepts broadcast or
887c478bd9Sstevel@tonic-gate  * unicast reports from all interfaces.  Thus, this program will hear the
897c478bd9Sstevel@tonic-gate  * reports of old, non-multicasting rwhods, but, if multicasting is used,
907c478bd9Sstevel@tonic-gate  * those old rwhods won't hear the reports generated by this program.
917c478bd9Sstevel@tonic-gate  *
927c478bd9Sstevel@tonic-gate  *                  -- Steve Deering, Stanford University, February 1989
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #define	NO_MULTICAST		0	  /* multicast modes */
967c478bd9Sstevel@tonic-gate #define	PER_INTERFACE_MULTICAST	1
977c478bd9Sstevel@tonic-gate #define	SCOPED_MULTICAST	2
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #define	MAX_MULTICAST_SCOPE	32	  /* "site-wide", by convention */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate #define	INADDR_WHOD_GROUP	(ulong_t)0xe0000103	/* 224.0.1.3 */
1027c478bd9Sstevel@tonic-gate 					/* (belongs in protocols/rwhod.h) */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static int			multicast_mode  = NO_MULTICAST;
1057c478bd9Sstevel@tonic-gate static int			multicast_scope;
1067c478bd9Sstevel@tonic-gate static struct sockaddr_in	multicast_addr  = { AF_INET };
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * Alarm interval. Don't forget to change the down time check in ruptime
1117c478bd9Sstevel@tonic-gate  * if this is changed.
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate #define	AL_INTERVAL (3 * 60)
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static struct	sockaddr_in sin = { AF_INET };
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static char	myname[MAXHOSTNAMELEN];
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * We communicate with each neighbor in
1217c478bd9Sstevel@tonic-gate  * a list constructed at the time we're
1227c478bd9Sstevel@tonic-gate  * started up.  Neighbors are currently
1237c478bd9Sstevel@tonic-gate  * directly connected via a hardware interface.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate struct	neighbor {
1267c478bd9Sstevel@tonic-gate 	struct	neighbor *n_next;
1277c478bd9Sstevel@tonic-gate 	char	*n_name;		/* interface name */
1287c478bd9Sstevel@tonic-gate 	char	*n_addr;		/* who to send to */
1297c478bd9Sstevel@tonic-gate 	int	n_addrlen;		/* size of address */
1307c478bd9Sstevel@tonic-gate 	ulong_t	n_subnet;		/* AF_INET subnet */
1317c478bd9Sstevel@tonic-gate 	uint_t	n_flags;		/* should forward?, interface flags */
1327c478bd9Sstevel@tonic-gate };
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate static struct	neighbor *neighbors;
1357c478bd9Sstevel@tonic-gate static struct	whod mywd;
1367c478bd9Sstevel@tonic-gate static struct	servent *sp;
1377c478bd9Sstevel@tonic-gate static int	s;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate #define	WHDRSIZE	(sizeof (mywd) - sizeof (mywd.wd_we))
1407c478bd9Sstevel@tonic-gate #define	RWHODIR		"/var/spool/rwho"
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static void		onalrm(void);
1437c478bd9Sstevel@tonic-gate static void		getkmem(void);
1447c478bd9Sstevel@tonic-gate static boolean_t	configure(int);
1457c478bd9Sstevel@tonic-gate static int		verify(const struct whod *);
1467c478bd9Sstevel@tonic-gate 
147*740638c8Sbw int
1487c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	struct sockaddr_in from;
1517c478bd9Sstevel@tonic-gate 	struct stat st;
1527c478bd9Sstevel@tonic-gate 	char path[64];
1537c478bd9Sstevel@tonic-gate 	struct hostent *hp;
1547c478bd9Sstevel@tonic-gate 	int on = 1;
1557c478bd9Sstevel@tonic-gate 	char *cp;
1567c478bd9Sstevel@tonic-gate 	struct stat sb;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (getuid()) {
1597c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "in.rwhod: not super user\n");
1607c478bd9Sstevel@tonic-gate 		exit(1);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 	sp = getservbyname("who", "udp");
1637c478bd9Sstevel@tonic-gate 	if (sp == NULL) {
1647c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "in.rwhod: udp/who: unknown service\n");
1657c478bd9Sstevel@tonic-gate 		exit(1);
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	argv++;
1687c478bd9Sstevel@tonic-gate 	argc--;
1697c478bd9Sstevel@tonic-gate 	while (argc > 0 && *argv[0] == '-') {
1707c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "-m") == 0) {
1717c478bd9Sstevel@tonic-gate 			if (argc > 1 && isdigit(*(argv + 1)[0])) {
1727c478bd9Sstevel@tonic-gate 				argv++;
1737c478bd9Sstevel@tonic-gate 				argc--;
1747c478bd9Sstevel@tonic-gate 				multicast_mode  = SCOPED_MULTICAST;
1757c478bd9Sstevel@tonic-gate 				multicast_scope = atoi(*argv);
1767c478bd9Sstevel@tonic-gate 				if (multicast_scope > MAX_MULTICAST_SCOPE) {
1777c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
1787c478bd9Sstevel@tonic-gate 					    "in.rwhod: "
1797c478bd9Sstevel@tonic-gate 					    "ttl must not exceed %u\n",
1807c478bd9Sstevel@tonic-gate 					    MAX_MULTICAST_SCOPE);
1817c478bd9Sstevel@tonic-gate 					exit(1);
1827c478bd9Sstevel@tonic-gate 				}
1837c478bd9Sstevel@tonic-gate 			} else {
1847c478bd9Sstevel@tonic-gate 				multicast_mode = PER_INTERFACE_MULTICAST;
1857c478bd9Sstevel@tonic-gate 			}
1867c478bd9Sstevel@tonic-gate 		} else {
1877c478bd9Sstevel@tonic-gate 			goto usage;
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 		argv++;
1907c478bd9Sstevel@tonic-gate 		argc--;
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 	if (argc > 0)
1937c478bd9Sstevel@tonic-gate 		goto usage;
1947c478bd9Sstevel@tonic-gate 	if (chdir(RWHODIR) < 0) {
1957c478bd9Sstevel@tonic-gate 		perror(RWHODIR);
1967c478bd9Sstevel@tonic-gate 		exit(1);
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate #ifndef DEBUG
1997c478bd9Sstevel@tonic-gate 	if (fork())
2007c478bd9Sstevel@tonic-gate 		exit(0);
2017c478bd9Sstevel@tonic-gate 	/* CSTYLED */
2027c478bd9Sstevel@tonic-gate 	{
2037c478bd9Sstevel@tonic-gate 		(void) close(0);
2047c478bd9Sstevel@tonic-gate 		(void) close(1);
2057c478bd9Sstevel@tonic-gate 		(void) close(2);
2067c478bd9Sstevel@tonic-gate 		(void) open("/", 0);
2077c478bd9Sstevel@tonic-gate 		(void) dup2(0, 1);
2087c478bd9Sstevel@tonic-gate 		(void) dup2(0, 2);
2097c478bd9Sstevel@tonic-gate 		(void) setsid();
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate #endif
2127c478bd9Sstevel@tonic-gate 	(void) sigset(SIGHUP, (void (*)())getkmem);
2137c478bd9Sstevel@tonic-gate 	openlog("in.rwhod", LOG_PID, LOG_DAEMON);
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * Establish host name as returned by system.
2167c478bd9Sstevel@tonic-gate 	 */
2177c478bd9Sstevel@tonic-gate 	if (gethostname(myname, sizeof (myname) - 1) < 0) {
2187c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "main: gethostname: %m");
2197c478bd9Sstevel@tonic-gate 		exit(1);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 	if ((cp = index(myname, '.')) != NULL)
2227c478bd9Sstevel@tonic-gate 		*cp = '\0';
2237c478bd9Sstevel@tonic-gate 	(void) strlcpy(mywd.wd_hostname, myname, sizeof (mywd.wd_hostname));
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	if (stat(UTMPX_FILE, &sb) < 0) {
2267c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "main: stat: %s: %m", UTMPX_FILE);
2277c478bd9Sstevel@tonic-gate 		exit(1);
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 	getkmem();
2307c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
2317c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "main: socket: %m");
2327c478bd9Sstevel@tonic-gate 		exit(1);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
2357c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "main: setsockopt SO_BROADCAST: %m");
2367c478bd9Sstevel@tonic-gate 		exit(1);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 	hp = gethostbyname(myname);
2397c478bd9Sstevel@tonic-gate 	if (hp == NULL) {
2407c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "main: %s: don't know my own name\n", myname);
2417c478bd9Sstevel@tonic-gate 		exit(1);
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 	sin.sin_family = hp->h_addrtype;
2447c478bd9Sstevel@tonic-gate 	sin.sin_port = sp->s_port;
2457c478bd9Sstevel@tonic-gate 	if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
2467c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "main: bind: %m");
2477c478bd9Sstevel@tonic-gate 		exit(1);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 	if (!configure(s))
2507c478bd9Sstevel@tonic-gate 		exit(1);
2517c478bd9Sstevel@tonic-gate 	(void) sigset(SIGALRM, (void (*)())onalrm);
2527c478bd9Sstevel@tonic-gate 	onalrm();
2537c478bd9Sstevel@tonic-gate 	for (;;) {
2547c478bd9Sstevel@tonic-gate 		struct whod wd;
2557c478bd9Sstevel@tonic-gate 		int cc, whod;
2567c478bd9Sstevel@tonic-gate 		socklen_t len = sizeof (from);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 		cc = recvfrom(s, &wd, sizeof (struct whod), 0,
2597c478bd9Sstevel@tonic-gate 		    (struct sockaddr *)&from, &len);
2607c478bd9Sstevel@tonic-gate 		if (cc <= 0) {
2617c478bd9Sstevel@tonic-gate 			if (cc < 0 && errno != EINTR)
2627c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING, "main: recvfrom: %m");
2637c478bd9Sstevel@tonic-gate 			continue;
2647c478bd9Sstevel@tonic-gate 		}
2657c478bd9Sstevel@tonic-gate 		if (from.sin_port != sp->s_port) {
2667c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING, "main: %d: bad from port",
2677c478bd9Sstevel@tonic-gate 			    ntohs(from.sin_port));
2687c478bd9Sstevel@tonic-gate 			continue;
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate #ifdef notdef
2717c478bd9Sstevel@tonic-gate 		if (gethostbyname(wd.wd_hostname) == 0) {
2727c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING, "main: %s: unknown host",
2737c478bd9Sstevel@tonic-gate 			    wd.wd_hostname);
2747c478bd9Sstevel@tonic-gate 			continue;
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate #endif
2777c478bd9Sstevel@tonic-gate 		if (wd.wd_vers != WHODVERSION)
2787c478bd9Sstevel@tonic-gate 			continue;
2797c478bd9Sstevel@tonic-gate 		if (wd.wd_type != WHODTYPE_STATUS)
2807c478bd9Sstevel@tonic-gate 			continue;
2817c478bd9Sstevel@tonic-gate 		if (!verify(&wd)) {
2827c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING, "main: malformed host name from %x",
2837c478bd9Sstevel@tonic-gate 			    from.sin_addr.s_addr);
2847c478bd9Sstevel@tonic-gate 			continue;
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 		(void) sprintf(path, "whod.%s", wd.wd_hostname);
2877c478bd9Sstevel@tonic-gate 		/*
2887c478bd9Sstevel@tonic-gate 		 * Rather than truncating and growing the file each time,
2897c478bd9Sstevel@tonic-gate 		 * use ftruncate if size is less than previous size.
2907c478bd9Sstevel@tonic-gate 		 */
2917c478bd9Sstevel@tonic-gate 		whod = open(path, O_WRONLY | O_CREAT, 0644);
2927c478bd9Sstevel@tonic-gate 		if (whod < 0) {
2937c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING, "main: open: %s: %m", path);
2947c478bd9Sstevel@tonic-gate 			continue;
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
2977c478bd9Sstevel@tonic-gate 		/* CSTYLED */
2987c478bd9Sstevel@tonic-gate 		{
2997c478bd9Sstevel@tonic-gate 			int i, n = (cc - WHDRSIZE)/sizeof (struct whoent);
3007c478bd9Sstevel@tonic-gate 			struct whoent *we;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 			/* undo header byte swapping before writing to file */
3037c478bd9Sstevel@tonic-gate 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
3047c478bd9Sstevel@tonic-gate 			for (i = 0; i < 3; i++)
3057c478bd9Sstevel@tonic-gate 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
3067c478bd9Sstevel@tonic-gate 			wd.wd_boottime = ntohl(wd.wd_boottime);
3077c478bd9Sstevel@tonic-gate 			we = wd.wd_we;
3087c478bd9Sstevel@tonic-gate 			for (i = 0; i < n; i++) {
3097c478bd9Sstevel@tonic-gate 				we->we_idle = ntohl(we->we_idle);
3107c478bd9Sstevel@tonic-gate 				we->we_utmp.out_time =
3117c478bd9Sstevel@tonic-gate 				    ntohl(we->we_utmp.out_time);
3127c478bd9Sstevel@tonic-gate 				we++;
3137c478bd9Sstevel@tonic-gate 			}
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate #endif
3167c478bd9Sstevel@tonic-gate 		(void) time((time_t *)&wd.wd_recvtime);
3177c478bd9Sstevel@tonic-gate 		(void) write(whod, &wd, cc);
3187c478bd9Sstevel@tonic-gate 		if (fstat(whod, &st) < 0 || st.st_size > cc)
3197c478bd9Sstevel@tonic-gate 			(void) ftruncate(whod, cc);
3207c478bd9Sstevel@tonic-gate 		(void) close(whod);
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
3237c478bd9Sstevel@tonic-gate usage:
3247c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "usage: in.rwhod [ -m [ ttl ] ]\n");
325*740638c8Sbw 	return (1);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate  * Check out host name for unprintables
3307c478bd9Sstevel@tonic-gate  * and other funnies before allowing a file
3317c478bd9Sstevel@tonic-gate  * to be created.  Sorry, but blanks aren't allowed.
3327c478bd9Sstevel@tonic-gate  */
3337c478bd9Sstevel@tonic-gate static int
3347c478bd9Sstevel@tonic-gate verify(const struct whod *wd)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	int size = 0;
3377c478bd9Sstevel@tonic-gate 	const char *name = wd->wd_hostname;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	/*
3407c478bd9Sstevel@tonic-gate 	 * We shouldn't assume the name is NUL terminated, so bound the
3417c478bd9Sstevel@tonic-gate 	 * checks at the size of the whod structures wd_hostname field.
3427c478bd9Sstevel@tonic-gate 	 */
3437c478bd9Sstevel@tonic-gate 	while ((size < sizeof (wd->wd_hostname)) &&
3447c478bd9Sstevel@tonic-gate 	    (*name != '\0')) {
3457c478bd9Sstevel@tonic-gate 		if (*name == '/' || !isascii(*name) ||
3467c478bd9Sstevel@tonic-gate 		    !(isalnum(*name) || ispunct(*name)))
3477c478bd9Sstevel@tonic-gate 			return (0);
3487c478bd9Sstevel@tonic-gate 		name++, size++;
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 	/*
3517c478bd9Sstevel@tonic-gate 	 * Fail the verification if NULL name or it wasn't NUL terminated.
3527c478bd9Sstevel@tonic-gate 	 */
3537c478bd9Sstevel@tonic-gate 	return ((size > 0) && (size < sizeof (wd->wd_hostname)));
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate static int	utmpxtime;
3577c478bd9Sstevel@tonic-gate static int	utmpxent;
3587c478bd9Sstevel@tonic-gate static int	alarmcount;
3597c478bd9Sstevel@tonic-gate struct	utmpx *utmpx;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate static void
3627c478bd9Sstevel@tonic-gate onalrm(void)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	int i;
3657c478bd9Sstevel@tonic-gate 	struct stat stb;
3667c478bd9Sstevel@tonic-gate 	int	utmpxsize = 0;
3677c478bd9Sstevel@tonic-gate 	int	entries;
3687c478bd9Sstevel@tonic-gate 	struct	utmpx *utp;
3697c478bd9Sstevel@tonic-gate 	struct	utmpx *utmpxbegin;
3707c478bd9Sstevel@tonic-gate 	struct whoent *we = mywd.wd_we, *wlast;
3717c478bd9Sstevel@tonic-gate 	int cc, cnt;
3727c478bd9Sstevel@tonic-gate 	double avenrun[3];
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	time_t now = time(0);
3757c478bd9Sstevel@tonic-gate 	struct neighbor *np;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if (alarmcount % 10 == 0)
3787c478bd9Sstevel@tonic-gate 		getkmem();
3797c478bd9Sstevel@tonic-gate 	alarmcount++;
3807c478bd9Sstevel@tonic-gate 	(void) stat(UTMPX_FILE, &stb);
3817c478bd9Sstevel@tonic-gate 	entries = stb.st_size / sizeof (struct futmpx);
3827c478bd9Sstevel@tonic-gate 	if ((stb.st_mtime != utmpxtime) || (entries > utmpxent)) {
3837c478bd9Sstevel@tonic-gate 		utmpxtime = stb.st_mtime;
3847c478bd9Sstevel@tonic-gate 		if (entries > utmpxent) {
3857c478bd9Sstevel@tonic-gate 			utmpxent = entries;
3867c478bd9Sstevel@tonic-gate 			utmpxsize = utmpxent * sizeof (struct utmpx);
3877c478bd9Sstevel@tonic-gate 			utmpx = realloc(utmpx, utmpxsize);
3887c478bd9Sstevel@tonic-gate 			if (utmpx == NULL) {
3897c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "onalrm: realloc: %m");
3907c478bd9Sstevel@tonic-gate 				utmpxsize = 0;
3917c478bd9Sstevel@tonic-gate 				goto done;
3927c478bd9Sstevel@tonic-gate 			}
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 		utmpxbegin = utmpx;
3957c478bd9Sstevel@tonic-gate 		setutxent();
3967c478bd9Sstevel@tonic-gate 		cnt = 0;
3977c478bd9Sstevel@tonic-gate 		while (cnt++ < utmpxent && (utp = getutxent()) != NULL)
3987c478bd9Sstevel@tonic-gate 			(void) memcpy(utmpxbegin++, utp, sizeof (struct utmpx));
3997c478bd9Sstevel@tonic-gate 		endutxent();
4007c478bd9Sstevel@tonic-gate 		wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
4017c478bd9Sstevel@tonic-gate 		for (i = 0; i < utmpxent; i++) {
4027c478bd9Sstevel@tonic-gate 			if (utmpx[i].ut_name[0] &&
4037c478bd9Sstevel@tonic-gate 			    utmpx[i].ut_type == USER_PROCESS) {
4047c478bd9Sstevel@tonic-gate 				/*
4057c478bd9Sstevel@tonic-gate 				 * XXX - utmpx name and line lengths should
4067c478bd9Sstevel@tonic-gate 				 * be here
4077c478bd9Sstevel@tonic-gate 				 */
4087c478bd9Sstevel@tonic-gate 				bcopy(utmpx[i].ut_line, we->we_utmp.out_line,
4097c478bd9Sstevel@tonic-gate 				    sizeof (we->we_utmp.out_line));
4107c478bd9Sstevel@tonic-gate 				bcopy(utmpx[i].ut_name, we->we_utmp.out_name,
4117c478bd9Sstevel@tonic-gate 				    sizeof (we->we_utmp.out_name));
4127c478bd9Sstevel@tonic-gate 				we->we_utmp.out_time =
4137c478bd9Sstevel@tonic-gate 				    htonl(utmpx[i].ut_xtime);
4147c478bd9Sstevel@tonic-gate 				if (we >= wlast)
4157c478bd9Sstevel@tonic-gate 					break;
4167c478bd9Sstevel@tonic-gate 				we++;
4177c478bd9Sstevel@tonic-gate 			}
4187c478bd9Sstevel@tonic-gate 		}
4197c478bd9Sstevel@tonic-gate 		utmpxent = we - mywd.wd_we;
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	/*
4237c478bd9Sstevel@tonic-gate 	 * The test on utmpxent looks silly---after all, if no one is
4247c478bd9Sstevel@tonic-gate 	 * logged on, why worry about efficiency?---but is useful on
4257c478bd9Sstevel@tonic-gate 	 * (e.g.) compute servers.
4267c478bd9Sstevel@tonic-gate 	 */
4277c478bd9Sstevel@tonic-gate 	if (utmpxent > 0 && chdir("/dev") == -1) {
4287c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "onalrm: chdir /dev: %m");
4297c478bd9Sstevel@tonic-gate 		exit(1);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 	we = mywd.wd_we;
4327c478bd9Sstevel@tonic-gate 	for (i = 0; i < utmpxent; i++) {
4337c478bd9Sstevel@tonic-gate 		if (stat(we->we_utmp.out_line, &stb) >= 0)
4347c478bd9Sstevel@tonic-gate 			we->we_idle = htonl(now - stb.st_atime);
4357c478bd9Sstevel@tonic-gate 		we++;
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 	if (getloadavg(avenrun, 3) == -1) {
4387c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "onalrm: getloadavg: %m");
4397c478bd9Sstevel@tonic-gate 		exit(1);
4407c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++)
4437c478bd9Sstevel@tonic-gate 		mywd.wd_loadav[i] = htonl((ulong_t)(avenrun[i] * 100));
4447c478bd9Sstevel@tonic-gate 	cc = (char *)we - (char *)&mywd;
4457c478bd9Sstevel@tonic-gate 	mywd.wd_sendtime = htonl(time(0));
4467c478bd9Sstevel@tonic-gate 	mywd.wd_vers = WHODVERSION;
4477c478bd9Sstevel@tonic-gate 	mywd.wd_type = WHODTYPE_STATUS;
4487c478bd9Sstevel@tonic-gate 	if (multicast_mode == SCOPED_MULTICAST) {
4497c478bd9Sstevel@tonic-gate 		(void) sendto(s, &mywd, cc, 0,
4507c478bd9Sstevel@tonic-gate 		    (struct sockaddr *)&multicast_addr,
4517c478bd9Sstevel@tonic-gate 		    sizeof (multicast_addr));
4527c478bd9Sstevel@tonic-gate 	} else for (np = neighbors; np != NULL; np = np->n_next) {
4537c478bd9Sstevel@tonic-gate 		if (multicast_mode == PER_INTERFACE_MULTICAST &&
4547c478bd9Sstevel@tonic-gate 		    np->n_flags & IFF_MULTICAST) {
4557c478bd9Sstevel@tonic-gate 			/*
4567c478bd9Sstevel@tonic-gate 			 * Select the outgoing interface for the multicast.
4577c478bd9Sstevel@tonic-gate 			 */
4587c478bd9Sstevel@tonic-gate 			if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
4597c478bd9Sstevel@tonic-gate 			    &(((struct sockaddr_in *)np->n_addr)->sin_addr),
4607c478bd9Sstevel@tonic-gate 			    sizeof (struct in_addr)) < 0) {
4617c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
4627c478bd9Sstevel@tonic-gate 				    "onalrm: setsockopt IP_MULTICAST_IF: %m");
4637c478bd9Sstevel@tonic-gate 				exit(1);
4647c478bd9Sstevel@tonic-gate 			}
4657c478bd9Sstevel@tonic-gate 			(void) sendto(s, &mywd, cc, 0,
4667c478bd9Sstevel@tonic-gate 			    (struct sockaddr *)&multicast_addr,
4677c478bd9Sstevel@tonic-gate 			    sizeof (multicast_addr));
4687c478bd9Sstevel@tonic-gate 		} else {
4697c478bd9Sstevel@tonic-gate 			(void) sendto(s, &mywd, cc, 0,
4707c478bd9Sstevel@tonic-gate 			    (struct sockaddr *)np->n_addr, np->n_addrlen);
4717c478bd9Sstevel@tonic-gate 		}
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 	if (utmpxent > 0 && chdir(RWHODIR) == -1) {
4747c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "onalrm: chdir %s: %m", RWHODIR);
4757c478bd9Sstevel@tonic-gate 		exit(1);
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate done:
4787c478bd9Sstevel@tonic-gate 	(void) alarm(AL_INTERVAL);
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate static void
4827c478bd9Sstevel@tonic-gate getkmem(void)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	struct utmpx *utmpx, utmpx_id;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	utmpx_id.ut_type = BOOT_TIME;
4877c478bd9Sstevel@tonic-gate 	if ((utmpx = getutxid(&utmpx_id)) != NULL)
4887c478bd9Sstevel@tonic-gate 		mywd.wd_boottime = utmpx->ut_xtime;
4897c478bd9Sstevel@tonic-gate 	endutxent();
4907c478bd9Sstevel@tonic-gate 	mywd.wd_boottime = htonl(mywd.wd_boottime);
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate /*
4947c478bd9Sstevel@tonic-gate  * Figure out device configuration and select
4957c478bd9Sstevel@tonic-gate  * networks which deserve status information.
4967c478bd9Sstevel@tonic-gate  */
4977c478bd9Sstevel@tonic-gate static boolean_t
4987c478bd9Sstevel@tonic-gate configure(int s)
4997c478bd9Sstevel@tonic-gate {
5007c478bd9Sstevel@tonic-gate 	char *buf;
5017c478bd9Sstevel@tonic-gate 	struct ifconf ifc;
5027c478bd9Sstevel@tonic-gate 	struct ifreq ifreq, *ifr;
5037c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
5047c478bd9Sstevel@tonic-gate 	struct neighbor *np;
5057c478bd9Sstevel@tonic-gate 	struct neighbor *np2;
5067c478bd9Sstevel@tonic-gate 	int n;
5077c478bd9Sstevel@tonic-gate 	int numifs;
5087c478bd9Sstevel@tonic-gate 	unsigned bufsize;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	if (multicast_mode == SCOPED_MULTICAST) {
5117c478bd9Sstevel@tonic-gate 		struct ip_mreq mreq;
5127c478bd9Sstevel@tonic-gate 		unsigned char ttl;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 		mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
5157c478bd9Sstevel@tonic-gate 		mreq.imr_interface.s_addr = htonl(INADDR_ANY);
5167c478bd9Sstevel@tonic-gate 		if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
5177c478bd9Sstevel@tonic-gate 		    sizeof (mreq)) < 0) {
5187c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
5197c478bd9Sstevel@tonic-gate 			    "configure: setsockopt IP_ADD_MEMBERSHIP: %m");
5207c478bd9Sstevel@tonic-gate 			return (B_FALSE);
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 		ttl = multicast_scope;
5237c478bd9Sstevel@tonic-gate 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
5247c478bd9Sstevel@tonic-gate 		    sizeof (ttl)) < 0) {
5257c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
5267c478bd9Sstevel@tonic-gate 			    "configure: setsockopt IP_MULTICAST_TTL: %m");
5277c478bd9Sstevel@tonic-gate 			return (B_FALSE);
5287c478bd9Sstevel@tonic-gate 		}
5297c478bd9Sstevel@tonic-gate 		multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP);
5307c478bd9Sstevel@tonic-gate 		multicast_addr.sin_port = sp->s_port;
5317c478bd9Sstevel@tonic-gate 		return (B_TRUE);
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) {
5357c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "configure: ioctl SIOCGIFNUM: %m");
5367c478bd9Sstevel@tonic-gate 		return (B_FALSE);
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 	bufsize = numifs * sizeof (struct ifreq);
5397c478bd9Sstevel@tonic-gate 	buf = malloc(bufsize);
5407c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
5417c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "configure: malloc: %m");
5427c478bd9Sstevel@tonic-gate 		return (B_FALSE);
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 	ifc.ifc_len = bufsize;
5457c478bd9Sstevel@tonic-gate 	ifc.ifc_buf = buf;
5467c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
5477c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5487c478bd9Sstevel@tonic-gate 		    "configure: ioctl (get interface configuration): %m");
5497c478bd9Sstevel@tonic-gate 		(void) free(buf);
5507c478bd9Sstevel@tonic-gate 		return (B_FALSE);
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 	ifr = ifc.ifc_req;
5537c478bd9Sstevel@tonic-gate 	for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
5547c478bd9Sstevel@tonic-gate 		/* Skip all logical interfaces */
5557c478bd9Sstevel@tonic-gate 		if (index(ifr->ifr_name, ':') != NULL)
5567c478bd9Sstevel@tonic-gate 			continue;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 		for (np = neighbors; np != NULL; np = np->n_next) {
5597c478bd9Sstevel@tonic-gate 			if (np->n_name &&
5607c478bd9Sstevel@tonic-gate 			    strcmp(ifr->ifr_name, np->n_name) == 0)
5617c478bd9Sstevel@tonic-gate 				break;
5627c478bd9Sstevel@tonic-gate 		}
5637c478bd9Sstevel@tonic-gate 		if (np != NULL)
5647c478bd9Sstevel@tonic-gate 			continue;
5657c478bd9Sstevel@tonic-gate 		ifreq = *ifr;
5667c478bd9Sstevel@tonic-gate 		np = (struct neighbor *)malloc(sizeof (*np));
5677c478bd9Sstevel@tonic-gate 		if (np == NULL)
5687c478bd9Sstevel@tonic-gate 			continue;
5697c478bd9Sstevel@tonic-gate 		np->n_name = malloc(strlen(ifr->ifr_name) + 1);
5707c478bd9Sstevel@tonic-gate 		if (np->n_name == NULL) {
5717c478bd9Sstevel@tonic-gate 			free(np);
5727c478bd9Sstevel@tonic-gate 			continue;
5737c478bd9Sstevel@tonic-gate 		}
5747c478bd9Sstevel@tonic-gate 		(void) strcpy(np->n_name, ifr->ifr_name);
5757c478bd9Sstevel@tonic-gate 		np->n_addrlen = sizeof (ifr->ifr_addr);
5767c478bd9Sstevel@tonic-gate 		np->n_addr = malloc(np->n_addrlen);
5777c478bd9Sstevel@tonic-gate 		if (np->n_addr == NULL) {
5787c478bd9Sstevel@tonic-gate 			free(np->n_name);
5797c478bd9Sstevel@tonic-gate 			free(np);
5807c478bd9Sstevel@tonic-gate 			continue;
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 		bcopy(&ifr->ifr_addr, np->n_addr, np->n_addrlen);
5837c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
5847c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
5857c478bd9Sstevel@tonic-gate 			    "configure: ioctl (get interface flags): %m");
5867c478bd9Sstevel@tonic-gate 			free(np->n_addr);
5877c478bd9Sstevel@tonic-gate 			free(np->n_name);
5887c478bd9Sstevel@tonic-gate 			free(np);
5897c478bd9Sstevel@tonic-gate 			continue;
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 		np->n_flags = ifreq.ifr_flags;
5927c478bd9Sstevel@tonic-gate 		if (((struct sockaddr_in *)np->n_addr)->sin_family == AF_INET &&
5937c478bd9Sstevel@tonic-gate 		    ioctl(s, SIOCGIFNETMASK, (char *)&ifreq) >= 0) {
5947c478bd9Sstevel@tonic-gate 			sin = (struct sockaddr_in *)np->n_addr;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 			np->n_subnet = sin->sin_addr.s_addr &
5977c478bd9Sstevel@tonic-gate 			    ((struct sockaddr_in *)&ifreq.ifr_addr)->
5987c478bd9Sstevel@tonic-gate 			    sin_addr.s_addr;
5997c478bd9Sstevel@tonic-gate 		}
6007c478bd9Sstevel@tonic-gate 		if (multicast_mode == PER_INTERFACE_MULTICAST &&
6017c478bd9Sstevel@tonic-gate 		    (np->n_flags & IFF_UP) &&
6027c478bd9Sstevel@tonic-gate 		    (np->n_flags & IFF_MULTICAST) &&
6037c478bd9Sstevel@tonic-gate 		    !(np->n_flags & IFF_LOOPBACK)) {
6047c478bd9Sstevel@tonic-gate 			struct ip_mreq mreq;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 			/*
6077c478bd9Sstevel@tonic-gate 			 * Skip interfaces that have matching subnets i.e.
6087c478bd9Sstevel@tonic-gate 			 * (addr & netmask) are identical.
6097c478bd9Sstevel@tonic-gate 			 * Such interfaces are connected to the same
6107c478bd9Sstevel@tonic-gate 			 * physical wire.
6117c478bd9Sstevel@tonic-gate 			 */
6127c478bd9Sstevel@tonic-gate 			for (np2 = neighbors; np2 != NULL; np2 = np2->n_next) {
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 				if (!(np->n_flags & IFF_POINTOPOINT) &&
6157c478bd9Sstevel@tonic-gate 				    !(np2->n_flags & IFF_POINTOPOINT) &&
6167c478bd9Sstevel@tonic-gate 				    (np->n_subnet == np2->n_subnet)) {
6177c478bd9Sstevel@tonic-gate 					free(np->n_addr);
6187c478bd9Sstevel@tonic-gate 					free(np->n_name);
6197c478bd9Sstevel@tonic-gate 					free(np);
6207c478bd9Sstevel@tonic-gate 					break;
6217c478bd9Sstevel@tonic-gate 				}
6227c478bd9Sstevel@tonic-gate 			}
6237c478bd9Sstevel@tonic-gate 			if (np2 != NULL)
6247c478bd9Sstevel@tonic-gate 				continue;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 			mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
6277c478bd9Sstevel@tonic-gate 			mreq.imr_interface.s_addr =
6287c478bd9Sstevel@tonic-gate 			    ((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr;
6297c478bd9Sstevel@tonic-gate 			if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
6307c478bd9Sstevel@tonic-gate 			    sizeof (mreq)) < 0) {
6317c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
6327c478bd9Sstevel@tonic-gate 				    "configure: "
6337c478bd9Sstevel@tonic-gate 				    "setsockopt IP_ADD_MEMBERSHIP: %m");
6347c478bd9Sstevel@tonic-gate 				free(np->n_addr);
6357c478bd9Sstevel@tonic-gate 				free(np->n_name);
6367c478bd9Sstevel@tonic-gate 				free(np);
6377c478bd9Sstevel@tonic-gate 				continue;
6387c478bd9Sstevel@tonic-gate 			}
6397c478bd9Sstevel@tonic-gate 			multicast_addr.sin_addr.s_addr =
6407c478bd9Sstevel@tonic-gate 			    htonl(INADDR_WHOD_GROUP);
6417c478bd9Sstevel@tonic-gate 			multicast_addr.sin_port = sp->s_port;
6427c478bd9Sstevel@tonic-gate 			np->n_next = neighbors;
6437c478bd9Sstevel@tonic-gate 			neighbors = np;
6447c478bd9Sstevel@tonic-gate 			continue;
6457c478bd9Sstevel@tonic-gate 		}
6467c478bd9Sstevel@tonic-gate 		if ((np->n_flags & IFF_UP) == 0 ||
6477c478bd9Sstevel@tonic-gate 		    (np->n_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
6487c478bd9Sstevel@tonic-gate 			free(np->n_addr);
6497c478bd9Sstevel@tonic-gate 			free(np->n_name);
6507c478bd9Sstevel@tonic-gate 			free(np);
6517c478bd9Sstevel@tonic-gate 			continue;
6527c478bd9Sstevel@tonic-gate 		}
6537c478bd9Sstevel@tonic-gate 		if (np->n_flags & IFF_POINTOPOINT) {
6547c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
6557c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
6567c478bd9Sstevel@tonic-gate 				    "configure: ioctl (get dstaddr): %m");
6577c478bd9Sstevel@tonic-gate 				free(np->n_addr);
6587c478bd9Sstevel@tonic-gate 				free(np->n_name);
6597c478bd9Sstevel@tonic-gate 				free(np);
6607c478bd9Sstevel@tonic-gate 				continue;
6617c478bd9Sstevel@tonic-gate 			}
6627c478bd9Sstevel@tonic-gate 			/* we assume addresses are all the same size */
6637c478bd9Sstevel@tonic-gate 			bcopy(&ifreq.ifr_dstaddr, np->n_addr, np->n_addrlen);
6647c478bd9Sstevel@tonic-gate 		}
6657c478bd9Sstevel@tonic-gate 		if (np->n_flags & IFF_BROADCAST) {
6667c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
6677c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
6687c478bd9Sstevel@tonic-gate 				    "configure: ioctl (get broadaddr): %m");
6697c478bd9Sstevel@tonic-gate 				free(np->n_addr);
6707c478bd9Sstevel@tonic-gate 				free(np->n_name);
6717c478bd9Sstevel@tonic-gate 				free(np);
6727c478bd9Sstevel@tonic-gate 				continue;
6737c478bd9Sstevel@tonic-gate 			}
6747c478bd9Sstevel@tonic-gate 			/* we assume addresses are all the same size */
6757c478bd9Sstevel@tonic-gate 			bcopy(&ifreq.ifr_broadaddr, np->n_addr, np->n_addrlen);
6767c478bd9Sstevel@tonic-gate 		}
6777c478bd9Sstevel@tonic-gate 		/* gag, wish we could get rid of Internet dependencies */
6787c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)np->n_addr;
6797c478bd9Sstevel@tonic-gate 		sin->sin_port = sp->s_port;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 		/*
6827c478bd9Sstevel@tonic-gate 		 * Avoid adding duplicate broadcast and pt-pt destinations
6837c478bd9Sstevel@tonic-gate 		 * to the list.
6847c478bd9Sstevel@tonic-gate 		 */
6857c478bd9Sstevel@tonic-gate 		for (np2 = neighbors; np2 != NULL; np2 = np2->n_next) {
6867c478bd9Sstevel@tonic-gate 			struct sockaddr_in *sin2;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 			sin2 = (struct sockaddr_in *)np2->n_addr;
6897c478bd9Sstevel@tonic-gate 			if (sin2->sin_addr.s_addr == sin->sin_addr.s_addr) {
6907c478bd9Sstevel@tonic-gate 				free(np->n_addr);
6917c478bd9Sstevel@tonic-gate 				free(np->n_name);
6927c478bd9Sstevel@tonic-gate 				free(np);
6937c478bd9Sstevel@tonic-gate 				break;
6947c478bd9Sstevel@tonic-gate 			}
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 		if (np2 != NULL)
6977c478bd9Sstevel@tonic-gate 			continue;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 		np->n_next = neighbors;
7007c478bd9Sstevel@tonic-gate 		neighbors = np;
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 	(void) free(buf);
7037c478bd9Sstevel@tonic-gate 	return (B_TRUE);
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate #ifdef DEBUG
7077c478bd9Sstevel@tonic-gate static char *interval(uint_t, char *);
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate /* ARGSUSED */
7107c478bd9Sstevel@tonic-gate static ssize_t
7117c478bd9Sstevel@tonic-gate sendto(int s, const void *buf, size_t cc, int flags, const struct sockaddr *to,
7127c478bd9Sstevel@tonic-gate     socklen_t tolen)
7137c478bd9Sstevel@tonic-gate {
7147c478bd9Sstevel@tonic-gate 	struct whod *w = (struct whod *)buf;
7157c478bd9Sstevel@tonic-gate 	struct whoent *we;
7167c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
7177c478bd9Sstevel@tonic-gate 	int nsz;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	(void) printf("sendto %x.%d\n", ntohl(sin->sin_addr.s_addr),
7207c478bd9Sstevel@tonic-gate 	    ntohs(sin->sin_port));
7217c478bd9Sstevel@tonic-gate 	(void) printf("hostname %s %s\n", w->wd_hostname,
7227c478bd9Sstevel@tonic-gate 	    interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
7237c478bd9Sstevel@tonic-gate 	(void) printf("load %4.2f, %4.2f, %4.2f\n",
7247c478bd9Sstevel@tonic-gate 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
7257c478bd9Sstevel@tonic-gate 	    ntohl(w->wd_loadav[2]) / 100.0);
7267c478bd9Sstevel@tonic-gate 	cc -= WHDRSIZE;
7277c478bd9Sstevel@tonic-gate 	for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
7287c478bd9Sstevel@tonic-gate 		time_t t = ntohl(we->we_utmp.out_time);
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		nsz = sizeof (we->we_utmp.out_name);
7317c478bd9Sstevel@tonic-gate 		(void) printf("%-*.*s %s:%s %.12s",
7327c478bd9Sstevel@tonic-gate 		    nsz,
7337c478bd9Sstevel@tonic-gate 		    nsz,
7347c478bd9Sstevel@tonic-gate 		    we->we_utmp.out_name,
7357c478bd9Sstevel@tonic-gate 		    w->wd_hostname,
7367c478bd9Sstevel@tonic-gate 		    we->we_utmp.out_line,
7377c478bd9Sstevel@tonic-gate 		    ctime(&t)+4);
7387c478bd9Sstevel@tonic-gate 		we->we_idle = ntohl(we->we_idle) / 60;
7397c478bd9Sstevel@tonic-gate 		if (we->we_idle) {
7407c478bd9Sstevel@tonic-gate 			if (we->we_idle >= 100*60)
7417c478bd9Sstevel@tonic-gate 				we->we_idle = 100*60 - 1;
7427c478bd9Sstevel@tonic-gate 			if (we->we_idle >= 60)
7437c478bd9Sstevel@tonic-gate 				(void) printf(" %2d", we->we_idle / 60);
7447c478bd9Sstevel@tonic-gate 			else
7457c478bd9Sstevel@tonic-gate 				(void) printf("   ");
7467c478bd9Sstevel@tonic-gate 			(void) printf(":%02d", we->we_idle % 60);
7477c478bd9Sstevel@tonic-gate 		}
7487c478bd9Sstevel@tonic-gate 		(void) printf("\n");
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 	return (0);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate static char *
7547c478bd9Sstevel@tonic-gate interval(uint_t time, char *updown)
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate 	static char resbuf[32];
7577c478bd9Sstevel@tonic-gate 	int days, hours, minutes;
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	if (time > 3*30*24*60*60) {
7607c478bd9Sstevel@tonic-gate 		(void) sprintf(resbuf, "   %s ??:??", updown);
7617c478bd9Sstevel@tonic-gate 		return (resbuf);
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate 	minutes = (time + 59) / 60;		/* round to minutes */
7647c478bd9Sstevel@tonic-gate 	hours = minutes / 60;
7657c478bd9Sstevel@tonic-gate 	minutes %= 60;
7667c478bd9Sstevel@tonic-gate 	days = hours / 24;
7677c478bd9Sstevel@tonic-gate 	hours %= 24;
7687c478bd9Sstevel@tonic-gate 	if (days > 0) {
7697c478bd9Sstevel@tonic-gate 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
7707c478bd9Sstevel@tonic-gate 		    updown, days, hours, minutes);
7717c478bd9Sstevel@tonic-gate 	} else {
7727c478bd9Sstevel@tonic-gate 		(void) sprintf(resbuf, "%s    %2d:%02d",
7737c478bd9Sstevel@tonic-gate 		    updown, hours, minutes);
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 	return (resbuf);
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate #endif
778