add confSORT_QUEUE_BY_HOST and confBROKEN_SMTP_PEERS
[unix-history] / usr / src / usr.sbin / rwhod / rwhod.c
index ef6d940..fad8234 100644 (file)
@@ -1,25 +1,44 @@
+/*
+ * Copyright (c) 1983, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)rwhod.c    4.27 (Berkeley) 84/10/23";
-#endif
+static char copyright[] =
+"@(#) Copyright (c) 1983, 1993\n\
+       The Regents of the University of California.  All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)rwhod.c    8.1 (Berkeley) %G%";
+#endif /* not lint */
 
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
+#include <sys/signal.h>
 #include <sys/ioctl.h>
 #include <sys/ioctl.h>
-#include <sys/file.h>
+#include <sys/sysctl.h>
 
 #include <net/if.h>
 
 #include <net/if.h>
+#include <net/if_dl.h>
+#include <net/route.h>
 #include <netinet/in.h>
 #include <netinet/in.h>
+#include <protocols/rwhod.h>
 
 
-#include <nlist.h>
-#include <stdio.h>
-#include <signal.h>
-#include <errno.h>
-#include <utmp.h>
 #include <ctype.h>
 #include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
 #include <netdb.h>
 #include <netdb.h>
+#include <paths.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <syslog.h>
 #include <syslog.h>
-#include "rwhod.h"
+#include <unistd.h>
+#include <utmp.h>
 
 /*
  * Alarm interval. Don't forget to change the down time check in ruptime
 
 /*
  * Alarm interval. Don't forget to change the down time check in ruptime
@@ -27,30 +46,17 @@ static char sccsid[] = "@(#)rwhod.c 4.27 (Berkeley) 84/10/23";
  */
 #define AL_INTERVAL (3 * 60)
 
  */
 #define AL_INTERVAL (3 * 60)
 
-struct sockaddr_in sin = { AF_INET };
-
-extern errno;
-
-char   myname[32];
-
-struct nlist nl[] = {
-#define        NL_AVENRUN      0
-       { "_avenrun" },
-#define        NL_BOOTTIME     1
-       { "_boottime" },
-       0
-};
+char   myname[MAXHOSTNAMELEN];
 
 /*
 
 /*
- * We communicate with each neighbor in
- * a list constructed at the time we're
- * started up.  Neighbors are currently
- * directly connected via a hardware interface.
+ * We communicate with each neighbor in a list constructed at the time we're
+ * started up.  Neighbors are currently directly connected via a hardware
+ * interface.
  */
 struct neighbor {
        struct  neighbor *n_next;
        char    *n_name;                /* interface name */
  */
 struct neighbor {
        struct  neighbor *n_next;
        char    *n_name;                /* interface name */
-       char    *n_addr;                /* who to send to */
+       struct  sockaddr *n_addr;               /* who to send to */
        int     n_addrlen;              /* size of address */
        int     n_flags;                /* should forward?, interface flags */
 };
        int     n_addrlen;              /* size of address */
        int     n_flags;                /* should forward?, interface flags */
 };
@@ -58,99 +64,94 @@ struct      neighbor {
 struct neighbor *neighbors;
 struct whod mywd;
 struct servent *sp;
 struct neighbor *neighbors;
 struct whod mywd;
 struct servent *sp;
-int    s, utmpf, kmemf = -1;
+int    s, utmpf;
 
 
-#define        WHDRSIZE        (sizeof (mywd) - sizeof (mywd.wd_we))
-#define        RWHODIR         "/usr/spool/rwho"
+#define        WHDRSIZE        (sizeof(mywd) - sizeof(mywd.wd_we))
 
 
-int    onalrm();
-char   *strcpy(), *sprintf(), *malloc();
-long   lseek();
-int    getkmem();
-struct in_addr inet_makeaddr();
+int     configure __P((int));
+void    getboottime __P((int));
+void    onalrm __P((int));
+void    quit __P((char *));
+void    rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
+int     verify __P((char *));
+#ifdef DEBUG
+char   *interval __P((int, char *));
+void    Sendto __P((int, char *, int, int, char *, int));
+#define         sendto Sendto
+#endif
 
 
-main()
+int
+main(argc, argv)
+       int argc;
+       char argv[];
 {
        struct sockaddr_in from;
 {
        struct sockaddr_in from;
+       struct stat st;
        char path[64];
        char path[64];
-       int addr, on = 1;
-       struct hostent *hp;
+       int on = 1;
+       char *cp;
+       struct sockaddr_in sin;
 
        if (getuid()) {
                fprintf(stderr, "rwhod: not super user\n");
                exit(1);
        }
        sp = getservbyname("who", "udp");
 
        if (getuid()) {
                fprintf(stderr, "rwhod: not super user\n");
                exit(1);
        }
        sp = getservbyname("who", "udp");
-       if (sp == 0) {
+       if (sp == NULL) {
                fprintf(stderr, "rwhod: udp/who: unknown service\n");
                exit(1);
        }
 #ifndef DEBUG
                fprintf(stderr, "rwhod: udp/who: unknown service\n");
                exit(1);
        }
 #ifndef DEBUG
-       if (fork())
-               exit(0);
-       { int s;
-         for (s = 0; s < 10; s++)
-               (void) close(s);
-         (void) open("/", 0);
-         (void) dup2(0, 1);
-         (void) dup2(0, 2);
-         s = open("/dev/tty", 2);
-         if (s >= 0) {
-               ioctl(s, TIOCNOTTY, 0);
-               (void) close(s);
-         }
-       }
+       daemon(1, 0);
 #endif
 #endif
-       (void) chdir("/dev");
-       (void) signal(SIGHUP, getkmem);
-       openlog("rwhod", LOG_PID, 0);
+       if (chdir(_PATH_RWHODIR) < 0) {
+               (void)fprintf(stderr, "rwhod: %s: %s\n",
+                   _PATH_RWHODIR, strerror(errno));
+               exit(1);
+       }
+       (void) signal(SIGHUP, getboottime);
+       openlog("rwhod", LOG_PID, LOG_DAEMON);
        /*
         * Establish host name as returned by system.
         */
        /*
         * Establish host name as returned by system.
         */
-       if (gethostname(myname, sizeof (myname) - 1) < 0) {
+       if (gethostname(myname, sizeof(myname) - 1) < 0) {
                syslog(LOG_ERR, "gethostname: %m");
                exit(1);
        }
                syslog(LOG_ERR, "gethostname: %m");
                exit(1);
        }
-       strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
-       utmpf = open("/etc/utmp", O_RDONLY);
+       if ((cp = index(myname, '.')) != NULL)
+               *cp = '\0';
+       strncpy(mywd.wd_hostname, myname, sizeof(myname) - 1);
+       utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
        if (utmpf < 0) {
        if (utmpf < 0) {
-               (void) close(creat("/etc/utmp", 0644));
-               utmpf = open("/etc/utmp", O_RDONLY);
-       }
-       if (utmpf < 0) {
-               syslog(LOG_ERR, "/etc/utmp: %m");
+               syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
                exit(1);
        }
                exit(1);
        }
-       getkmem();
+       getboottime(0);
        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
                syslog(LOG_ERR, "socket: %m");
                exit(1);
        }
        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
                syslog(LOG_ERR, "socket: %m");
                exit(1);
        }
-       if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
+       if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
                syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
                exit(1);
        }
                syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
                exit(1);
        }
-       hp = gethostbyname(myname);
-       if (hp == NULL) {
-               syslog(LOG_ERR, "%s: don't know my own name", myname);
-               exit(1);
-       }
-       sin.sin_family = hp->h_addrtype;
+       memset(&sin, 0, sizeof(sin));
+       sin.sin_family = AF_INET;
        sin.sin_port = sp->s_port;
        sin.sin_port = sp->s_port;
-       if (bind(s, &sin, sizeof (sin)) < 0) {
+       if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
                syslog(LOG_ERR, "bind: %m");
                exit(1);
        }
        if (!configure(s))
                exit(1);
        signal(SIGALRM, onalrm);
                syslog(LOG_ERR, "bind: %m");
                exit(1);
        }
        if (!configure(s))
                exit(1);
        signal(SIGALRM, onalrm);
-       onalrm();
+       onalrm(0);
        for (;;) {
                struct whod wd;
        for (;;) {
                struct whod wd;
-               int cc, whod, len = sizeof (from);
+               int cc, whod, len = sizeof(from);
 
 
-               cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
-                       &from, &len);
+               cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0,
+                       (struct sockaddr *)&from, &len);
                if (cc <= 0) {
                        if (cc < 0 && errno != EINTR)
                                syslog(LOG_WARNING, "recv: %m");
                if (cc <= 0) {
                        if (cc < 0 && errno != EINTR)
                                syslog(LOG_WARNING, "recv: %m");
@@ -161,13 +162,6 @@ main()
                                ntohs(from.sin_port));
                        continue;
                }
                                ntohs(from.sin_port));
                        continue;
                }
-#ifdef notdef
-               if (gethostbyname(wd.wd_hostname) == 0) {
-                       syslog(LOG_WARNING, "%s: unknown host",
-                               wd.wd_hostname);
-                       continue;
-               }
-#endif
                if (wd.wd_vers != WHODVERSION)
                        continue;
                if (wd.wd_type != WHODTYPE_STATUS)
                if (wd.wd_vers != WHODVERSION)
                        continue;
                if (wd.wd_type != WHODTYPE_STATUS)
@@ -177,13 +171,17 @@ main()
                                from.sin_addr);
                        continue;
                }
                                from.sin_addr);
                        continue;
                }
-               (void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname);
-               whod = creat(path, 0666);
+               (void) sprintf(path, "whod.%s", wd.wd_hostname);
+               /*
+                * Rather than truncating and growing the file each time,
+                * use ftruncate if size is less than previous size.
+                */
+               whod = open(path, O_WRONLY | O_CREAT, 0644);
                if (whod < 0) {
                        syslog(LOG_WARNING, "%s: %m", path);
                        continue;
                }
                if (whod < 0) {
                        syslog(LOG_WARNING, "%s: %m", path);
                        continue;
                }
-#if vax || pdp11
+#if ENDIAN != BIG_ENDIAN
                {
                        int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
                        struct whoent *we;
                {
                        int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
                        struct whoent *we;
@@ -202,8 +200,10 @@ main()
                        }
                }
 #endif
                        }
                }
 #endif
-               (void) time(&wd.wd_recvtime);
+               (void) time((time_t *)&wd.wd_recvtime);
                (void) write(whod, (char *)&wd, cc);
                (void) write(whod, (char *)&wd, cc);
+               if (fstat(whod, &st) < 0 || st.st_size > cc)
+                       ftruncate(whod, cc);
                (void) close(whod);
        }
 }
                (void) close(whod);
        }
 }
@@ -213,6 +213,7 @@ main()
  * and other funnies before allowing a file
  * to be created.  Sorry, but blanks aren't allowed.
  */
  * and other funnies before allowing a file
  * to be created.  Sorry, but blanks aren't allowed.
  */
+int
 verify(name)
        register char *name;
 {
 verify(name)
        register char *name;
 {
@@ -228,39 +229,56 @@ verify(name)
 
 int    utmptime;
 int    utmpent;
 
 int    utmptime;
 int    utmpent;
-struct utmp utmp[100];
+int    utmpsize = 0;
+struct utmp *utmp;
 int    alarmcount;
 
 int    alarmcount;
 
-onalrm()
+void
+onalrm(signo)
+       int signo;
 {
 {
+       register struct neighbor *np;
+       register struct whoent *we = mywd.wd_we, *wlast;
        register int i;
        struct stat stb;
        register int i;
        struct stat stb;
-       register struct whoent *we = mywd.wd_we, *wlast;
-       int cc;
        double avenrun[3];
        double avenrun[3];
-       time_t now = time(0);
-       register struct neighbor *np;
+       time_t now;
+       int cc;
 
 
+       now = time(NULL);
        if (alarmcount % 10 == 0)
        if (alarmcount % 10 == 0)
-               getkmem();
+               getboottime(0);
        alarmcount++;
        (void) fstat(utmpf, &stb);
        alarmcount++;
        (void) fstat(utmpf, &stb);
-       if (stb.st_mtime != utmptime) {
+       if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
                utmptime = stb.st_mtime;
                utmptime = stb.st_mtime;
-               (void) lseek(utmpf, (long)0, L_SET);
-               cc = read(utmpf, (char *)utmp, sizeof (utmp));
+               if (stb.st_size > utmpsize) {
+                       utmpsize = stb.st_size + 10 * sizeof(struct utmp);
+                       if (utmp)
+                               utmp = (struct utmp *)realloc(utmp, utmpsize);
+                       else
+                               utmp = (struct utmp *)malloc(utmpsize);
+                       if (! utmp) {
+                               fprintf(stderr, "rwhod: malloc failed\n");
+                               utmpsize = 0;
+                               goto done;
+                       }
+               }
+               (void) lseek(utmpf, (off_t)0, L_SET);
+               cc = read(utmpf, (char *)utmp, stb.st_size);
                if (cc < 0) {
                if (cc < 0) {
-                       perror("/etc/utmp");
+                       fprintf(stderr, "rwhod: %s: %s\n",
+                           _PATH_UTMP, strerror(errno));
                        goto done;
                }
                        goto done;
                }
-               wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
-               utmpent = cc / sizeof (struct utmp);
+               wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1];
+               utmpent = cc / sizeof(struct utmp);
                for (i = 0; i < utmpent; i++)
                        if (utmp[i].ut_name[0]) {
                for (i = 0; i < utmpent; i++)
                        if (utmp[i].ut_name[0]) {
-                               bcopy(utmp[i].ut_line, we->we_utmp.out_line,
-                                  sizeof (utmp[i].ut_line));
-                               bcopy(utmp[i].ut_name, we->we_utmp.out_name,
-                                  sizeof (utmp[i].ut_name));
+                               memcpy(we->we_utmp.out_line, utmp[i].ut_line,
+                                  sizeof(utmp[i].ut_line));
+                               memcpy(we->we_utmp.out_name, utmp[i].ut_name,
+                                  sizeof(utmp[i].ut_name));
                                we->we_utmp.out_time = htonl(utmp[i].ut_time);
                                if (we >= wlast)
                                        break;
                                we->we_utmp.out_time = htonl(utmp[i].ut_time);
                                if (we >= wlast)
                                        break;
@@ -268,14 +286,23 @@ onalrm()
                        }
                utmpent = we - mywd.wd_we;
        }
                        }
                utmpent = we - mywd.wd_we;
        }
+
+       /*
+        * The test on utmpent looks silly---after all, if no one is
+        * logged on, why worry about efficiency?---but is useful on
+        * (e.g.) compute servers.
+        */
+       if (utmpent && chdir(_PATH_DEV)) {
+               syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
+               exit(1);
+       }
        we = mywd.wd_we;
        for (i = 0; i < utmpent; i++) {
                if (stat(we->we_utmp.out_line, &stb) >= 0)
                        we->we_idle = htonl(now - stb.st_atime);
                we++;
        }
        we = mywd.wd_we;
        for (i = 0; i < utmpent; i++) {
                if (stat(we->we_utmp.out_line, &stb) >= 0)
                        we->we_idle = htonl(now - stb.st_atime);
                we++;
        }
-       (void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET);
-       (void) read(kmemf, (char *)avenrun, sizeof (avenrun));
+       (void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
        for (i = 0; i < 3; i++)
                mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
        cc = (char *)we - (char *)&mywd;
        for (i = 0; i < 3; i++)
                mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
        cc = (char *)we - (char *)&mywd;
@@ -283,158 +310,146 @@ onalrm()
        mywd.wd_vers = WHODVERSION;
        mywd.wd_type = WHODTYPE_STATUS;
        for (np = neighbors; np != NULL; np = np->n_next)
        mywd.wd_vers = WHODVERSION;
        mywd.wd_type = WHODTYPE_STATUS;
        for (np = neighbors; np != NULL; np = np->n_next)
-               (void) sendto(s, (char *)&mywd, cc, 0,
-                       np->n_addr, np->n_addrlen);
+               (void)sendto(s, (char *)&mywd, cc, 0,
+                               np->n_addr, np->n_addrlen);
+       if (utmpent && chdir(_PATH_RWHODIR)) {
+               syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
+               exit(1);
+       }
 done:
        (void) alarm(AL_INTERVAL);
 }
 
 done:
        (void) alarm(AL_INTERVAL);
 }
 
-getkmem()
+void
+getboottime(signo)
+       int signo;
 {
 {
-       struct nlist *nlp;
-       static ino_t vmunixino;
-       static time_t vmunixctime;
-       struct stat sb;
-
-       if (stat("/vmunix", &sb) < 0) {
-               if (vmunixctime)
-                       return;
-       } else {
-               if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
-                       return;
-               vmunixctime = sb.st_ctime;
-               vmunixino= sb.st_ino;
-       }
-       if (kmemf >= 0)
-               (void) close(kmemf);
-loop:
-       if (nlist("/vmunix", nl)) {
-               syslog(LOG_WARNING, "/vmunix namelist botch");
-               sleep(300);
-               goto loop;
-       }
-       kmemf = open("/dev/kmem", O_RDONLY);
-       if (kmemf < 0) {
-               syslog(LOG_ERR, "/dev/kmem: %m");
+       int mib[2];
+       size_t size;
+       struct timeval tm;
+
+       mib[0] = CTL_KERN;
+       mib[1] = KERN_BOOTTIME;
+       size = sizeof(tm);
+       if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) {
+               syslog(LOG_ERR, "cannot get boottime: %m");
                exit(1);
        }
                exit(1);
        }
-       (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET);
-       (void) read(kmemf, (char *)&mywd.wd_boottime,
-           sizeof (mywd.wd_boottime));
-       mywd.wd_boottime = htonl(mywd.wd_boottime);
+       mywd.wd_boottime = htonl(tm.tv_sec);
+}
+
+void
+quit(msg)
+       char *msg;
+{
+       syslog(LOG_ERR, msg);
+       exit(1);
+}
+
+#define ROUNDUP(a) \
+       ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
+#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
+
+void
+rt_xaddrs(cp, cplim, rtinfo)
+       register caddr_t cp, cplim;
+       register struct rt_addrinfo *rtinfo;
+{
+       register struct sockaddr *sa;
+       register int i;
+
+       memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
+       for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
+               if ((rtinfo->rti_addrs & (1 << i)) == 0)
+                       continue;
+               rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
+               ADVANCE(cp, sa);
+       }
 }
 
 /*
  * Figure out device configuration and select
  * networks which deserve status information.
  */
 }
 
 /*
  * Figure out device configuration and select
  * networks which deserve status information.
  */
+int
 configure(s)
        int s;
 {
 configure(s)
        int s;
 {
-       char buf[BUFSIZ];
-       struct ifconf ifc;
-       struct ifreq ifreq, *ifr;
-       struct sockaddr_in *sin;
        register struct neighbor *np;
        register struct neighbor *np;
-       int n;
+       register struct if_msghdr *ifm;
+       register struct ifa_msghdr *ifam;
+       struct sockaddr_dl *sdl;
+       size_t needed;
+       int mib[6], flags = 0, len;
+       char *buf, *lim, *next;
+       struct rt_addrinfo info;
 
 
-       ifc.ifc_len = sizeof (buf);
-       ifc.ifc_buf = buf;
-       if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
-               syslog(LOG_ERR, "ioctl (get interface configuration)");
-               return (0);
-       }
-       ifr = ifc.ifc_req;
-       for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
-               for (np = neighbors; np != NULL; np = np->n_next)
-                       if (np->n_name &&
-                           strcmp(ifr->ifr_name, np->n_name) == 0)
-                               break;
-               if (np != NULL)
-                       continue;
-               ifreq = *ifr;
-               np = (struct neighbor *)malloc(sizeof (*np));
-               if (np == NULL)
-                       continue;
-               np->n_name = malloc(strlen(ifr->ifr_name) + 1);
-               if (np->n_name == NULL) {
-                       free((char *)np);
+       mib[0] = CTL_NET;
+       mib[1] = PF_ROUTE;
+       mib[2] = 0;
+       mib[3] = AF_INET;
+       mib[4] = NET_RT_IFLIST;
+       mib[5] = 0;
+       if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
+               quit("route-sysctl-estimate");
+       if ((buf = malloc(needed)) == NULL)
+               quit("malloc");
+       if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
+               quit("actual retrieval of interface table");
+       lim = buf + needed;
+
+       sdl = NULL;             /* XXX just to keep gcc -Wall happy */
+       for (next = buf; next < lim; next += ifm->ifm_msglen) {
+               ifm = (struct if_msghdr *)next;
+               if (ifm->ifm_type == RTM_IFINFO) {
+                       sdl = (struct sockaddr_dl *)(ifm + 1);
+                       flags = ifm->ifm_flags;
                        continue;
                }
                        continue;
                }
-               strcpy(np->n_name, ifr->ifr_name);
-               np->n_addrlen = sizeof (ifr->ifr_addr);
-               np->n_addr = malloc(np->n_addrlen);
-               if (np->n_addr == NULL) {
-                       free(np->n_name);
-                       free((char *)np);
+               if ((flags & IFF_UP) == 0 ||
+                   (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)
                        continue;
                        continue;
-               }
-               bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
-               if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
-                       syslog(LOG_ERR, "ioctl (get interface flags)");
-                       free((char *)np);
+               if (ifm->ifm_type != RTM_NEWADDR)
+                       quit("out of sync parsing NET_RT_IFLIST");
+               ifam = (struct ifa_msghdr *)ifm;
+               info.rti_addrs = ifam->ifam_addrs;
+               rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
+                       &info);
+               /* gag, wish we could get rid of Internet dependencies */
+#define dstaddr        info.rti_info[RTAX_BRD]
+#define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
+#define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
+               if (dstaddr == 0 || dstaddr->sa_family != AF_INET)
                        continue;
                        continue;
-               }
-               if ((ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
-                       free((char *)np);
+               PORT_SA(dstaddr) = sp->s_port;
+               for (np = neighbors; np != NULL; np = np->n_next)
+                       if (memcmp(sdl->sdl_data, np->n_name,
+                                  sdl->sdl_nlen) == 0 &&
+                           IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr))
+                               break;
+               if (np != NULL)
                        continue;
                        continue;
-               }
-               np->n_flags = ifreq.ifr_flags;
-               if (np->n_flags & IFF_POINTOPOINT) {
-                       if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
-                               syslog(LOG_ERR, "ioctl (get dstaddr)");
-                               free((char *)np);
-                               continue;
-                       }
-                       /* we assume addresses are all the same size */
-                       bcopy((char *)&ifreq.ifr_dstaddr,
-                         np->n_addr, np->n_addrlen);
-               }
-               if (np->n_flags & IFF_BROADCAST) {
-                       /* we assume addresses are all the same size */
-                       sin = (struct sockaddr_in *)np->n_addr;
-                       sin->sin_addr = inet_makeaddr((np->n_flags & IFF_LOCAL)?
-                               inet_subnetof(sin->sin_addr) :
-                               inet_netof(sin->sin_addr),
-                               INADDR_ANY);
-               }
-               /* gag, wish we could get rid of Internet dependencies */
-               sin = (struct sockaddr_in *)np->n_addr;
-               sin->sin_port = sp->s_port;
+               len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1;
+               np = (struct neighbor *)malloc(len);
+               if (np == NULL)
+                       quit("malloc of neighbor structure");
+               memset(np, 0, len);
+               np->n_flags = flags;
+               np->n_addr = (struct sockaddr *)(np + 1);
+               np->n_addrlen = dstaddr->sa_len;
+               np->n_name = np->n_addrlen + (char *)np->n_addr;
                np->n_next = neighbors;
                neighbors = np;
                np->n_next = neighbors;
                neighbors = np;
+               memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen);
+               memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen);
        }
        }
+       free(buf);
        return (1);
 }
 
        return (1);
 }
 
-/*
- * Return the possible subnetwork number from an internet address.
- * If the address is of the form of a subnet address (most significant
- * bit of the host part is set), believe the subnet exists.
- * Otherwise, return the network number.  Any subnet number is only valid
- * if this is a ``local'' net.
- */
-inet_subnetof(in)
-       struct in_addr in;
-{
-       register u_long i = ntohl(in.s_addr);
-
-       if (IN_CLASSA(i)) {
-               if (IN_SUBNETA(i))
-                       return ((i & IN_CLASSA_SUBNET) >> IN_CLASSA_SUBNSHIFT);
-               else
-                       return ((i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
-       } else if (IN_CLASSB(i)) {
-               if (IN_SUBNETB(i))
-                       return ((i & IN_CLASSB_SUBNET) >> IN_CLASSB_SUBNSHIFT);
-               else
-                       return ((i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
-       } else
-               return ((i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
-}
-
 #ifdef DEBUG
 #ifdef DEBUG
-sendto(s, buf, cc, flags, to, tolen)
+void
+Sendto(s, buf, cc, flags, to, tolen)
        int s;
        char *buf;
        int cc, flags;
        int s;
        char *buf;
        int cc, flags;
@@ -444,7 +459,6 @@ sendto(s, buf, cc, flags, to, tolen)
        register struct whod *w = (struct whod *)buf;
        register struct whoent *we;
        struct sockaddr_in *sin = (struct sockaddr_in *)to;
        register struct whod *w = (struct whod *)buf;
        register struct whoent *we;
        struct sockaddr_in *sin = (struct sockaddr_in *)to;
-       char *interval();
 
        printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
        printf("hostname %s %s\n", w->wd_hostname,
 
        printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
        printf("hostname %s %s\n", w->wd_hostname,
@@ -453,7 +467,7 @@ sendto(s, buf, cc, flags, to, tolen)
            ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
            ntohl(w->wd_loadav[2]) / 100.0);
        cc -= WHDRSIZE;
            ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
            ntohl(w->wd_loadav[2]) / 100.0);
        cc -= WHDRSIZE;
-       for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
+       for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) {
                time_t t = ntohl(we->we_utmp.out_time);
                printf("%-8.8s %s:%s %.12s",
                        we->we_utmp.out_name,
                time_t t = ntohl(we->we_utmp.out_time);
                printf("%-8.8s %s:%s %.12s",
                        we->we_utmp.out_name,