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