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