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