1e541169cScgd /* 29afcf17bSjtc * Copyright (c) 1983, 1993 39afcf17bSjtc * The Regents of the University of California. All rights reserved. 4e541169cScgd * 5e541169cScgd * Redistribution and use in source and binary forms, with or without 6e541169cScgd * modification, are permitted provided that the following conditions 7e541169cScgd * are met: 8e541169cScgd * 1. Redistributions of source code must retain the above copyright 9e541169cScgd * notice, this list of conditions and the following disclaimer. 10e541169cScgd * 2. Redistributions in binary form must reproduce the above copyright 11e541169cScgd * notice, this list of conditions and the following disclaimer in the 12e541169cScgd * documentation and/or other materials provided with the distribution. 13e541169cScgd * 3. All advertising materials mentioning features or use of this software 14e541169cScgd * must display the following acknowledgement: 15e541169cScgd * This product includes software developed by the University of 16e541169cScgd * California, Berkeley and its contributors. 17e541169cScgd * 4. Neither the name of the University nor the names of its contributors 18e541169cScgd * may be used to endorse or promote products derived from this software 19e541169cScgd * without specific prior written permission. 20e541169cScgd * 21e541169cScgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22e541169cScgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23e541169cScgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24e541169cScgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25e541169cScgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26e541169cScgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27e541169cScgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28e541169cScgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29e541169cScgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30e541169cScgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31e541169cScgd * SUCH DAMAGE. 32e541169cScgd */ 33e541169cScgd 348d79db10Slukem #include <sys/cdefs.h> 35e541169cScgd #ifndef lint 368d79db10Slukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\ 378d79db10Slukem The Regents of the University of California. All rights reserved.\n"); 38e541169cScgd #endif /* not lint */ 39e541169cScgd 40e541169cScgd #ifndef lint 418d79db10Slukem #if 0 428d79db10Slukem static char sccsid[] = "@(#)rwhod.c 8.1 (Berkeley) 6/6/93"; 438d79db10Slukem #else 44*32f51971Smrg __RCSID("$NetBSD: rwhod.c,v 1.13 1998/07/06 06:58:26 mrg Exp $"); 458d79db10Slukem #endif 46e541169cScgd #endif /* not lint */ 47e541169cScgd 48e541169cScgd #include <sys/param.h> 49e541169cScgd #include <sys/socket.h> 50e541169cScgd #include <sys/stat.h> 51e541169cScgd #include <sys/signal.h> 52e541169cScgd #include <sys/ioctl.h> 539afcf17bSjtc #include <sys/sysctl.h> 54e541169cScgd 55e541169cScgd #include <net/if.h> 569afcf17bSjtc #include <net/if_dl.h> 579afcf17bSjtc #include <net/route.h> 58e541169cScgd #include <netinet/in.h> 59e541169cScgd #include <protocols/rwhod.h> 608d79db10Slukem #include <arpa/inet.h> 619afcf17bSjtc 629afcf17bSjtc #include <ctype.h> 63e5d6d67cSlukem #include <err.h> 649afcf17bSjtc #include <errno.h> 659afcf17bSjtc #include <fcntl.h> 669afcf17bSjtc #include <netdb.h> 67e541169cScgd #include <paths.h> 689afcf17bSjtc #include <stdio.h> 699afcf17bSjtc #include <stdlib.h> 709afcf17bSjtc #include <string.h> 719afcf17bSjtc #include <syslog.h> 729afcf17bSjtc #include <unistd.h> 739afcf17bSjtc #include <utmp.h> 74e541169cScgd 75e541169cScgd /* 76e541169cScgd * Alarm interval. Don't forget to change the down time check in ruptime 77e541169cScgd * if this is changed. 78e541169cScgd */ 79e541169cScgd #define AL_INTERVAL (3 * 60) 80e541169cScgd 81*32f51971Smrg char myname[MAXHOSTNAMELEN + 1]; 82e541169cScgd 83e541169cScgd /* 849afcf17bSjtc * We communicate with each neighbor in a list constructed at the time we're 859afcf17bSjtc * started up. Neighbors are currently directly connected via a hardware 869afcf17bSjtc * interface. 87e541169cScgd */ 88e541169cScgd struct neighbor { 89e541169cScgd struct neighbor *n_next; 90e541169cScgd char *n_name; /* interface name */ 919afcf17bSjtc struct sockaddr *n_addr; /* who to send to */ 92e541169cScgd int n_addrlen; /* size of address */ 93e541169cScgd int n_flags; /* should forward?, interface flags */ 94e541169cScgd }; 95e541169cScgd 96e541169cScgd struct neighbor *neighbors; 97e541169cScgd struct whod mywd; 98e541169cScgd struct servent *sp; 99b55bc39bSandrew int s, utmpf; 100e541169cScgd 101e541169cScgd #define WHDRSIZE (sizeof(mywd) - sizeof(mywd.wd_we)) 102e541169cScgd 1039afcf17bSjtc int configure __P((int)); 1049afcf17bSjtc void getboottime __P((int)); 1058d79db10Slukem int main __P((int, char **)); 1069afcf17bSjtc void onalrm __P((int)); 1079afcf17bSjtc void quit __P((char *)); 1089afcf17bSjtc void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *)); 1099afcf17bSjtc int verify __P((char *)); 1109afcf17bSjtc #ifdef DEBUG 1119afcf17bSjtc char *interval __P((int, char *)); 1129afcf17bSjtc void Sendto __P((int, char *, int, int, char *, int)); 1139afcf17bSjtc #define sendto Sendto 1149afcf17bSjtc #endif 115e541169cScgd 1169afcf17bSjtc int 1179afcf17bSjtc main(argc, argv) 1189afcf17bSjtc int argc; 1198d79db10Slukem char *argv[]; 120e541169cScgd { 121e541169cScgd struct sockaddr_in from; 122e541169cScgd struct stat st; 123e541169cScgd char path[64]; 124e541169cScgd int on = 1; 1259afcf17bSjtc char *cp; 1269afcf17bSjtc struct sockaddr_in sin; 127e541169cScgd 128e5d6d67cSlukem if (getuid()) 129e5d6d67cSlukem errx(1, "not super user"); 130e541169cScgd sp = getservbyname("who", "udp"); 131e5d6d67cSlukem if (sp == NULL) 132e5d6d67cSlukem errx(1, "udp/who: unknown service"); 133e541169cScgd #ifndef DEBUG 134e541169cScgd daemon(1, 0); 135e541169cScgd #endif 136e5d6d67cSlukem if (chdir(_PATH_RWHODIR) < 0) 137e5d6d67cSlukem err(1, "%s", _PATH_RWHODIR); 1389afcf17bSjtc (void) signal(SIGHUP, getboottime); 139e541169cScgd openlog("rwhod", LOG_PID, LOG_DAEMON); 140e541169cScgd /* 141e541169cScgd * Establish host name as returned by system. 142e541169cScgd */ 143e541169cScgd if (gethostname(myname, sizeof(myname) - 1) < 0) { 144e541169cScgd syslog(LOG_ERR, "gethostname: %m"); 145e541169cScgd exit(1); 146e541169cScgd } 147*32f51971Smrg myname[sizeof(myname) - 1] = '\0'; 148e5d6d67cSlukem if ((cp = strchr(myname, '.')) != NULL) 149e541169cScgd *cp = '\0'; 150e541169cScgd strncpy(mywd.wd_hostname, myname, sizeof(myname) - 1); 151e541169cScgd utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644); 152e541169cScgd if (utmpf < 0) { 153e541169cScgd syslog(LOG_ERR, "%s: %m", _PATH_UTMP); 154e541169cScgd exit(1); 155e541169cScgd } 1569afcf17bSjtc getboottime(0); 157e541169cScgd if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 158e541169cScgd syslog(LOG_ERR, "socket: %m"); 159e541169cScgd exit(1); 160e541169cScgd } 161e541169cScgd if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) { 162e541169cScgd syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 163e541169cScgd exit(1); 164e541169cScgd } 1659afcf17bSjtc memset(&sin, 0, sizeof(sin)); 1669afcf17bSjtc sin.sin_family = AF_INET; 1679afcf17bSjtc sin.sin_port = sp->s_port; 1689afcf17bSjtc if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 169e541169cScgd syslog(LOG_ERR, "bind: %m"); 170e541169cScgd exit(1); 171e541169cScgd } 172e541169cScgd if (!configure(s)) 173e541169cScgd exit(1); 174e541169cScgd signal(SIGALRM, onalrm); 1759afcf17bSjtc onalrm(0); 176e541169cScgd for (;;) { 177e541169cScgd struct whod wd; 178e541169cScgd int cc, whod, len = sizeof(from); 179e541169cScgd 180e541169cScgd cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0, 181e541169cScgd (struct sockaddr *)&from, &len); 182e541169cScgd if (cc <= 0) { 183e541169cScgd if (cc < 0 && errno != EINTR) 184e541169cScgd syslog(LOG_WARNING, "recv: %m"); 185e541169cScgd continue; 186e541169cScgd } 187e541169cScgd if (from.sin_port != sp->s_port) { 188e541169cScgd syslog(LOG_WARNING, "%d: bad from port", 189e541169cScgd ntohs(from.sin_port)); 190e541169cScgd continue; 191e541169cScgd } 192e541169cScgd if (wd.wd_vers != WHODVERSION) 193e541169cScgd continue; 194e541169cScgd if (wd.wd_type != WHODTYPE_STATUS) 195e541169cScgd continue; 196d3a4eeb1Sexplorer /* 197d3a4eeb1Sexplorer * Ensure null termination of the name within the packet. 198d3a4eeb1Sexplorer * Otherwise we might overflow or read past the end. 199d3a4eeb1Sexplorer */ 200d3a4eeb1Sexplorer wd.wd_hostname[sizeof(wd.wd_hostname)-1] = 0; 201e541169cScgd if (!verify(wd.wd_hostname)) { 2028d79db10Slukem syslog(LOG_WARNING, "malformed host name from %s", 2038d79db10Slukem inet_ntoa(from.sin_addr)); 204e541169cScgd continue; 205e541169cScgd } 206d3a4eeb1Sexplorer snprintf(path, sizeof(path), "whod.%s", wd.wd_hostname); 207e541169cScgd /* 208e541169cScgd * Rather than truncating and growing the file each time, 209e541169cScgd * use ftruncate if size is less than previous size. 210e541169cScgd */ 211e541169cScgd whod = open(path, O_WRONLY | O_CREAT, 0644); 212e541169cScgd if (whod < 0) { 213e541169cScgd syslog(LOG_WARNING, "%s: %m", path); 214e541169cScgd continue; 215e541169cScgd } 216e541169cScgd #if ENDIAN != BIG_ENDIAN 217e541169cScgd { 218e541169cScgd int i, n = (cc - WHDRSIZE)/sizeof(struct whoent); 219e541169cScgd struct whoent *we; 220e541169cScgd 221e541169cScgd /* undo header byte swapping before writing to file */ 222e541169cScgd wd.wd_sendtime = ntohl(wd.wd_sendtime); 223e541169cScgd for (i = 0; i < 3; i++) 224e541169cScgd wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 225e541169cScgd wd.wd_boottime = ntohl(wd.wd_boottime); 226e541169cScgd we = wd.wd_we; 227e541169cScgd for (i = 0; i < n; i++) { 228e541169cScgd we->we_idle = ntohl(we->we_idle); 229e541169cScgd we->we_utmp.out_time = 230e541169cScgd ntohl(we->we_utmp.out_time); 231e541169cScgd we++; 232e541169cScgd } 233e541169cScgd } 234e541169cScgd #endif 235e541169cScgd (void) time((time_t *)&wd.wd_recvtime); 236e541169cScgd (void) write(whod, (char *)&wd, cc); 237e541169cScgd if (fstat(whod, &st) < 0 || st.st_size > cc) 238e541169cScgd ftruncate(whod, cc); 239e541169cScgd (void) close(whod); 240e541169cScgd } 241e541169cScgd } 242e541169cScgd 243e541169cScgd /* 244e541169cScgd * Check out host name for unprintables 245e541169cScgd * and other funnies before allowing a file 246e541169cScgd * to be created. Sorry, but blanks aren't allowed. 247e541169cScgd */ 2489afcf17bSjtc int 249e541169cScgd verify(name) 250e5d6d67cSlukem char *name; 251e541169cScgd { 252e5d6d67cSlukem int size = 0; 253e541169cScgd 254e541169cScgd while (*name) { 255e541169cScgd if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 256e541169cScgd return (0); 257e541169cScgd name++, size++; 258e541169cScgd } 259e541169cScgd return (size > 0); 260e541169cScgd } 261e541169cScgd 262e541169cScgd int utmptime; 263e541169cScgd int utmpent; 264e541169cScgd int utmpsize = 0; 265e541169cScgd struct utmp *utmp; 266e541169cScgd int alarmcount; 267e541169cScgd 268e541169cScgd void 2699afcf17bSjtc onalrm(signo) 2709afcf17bSjtc int signo; 271e541169cScgd { 272e5d6d67cSlukem struct neighbor *np; 273e5d6d67cSlukem struct whoent *we = mywd.wd_we, *wlast; 274e5d6d67cSlukem int i; 275e541169cScgd struct stat stb; 276e541169cScgd double avenrun[3]; 2779afcf17bSjtc time_t now; 2789afcf17bSjtc int cc; 279e541169cScgd 2809afcf17bSjtc now = time(NULL); 281e541169cScgd if (alarmcount % 10 == 0) 2829afcf17bSjtc getboottime(0); 283e541169cScgd alarmcount++; 284e541169cScgd (void) fstat(utmpf, &stb); 285e541169cScgd if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) { 286e541169cScgd utmptime = stb.st_mtime; 287e541169cScgd if (stb.st_size > utmpsize) { 288e541169cScgd utmpsize = stb.st_size + 10 * sizeof(struct utmp); 289e541169cScgd if (utmp) 290e541169cScgd utmp = (struct utmp *)realloc(utmp, utmpsize); 291e541169cScgd else 292e541169cScgd utmp = (struct utmp *)malloc(utmpsize); 293e541169cScgd if (! utmp) { 294e5d6d67cSlukem warn("malloc failed"); 295e541169cScgd utmpsize = 0; 296e541169cScgd goto done; 297e541169cScgd } 298e541169cScgd } 29966105c37Skleink (void)lseek(utmpf, (off_t)0, SEEK_SET); 300e541169cScgd cc = read(utmpf, (char *)utmp, stb.st_size); 301e541169cScgd if (cc < 0) { 302e5d6d67cSlukem warn("%s", _PATH_UTMP); 303e541169cScgd goto done; 304e541169cScgd } 305e541169cScgd wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1]; 306e541169cScgd utmpent = cc / sizeof(struct utmp); 307e541169cScgd for (i = 0; i < utmpent; i++) 308e541169cScgd if (utmp[i].ut_name[0]) { 3099afcf17bSjtc memcpy(we->we_utmp.out_line, utmp[i].ut_line, 310e541169cScgd sizeof(utmp[i].ut_line)); 3119afcf17bSjtc memcpy(we->we_utmp.out_name, utmp[i].ut_name, 312e541169cScgd sizeof(utmp[i].ut_name)); 313e541169cScgd we->we_utmp.out_time = htonl(utmp[i].ut_time); 314e541169cScgd if (we >= wlast) 315e541169cScgd break; 316e541169cScgd we++; 317e541169cScgd } 318e541169cScgd utmpent = we - mywd.wd_we; 319e541169cScgd } 320e541169cScgd 321e541169cScgd /* 322e541169cScgd * The test on utmpent looks silly---after all, if no one is 323e541169cScgd * logged on, why worry about efficiency?---but is useful on 324e541169cScgd * (e.g.) compute servers. 325e541169cScgd */ 326e541169cScgd if (utmpent && chdir(_PATH_DEV)) { 327e541169cScgd syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV); 328e541169cScgd exit(1); 329e541169cScgd } 330e541169cScgd we = mywd.wd_we; 331e541169cScgd for (i = 0; i < utmpent; i++) { 332e541169cScgd if (stat(we->we_utmp.out_line, &stb) >= 0) 333e541169cScgd we->we_idle = htonl(now - stb.st_atime); 334e541169cScgd we++; 335e541169cScgd } 336e541169cScgd (void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); 337e541169cScgd for (i = 0; i < 3; i++) 338e541169cScgd mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 339e541169cScgd cc = (char *)we - (char *)&mywd; 340e541169cScgd mywd.wd_sendtime = htonl(time(0)); 341e541169cScgd mywd.wd_vers = WHODVERSION; 342e541169cScgd mywd.wd_type = WHODTYPE_STATUS; 343e541169cScgd for (np = neighbors; np != NULL; np = np->n_next) 344e541169cScgd (void)sendto(s, (char *)&mywd, cc, 0, 3459afcf17bSjtc np->n_addr, np->n_addrlen); 346e541169cScgd if (utmpent && chdir(_PATH_RWHODIR)) { 347e541169cScgd syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR); 348e541169cScgd exit(1); 349e541169cScgd } 350e541169cScgd done: 351e541169cScgd (void) alarm(AL_INTERVAL); 352e541169cScgd } 353e541169cScgd 354e541169cScgd void 3559afcf17bSjtc getboottime(signo) 3569afcf17bSjtc int signo; 357e541169cScgd { 3589afcf17bSjtc int mib[2]; 3599afcf17bSjtc size_t size; 3609afcf17bSjtc struct timeval tm; 361e541169cScgd 3629afcf17bSjtc mib[0] = CTL_KERN; 3639afcf17bSjtc mib[1] = KERN_BOOTTIME; 3649afcf17bSjtc size = sizeof(tm); 3659afcf17bSjtc if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) { 3669afcf17bSjtc syslog(LOG_ERR, "cannot get boottime: %m"); 3679afcf17bSjtc exit(1); 3689afcf17bSjtc } 3699afcf17bSjtc mywd.wd_boottime = htonl(tm.tv_sec); 370e541169cScgd } 371b55bc39bSandrew 3729afcf17bSjtc void 3739afcf17bSjtc quit(msg) 3749afcf17bSjtc char *msg; 3759afcf17bSjtc { 3769afcf17bSjtc syslog(LOG_ERR, msg); 3779afcf17bSjtc exit(1); 378e541169cScgd } 3799afcf17bSjtc 3809afcf17bSjtc #define ROUNDUP(a) \ 3819afcf17bSjtc ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 3829afcf17bSjtc #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 3839afcf17bSjtc 3849afcf17bSjtc void 3859afcf17bSjtc rt_xaddrs(cp, cplim, rtinfo) 386e5d6d67cSlukem caddr_t cp, cplim; 387e5d6d67cSlukem struct rt_addrinfo *rtinfo; 3889afcf17bSjtc { 389e5d6d67cSlukem struct sockaddr *sa; 390e5d6d67cSlukem int i; 3919afcf17bSjtc 3929afcf17bSjtc memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info)); 3939afcf17bSjtc for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 3949afcf17bSjtc if ((rtinfo->rti_addrs & (1 << i)) == 0) 3959afcf17bSjtc continue; 3969afcf17bSjtc rtinfo->rti_info[i] = sa = (struct sockaddr *)cp; 3979afcf17bSjtc ADVANCE(cp, sa); 3989afcf17bSjtc } 399e541169cScgd } 400e541169cScgd 401e541169cScgd /* 402e541169cScgd * Figure out device configuration and select 403e541169cScgd * networks which deserve status information. 404e541169cScgd */ 4059afcf17bSjtc int 406e541169cScgd configure(s) 407e541169cScgd int s; 408e541169cScgd { 409e5d6d67cSlukem struct neighbor *np; 410e5d6d67cSlukem struct if_msghdr *ifm; 411e5d6d67cSlukem struct ifa_msghdr *ifam; 4129afcf17bSjtc struct sockaddr_dl *sdl; 4139afcf17bSjtc size_t needed; 4149afcf17bSjtc int mib[6], flags = 0, len; 4159afcf17bSjtc char *buf, *lim, *next; 4169afcf17bSjtc struct rt_addrinfo info; 417e541169cScgd 4189afcf17bSjtc mib[0] = CTL_NET; 4199afcf17bSjtc mib[1] = PF_ROUTE; 4209afcf17bSjtc mib[2] = 0; 4219afcf17bSjtc mib[3] = AF_INET; 4229afcf17bSjtc mib[4] = NET_RT_IFLIST; 4239afcf17bSjtc mib[5] = 0; 4249afcf17bSjtc if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 4259afcf17bSjtc quit("route-sysctl-estimate"); 4269afcf17bSjtc if ((buf = malloc(needed)) == NULL) 4279afcf17bSjtc quit("malloc"); 4289afcf17bSjtc if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 4299afcf17bSjtc quit("actual retrieval of interface table"); 4309afcf17bSjtc lim = buf + needed; 4319afcf17bSjtc 4329afcf17bSjtc sdl = NULL; /* XXX just to keep gcc -Wall happy */ 4339afcf17bSjtc for (next = buf; next < lim; next += ifm->ifm_msglen) { 4349afcf17bSjtc ifm = (struct if_msghdr *)next; 4359afcf17bSjtc if (ifm->ifm_type == RTM_IFINFO) { 4369afcf17bSjtc sdl = (struct sockaddr_dl *)(ifm + 1); 4379afcf17bSjtc flags = ifm->ifm_flags; 4389afcf17bSjtc continue; 439e541169cScgd } 4409afcf17bSjtc if ((flags & IFF_UP) == 0 || 4419afcf17bSjtc (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) 4429afcf17bSjtc continue; 4439afcf17bSjtc if (ifm->ifm_type != RTM_NEWADDR) 4449afcf17bSjtc quit("out of sync parsing NET_RT_IFLIST"); 4459afcf17bSjtc ifam = (struct ifa_msghdr *)ifm; 4469afcf17bSjtc info.rti_addrs = ifam->ifam_addrs; 4479afcf17bSjtc rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam, 4489afcf17bSjtc &info); 4499afcf17bSjtc /* gag, wish we could get rid of Internet dependencies */ 4509afcf17bSjtc #define dstaddr info.rti_info[RTAX_BRD] 4519afcf17bSjtc #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr 4529afcf17bSjtc #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port 4539afcf17bSjtc if (dstaddr == 0 || dstaddr->sa_family != AF_INET) 4549afcf17bSjtc continue; 4559afcf17bSjtc PORT_SA(dstaddr) = sp->s_port; 456e541169cScgd for (np = neighbors; np != NULL; np = np->n_next) 4579afcf17bSjtc if (memcmp(sdl->sdl_data, np->n_name, 4589afcf17bSjtc sdl->sdl_nlen) == 0 && 4599afcf17bSjtc IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr)) 460e541169cScgd break; 461e541169cScgd if (np != NULL) 462e541169cScgd continue; 4639afcf17bSjtc len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1; 4649afcf17bSjtc np = (struct neighbor *)malloc(len); 465e541169cScgd if (np == NULL) 4669afcf17bSjtc quit("malloc of neighbor structure"); 4679afcf17bSjtc memset(np, 0, len); 4689afcf17bSjtc np->n_flags = flags; 4699afcf17bSjtc np->n_addr = (struct sockaddr *)(np + 1); 4709afcf17bSjtc np->n_addrlen = dstaddr->sa_len; 4719afcf17bSjtc np->n_name = np->n_addrlen + (char *)np->n_addr; 472e541169cScgd np->n_next = neighbors; 473e541169cScgd neighbors = np; 4749afcf17bSjtc memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen); 4759afcf17bSjtc memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen); 476e541169cScgd } 4779afcf17bSjtc free(buf); 478e541169cScgd return (1); 479e541169cScgd } 480e541169cScgd 481e541169cScgd #ifdef DEBUG 4829afcf17bSjtc void 4839afcf17bSjtc Sendto(s, buf, cc, flags, to, tolen) 484e541169cScgd int s; 485e541169cScgd char *buf; 486e541169cScgd int cc, flags; 487e541169cScgd char *to; 488e541169cScgd int tolen; 489e541169cScgd { 490e5d6d67cSlukem struct whod *w = (struct whod *)buf; 491e5d6d67cSlukem struct whoent *we; 4929afcf17bSjtc struct sockaddr_in *sin = (struct sockaddr_in *)to; 493e541169cScgd 4949afcf17bSjtc printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port)); 495e541169cScgd printf("hostname %s %s\n", w->wd_hostname, 496e541169cScgd interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 497e541169cScgd printf("load %4.2f, %4.2f, %4.2f\n", 498e541169cScgd ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 499e541169cScgd ntohl(w->wd_loadav[2]) / 100.0); 500e541169cScgd cc -= WHDRSIZE; 501e541169cScgd for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) { 502e541169cScgd time_t t = ntohl(we->we_utmp.out_time); 503e541169cScgd printf("%-8.8s %s:%s %.12s", 504e541169cScgd we->we_utmp.out_name, 505e541169cScgd w->wd_hostname, we->we_utmp.out_line, 506e541169cScgd ctime(&t)+4); 507e541169cScgd we->we_idle = ntohl(we->we_idle) / 60; 508e541169cScgd if (we->we_idle) { 509e541169cScgd if (we->we_idle >= 100*60) 510e541169cScgd we->we_idle = 100*60 - 1; 511e541169cScgd if (we->we_idle >= 60) 512e541169cScgd printf(" %2d", we->we_idle / 60); 513e541169cScgd else 514e541169cScgd printf(" "); 515e541169cScgd printf(":%02d", we->we_idle % 60); 516e541169cScgd } 517e541169cScgd printf("\n"); 518e541169cScgd } 519e541169cScgd } 520e541169cScgd 521e541169cScgd char * 522e541169cScgd interval(time, updown) 523e541169cScgd int time; 524e541169cScgd char *updown; 525e541169cScgd { 526e541169cScgd static char resbuf[32]; 527e541169cScgd int days, hours, minutes; 528e541169cScgd 529e541169cScgd if (time < 0 || time > 3*30*24*60*60) { 530e541169cScgd (void) sprintf(resbuf, " %s ??:??", updown); 531e541169cScgd return (resbuf); 532e541169cScgd } 533e541169cScgd minutes = (time + 59) / 60; /* round to minutes */ 534e541169cScgd hours = minutes / 60; minutes %= 60; 535e541169cScgd days = hours / 24; hours %= 24; 536e541169cScgd if (days) 537e541169cScgd (void) sprintf(resbuf, "%s %2d+%02d:%02d", 538e541169cScgd updown, days, hours, minutes); 539e541169cScgd else 540e541169cScgd (void) sprintf(resbuf, "%s %2d:%02d", 541e541169cScgd updown, hours, minutes); 542e541169cScgd return (resbuf); 543e541169cScgd } 544e541169cScgd #endif 545