xref: /csrg-svn/usr.sbin/rwhod/rwhod.c (revision 60119)
122549Sdist /*
235392Sbostic  * Copyright (c) 1983 The Regents of the University of California.
335392Sbostic  * All rights reserved.
435392Sbostic  *
542819Sbostic  * %sccs.include.redist.c%
622549Sdist  */
722549Sdist 
86460Swnj #ifndef lint
922549Sdist char copyright[] =
1035392Sbostic "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
1122549Sdist  All rights reserved.\n";
1235392Sbostic #endif /* not lint */
136460Swnj 
1422549Sdist #ifndef lint
15*60119Storek static char sccsid[] = "@(#)rwhod.c	5.25 (Berkeley) 05/18/93";
1635392Sbostic #endif /* not lint */
1722549Sdist 
1842265Skarels #include <sys/param.h>
199215Ssam #include <sys/socket.h>
209215Ssam #include <sys/stat.h>
2137295Sbostic #include <sys/signal.h>
229215Ssam #include <sys/ioctl.h>
2358922Smckusick #include <sys/sysctl.h>
249215Ssam 
2512236Ssam #include <net/if.h>
2652574Ssklower #include <net/if_dl.h>
2752574Ssklower #include <net/route.h>
289215Ssam #include <netinet/in.h>
2957744Sbostic #include <protocols/rwhod.h>
309215Ssam 
3157744Sbostic #include <ctype.h>
326460Swnj #include <errno.h>
3357744Sbostic #include <fcntl.h>
348486Ssam #include <netdb.h>
3557744Sbostic #include <nlist.h>
3657744Sbostic #include <paths.h>
3757744Sbostic #include <stdio.h>
3857744Sbostic #include <stdlib.h>
3957744Sbostic #include <string.h>
4016673Sralph #include <syslog.h>
4157739Ssklower #include <unistd.h>
4257744Sbostic #include <utmp.h>
436460Swnj 
4415541Sralph /*
4515541Sralph  * Alarm interval. Don't forget to change the down time check in ruptime
4615541Sralph  * if this is changed.
4715541Sralph  */
4816673Sralph #define AL_INTERVAL (3 * 60)
4915541Sralph 
5042265Skarels char	myname[MAXHOSTNAMELEN];
516460Swnj 
526460Swnj struct	nlist nl[] = {
5338182Smckusick #define	NL_BOOTTIME	0
549215Ssam 	{ "_boottime" },
55*60119Storek 	{ 0 }
566460Swnj };
576460Swnj 
5812236Ssam /*
5957744Sbostic  * We communicate with each neighbor in a list constructed at the time we're
6057744Sbostic  * started up.  Neighbors are currently directly connected via a hardware
6157744Sbostic  * interface.
6212236Ssam  */
6312236Ssam struct	neighbor {
6412236Ssam 	struct	neighbor *n_next;
6512236Ssam 	char	*n_name;		/* interface name */
6652574Ssklower 	struct	sockaddr *n_addr;		/* who to send to */
6712236Ssam 	int	n_addrlen;		/* size of address */
6812236Ssam 	int	n_flags;		/* should forward?, interface flags */
6912236Ssam };
7012236Ssam 
7112236Ssam struct	neighbor *neighbors;
726460Swnj struct	whod mywd;
7312236Ssam struct	servent *sp;
746460Swnj int	s, utmpf, kmemf = -1;
756460Swnj 
76*60119Storek #define	WHDRSIZE	(sizeof(mywd) - sizeof(mywd.wd_we))
7711834Ssam 
7857744Sbostic int	 configure __P((int));
7957744Sbostic void	 getkmem __P((int));
8057744Sbostic void	 onalrm __P((int));
8157744Sbostic void	 quit __P((char *));
8257744Sbostic void	 rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
8357744Sbostic int	 verify __P((char *));
8457744Sbostic #ifdef DEBUG
8557744Sbostic char	*interval __P((int, char *));
86*60119Storek void	 Sendto __P((int, char *, int, int, char *, int));
87*60119Storek #define	 sendto Sendto
8857744Sbostic #endif
896460Swnj 
9057744Sbostic int
9157744Sbostic main(argc, argv)
9257744Sbostic 	int argc;
9357744Sbostic 	char argv[];
946460Swnj {
956460Swnj 	struct sockaddr_in from;
9626016Skarels 	struct stat st;
976460Swnj 	char path[64];
9826484Smckusick 	int on = 1;
9957744Sbostic 	char *cp;
100*60119Storek 	struct sockaddr_in sin;
1016460Swnj 
10216673Sralph 	if (getuid()) {
10316673Sralph 		fprintf(stderr, "rwhod: not super user\n");
10416673Sralph 		exit(1);
10516673Sralph 	}
1068486Ssam 	sp = getservbyname("who", "udp");
10757744Sbostic 	if (sp == NULL) {
1088486Ssam 		fprintf(stderr, "rwhod: udp/who: unknown service\n");
1098486Ssam 		exit(1);
1108486Ssam 	}
1116460Swnj #ifndef DEBUG
11244707Skarels 	daemon(1, 0);
1136460Swnj #endif
11437295Sbostic 	if (chdir(_PATH_RWHODIR) < 0) {
11537295Sbostic 		(void)fprintf(stderr, "rwhod: %s: %s\n",
11637295Sbostic 		    _PATH_RWHODIR, strerror(errno));
11726011Smckusick 		exit(1);
11826011Smckusick 	}
1196460Swnj 	(void) signal(SIGHUP, getkmem);
12024853Seric 	openlog("rwhod", LOG_PID, LOG_DAEMON);
12112236Ssam 	/*
12212236Ssam 	 * Establish host name as returned by system.
12312236Ssam 	 */
124*60119Storek 	if (gethostname(myname, sizeof(myname) - 1) < 0) {
12516673Sralph 		syslog(LOG_ERR, "gethostname: %m");
1266460Swnj 		exit(1);
1276460Swnj 	}
12824745Sbloom 	if ((cp = index(myname, '.')) != NULL)
12924745Sbloom 		*cp = '\0';
130*60119Storek 	strncpy(mywd.wd_hostname, myname, sizeof(myname) - 1);
13137295Sbostic 	utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
1326460Swnj 	if (utmpf < 0) {
13337295Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
1346460Swnj 		exit(1);
1356460Swnj 	}
13657744Sbostic 	getkmem(0);
13713187Ssam 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
13816673Sralph 		syslog(LOG_ERR, "socket: %m");
1399215Ssam 		exit(1);
1406460Swnj 	}
141*60119Storek 	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
14217055Skarels 		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
14317055Skarels 		exit(1);
14417055Skarels 	}
145*60119Storek 	memset(&sin, 0, sizeof(sin));
14642265Skarels 	sin.sin_family = AF_INET;
14712236Ssam 	sin.sin_port = sp->s_port;
148*60119Storek 	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
14916673Sralph 		syslog(LOG_ERR, "bind: %m");
1509215Ssam 		exit(1);
1519215Ssam 	}
15212236Ssam 	if (!configure(s))
15312236Ssam 		exit(1);
15413027Ssam 	signal(SIGALRM, onalrm);
15557744Sbostic 	onalrm(0);
1566460Swnj 	for (;;) {
1576460Swnj 		struct whod wd;
158*60119Storek 		int cc, whod, len = sizeof(from);
1596460Swnj 
160*60119Storek 		cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0,
16146927Sbostic 			(struct sockaddr *)&from, &len);
1626460Swnj 		if (cc <= 0) {
1636460Swnj 			if (cc < 0 && errno != EINTR)
16416673Sralph 				syslog(LOG_WARNING, "recv: %m");
1656460Swnj 			continue;
1666460Swnj 		}
1678486Ssam 		if (from.sin_port != sp->s_port) {
16816673Sralph 			syslog(LOG_WARNING, "%d: bad from port",
1698486Ssam 				ntohs(from.sin_port));
1706460Swnj 			continue;
1716460Swnj 		}
17212347Ssam 		if (wd.wd_vers != WHODVERSION)
17312347Ssam 			continue;
17412236Ssam 		if (wd.wd_type != WHODTYPE_STATUS)
17512236Ssam 			continue;
1768486Ssam 		if (!verify(wd.wd_hostname)) {
17716673Sralph 			syslog(LOG_WARNING, "malformed host name from %x",
1788486Ssam 				from.sin_addr);
1798486Ssam 			continue;
1808486Ssam 		}
18126011Smckusick 		(void) sprintf(path, "whod.%s", wd.wd_hostname);
18226016Skarels 		/*
18326016Skarels 		 * Rather than truncating and growing the file each time,
18426016Skarels 		 * use ftruncate if size is less than previous size.
18526016Skarels 		 */
18626016Skarels 		whod = open(path, O_WRONLY | O_CREAT, 0644);
1876460Swnj 		if (whod < 0) {
18816673Sralph 			syslog(LOG_WARNING, "%s: %m", path);
1896460Swnj 			continue;
1906460Swnj 		}
19142265Skarels #if ENDIAN != BIG_ENDIAN
19211834Ssam 		{
19313187Ssam 			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
19411834Ssam 			struct whoent *we;
19511834Ssam 
19611834Ssam 			/* undo header byte swapping before writing to file */
19711834Ssam 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
19811834Ssam 			for (i = 0; i < 3; i++)
19911834Ssam 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
20011834Ssam 			wd.wd_boottime = ntohl(wd.wd_boottime);
20111834Ssam 			we = wd.wd_we;
20211834Ssam 			for (i = 0; i < n; i++) {
20311834Ssam 				we->we_idle = ntohl(we->we_idle);
20412872Ssam 				we->we_utmp.out_time =
20512872Ssam 				    ntohl(we->we_utmp.out_time);
20611834Ssam 				we++;
20711834Ssam 			}
20811834Ssam 		}
20911834Ssam #endif
21046927Sbostic 		(void) time((time_t *)&wd.wd_recvtime);
2116460Swnj 		(void) write(whod, (char *)&wd, cc);
21226016Skarels 		if (fstat(whod, &st) < 0 || st.st_size > cc)
21326016Skarels 			ftruncate(whod, cc);
2146460Swnj 		(void) close(whod);
2156460Swnj 	}
2166460Swnj }
2176460Swnj 
2188486Ssam /*
2198486Ssam  * Check out host name for unprintables
2208486Ssam  * and other funnies before allowing a file
2218486Ssam  * to be created.  Sorry, but blanks aren't allowed.
2228486Ssam  */
22357744Sbostic int
2248486Ssam verify(name)
2258486Ssam 	register char *name;
2268486Ssam {
2278486Ssam 	register int size = 0;
2288486Ssam 
2298486Ssam 	while (*name) {
23015214Sleres 		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
2318486Ssam 			return (0);
2328486Ssam 		name++, size++;
2338486Ssam 	}
2348486Ssam 	return (size > 0);
2358486Ssam }
2368486Ssam 
2376460Swnj int	utmptime;
2386460Swnj int	utmpent;
23922546Sbloom int	utmpsize = 0;
24022546Sbloom struct	utmp *utmp;
2416460Swnj int	alarmcount;
2426460Swnj 
24346927Sbostic void
24457744Sbostic onalrm(signo)
24557744Sbostic 	int signo;
2466460Swnj {
24737295Sbostic 	register struct neighbor *np;
24837295Sbostic 	register struct whoent *we = mywd.wd_we, *wlast;
2496460Swnj 	register int i;
2506460Swnj 	struct stat stb;
25157744Sbostic 	double avenrun[3];
25257744Sbostic 	time_t now;
2536460Swnj 	int cc;
2546460Swnj 
25557744Sbostic 	now = time(NULL);
2566460Swnj 	if (alarmcount % 10 == 0)
25757744Sbostic 		getkmem(0);
2586460Swnj 	alarmcount++;
2596460Swnj 	(void) fstat(utmpf, &stb);
26022546Sbloom 	if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
26117303Stef 		utmptime = stb.st_mtime;
26222546Sbloom 		if (stb.st_size > utmpsize) {
26322546Sbloom 			utmpsize = stb.st_size + 10 * sizeof(struct utmp);
26422546Sbloom 			if (utmp)
26522546Sbloom 				utmp = (struct utmp *)realloc(utmp, utmpsize);
26622546Sbloom 			else
26722546Sbloom 				utmp = (struct utmp *)malloc(utmpsize);
26822546Sbloom 			if (! utmp) {
26922546Sbloom 				fprintf(stderr, "rwhod: malloc failed\n");
27022546Sbloom 				utmpsize = 0;
27122546Sbloom 				goto done;
27222546Sbloom 			}
27322546Sbloom 		}
27457739Ssklower 		(void) lseek(utmpf, (off_t)0, L_SET);
27522546Sbloom 		cc = read(utmpf, (char *)utmp, stb.st_size);
2766460Swnj 		if (cc < 0) {
27737295Sbostic 			fprintf(stderr, "rwhod: %s: %s\n",
27837295Sbostic 			    _PATH_UTMP, strerror(errno));
27913469Ssam 			goto done;
2806460Swnj 		}
281*60119Storek 		wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1];
282*60119Storek 		utmpent = cc / sizeof(struct utmp);
2836460Swnj 		for (i = 0; i < utmpent; i++)
2846460Swnj 			if (utmp[i].ut_name[0]) {
285*60119Storek 				memcpy(we->we_utmp.out_line, utmp[i].ut_line,
286*60119Storek 				   sizeof(utmp[i].ut_line));
287*60119Storek 				memcpy(we->we_utmp.out_name, utmp[i].ut_name,
288*60119Storek 				   sizeof(utmp[i].ut_name));
28912807Ssam 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
2906577Ssam 				if (we >= wlast)
2916577Ssam 					break;
2926460Swnj 				we++;
2936460Swnj 			}
2946460Swnj 		utmpent = we - mywd.wd_we;
2956460Swnj 	}
29626484Smckusick 
29726484Smckusick 	/*
29826484Smckusick 	 * The test on utmpent looks silly---after all, if no one is
29926484Smckusick 	 * logged on, why worry about efficiency?---but is useful on
30026484Smckusick 	 * (e.g.) compute servers.
30126484Smckusick 	 */
30237971Sbostic 	if (utmpent && chdir(_PATH_DEV)) {
30337971Sbostic 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
30426484Smckusick 		exit(1);
30526484Smckusick 	}
3066460Swnj 	we = mywd.wd_we;
3076460Swnj 	for (i = 0; i < utmpent; i++) {
30826484Smckusick 		if (stat(we->we_utmp.out_line, &stb) >= 0)
30912807Ssam 			we->we_idle = htonl(now - stb.st_atime);
3106460Swnj 		we++;
3116460Swnj 	}
312*60119Storek 	(void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
3136460Swnj 	for (i = 0; i < 3; i++)
31412873Ssam 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
3156460Swnj 	cc = (char *)we - (char *)&mywd;
31612807Ssam 	mywd.wd_sendtime = htonl(time(0));
31712236Ssam 	mywd.wd_vers = WHODVERSION;
31812236Ssam 	mywd.wd_type = WHODTYPE_STATUS;
31912236Ssam 	for (np = neighbors; np != NULL; np = np->n_next)
320*60119Storek 		(void)sendto(s, (char *)&mywd, cc, 0,
32152574Ssklower 				np->n_addr, np->n_addrlen);
32237295Sbostic 	if (utmpent && chdir(_PATH_RWHODIR)) {
32337295Sbostic 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
32426484Smckusick 		exit(1);
32526484Smckusick 	}
32613469Ssam done:
32715541Sralph 	(void) alarm(AL_INTERVAL);
3286460Swnj }
3296460Swnj 
33046927Sbostic void
33157744Sbostic getkmem(signo)
33257744Sbostic 	int signo;
3336460Swnj {
33416663Ssam 	static ino_t vmunixino;
33516663Ssam 	static time_t vmunixctime;
33616663Ssam 	struct stat sb;
3376460Swnj 
33837295Sbostic 	if (stat(_PATH_UNIX, &sb) < 0) {
33916663Ssam 		if (vmunixctime)
34016663Ssam 			return;
34116663Ssam 	} else {
34216663Ssam 		if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
34316663Ssam 			return;
34416663Ssam 		vmunixctime = sb.st_ctime;
345*60119Storek 		vmunixino = sb.st_ino;
34616663Ssam 	}
3476460Swnj 	if (kmemf >= 0)
348*60119Storek 		(void)close(kmemf);
3496460Swnj loop:
35037295Sbostic 	if (nlist(_PATH_UNIX, nl)) {
35137295Sbostic 		syslog(LOG_WARNING, "%s: namelist botch", _PATH_UNIX);
3526460Swnj 		sleep(300);
3536460Swnj 		goto loop;
3546460Swnj 	}
35537295Sbostic 	kmemf = open(_PATH_KMEM, O_RDONLY, 0);
3566460Swnj 	if (kmemf < 0) {
35737295Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_KMEM);
35816673Sralph 		exit(1);
3596460Swnj 	}
36057739Ssklower 	(void) lseek(kmemf, (off_t)nl[NL_BOOTTIME].n_value, L_SET);
36113187Ssam 	(void) read(kmemf, (char *)&mywd.wd_boottime,
362*60119Storek 	    sizeof(mywd.wd_boottime));
36312807Ssam 	mywd.wd_boottime = htonl(mywd.wd_boottime);
3646460Swnj }
36512236Ssam 
36652574Ssklower void
36752574Ssklower quit(msg)
36857744Sbostic 	char *msg;
36952574Ssklower {
37052574Ssklower 	syslog(LOG_ERR, msg);
37152574Ssklower 	exit(1);
37252574Ssklower }
37352574Ssklower 
37452574Ssklower #define ROUNDUP(a) \
37552574Ssklower 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
37652574Ssklower #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
37752574Ssklower 
37852574Ssklower void
37952574Ssklower rt_xaddrs(cp, cplim, rtinfo)
38052574Ssklower 	register caddr_t cp, cplim;
38152574Ssklower 	register struct rt_addrinfo *rtinfo;
38252574Ssklower {
38352574Ssklower 	register struct sockaddr *sa;
38452574Ssklower 	register int i;
38552574Ssklower 
386*60119Storek 	memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
38752574Ssklower 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
38852574Ssklower 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
38952574Ssklower 			continue;
39052574Ssklower 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
39152574Ssklower 		ADVANCE(cp, sa);
39252574Ssklower 	}
39352574Ssklower }
39452574Ssklower 
39512236Ssam /*
39612236Ssam  * Figure out device configuration and select
39712236Ssam  * networks which deserve status information.
39812236Ssam  */
39957744Sbostic int
40012236Ssam configure(s)
40112236Ssam 	int s;
40212236Ssam {
40352574Ssklower 	register struct neighbor *np;
40452574Ssklower 	register struct if_msghdr *ifm;
40552574Ssklower 	register struct ifa_msghdr *ifam;
40652574Ssklower 	struct sockaddr_dl *sdl;
40758922Smckusick 	size_t needed;
40858922Smckusick 	int mib[6], flags = 0, len;
40952574Ssklower 	char *buf, *lim, *next;
41052574Ssklower 	struct rt_addrinfo info;
41112236Ssam 
41258922Smckusick 	mib[0] = CTL_NET;
41358922Smckusick 	mib[1] = PF_ROUTE;
41458922Smckusick 	mib[2] = 0;
41558922Smckusick 	mib[3] = AF_INET;
41658922Smckusick 	mib[4] = NET_RT_IFLIST;
41758922Smckusick 	mib[5] = 0;
41858922Smckusick 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
41958922Smckusick 		quit("route-sysctl-estimate");
42052574Ssklower 	if ((buf = malloc(needed)) == NULL)
42152574Ssklower 		quit("malloc");
42258922Smckusick 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
42352574Ssklower 		quit("actual retrieval of interface table");
42458922Smckusick 	lim = buf + needed;
42552574Ssklower 
426*60119Storek 	sdl = NULL;		/* XXX just to keep gcc -Wall happy */
42752574Ssklower 	for (next = buf; next < lim; next += ifm->ifm_msglen) {
42852574Ssklower 		ifm = (struct if_msghdr *)next;
42952574Ssklower 		if (ifm->ifm_type == RTM_IFINFO) {
43052574Ssklower 			sdl = (struct sockaddr_dl *)(ifm + 1);
43152574Ssklower 			flags = ifm->ifm_flags;
43252574Ssklower 			continue;
43352574Ssklower 		}
43452574Ssklower 		if ((flags & IFF_UP) == 0 ||
43552574Ssklower 		    (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)
43652574Ssklower 			continue;
43752574Ssklower 		if (ifm->ifm_type != RTM_NEWADDR)
43858922Smckusick 			quit("out of sync parsing NET_RT_IFLIST");
43952574Ssklower 		ifam = (struct ifa_msghdr *)ifm;
44052574Ssklower 		info.rti_addrs = ifam->ifam_addrs;
44152574Ssklower 		rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
44252574Ssklower 			&info);
44352574Ssklower 		/* gag, wish we could get rid of Internet dependencies */
44452574Ssklower #define dstaddr	info.rti_info[RTAX_BRD]
44552574Ssklower #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
44652574Ssklower #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
44752574Ssklower 		if (dstaddr == 0 || dstaddr->sa_family != AF_INET)
44852574Ssklower 			continue;
44952574Ssklower 		PORT_SA(dstaddr) = sp->s_port;
45012236Ssam 		for (np = neighbors; np != NULL; np = np->n_next)
451*60119Storek 			if (memcmp(sdl->sdl_data, np->n_name,
452*60119Storek 				   sdl->sdl_nlen) == 0 &&
453*60119Storek 			    IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr))
45412236Ssam 				break;
45512236Ssam 		if (np != NULL)
45612236Ssam 			continue;
45752574Ssklower 		len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1;
45852574Ssklower 		np = (struct neighbor *)malloc(len);
45912236Ssam 		if (np == NULL)
46052574Ssklower 			quit("malloc of neighbor structure");
461*60119Storek 		memset(np, 0, len);
46252574Ssklower 		np->n_flags = flags;
46352574Ssklower 		np->n_addr = (struct sockaddr *)(np + 1);
46452574Ssklower 		np->n_addrlen = dstaddr->sa_len;
46552574Ssklower 		np->n_name = np->n_addrlen + (char *)np->n_addr;
46612236Ssam 		np->n_next = neighbors;
46712236Ssam 		neighbors = np;
468*60119Storek 		memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen);
469*60119Storek 		memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen);
47012236Ssam 	}
47152574Ssklower 	free(buf);
47212236Ssam 	return (1);
47312236Ssam }
47412807Ssam 
47512807Ssam #ifdef DEBUG
47657744Sbostic void
477*60119Storek Sendto(s, buf, cc, flags, to, tolen)
47812807Ssam 	int s;
47912807Ssam 	char *buf;
48012807Ssam 	int cc, flags;
48112807Ssam 	char *to;
48212807Ssam 	int tolen;
48312807Ssam {
48412807Ssam 	register struct whod *w = (struct whod *)buf;
48512807Ssam 	register struct whoent *we;
48612807Ssam 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
48712807Ssam 
48812807Ssam 	printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
48912807Ssam 	printf("hostname %s %s\n", w->wd_hostname,
49013187Ssam 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
49112807Ssam 	printf("load %4.2f, %4.2f, %4.2f\n",
49213187Ssam 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
49313187Ssam 	    ntohl(w->wd_loadav[2]) / 100.0);
49412807Ssam 	cc -= WHDRSIZE;
495*60119Storek 	for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) {
49613187Ssam 		time_t t = ntohl(we->we_utmp.out_time);
49712807Ssam 		printf("%-8.8s %s:%s %.12s",
49813187Ssam 			we->we_utmp.out_name,
49913187Ssam 			w->wd_hostname, we->we_utmp.out_line,
50013187Ssam 			ctime(&t)+4);
50113187Ssam 		we->we_idle = ntohl(we->we_idle) / 60;
50212807Ssam 		if (we->we_idle) {
50312807Ssam 			if (we->we_idle >= 100*60)
50412807Ssam 				we->we_idle = 100*60 - 1;
50512807Ssam 			if (we->we_idle >= 60)
50612807Ssam 				printf(" %2d", we->we_idle / 60);
50712807Ssam 			else
50812807Ssam 				printf("   ");
50912807Ssam 			printf(":%02d", we->we_idle % 60);
51012807Ssam 		}
51112807Ssam 		printf("\n");
51212807Ssam 	}
51312807Ssam }
51412807Ssam 
51512807Ssam char *
51612807Ssam interval(time, updown)
51712807Ssam 	int time;
51812807Ssam 	char *updown;
51912807Ssam {
52012807Ssam 	static char resbuf[32];
52112807Ssam 	int days, hours, minutes;
52212807Ssam 
52312807Ssam 	if (time < 0 || time > 3*30*24*60*60) {
52412807Ssam 		(void) sprintf(resbuf, "   %s ??:??", updown);
52512807Ssam 		return (resbuf);
52612807Ssam 	}
52712807Ssam 	minutes = (time + 59) / 60;		/* round to minutes */
52812807Ssam 	hours = minutes / 60; minutes %= 60;
52912807Ssam 	days = hours / 24; hours %= 24;
53012807Ssam 	if (days)
53112807Ssam 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
53212807Ssam 		    updown, days, hours, minutes);
53312807Ssam 	else
53412807Ssam 		(void) sprintf(resbuf, "%s    %2d:%02d",
53512807Ssam 		    updown, hours, minutes);
53612807Ssam 	return (resbuf);
53712807Ssam }
53812807Ssam #endif
539