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