Home | History | Annotate | Line # | Download | only in rwhod
rwhod.c revision 1.16
      1   1.1       cgd /*
      2   1.8       jtc  * Copyright (c) 1983, 1993
      3   1.8       jtc  *	The Regents of the University of California.  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.11     lukem #include <sys/cdefs.h>
     35   1.1       cgd #ifndef lint
     36  1.11     lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
     37  1.11     lukem 	The Regents of the University of California.  All rights reserved.\n");
     38   1.1       cgd #endif /* not lint */
     39   1.1       cgd 
     40   1.1       cgd #ifndef lint
     41  1.11     lukem #if 0
     42  1.11     lukem static char sccsid[] = "@(#)rwhod.c	8.1 (Berkeley) 6/6/93";
     43  1.11     lukem #else
     44  1.16       mjl __RCSID("$NetBSD: rwhod.c,v 1.16 1999/11/15 15:59:24 mjl Exp $");
     45  1.11     lukem #endif
     46   1.1       cgd #endif /* not lint */
     47   1.1       cgd 
     48   1.1       cgd #include <sys/param.h>
     49   1.1       cgd #include <sys/socket.h>
     50   1.1       cgd #include <sys/stat.h>
     51   1.1       cgd #include <sys/signal.h>
     52   1.1       cgd #include <sys/ioctl.h>
     53   1.8       jtc #include <sys/sysctl.h>
     54   1.1       cgd 
     55   1.1       cgd #include <net/if.h>
     56   1.8       jtc #include <net/if_dl.h>
     57   1.8       jtc #include <net/route.h>
     58   1.1       cgd #include <netinet/in.h>
     59   1.8       jtc #include <protocols/rwhod.h>
     60  1.11     lukem #include <arpa/inet.h>
     61   1.1       cgd 
     62   1.8       jtc #include <ctype.h>
     63  1.12     lukem #include <err.h>
     64   1.1       cgd #include <errno.h>
     65   1.8       jtc #include <fcntl.h>
     66   1.1       cgd #include <netdb.h>
     67   1.8       jtc #include <paths.h>
     68   1.8       jtc #include <stdio.h>
     69   1.8       jtc #include <stdlib.h>
     70   1.8       jtc #include <string.h>
     71   1.1       cgd #include <syslog.h>
     72   1.8       jtc #include <unistd.h>
     73  1.15   thorpej #include <util.h>
     74   1.8       jtc #include <utmp.h>
     75   1.1       cgd 
     76   1.1       cgd /*
     77   1.1       cgd  * Alarm interval. Don't forget to change the down time check in ruptime
     78   1.1       cgd  * if this is changed.
     79   1.1       cgd  */
     80   1.1       cgd #define AL_INTERVAL (3 * 60)
     81   1.1       cgd 
     82  1.13       mrg char	myname[MAXHOSTNAMELEN + 1];
     83   1.1       cgd 
     84   1.1       cgd /*
     85   1.8       jtc  * We communicate with each neighbor in a list constructed at the time we're
     86   1.8       jtc  * started up.  Neighbors are currently directly connected via a hardware
     87   1.8       jtc  * interface.
     88   1.1       cgd  */
     89   1.1       cgd struct	neighbor {
     90   1.1       cgd 	struct	neighbor *n_next;
     91   1.1       cgd 	char	*n_name;		/* interface name */
     92  1.14       mrg 	struct	sockaddr *n_addr;	/* who to send to */
     93   1.1       cgd 	int	n_addrlen;		/* size of address */
     94   1.1       cgd 	int	n_flags;		/* should forward?, interface flags */
     95   1.1       cgd };
     96   1.1       cgd 
     97   1.1       cgd struct	neighbor *neighbors;
     98   1.1       cgd struct	whod mywd;
     99   1.1       cgd struct	servent *sp;
    100   1.7    andrew int	s, utmpf;
    101   1.1       cgd 
    102   1.8       jtc #define	WHDRSIZE	(sizeof(mywd) - sizeof(mywd.wd_we))
    103   1.1       cgd 
    104   1.8       jtc int	 configure __P((int));
    105   1.8       jtc void	 getboottime __P((int));
    106  1.11     lukem int	 main __P((int, char **));
    107   1.8       jtc void	 onalrm __P((int));
    108   1.8       jtc void	 quit __P((char *));
    109   1.8       jtc void	 rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
    110   1.8       jtc int	 verify __P((char *));
    111   1.8       jtc #ifdef DEBUG
    112   1.8       jtc char	*interval __P((int, char *));
    113   1.8       jtc void	 Sendto __P((int, char *, int, int, char *, int));
    114   1.8       jtc #define	 sendto Sendto
    115   1.8       jtc #endif
    116   1.1       cgd 
    117   1.8       jtc int
    118   1.8       jtc main(argc, argv)
    119   1.8       jtc 	int argc;
    120  1.11     lukem 	char *argv[];
    121   1.1       cgd {
    122   1.1       cgd 	struct sockaddr_in from;
    123   1.1       cgd 	struct stat st;
    124   1.1       cgd 	char path[64];
    125   1.1       cgd 	int on = 1;
    126   1.8       jtc 	char *cp;
    127   1.8       jtc 	struct sockaddr_in sin;
    128   1.1       cgd 
    129  1.12     lukem 	if (getuid())
    130  1.12     lukem 		errx(1, "not super user");
    131   1.1       cgd 	sp = getservbyname("who", "udp");
    132  1.12     lukem 	if (sp == NULL)
    133  1.12     lukem 		errx(1, "udp/who: unknown service");
    134   1.1       cgd #ifndef DEBUG
    135   1.1       cgd 	daemon(1, 0);
    136  1.15   thorpej 	pidfile(NULL);
    137   1.1       cgd #endif
    138  1.12     lukem 	if (chdir(_PATH_RWHODIR) < 0)
    139  1.12     lukem 		err(1, "%s", _PATH_RWHODIR);
    140  1.14       mrg 	(void)signal(SIGHUP, getboottime);
    141   1.1       cgd 	openlog("rwhod", LOG_PID, LOG_DAEMON);
    142   1.1       cgd 	/*
    143   1.1       cgd 	 * Establish host name as returned by system.
    144   1.1       cgd 	 */
    145   1.8       jtc 	if (gethostname(myname, sizeof(myname) - 1) < 0) {
    146   1.1       cgd 		syslog(LOG_ERR, "gethostname: %m");
    147   1.1       cgd 		exit(1);
    148   1.1       cgd 	}
    149  1.13       mrg 	myname[sizeof(myname) - 1] = '\0';
    150  1.12     lukem 	if ((cp = strchr(myname, '.')) != NULL)
    151   1.1       cgd 		*cp = '\0';
    152  1.14       mrg 	strncpy(mywd.wd_hostname, myname, sizeof(mywd.wd_hostname) - 1);
    153   1.1       cgd 	utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
    154   1.1       cgd 	if (utmpf < 0) {
    155   1.1       cgd 		syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
    156   1.1       cgd 		exit(1);
    157   1.1       cgd 	}
    158   1.8       jtc 	getboottime(0);
    159   1.1       cgd 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    160   1.1       cgd 		syslog(LOG_ERR, "socket: %m");
    161   1.1       cgd 		exit(1);
    162   1.1       cgd 	}
    163   1.8       jtc 	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
    164   1.1       cgd 		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
    165   1.1       cgd 		exit(1);
    166   1.1       cgd 	}
    167   1.8       jtc 	memset(&sin, 0, sizeof(sin));
    168   1.8       jtc 	sin.sin_family = AF_INET;
    169   1.8       jtc 	sin.sin_port = sp->s_port;
    170   1.8       jtc 	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    171   1.1       cgd 		syslog(LOG_ERR, "bind: %m");
    172   1.1       cgd 		exit(1);
    173   1.1       cgd 	}
    174   1.1       cgd 	if (!configure(s))
    175   1.1       cgd 		exit(1);
    176   1.1       cgd 	signal(SIGALRM, onalrm);
    177   1.8       jtc 	onalrm(0);
    178   1.1       cgd 	for (;;) {
    179   1.1       cgd 		struct whod wd;
    180   1.8       jtc 		int cc, whod, len = sizeof(from);
    181   1.1       cgd 
    182   1.8       jtc 		cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0,
    183   1.1       cgd 			(struct sockaddr *)&from, &len);
    184   1.1       cgd 		if (cc <= 0) {
    185   1.1       cgd 			if (cc < 0 && errno != EINTR)
    186   1.1       cgd 				syslog(LOG_WARNING, "recv: %m");
    187   1.1       cgd 			continue;
    188   1.1       cgd 		}
    189   1.1       cgd 		if (from.sin_port != sp->s_port) {
    190   1.1       cgd 			syslog(LOG_WARNING, "%d: bad from port",
    191   1.1       cgd 				ntohs(from.sin_port));
    192   1.1       cgd 			continue;
    193   1.1       cgd 		}
    194  1.16       mjl 		if (cc < WHDRSIZE) {
    195  1.16       mjl 			syslog(LOG_WARNING, "Short packet from %s",
    196  1.16       mjl 				inet_ntoa(from.sin_addr));
    197  1.16       mjl 			continue;
    198  1.16       mjl 		}
    199  1.16       mjl 
    200   1.1       cgd 		if (wd.wd_vers != WHODVERSION)
    201   1.1       cgd 			continue;
    202   1.1       cgd 		if (wd.wd_type != WHODTYPE_STATUS)
    203   1.1       cgd 			continue;
    204   1.9  explorer 		/*
    205   1.9  explorer 		 * Ensure null termination of the name within the packet.
    206   1.9  explorer 		 * Otherwise we might overflow or read past the end.
    207   1.9  explorer 		 */
    208   1.9  explorer 		wd.wd_hostname[sizeof(wd.wd_hostname)-1] = 0;
    209   1.1       cgd 		if (!verify(wd.wd_hostname)) {
    210  1.11     lukem 			syslog(LOG_WARNING, "malformed host name from %s",
    211  1.11     lukem 				inet_ntoa(from.sin_addr));
    212   1.1       cgd 			continue;
    213   1.1       cgd 		}
    214   1.9  explorer 		snprintf(path, sizeof(path), "whod.%s", wd.wd_hostname);
    215   1.1       cgd 		/*
    216   1.1       cgd 		 * Rather than truncating and growing the file each time,
    217   1.1       cgd 		 * use ftruncate if size is less than previous size.
    218   1.1       cgd 		 */
    219   1.1       cgd 		whod = open(path, O_WRONLY | O_CREAT, 0644);
    220   1.1       cgd 		if (whod < 0) {
    221   1.1       cgd 			syslog(LOG_WARNING, "%s: %m", path);
    222   1.1       cgd 			continue;
    223   1.1       cgd 		}
    224   1.1       cgd #if ENDIAN != BIG_ENDIAN
    225   1.1       cgd 		{
    226   1.1       cgd 			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
    227   1.1       cgd 			struct whoent *we;
    228   1.1       cgd 
    229   1.1       cgd 			/* undo header byte swapping before writing to file */
    230   1.1       cgd 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
    231   1.1       cgd 			for (i = 0; i < 3; i++)
    232   1.1       cgd 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
    233   1.1       cgd 			wd.wd_boottime = ntohl(wd.wd_boottime);
    234   1.1       cgd 			we = wd.wd_we;
    235   1.1       cgd 			for (i = 0; i < n; i++) {
    236   1.1       cgd 				we->we_idle = ntohl(we->we_idle);
    237   1.1       cgd 				we->we_utmp.out_time =
    238   1.1       cgd 				    ntohl(we->we_utmp.out_time);
    239   1.1       cgd 				we++;
    240   1.1       cgd 			}
    241   1.1       cgd 		}
    242   1.1       cgd #endif
    243  1.14       mrg 		(void)time((time_t *)&wd.wd_recvtime);
    244  1.14       mrg 		(void)write(whod, (char *)&wd, cc);
    245   1.1       cgd 		if (fstat(whod, &st) < 0 || st.st_size > cc)
    246   1.1       cgd 			ftruncate(whod, cc);
    247  1.14       mrg 		(void)close(whod);
    248   1.1       cgd 	}
    249   1.1       cgd }
    250   1.1       cgd 
    251   1.1       cgd /*
    252   1.1       cgd  * Check out host name for unprintables
    253   1.1       cgd  * and other funnies before allowing a file
    254   1.1       cgd  * to be created.  Sorry, but blanks aren't allowed.
    255   1.1       cgd  */
    256   1.8       jtc int
    257   1.1       cgd verify(name)
    258  1.12     lukem 	char *name;
    259   1.1       cgd {
    260  1.12     lukem 	int size = 0;
    261   1.1       cgd 
    262   1.1       cgd 	while (*name) {
    263   1.1       cgd 		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
    264   1.1       cgd 			return (0);
    265   1.1       cgd 		name++, size++;
    266   1.1       cgd 	}
    267   1.1       cgd 	return (size > 0);
    268   1.1       cgd }
    269   1.1       cgd 
    270   1.1       cgd int	utmptime;
    271   1.1       cgd int	utmpent;
    272   1.1       cgd int	utmpsize = 0;
    273   1.1       cgd struct	utmp *utmp;
    274   1.1       cgd int	alarmcount;
    275   1.1       cgd 
    276   1.1       cgd void
    277   1.8       jtc onalrm(signo)
    278   1.8       jtc 	int signo;
    279   1.1       cgd {
    280  1.12     lukem 	struct neighbor *np;
    281  1.12     lukem 	struct whoent *we = mywd.wd_we, *wlast;
    282  1.12     lukem 	int i;
    283   1.1       cgd 	struct stat stb;
    284   1.8       jtc 	double avenrun[3];
    285   1.8       jtc 	time_t now;
    286   1.1       cgd 	int cc;
    287   1.1       cgd 
    288   1.8       jtc 	now = time(NULL);
    289   1.1       cgd 	if (alarmcount % 10 == 0)
    290   1.8       jtc 		getboottime(0);
    291   1.1       cgd 	alarmcount++;
    292  1.14       mrg 	(void)fstat(utmpf, &stb);
    293   1.1       cgd 	if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
    294   1.1       cgd 		utmptime = stb.st_mtime;
    295   1.1       cgd 		if (stb.st_size > utmpsize) {
    296   1.1       cgd 			utmpsize = stb.st_size + 10 * sizeof(struct utmp);
    297   1.1       cgd 			if (utmp)
    298   1.1       cgd 				utmp = (struct utmp *)realloc(utmp, utmpsize);
    299   1.1       cgd 			else
    300   1.1       cgd 				utmp = (struct utmp *)malloc(utmpsize);
    301   1.1       cgd 			if (! utmp) {
    302  1.12     lukem 				warn("malloc failed");
    303   1.1       cgd 				utmpsize = 0;
    304   1.1       cgd 				goto done;
    305   1.1       cgd 			}
    306   1.1       cgd 		}
    307  1.10    kleink 		(void)lseek(utmpf, (off_t)0, SEEK_SET);
    308   1.1       cgd 		cc = read(utmpf, (char *)utmp, stb.st_size);
    309   1.1       cgd 		if (cc < 0) {
    310  1.12     lukem 			warn("%s", _PATH_UTMP);
    311   1.1       cgd 			goto done;
    312   1.1       cgd 		}
    313   1.8       jtc 		wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1];
    314   1.8       jtc 		utmpent = cc / sizeof(struct utmp);
    315   1.1       cgd 		for (i = 0; i < utmpent; i++)
    316   1.1       cgd 			if (utmp[i].ut_name[0]) {
    317   1.8       jtc 				memcpy(we->we_utmp.out_line, utmp[i].ut_line,
    318   1.8       jtc 				   sizeof(utmp[i].ut_line));
    319   1.8       jtc 				memcpy(we->we_utmp.out_name, utmp[i].ut_name,
    320   1.8       jtc 				   sizeof(utmp[i].ut_name));
    321   1.1       cgd 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
    322   1.1       cgd 				if (we >= wlast)
    323   1.1       cgd 					break;
    324   1.1       cgd 				we++;
    325   1.1       cgd 			}
    326   1.1       cgd 		utmpent = we - mywd.wd_we;
    327   1.1       cgd 	}
    328   1.1       cgd 
    329   1.1       cgd 	/*
    330   1.1       cgd 	 * The test on utmpent looks silly---after all, if no one is
    331   1.1       cgd 	 * logged on, why worry about efficiency?---but is useful on
    332   1.1       cgd 	 * (e.g.) compute servers.
    333   1.1       cgd 	 */
    334   1.1       cgd 	if (utmpent && chdir(_PATH_DEV)) {
    335   1.1       cgd 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
    336   1.1       cgd 		exit(1);
    337   1.1       cgd 	}
    338   1.1       cgd 	we = mywd.wd_we;
    339   1.1       cgd 	for (i = 0; i < utmpent; i++) {
    340   1.1       cgd 		if (stat(we->we_utmp.out_line, &stb) >= 0)
    341   1.1       cgd 			we->we_idle = htonl(now - stb.st_atime);
    342   1.1       cgd 		we++;
    343   1.1       cgd 	}
    344   1.8       jtc 	(void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
    345   1.1       cgd 	for (i = 0; i < 3; i++)
    346   1.1       cgd 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
    347   1.1       cgd 	cc = (char *)we - (char *)&mywd;
    348   1.1       cgd 	mywd.wd_sendtime = htonl(time(0));
    349   1.1       cgd 	mywd.wd_vers = WHODVERSION;
    350   1.1       cgd 	mywd.wd_type = WHODTYPE_STATUS;
    351   1.1       cgd 	for (np = neighbors; np != NULL; np = np->n_next)
    352   1.8       jtc 		(void)sendto(s, (char *)&mywd, cc, 0,
    353   1.8       jtc 				np->n_addr, np->n_addrlen);
    354   1.1       cgd 	if (utmpent && chdir(_PATH_RWHODIR)) {
    355   1.1       cgd 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
    356   1.1       cgd 		exit(1);
    357   1.1       cgd 	}
    358   1.1       cgd done:
    359  1.14       mrg 	(void)alarm(AL_INTERVAL);
    360   1.1       cgd }
    361   1.1       cgd 
    362   1.1       cgd void
    363   1.8       jtc getboottime(signo)
    364   1.8       jtc 	int signo;
    365   1.1       cgd {
    366   1.8       jtc 	int mib[2];
    367   1.8       jtc 	size_t size;
    368   1.8       jtc 	struct timeval tm;
    369   1.8       jtc 
    370   1.8       jtc 	mib[0] = CTL_KERN;
    371   1.8       jtc 	mib[1] = KERN_BOOTTIME;
    372   1.8       jtc 	size = sizeof(tm);
    373   1.8       jtc 	if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) {
    374   1.8       jtc 		syslog(LOG_ERR, "cannot get boottime: %m");
    375   1.8       jtc 		exit(1);
    376   1.8       jtc 	}
    377   1.8       jtc 	mywd.wd_boottime = htonl(tm.tv_sec);
    378   1.8       jtc }
    379   1.8       jtc 
    380   1.8       jtc void
    381   1.8       jtc quit(msg)
    382   1.8       jtc 	char *msg;
    383   1.8       jtc {
    384   1.8       jtc 	syslog(LOG_ERR, msg);
    385   1.8       jtc 	exit(1);
    386   1.8       jtc }
    387   1.8       jtc 
    388   1.8       jtc #define ROUNDUP(a) \
    389   1.8       jtc 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
    390   1.8       jtc #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
    391   1.8       jtc 
    392   1.8       jtc void
    393   1.8       jtc rt_xaddrs(cp, cplim, rtinfo)
    394  1.12     lukem 	caddr_t cp, cplim;
    395  1.12     lukem 	struct rt_addrinfo *rtinfo;
    396   1.8       jtc {
    397  1.12     lukem 	struct sockaddr *sa;
    398  1.12     lukem 	int i;
    399   1.8       jtc 
    400   1.8       jtc 	memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
    401   1.8       jtc 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
    402   1.8       jtc 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
    403   1.8       jtc 			continue;
    404   1.8       jtc 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
    405   1.8       jtc 		ADVANCE(cp, sa);
    406   1.8       jtc 	}
    407   1.1       cgd }
    408   1.1       cgd 
    409   1.1       cgd /*
    410   1.1       cgd  * Figure out device configuration and select
    411   1.1       cgd  * networks which deserve status information.
    412   1.1       cgd  */
    413   1.8       jtc int
    414   1.1       cgd configure(s)
    415   1.1       cgd 	int s;
    416   1.1       cgd {
    417  1.12     lukem 	struct neighbor *np;
    418  1.12     lukem 	struct if_msghdr *ifm;
    419  1.12     lukem 	struct ifa_msghdr *ifam;
    420   1.8       jtc 	struct sockaddr_dl *sdl;
    421   1.8       jtc 	size_t needed;
    422   1.8       jtc 	int mib[6], flags = 0, len;
    423   1.8       jtc 	char *buf, *lim, *next;
    424   1.8       jtc 	struct rt_addrinfo info;
    425   1.8       jtc 
    426   1.8       jtc 	mib[0] = CTL_NET;
    427   1.8       jtc 	mib[1] = PF_ROUTE;
    428   1.8       jtc 	mib[2] = 0;
    429   1.8       jtc 	mib[3] = AF_INET;
    430   1.8       jtc 	mib[4] = NET_RT_IFLIST;
    431   1.8       jtc 	mib[5] = 0;
    432   1.8       jtc 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    433   1.8       jtc 		quit("route-sysctl-estimate");
    434   1.8       jtc 	if ((buf = malloc(needed)) == NULL)
    435   1.8       jtc 		quit("malloc");
    436   1.8       jtc 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    437   1.8       jtc 		quit("actual retrieval of interface table");
    438   1.8       jtc 	lim = buf + needed;
    439   1.8       jtc 
    440   1.8       jtc 	sdl = NULL;		/* XXX just to keep gcc -Wall happy */
    441   1.8       jtc 	for (next = buf; next < lim; next += ifm->ifm_msglen) {
    442   1.8       jtc 		ifm = (struct if_msghdr *)next;
    443   1.8       jtc 		if (ifm->ifm_type == RTM_IFINFO) {
    444   1.8       jtc 			sdl = (struct sockaddr_dl *)(ifm + 1);
    445   1.8       jtc 			flags = ifm->ifm_flags;
    446   1.8       jtc 			continue;
    447   1.8       jtc 		}
    448   1.8       jtc 		if ((flags & IFF_UP) == 0 ||
    449   1.8       jtc 		    (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)
    450   1.8       jtc 			continue;
    451   1.8       jtc 		if (ifm->ifm_type != RTM_NEWADDR)
    452   1.8       jtc 			quit("out of sync parsing NET_RT_IFLIST");
    453   1.8       jtc 		ifam = (struct ifa_msghdr *)ifm;
    454   1.8       jtc 		info.rti_addrs = ifam->ifam_addrs;
    455   1.8       jtc 		rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
    456   1.8       jtc 			&info);
    457   1.8       jtc 		/* gag, wish we could get rid of Internet dependencies */
    458   1.8       jtc #define dstaddr	info.rti_info[RTAX_BRD]
    459   1.8       jtc #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
    460   1.8       jtc #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
    461   1.8       jtc 		if (dstaddr == 0 || dstaddr->sa_family != AF_INET)
    462   1.8       jtc 			continue;
    463   1.8       jtc 		PORT_SA(dstaddr) = sp->s_port;
    464   1.1       cgd 		for (np = neighbors; np != NULL; np = np->n_next)
    465   1.8       jtc 			if (memcmp(sdl->sdl_data, np->n_name,
    466   1.8       jtc 				   sdl->sdl_nlen) == 0 &&
    467   1.8       jtc 			    IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr))
    468   1.1       cgd 				break;
    469   1.1       cgd 		if (np != NULL)
    470   1.1       cgd 			continue;
    471   1.8       jtc 		len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1;
    472   1.8       jtc 		np = (struct neighbor *)malloc(len);
    473   1.1       cgd 		if (np == NULL)
    474   1.8       jtc 			quit("malloc of neighbor structure");
    475   1.8       jtc 		memset(np, 0, len);
    476   1.8       jtc 		np->n_flags = flags;
    477   1.8       jtc 		np->n_addr = (struct sockaddr *)(np + 1);
    478   1.8       jtc 		np->n_addrlen = dstaddr->sa_len;
    479   1.8       jtc 		np->n_name = np->n_addrlen + (char *)np->n_addr;
    480   1.1       cgd 		np->n_next = neighbors;
    481   1.1       cgd 		neighbors = np;
    482   1.8       jtc 		memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen);
    483   1.8       jtc 		memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen);
    484   1.1       cgd 	}
    485   1.8       jtc 	free(buf);
    486   1.1       cgd 	return (1);
    487   1.1       cgd }
    488   1.1       cgd 
    489   1.1       cgd #ifdef DEBUG
    490   1.8       jtc void
    491   1.8       jtc Sendto(s, buf, cc, flags, to, tolen)
    492   1.1       cgd 	int s;
    493   1.1       cgd 	char *buf;
    494   1.1       cgd 	int cc, flags;
    495   1.1       cgd 	char *to;
    496   1.1       cgd 	int tolen;
    497   1.1       cgd {
    498  1.12     lukem 	struct whod *w = (struct whod *)buf;
    499  1.12     lukem 	struct whoent *we;
    500   1.8       jtc 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
    501   1.1       cgd 
    502   1.8       jtc 	printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
    503   1.1       cgd 	printf("hostname %s %s\n", w->wd_hostname,
    504   1.1       cgd 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
    505   1.1       cgd 	printf("load %4.2f, %4.2f, %4.2f\n",
    506   1.1       cgd 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
    507   1.1       cgd 	    ntohl(w->wd_loadav[2]) / 100.0);
    508   1.1       cgd 	cc -= WHDRSIZE;
    509   1.8       jtc 	for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) {
    510   1.1       cgd 		time_t t = ntohl(we->we_utmp.out_time);
    511   1.1       cgd 		printf("%-8.8s %s:%s %.12s",
    512   1.1       cgd 			we->we_utmp.out_name,
    513   1.1       cgd 			w->wd_hostname, we->we_utmp.out_line,
    514   1.1       cgd 			ctime(&t)+4);
    515   1.1       cgd 		we->we_idle = ntohl(we->we_idle) / 60;
    516   1.1       cgd 		if (we->we_idle) {
    517   1.1       cgd 			if (we->we_idle >= 100*60)
    518   1.1       cgd 				we->we_idle = 100*60 - 1;
    519   1.1       cgd 			if (we->we_idle >= 60)
    520   1.1       cgd 				printf(" %2d", we->we_idle / 60);
    521   1.1       cgd 			else
    522   1.1       cgd 				printf("   ");
    523   1.1       cgd 			printf(":%02d", we->we_idle % 60);
    524   1.1       cgd 		}
    525   1.1       cgd 		printf("\n");
    526   1.1       cgd 	}
    527   1.1       cgd }
    528   1.1       cgd 
    529   1.1       cgd char *
    530   1.1       cgd interval(time, updown)
    531   1.1       cgd 	int time;
    532   1.1       cgd 	char *updown;
    533   1.1       cgd {
    534   1.1       cgd 	static char resbuf[32];
    535   1.1       cgd 	int days, hours, minutes;
    536   1.1       cgd 
    537   1.1       cgd 	if (time < 0 || time > 3*30*24*60*60) {
    538  1.14       mrg 		(void)sprintf(resbuf, "   %s ??:??", updown);
    539   1.1       cgd 		return (resbuf);
    540   1.1       cgd 	}
    541   1.1       cgd 	minutes = (time + 59) / 60;		/* round to minutes */
    542   1.1       cgd 	hours = minutes / 60; minutes %= 60;
    543   1.1       cgd 	days = hours / 24; hours %= 24;
    544   1.1       cgd 	if (days)
    545  1.14       mrg 		(void)sprintf(resbuf, "%s %2d+%02d:%02d",
    546   1.1       cgd 		    updown, days, hours, minutes);
    547   1.1       cgd 	else
    548  1.14       mrg 		(void)sprintf(resbuf, "%s    %2d:%02d",
    549   1.1       cgd 		    updown, hours, minutes);
    550   1.1       cgd 	return (resbuf);
    551   1.1       cgd }
    552   1.1       cgd #endif
    553