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