rwhod.c revision 10087
1107543Sscottl/*
2107543Sscottl * Copyright (c) 1983, 1993
3111015Smtm *	The Regents of the University of California.  All rights reserved.
4107543Sscottl *
5107543Sscottl * Redistribution and use in source and binary forms, with or without
6107543Sscottl * modification, are permitted provided that the following conditions
7107543Sscottl * are met:
8107543Sscottl * 1. Redistributions of source code must retain the above copyright
9107543Sscottl *    notice, this list of conditions and the following disclaimer.
10107543Sscottl * 2. Redistributions in binary form must reproduce the above copyright
11107543Sscottl *    notice, this list of conditions and the following disclaimer in the
12107543Sscottl *    documentation and/or other materials provided with the distribution.
13107543Sscottl * 3. All advertising materials mentioning features or use of this software
14107543Sscottl *    must display the following acknowledgement:
15107543Sscottl *	This product includes software developed by the University of
16107543Sscottl *	California, Berkeley and its contributors.
17107543Sscottl * 4. Neither the name of the University nor the names of its contributors
18107543Sscottl *    may be used to endorse or promote products derived from this software
19107543Sscottl *    without specific prior written permission.
20107543Sscottl *
21107543Sscottl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22107543Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23107543Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24107543Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25116624Smtm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26107543Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27107543Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28107543Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29107543Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30107543Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31107543Sscottl * SUCH DAMAGE.
32107543Sscottl */
33107543Sscottl
34107694Stjr#ifndef lint
35107543Sscottlstatic char copyright[] =
36107543Sscottl"@(#) Copyright (c) 1983, 1993\n\
37107543Sscottl	The Regents of the University of California.  All rights reserved.\n";
38107543Sscottl#endif /* not lint */
39107543Sscottl
40107543Sscottl#ifndef lint
41107543Sscottlstatic char sccsid[] = "@(#)rwhod.c	8.1 (Berkeley) 6/6/93";
42107543Sscottl#endif /* not lint */
43107543Sscottl
44111013Smtm#include <sys/param.h>
45111013Smtm#include <sys/socket.h>
46111013Smtm#include <sys/stat.h>
47111013Smtm#include <sys/signal.h>
48111013Smtm#include <sys/ioctl.h>
49111013Smtm#include <sys/sysctl.h>
50111013Smtm
51107543Sscottl#include <net/if.h>
52107543Sscottl#include <net/if_dl.h>
53107543Sscottl#include <net/route.h>
54107543Sscottl#include <netinet/in.h>
55107543Sscottl#include <protocols/rwhod.h>
56107543Sscottl
57107543Sscottl#include <ctype.h>
58126398Sschweikh#include <errno.h>
59111013Smtm#include <fcntl.h>
60107543Sscottl#include <netdb.h>
61111013Smtm#include <paths.h>
62107543Sscottl#include <stdio.h>
63107543Sscottl#include <stdlib.h>
64107543Sscottl#include <string.h>
65107543Sscottl#include <syslog.h>
66111013Smtm#include <unistd.h>
67126398Sschweikh#include <utmp.h>
68107694Stjr
69111013Smtm/*
70111013Smtm * This version of Berkeley's rwhod has been modified to use IP multicast
71107543Sscottl * datagrams, under control of a new command-line option:
72111013Smtm *
73107543Sscottl *	rwhod -m	causes rwhod to use IP multicast (instead of
74107543Sscottl *			broadcast or unicast) on all interfaces that have
75107543Sscottl *			the IFF_MULTICAST flag set in their "ifnet" structs
76107543Sscottl *			(excluding the loopback interface).  The multicast
77107543Sscottl *			reports are sent with a time-to-live of 1, to prevent
78107543Sscottl *			forwarding beyond the directly-connected subnet(s).
79107543Sscottl *
80107543Sscottl *	rwhod -m <ttl>	causes rwhod to send IP multicast datagrams with a
81107543Sscottl *			time-to-live of <ttl>, via a SINGLE interface rather
82107543Sscottl *			than all interfaces.  <ttl> must be between 0 and
83111013Smtm *			MAX_MULTICAST_SCOPE, defined below.  Note that "-m 1"
84107543Sscottl *			is different than "-m", in that "-m 1" specifies
85126398Sschweikh *			transmission on one interface only.
86111013Smtm *
87107543Sscottl * When "-m" is used without a <ttl> argument, the program accepts multicast
88107543Sscottl * rwhod reports from all multicast-capable interfaces.  If a <ttl> argument
89107543Sscottl * is given, it accepts multicast reports from only one interface, the one
90126398Sschweikh * on which reports are sent (which may be controlled via the host's routing
91111013Smtm * table).  Regardless of the "-m" option, the program accepts broadcast or
92107543Sscottl * unicast reports from all interfaces.  Thus, this program will hear the
93107543Sscottl * reports of old, non-multicasting rwhods, but, if multicasting is used,
94111013Smtm * those old rwhods won't hear the reports generated by this program.
95107543Sscottl *
96107543Sscottl *                  -- Steve Deering, Stanford University, February 1989
97107543Sscottl */
98107543Sscottl
99107543Sscottl#define NO_MULTICAST		0	  /* multicast modes */
100107543Sscottl#define PER_INTERFACE_MULTICAST	1
101107543Sscottl#define SCOPED_MULTICAST	2
102107543Sscottl
103107543Sscottl#define MAX_MULTICAST_SCOPE	32	  /* "site-wide", by convention */
104111013Smtm
105107543Sscottl#define INADDR_WHOD_GROUP (u_long)0xe0000103      /* 224.0.1.3 */
106107543Sscottl					  /* (belongs in protocols/rwhod.h) */
107107543Sscottl
108107543Sscottlint			multicast_mode  = NO_MULTICAST;
109109752Sfjoeint			multicast_scope;
110107543Sscottlstruct sockaddr_in	multicast_addr  = { sizeof multicast_addr, AF_INET };
111111013Smtm
112111013Smtm/*
113107543Sscottl * Alarm interval. Don't forget to change the down time check in ruptime
114107543Sscottl * if this is changed.
115107543Sscottl */
116107543Sscottl#define AL_INTERVAL (3 * 60)
117107543Sscottl
118107543Sscottlchar	myname[MAXHOSTNAMELEN];
119107543Sscottl
120107543Sscottl/*
121107543Sscottl * We communicate with each neighbor in a list constructed at the time we're
122107543Sscottl * started up.  Neighbors are currently directly connected via a hardware
123107543Sscottl * interface.
124111013Smtm */
125107543Sscottlstruct	neighbor {
126107543Sscottl	struct	neighbor *n_next;
127109752Sfjoe	char	*n_name;		/* interface name */
128107543Sscottl	struct	sockaddr *n_addr;		/* who to send to */
129111013Smtm	int	n_addrlen;		/* size of address */
130111013Smtm	int	n_flags;		/* should forward?, interface flags */
131107543Sscottl};
132107543Sscottl
133107543Sscottlstruct	neighbor *neighbors;
134107543Sscottlstruct	whod mywd;
135107543Sscottlstruct	servent *sp;
136107543Sscottlint	s, utmpf;
137107543Sscottl
138107543Sscottl#define	WHDRSIZE	(sizeof(mywd) - sizeof(mywd.wd_we))
139107543Sscottl
140111013Smtmint	 configure __P((int));
141107543Sscottlvoid	 getboottime __P((int));
142111013Smtmvoid	 onalrm __P((int));
143107543Sscottlvoid	 quit __P((char *));
144107543Sscottlvoid	 rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
145111013Smtmint	 verify __P((char *));
146107543Sscottl#ifdef DEBUG
147107543Sscottlchar	*interval __P((int, char *));
148126398Sschweikhvoid	 Sendto __P((int, char *, int, int, char *, int));
149126398Sschweikh#define	 sendto Sendto
150126398Sschweikh#endif
151126398Sschweikh
152126398Sschweikhint
153126398Sschweikhmain(argc, argv)
154126398Sschweikh	int argc;
155126398Sschweikh	char *argv[];
156126398Sschweikh{
157126398Sschweikh	struct sockaddr_in from;
158126398Sschweikh	struct stat st;
159126398Sschweikh	char path[64];
160126398Sschweikh	int on = 1;
161107543Sscottl	char *cp;
162107543Sscottl	struct sockaddr_in sin;
163107543Sscottl
164107543Sscottl	if (getuid()) {
165107543Sscottl		fprintf(stderr, "rwhod: not super user\n");
166107543Sscottl		exit(1);
167107543Sscottl	}
168107543Sscottl	argv++; argc--;
169107543Sscottl	while (argc > 0 && *argv[0] == '-') {
170107543Sscottl		if (strcmp(*argv, "-m") == 0) {
171107543Sscottl			if (argc > 1 && isdigit(*(argv + 1)[0])) {
172107543Sscottl				argv++, argc--;
173107543Sscottl				multicast_mode  = SCOPED_MULTICAST;
174111013Smtm				multicast_scope = atoi(*argv);
175111013Smtm				if (multicast_scope > MAX_MULTICAST_SCOPE) {
176111013Smtm					fprintf(stderr,
177111013Smtm					"rwhod: ttl must not exceed %u\n",
178111013Smtm					MAX_MULTICAST_SCOPE);
179111013Smtm					exit(1);
180111013Smtm				}
181107543Sscottl			}
182111013Smtm			else multicast_mode = PER_INTERFACE_MULTICAST;
183107543Sscottl		}
184107543Sscottl		else goto usage;
185107543Sscottl		argv++, argc--;
186107543Sscottl	}
187107543Sscottl	if (argc > 0) {
188107543Sscottlusage:		fprintf(stderr, "usage: rwhod [ -m [ ttl ] ]\n");
189107543Sscottl		exit(1);
190107543Sscottl	}
191107543Sscottl#ifndef DEBUG
192109750Sfjoe	daemon(1, 0);
193107543Sscottl#endif
194107543Sscottl	(void) signal(SIGHUP, getboottime);
195109750Sfjoe	openlog("rwhod", LOG_PID, LOG_DAEMON);
196107543Sscottl	sp = getservbyname("who", "udp");
197107543Sscottl	if (sp == NULL) {
198107543Sscottl		syslog(LOG_ERR, "rwhod: udp/who: unknown service\n");
199107543Sscottl		exit(1);
200107543Sscottl	}
201107543Sscottl	if (chdir(_PATH_RWHODIR) < 0) {
202107543Sscottl		syslog(LOG_ERR, "rwhod: %s: %m\n", _PATH_RWHODIR);
203107543Sscottl		exit(1);
204107543Sscottl	}
205107543Sscottl	/*
206107543Sscottl	 * Establish host name as returned by system.
207107543Sscottl	 */
208107543Sscottl	if (gethostname(myname, sizeof(myname) - 1) < 0) {
209107543Sscottl		syslog(LOG_ERR, "gethostname: %m");
210107543Sscottl		exit(1);
211107543Sscottl	}
212107543Sscottl	if ((cp = index(myname, '.')) != NULL)
213107543Sscottl		*cp = '\0';
214107543Sscottl	strncpy(mywd.wd_hostname, myname, sizeof(myname) - 1);
215111013Smtm	utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
216107543Sscottl	if (utmpf < 0) {
217107543Sscottl		syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
218107543Sscottl		exit(1);
219107543Sscottl	}
220107543Sscottl	getboottime(0);
221107543Sscottl	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
222107543Sscottl		syslog(LOG_ERR, "socket: %m");
223107543Sscottl		exit(1);
224107543Sscottl	}
225107543Sscottl	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
226107543Sscottl		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
227107543Sscottl		exit(1);
228111013Smtm	}
229107543Sscottl	memset(&sin, 0, sizeof(sin));
230107543Sscottl	sin.sin_len = sizeof(sin);
231107543Sscottl	sin.sin_family = AF_INET;
232107543Sscottl	sin.sin_port = sp->s_port;
233107543Sscottl	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
234107543Sscottl		syslog(LOG_ERR, "bind: %m");
235107543Sscottl		exit(1);
236111013Smtm	}
237107543Sscottl	if (!configure(s))
238107543Sscottl		exit(1);
239107543Sscottl	signal(SIGALRM, onalrm);
240107543Sscottl	onalrm(0);
241107543Sscottl	for (;;) {
242107543Sscottl		struct whod wd;
243107543Sscottl		int cc, whod, len = sizeof(from);
244107543Sscottl
245107543Sscottl		cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0,
246107543Sscottl			(struct sockaddr *)&from, &len);
247107543Sscottl		if (cc <= 0) {
248111013Smtm			if (cc < 0 && errno != EINTR)
249111013Smtm				syslog(LOG_WARNING, "recv: %m");
250111013Smtm			continue;
251111013Smtm		}
252107543Sscottl		if (from.sin_port != sp->s_port) {
253107543Sscottl			syslog(LOG_WARNING, "%d: bad from port",
254107543Sscottl				ntohs(from.sin_port));
255107543Sscottl			continue;
256107543Sscottl		}
257107543Sscottl		if (wd.wd_vers != WHODVERSION)
258107543Sscottl			continue;
259107543Sscottl		if (wd.wd_type != WHODTYPE_STATUS)
260107543Sscottl			continue;
261107543Sscottl		if (!verify(wd.wd_hostname)) {
262107543Sscottl			syslog(LOG_WARNING, "malformed host name from %x",
263107543Sscottl				from.sin_addr);
264107543Sscottl			continue;
265107543Sscottl		}
266107543Sscottl		(void) sprintf(path, "whod.%s", wd.wd_hostname);
267107543Sscottl		/*
268107543Sscottl		 * Rather than truncating and growing the file each time,
269107543Sscottl		 * use ftruncate if size is less than previous size.
270107543Sscottl		 */
271107543Sscottl		whod = open(path, O_WRONLY | O_CREAT, 0644);
272107543Sscottl		if (whod < 0) {
273107543Sscottl			syslog(LOG_WARNING, "%s: %m", path);
274107543Sscottl			continue;
275107543Sscottl		}
276107543Sscottl#if ENDIAN != BIG_ENDIAN
277107543Sscottl		{
278107543Sscottl			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
279107543Sscottl			struct whoent *we;
280107543Sscottl
281107543Sscottl			/* undo header byte swapping before writing to file */
282107543Sscottl			wd.wd_sendtime = ntohl(wd.wd_sendtime);
283107543Sscottl			for (i = 0; i < 3; i++)
284107543Sscottl				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
285107543Sscottl			wd.wd_boottime = ntohl(wd.wd_boottime);
286107543Sscottl			we = wd.wd_we;
287107543Sscottl			for (i = 0; i < n; i++) {
288107543Sscottl				we->we_idle = ntohl(we->we_idle);
289107543Sscottl				we->we_utmp.out_time =
290107543Sscottl				    ntohl(we->we_utmp.out_time);
291107543Sscottl				we++;
292107543Sscottl			}
293107543Sscottl		}
294107543Sscottl#endif
295107543Sscottl		(void) time((time_t *)&wd.wd_recvtime);
296107543Sscottl		(void) write(whod, (char *)&wd, cc);
297107543Sscottl		if (fstat(whod, &st) < 0 || st.st_size > cc)
298107543Sscottl			ftruncate(whod, cc);
299107543Sscottl		(void) close(whod);
300107543Sscottl	}
301107543Sscottl}
302107543Sscottl
303107543Sscottl/*
304146556Sadamw * Check out host name for unprintables
305107543Sscottl * and other funnies before allowing a file
306107543Sscottl * to be created.  Sorry, but blanks aren't allowed.
307107543Sscottl */
308107543Sscottlint
309107543Sscottlverify(name)
310107543Sscottl	register char *name;
311107543Sscottl{
312107543Sscottl	register int size = 0;
313107543Sscottl
314107543Sscottl	while (*name) {
315107543Sscottl		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
316107543Sscottl			return (0);
317107543Sscottl		name++, size++;
318107543Sscottl	}
319107543Sscottl	return (size > 0);
320107543Sscottl}
321107543Sscottl
322107543Sscottlint	utmptime;
323107543Sscottlint	utmpent;
324107543Sscottlint	utmpsize = 0;
325107543Sscottlstruct	utmp *utmp;
326107543Sscottlint	alarmcount;
327107543Sscottl
328107543Sscottlvoid
329107543Sscottlonalrm(signo)
330107543Sscottl	int signo;
331107543Sscottl{
332107543Sscottl	register struct neighbor *np;
333107543Sscottl	register struct whoent *we = mywd.wd_we, *wlast;
334107543Sscottl	register int i;
335107543Sscottl	struct stat stb;
336107543Sscottl	double avenrun[3];
337107543Sscottl	time_t now;
338109750Sfjoe	int cc;
339107543Sscottl
340107543Sscottl	now = time(NULL);
341107543Sscottl	if (alarmcount % 10 == 0)
342107543Sscottl		getboottime(0);
343107543Sscottl	alarmcount++;
344107543Sscottl	(void) fstat(utmpf, &stb);
345107543Sscottl	if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
346107543Sscottl		utmptime = stb.st_mtime;
347107543Sscottl		if (stb.st_size > utmpsize) {
348107543Sscottl			utmpsize = stb.st_size + 10 * sizeof(struct utmp);
349107543Sscottl			if (utmp)
350111013Smtm				utmp = (struct utmp *)realloc(utmp, utmpsize);
351111013Smtm			else
352107543Sscottl				utmp = (struct utmp *)malloc(utmpsize);
353107543Sscottl			if (! utmp) {
354126398Sschweikh				fprintf(stderr, "rwhod: malloc failed\n");
355107543Sscottl				utmpsize = 0;
356111013Smtm				goto done;
357107543Sscottl			}
358107543Sscottl		}
359111013Smtm		(void) lseek(utmpf, (off_t)0, L_SET);
360107543Sscottl		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	}
432done:
433	(void) alarm(AL_INTERVAL);
434}
435
436void
437getboottime(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
454void
455quit(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
466void
467rt_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 */
487int
488configure(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
617void
618Sendto(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
656char *
657interval(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