Home | History | Annotate | Line # | Download | only in rwhod
rwhod.c revision 1.4
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1983 The Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.1      cgd char copyright[] =
     36  1.1      cgd "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
     37  1.1      cgd  All rights reserved.\n";
     38  1.1      cgd #endif /* not lint */
     39  1.1      cgd 
     40  1.1      cgd #ifndef lint
     41  1.3  mycroft /*static char sccsid[] = "from: @(#)rwhod.c	5.20 (Berkeley) 3/2/91";*/
     42  1.4  mycroft static char rcsid[] = "$Id: rwhod.c,v 1.4 1993/12/15 23:52:40 mycroft Exp $";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.1      cgd #include <sys/param.h>
     46  1.1      cgd #include <sys/socket.h>
     47  1.1      cgd #include <sys/stat.h>
     48  1.1      cgd #include <sys/signal.h>
     49  1.1      cgd #include <sys/ioctl.h>
     50  1.1      cgd #include <sys/file.h>
     51  1.1      cgd 
     52  1.1      cgd #include <net/if.h>
     53  1.1      cgd #include <netinet/in.h>
     54  1.1      cgd 
     55  1.1      cgd #include <nlist.h>
     56  1.1      cgd #include <errno.h>
     57  1.1      cgd #include <utmp.h>
     58  1.1      cgd #include <ctype.h>
     59  1.1      cgd #include <netdb.h>
     60  1.1      cgd #include <syslog.h>
     61  1.1      cgd #include <protocols/rwhod.h>
     62  1.1      cgd #include <stdio.h>
     63  1.1      cgd #include <paths.h>
     64  1.1      cgd 
     65  1.1      cgd /*
     66  1.1      cgd  * Alarm interval. Don't forget to change the down time check in ruptime
     67  1.1      cgd  * if this is changed.
     68  1.1      cgd  */
     69  1.1      cgd #define AL_INTERVAL (3 * 60)
     70  1.1      cgd 
     71  1.2  mycroft struct	sockaddr_in s_in;
     72  1.1      cgd 
     73  1.1      cgd char	myname[MAXHOSTNAMELEN];
     74  1.1      cgd 
     75  1.1      cgd struct	nlist nl[] = {
     76  1.1      cgd #define	NL_BOOTTIME	0
     77  1.1      cgd 	{ "_boottime" },
     78  1.1      cgd 	0
     79  1.1      cgd };
     80  1.1      cgd 
     81  1.1      cgd /*
     82  1.1      cgd  * We communicate with each neighbor in
     83  1.1      cgd  * a list constructed at the time we're
     84  1.1      cgd  * started up.  Neighbors are currently
     85  1.1      cgd  * directly connected via a hardware interface.
     86  1.1      cgd  */
     87  1.1      cgd struct	neighbor {
     88  1.1      cgd 	struct	neighbor *n_next;
     89  1.1      cgd 	char	*n_name;		/* interface name */
     90  1.1      cgd 	char	*n_addr;		/* who to send to */
     91  1.1      cgd 	int	n_addrlen;		/* size of address */
     92  1.1      cgd 	int	n_flags;		/* should forward?, interface flags */
     93  1.1      cgd };
     94  1.1      cgd 
     95  1.1      cgd struct	neighbor *neighbors;
     96  1.1      cgd struct	whod mywd;
     97  1.1      cgd struct	servent *sp;
     98  1.1      cgd int	s, utmpf, kmemf = -1;
     99  1.1      cgd 
    100  1.1      cgd #define	WHDRSIZE	(sizeof (mywd) - sizeof (mywd.wd_we))
    101  1.1      cgd 
    102  1.1      cgd extern int errno;
    103  1.1      cgd char	*strcpy(), *malloc();
    104  1.1      cgd long	lseek();
    105  1.1      cgd void	getkmem(), onalrm();
    106  1.1      cgd struct	in_addr inet_makeaddr();
    107  1.1      cgd 
    108  1.1      cgd main()
    109  1.1      cgd {
    110  1.1      cgd 	struct sockaddr_in from;
    111  1.1      cgd 	struct stat st;
    112  1.1      cgd 	char path[64];
    113  1.1      cgd 	int on = 1;
    114  1.1      cgd 	char *cp, *index(), *strerror();
    115  1.1      cgd 
    116  1.1      cgd 	if (getuid()) {
    117  1.1      cgd 		fprintf(stderr, "rwhod: not super user\n");
    118  1.1      cgd 		exit(1);
    119  1.1      cgd 	}
    120  1.1      cgd 	sp = getservbyname("who", "udp");
    121  1.1      cgd 	if (sp == 0) {
    122  1.1      cgd 		fprintf(stderr, "rwhod: udp/who: unknown service\n");
    123  1.1      cgd 		exit(1);
    124  1.1      cgd 	}
    125  1.1      cgd #ifndef DEBUG
    126  1.1      cgd 	daemon(1, 0);
    127  1.1      cgd #endif
    128  1.1      cgd 	if (chdir(_PATH_RWHODIR) < 0) {
    129  1.1      cgd 		(void)fprintf(stderr, "rwhod: %s: %s\n",
    130  1.1      cgd 		    _PATH_RWHODIR, strerror(errno));
    131  1.1      cgd 		exit(1);
    132  1.1      cgd 	}
    133  1.1      cgd 	(void) signal(SIGHUP, getkmem);
    134  1.1      cgd 	openlog("rwhod", LOG_PID, LOG_DAEMON);
    135  1.1      cgd 	/*
    136  1.1      cgd 	 * Establish host name as returned by system.
    137  1.1      cgd 	 */
    138  1.1      cgd 	if (gethostname(myname, sizeof (myname) - 1) < 0) {
    139  1.1      cgd 		syslog(LOG_ERR, "gethostname: %m");
    140  1.1      cgd 		exit(1);
    141  1.1      cgd 	}
    142  1.1      cgd 	if ((cp = index(myname, '.')) != NULL)
    143  1.1      cgd 		*cp = '\0';
    144  1.1      cgd 	strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
    145  1.1      cgd 	utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
    146  1.1      cgd 	if (utmpf < 0) {
    147  1.1      cgd 		syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
    148  1.1      cgd 		exit(1);
    149  1.1      cgd 	}
    150  1.1      cgd 	getkmem();
    151  1.1      cgd 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    152  1.1      cgd 		syslog(LOG_ERR, "socket: %m");
    153  1.1      cgd 		exit(1);
    154  1.1      cgd 	}
    155  1.1      cgd 	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
    156  1.1      cgd 		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
    157  1.1      cgd 		exit(1);
    158  1.1      cgd 	}
    159  1.2  mycroft 	s_in.sin_family = AF_INET;
    160  1.2  mycroft 	s_in.sin_port = sp->s_port;
    161  1.2  mycroft 	if (bind(s, (struct sockaddr *)&s_in, sizeof (s_in)) < 0) {
    162  1.1      cgd 		syslog(LOG_ERR, "bind: %m");
    163  1.1      cgd 		exit(1);
    164  1.1      cgd 	}
    165  1.1      cgd 	if (!configure(s))
    166  1.1      cgd 		exit(1);
    167  1.1      cgd 	signal(SIGALRM, onalrm);
    168  1.1      cgd 	onalrm();
    169  1.1      cgd 	for (;;) {
    170  1.1      cgd 		struct whod wd;
    171  1.1      cgd 		int cc, whod, len = sizeof (from);
    172  1.1      cgd 
    173  1.1      cgd 		cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
    174  1.1      cgd 			(struct sockaddr *)&from, &len);
    175  1.1      cgd 		if (cc <= 0) {
    176  1.1      cgd 			if (cc < 0 && errno != EINTR)
    177  1.1      cgd 				syslog(LOG_WARNING, "recv: %m");
    178  1.1      cgd 			continue;
    179  1.1      cgd 		}
    180  1.1      cgd 		if (from.sin_port != sp->s_port) {
    181  1.1      cgd 			syslog(LOG_WARNING, "%d: bad from port",
    182  1.1      cgd 				ntohs(from.sin_port));
    183  1.1      cgd 			continue;
    184  1.1      cgd 		}
    185  1.1      cgd 		if (wd.wd_vers != WHODVERSION)
    186  1.1      cgd 			continue;
    187  1.1      cgd 		if (wd.wd_type != WHODTYPE_STATUS)
    188  1.1      cgd 			continue;
    189  1.1      cgd 		if (!verify(wd.wd_hostname)) {
    190  1.1      cgd 			syslog(LOG_WARNING, "malformed host name from %x",
    191  1.1      cgd 				from.sin_addr);
    192  1.1      cgd 			continue;
    193  1.1      cgd 		}
    194  1.1      cgd 		(void) sprintf(path, "whod.%s", wd.wd_hostname);
    195  1.1      cgd 		/*
    196  1.1      cgd 		 * Rather than truncating and growing the file each time,
    197  1.1      cgd 		 * use ftruncate if size is less than previous size.
    198  1.1      cgd 		 */
    199  1.1      cgd 		whod = open(path, O_WRONLY | O_CREAT, 0644);
    200  1.1      cgd 		if (whod < 0) {
    201  1.1      cgd 			syslog(LOG_WARNING, "%s: %m", path);
    202  1.1      cgd 			continue;
    203  1.1      cgd 		}
    204  1.1      cgd #if ENDIAN != BIG_ENDIAN
    205  1.1      cgd 		{
    206  1.1      cgd 			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
    207  1.1      cgd 			struct whoent *we;
    208  1.1      cgd 
    209  1.1      cgd 			/* undo header byte swapping before writing to file */
    210  1.1      cgd 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
    211  1.1      cgd 			for (i = 0; i < 3; i++)
    212  1.1      cgd 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
    213  1.1      cgd 			wd.wd_boottime = ntohl(wd.wd_boottime);
    214  1.1      cgd 			we = wd.wd_we;
    215  1.1      cgd 			for (i = 0; i < n; i++) {
    216  1.1      cgd 				we->we_idle = ntohl(we->we_idle);
    217  1.1      cgd 				we->we_utmp.out_time =
    218  1.1      cgd 				    ntohl(we->we_utmp.out_time);
    219  1.1      cgd 				we++;
    220  1.1      cgd 			}
    221  1.1      cgd 		}
    222  1.1      cgd #endif
    223  1.1      cgd 		(void) time((time_t *)&wd.wd_recvtime);
    224  1.1      cgd 		(void) write(whod, (char *)&wd, cc);
    225  1.1      cgd 		if (fstat(whod, &st) < 0 || st.st_size > cc)
    226  1.1      cgd 			ftruncate(whod, cc);
    227  1.1      cgd 		(void) close(whod);
    228  1.1      cgd 	}
    229  1.1      cgd }
    230  1.1      cgd 
    231  1.1      cgd /*
    232  1.1      cgd  * Check out host name for unprintables
    233  1.1      cgd  * and other funnies before allowing a file
    234  1.1      cgd  * to be created.  Sorry, but blanks aren't allowed.
    235  1.1      cgd  */
    236  1.1      cgd verify(name)
    237  1.1      cgd 	register char *name;
    238  1.1      cgd {
    239  1.1      cgd 	register int size = 0;
    240  1.1      cgd 
    241  1.1      cgd 	while (*name) {
    242  1.1      cgd 		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
    243  1.1      cgd 			return (0);
    244  1.1      cgd 		name++, size++;
    245  1.1      cgd 	}
    246  1.1      cgd 	return (size > 0);
    247  1.1      cgd }
    248  1.1      cgd 
    249  1.1      cgd int	utmptime;
    250  1.1      cgd int	utmpent;
    251  1.1      cgd int	utmpsize = 0;
    252  1.1      cgd struct	utmp *utmp;
    253  1.1      cgd int	alarmcount;
    254  1.1      cgd 
    255  1.1      cgd void
    256  1.1      cgd onalrm()
    257  1.1      cgd {
    258  1.1      cgd 	register struct neighbor *np;
    259  1.1      cgd 	register struct whoent *we = mywd.wd_we, *wlast;
    260  1.1      cgd 	register int i;
    261  1.1      cgd 	struct stat stb;
    262  1.1      cgd 	int cc;
    263  1.1      cgd 	double avenrun[3];
    264  1.1      cgd 	time_t now = time((time_t *)NULL);
    265  1.1      cgd 	char *strerror();
    266  1.1      cgd 
    267  1.1      cgd 	if (alarmcount % 10 == 0)
    268  1.1      cgd 		getkmem();
    269  1.1      cgd 	alarmcount++;
    270  1.1      cgd 	(void) fstat(utmpf, &stb);
    271  1.1      cgd 	if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
    272  1.1      cgd 		utmptime = stb.st_mtime;
    273  1.1      cgd 		if (stb.st_size > utmpsize) {
    274  1.1      cgd 			utmpsize = stb.st_size + 10 * sizeof(struct utmp);
    275  1.1      cgd 			if (utmp)
    276  1.1      cgd 				utmp = (struct utmp *)realloc(utmp, utmpsize);
    277  1.1      cgd 			else
    278  1.1      cgd 				utmp = (struct utmp *)malloc(utmpsize);
    279  1.1      cgd 			if (! utmp) {
    280  1.1      cgd 				fprintf(stderr, "rwhod: malloc failed\n");
    281  1.1      cgd 				utmpsize = 0;
    282  1.1      cgd 				goto done;
    283  1.1      cgd 			}
    284  1.1      cgd 		}
    285  1.1      cgd 		(void) lseek(utmpf, (long)0, L_SET);
    286  1.1      cgd 		cc = read(utmpf, (char *)utmp, stb.st_size);
    287  1.1      cgd 		if (cc < 0) {
    288  1.1      cgd 			fprintf(stderr, "rwhod: %s: %s\n",
    289  1.1      cgd 			    _PATH_UTMP, strerror(errno));
    290  1.1      cgd 			goto done;
    291  1.1      cgd 		}
    292  1.1      cgd 		wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
    293  1.1      cgd 		utmpent = cc / sizeof (struct utmp);
    294  1.1      cgd 		for (i = 0; i < utmpent; i++)
    295  1.1      cgd 			if (utmp[i].ut_name[0]) {
    296  1.1      cgd 				bcopy(utmp[i].ut_line, we->we_utmp.out_line,
    297  1.1      cgd 				   sizeof (utmp[i].ut_line));
    298  1.1      cgd 				bcopy(utmp[i].ut_name, we->we_utmp.out_name,
    299  1.1      cgd 				   sizeof (utmp[i].ut_name));
    300  1.1      cgd 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
    301  1.1      cgd 				if (we >= wlast)
    302  1.1      cgd 					break;
    303  1.1      cgd 				we++;
    304  1.1      cgd 			}
    305  1.1      cgd 		utmpent = we - mywd.wd_we;
    306  1.1      cgd 	}
    307  1.1      cgd 
    308  1.1      cgd 	/*
    309  1.1      cgd 	 * The test on utmpent looks silly---after all, if no one is
    310  1.1      cgd 	 * logged on, why worry about efficiency?---but is useful on
    311  1.1      cgd 	 * (e.g.) compute servers.
    312  1.1      cgd 	 */
    313  1.1      cgd 	if (utmpent && chdir(_PATH_DEV)) {
    314  1.1      cgd 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
    315  1.1      cgd 		exit(1);
    316  1.1      cgd 	}
    317  1.1      cgd 	we = mywd.wd_we;
    318  1.1      cgd 	for (i = 0; i < utmpent; i++) {
    319  1.1      cgd 		if (stat(we->we_utmp.out_line, &stb) >= 0)
    320  1.1      cgd 			we->we_idle = htonl(now - stb.st_atime);
    321  1.1      cgd 		we++;
    322  1.1      cgd 	}
    323  1.1      cgd 	(void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
    324  1.1      cgd 	for (i = 0; i < 3; i++)
    325  1.1      cgd 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
    326  1.1      cgd 	cc = (char *)we - (char *)&mywd;
    327  1.1      cgd 	mywd.wd_sendtime = htonl(time(0));
    328  1.1      cgd 	mywd.wd_vers = WHODVERSION;
    329  1.1      cgd 	mywd.wd_type = WHODTYPE_STATUS;
    330  1.1      cgd 	for (np = neighbors; np != NULL; np = np->n_next)
    331  1.1      cgd 		(void) sendto(s, (char *)&mywd, cc, 0,
    332  1.1      cgd 			(struct sockaddr *)np->n_addr, np->n_addrlen);
    333  1.1      cgd 	if (utmpent && chdir(_PATH_RWHODIR)) {
    334  1.1      cgd 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
    335  1.1      cgd 		exit(1);
    336  1.1      cgd 	}
    337  1.1      cgd done:
    338  1.1      cgd 	(void) alarm(AL_INTERVAL);
    339  1.1      cgd }
    340  1.1      cgd 
    341  1.1      cgd void
    342  1.1      cgd getkmem()
    343  1.1      cgd {
    344  1.1      cgd 	static ino_t vmunixino;
    345  1.1      cgd 	static time_t vmunixctime;
    346  1.1      cgd 	struct stat sb;
    347  1.1      cgd 
    348  1.1      cgd 	if (stat(_PATH_UNIX, &sb) < 0) {
    349  1.1      cgd 		if (vmunixctime)
    350  1.1      cgd 			return;
    351  1.1      cgd 	} else {
    352  1.1      cgd 		if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
    353  1.1      cgd 			return;
    354  1.1      cgd 		vmunixctime = sb.st_ctime;
    355  1.1      cgd 		vmunixino= sb.st_ino;
    356  1.1      cgd 	}
    357  1.1      cgd 	if (kmemf >= 0)
    358  1.1      cgd 		(void) close(kmemf);
    359  1.1      cgd loop:
    360  1.1      cgd 	if (nlist(_PATH_UNIX, nl)) {
    361  1.1      cgd 		syslog(LOG_WARNING, "%s: namelist botch", _PATH_UNIX);
    362  1.1      cgd 		sleep(300);
    363  1.1      cgd 		goto loop;
    364  1.1      cgd 	}
    365  1.1      cgd 	kmemf = open(_PATH_KMEM, O_RDONLY, 0);
    366  1.1      cgd 	if (kmemf < 0) {
    367  1.1      cgd 		syslog(LOG_ERR, "%s: %m", _PATH_KMEM);
    368  1.1      cgd 		exit(1);
    369  1.1      cgd 	}
    370  1.1      cgd 	(void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET);
    371  1.1      cgd 	(void) read(kmemf, (char *)&mywd.wd_boottime,
    372  1.1      cgd 	    sizeof (mywd.wd_boottime));
    373  1.1      cgd 	mywd.wd_boottime = htonl(mywd.wd_boottime);
    374  1.1      cgd }
    375  1.1      cgd 
    376  1.1      cgd /*
    377  1.1      cgd  * Figure out device configuration and select
    378  1.1      cgd  * networks which deserve status information.
    379  1.1      cgd  */
    380  1.1      cgd configure(s)
    381  1.1      cgd 	int s;
    382  1.1      cgd {
    383  1.1      cgd 	char buf[BUFSIZ], *cp, *cplim;
    384  1.1      cgd 	struct ifconf ifc;
    385  1.1      cgd 	struct ifreq ifreq, *ifr;
    386  1.2  mycroft 	struct sockaddr_in *s_in;
    387  1.1      cgd 	register struct neighbor *np;
    388  1.1      cgd 
    389  1.1      cgd 	ifc.ifc_len = sizeof (buf);
    390  1.1      cgd 	ifc.ifc_buf = buf;
    391  1.1      cgd 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
    392  1.1      cgd 		syslog(LOG_ERR, "ioctl (get interface configuration)");
    393  1.1      cgd 		return (0);
    394  1.1      cgd 	}
    395  1.1      cgd 	ifr = ifc.ifc_req;
    396  1.1      cgd #ifdef AF_LINK
    397  1.1      cgd #define max(a, b) (a > b ? a : b)
    398  1.1      cgd #define size(p)	max((p).sa_len, sizeof(p))
    399  1.1      cgd #else
    400  1.1      cgd #define size(p) (sizeof (p))
    401  1.1      cgd #endif
    402  1.1      cgd 	cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
    403  1.1      cgd 	for (cp = buf; cp < cplim;
    404  1.1      cgd 			cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
    405  1.1      cgd 		ifr = (struct ifreq *)cp;
    406  1.1      cgd 		for (np = neighbors; np != NULL; np = np->n_next)
    407  1.1      cgd 			if (np->n_name &&
    408  1.1      cgd 			    strcmp(ifr->ifr_name, np->n_name) == 0)
    409  1.1      cgd 				break;
    410  1.1      cgd 		if (np != NULL)
    411  1.1      cgd 			continue;
    412  1.1      cgd 		ifreq = *ifr;
    413  1.1      cgd 		np = (struct neighbor *)malloc(sizeof (*np));
    414  1.1      cgd 		if (np == NULL)
    415  1.1      cgd 			continue;
    416  1.1      cgd 		np->n_name = malloc(strlen(ifr->ifr_name) + 1);
    417  1.1      cgd 		if (np->n_name == NULL) {
    418  1.1      cgd 			free((char *)np);
    419  1.1      cgd 			continue;
    420  1.1      cgd 		}
    421  1.1      cgd 		strcpy(np->n_name, ifr->ifr_name);
    422  1.1      cgd 		np->n_addrlen = sizeof (ifr->ifr_addr);
    423  1.1      cgd 		np->n_addr = malloc(np->n_addrlen);
    424  1.1      cgd 		if (np->n_addr == NULL) {
    425  1.1      cgd 			free(np->n_name);
    426  1.1      cgd 			free((char *)np);
    427  1.1      cgd 			continue;
    428  1.1      cgd 		}
    429  1.1      cgd 		bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
    430  1.1      cgd 		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
    431  1.1      cgd 			syslog(LOG_ERR, "ioctl (get interface flags)");
    432  1.4  mycroft 			free(np->n_addr);
    433  1.4  mycroft 			free(np->n_name);
    434  1.1      cgd 			free((char *)np);
    435  1.1      cgd 			continue;
    436  1.1      cgd 		}
    437  1.1      cgd 		if ((ifreq.ifr_flags & IFF_UP) == 0 ||
    438  1.1      cgd 		    (ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
    439  1.4  mycroft 			free(np->n_addr);
    440  1.4  mycroft 			free(np->n_name);
    441  1.1      cgd 			free((char *)np);
    442  1.1      cgd 			continue;
    443  1.1      cgd 		}
    444  1.1      cgd 		np->n_flags = ifreq.ifr_flags;
    445  1.1      cgd 		if (np->n_flags & IFF_POINTOPOINT) {
    446  1.1      cgd 			if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
    447  1.1      cgd 				syslog(LOG_ERR, "ioctl (get dstaddr)");
    448  1.4  mycroft 				free(np->n_addr);
    449  1.4  mycroft 				free(np->n_name);
    450  1.1      cgd 				free((char *)np);
    451  1.1      cgd 				continue;
    452  1.1      cgd 			}
    453  1.1      cgd 			/* we assume addresses are all the same size */
    454  1.1      cgd 			bcopy((char *)&ifreq.ifr_dstaddr,
    455  1.1      cgd 			  np->n_addr, np->n_addrlen);
    456  1.1      cgd 		}
    457  1.1      cgd 		if (np->n_flags & IFF_BROADCAST) {
    458  1.1      cgd 			if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
    459  1.1      cgd 				syslog(LOG_ERR, "ioctl (get broadaddr)");
    460  1.4  mycroft 				free(np->n_addr);
    461  1.4  mycroft 				free(np->n_name);
    462  1.1      cgd 				free((char *)np);
    463  1.1      cgd 				continue;
    464  1.1      cgd 			}
    465  1.1      cgd 			/* we assume addresses are all the same size */
    466  1.1      cgd 			bcopy((char *)&ifreq.ifr_broadaddr,
    467  1.1      cgd 			  np->n_addr, np->n_addrlen);
    468  1.1      cgd 		}
    469  1.1      cgd 		/* gag, wish we could get rid of Internet dependencies */
    470  1.2  mycroft 		s_in = (struct sockaddr_in *)np->n_addr;
    471  1.2  mycroft 		s_in->sin_port = sp->s_port;
    472  1.1      cgd 		np->n_next = neighbors;
    473  1.1      cgd 		neighbors = np;
    474  1.1      cgd 	}
    475  1.1      cgd 	return (1);
    476  1.1      cgd }
    477  1.1      cgd 
    478  1.1      cgd #ifdef DEBUG
    479  1.1      cgd sendto(s, buf, cc, flags, to, tolen)
    480  1.1      cgd 	int s;
    481  1.1      cgd 	char *buf;
    482  1.1      cgd 	int cc, flags;
    483  1.1      cgd 	char *to;
    484  1.1      cgd 	int tolen;
    485  1.1      cgd {
    486  1.1      cgd 	register struct whod *w = (struct whod *)buf;
    487  1.1      cgd 	register struct whoent *we;
    488  1.2  mycroft 	struct sockaddr_in *s_in = (struct sockaddr_in *)to;
    489  1.1      cgd 	char *interval();
    490  1.1      cgd 
    491  1.2  mycroft 	printf("sendto %x.%d\n", ntohl(s_in->sin_addr), ntohs(s_in->sin_port));
    492  1.1      cgd 	printf("hostname %s %s\n", w->wd_hostname,
    493  1.1      cgd 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
    494  1.1      cgd 	printf("load %4.2f, %4.2f, %4.2f\n",
    495  1.1      cgd 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
    496  1.1      cgd 	    ntohl(w->wd_loadav[2]) / 100.0);
    497  1.1      cgd 	cc -= WHDRSIZE;
    498  1.1      cgd 	for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
    499  1.1      cgd 		time_t t = ntohl(we->we_utmp.out_time);
    500  1.1      cgd 		printf("%-8.8s %s:%s %.12s",
    501  1.1      cgd 			we->we_utmp.out_name,
    502  1.1      cgd 			w->wd_hostname, we->we_utmp.out_line,
    503  1.1      cgd 			ctime(&t)+4);
    504  1.1      cgd 		we->we_idle = ntohl(we->we_idle) / 60;
    505  1.1      cgd 		if (we->we_idle) {
    506  1.1      cgd 			if (we->we_idle >= 100*60)
    507  1.1      cgd 				we->we_idle = 100*60 - 1;
    508  1.1      cgd 			if (we->we_idle >= 60)
    509  1.1      cgd 				printf(" %2d", we->we_idle / 60);
    510  1.1      cgd 			else
    511  1.1      cgd 				printf("   ");
    512  1.1      cgd 			printf(":%02d", we->we_idle % 60);
    513  1.1      cgd 		}
    514  1.1      cgd 		printf("\n");
    515  1.1      cgd 	}
    516  1.1      cgd }
    517  1.1      cgd 
    518  1.1      cgd char *
    519  1.1      cgd interval(time, updown)
    520  1.1      cgd 	int time;
    521  1.1      cgd 	char *updown;
    522  1.1      cgd {
    523  1.1      cgd 	static char resbuf[32];
    524  1.1      cgd 	int days, hours, minutes;
    525  1.1      cgd 
    526  1.1      cgd 	if (time < 0 || time > 3*30*24*60*60) {
    527  1.1      cgd 		(void) sprintf(resbuf, "   %s ??:??", updown);
    528  1.1      cgd 		return (resbuf);
    529  1.1      cgd 	}
    530  1.1      cgd 	minutes = (time + 59) / 60;		/* round to minutes */
    531  1.1      cgd 	hours = minutes / 60; minutes %= 60;
    532  1.1      cgd 	days = hours / 24; hours %= 24;
    533  1.1      cgd 	if (days)
    534  1.1      cgd 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
    535  1.1      cgd 		    updown, days, hours, minutes);
    536  1.1      cgd 	else
    537  1.1      cgd 		(void) sprintf(resbuf, "%s    %2d:%02d",
    538  1.1      cgd 		    updown, hours, minutes);
    539  1.1      cgd 	return (resbuf);
    540  1.1      cgd }
    541  1.1      cgd #endif
    542