17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
23*740638c8Sbw * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/param.h>
377c478bd9Sstevel@tonic-gate #include <sys/socket.h>
387c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
417c478bd9Sstevel@tonic-gate #include <sys/file.h>
427c478bd9Sstevel@tonic-gate #include <sys/loadavg.h>
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate #include <net/if.h>
457c478bd9Sstevel@tonic-gate #include <netinet/in.h>
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate #include <stdio.h>
487c478bd9Sstevel@tonic-gate #include <signal.h>
497c478bd9Sstevel@tonic-gate #include <errno.h>
507c478bd9Sstevel@tonic-gate #include <utmpx.h>
517c478bd9Sstevel@tonic-gate #include <ctype.h>
527c478bd9Sstevel@tonic-gate #include <netdb.h>
537c478bd9Sstevel@tonic-gate #include <syslog.h>
547c478bd9Sstevel@tonic-gate #include <fcntl.h>
557c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h> /* for ENDIAN defines */
567c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
577c478bd9Sstevel@tonic-gate #include <protocols/rwhod.h>
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate #include <strings.h>
607c478bd9Sstevel@tonic-gate #include <stdlib.h>
617c478bd9Sstevel@tonic-gate #include <unistd.h>
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate * This version of Berkeley's rwhod has been modified to use IP multicast
657c478bd9Sstevel@tonic-gate * datagrams, under control of a new command-line option:
667c478bd9Sstevel@tonic-gate *
677c478bd9Sstevel@tonic-gate * rwhod -m causes rwhod to use IP multicast (instead of
687c478bd9Sstevel@tonic-gate * broadcast or unicast) on all interfaces that have
697c478bd9Sstevel@tonic-gate * the IFF_MULTICAST flag set in their "ifnet" structs
707c478bd9Sstevel@tonic-gate * (excluding the loopback interface). The multicast
717c478bd9Sstevel@tonic-gate * reports are sent with a time-to-live of 1, to prevent
727c478bd9Sstevel@tonic-gate * forwarding beyond the directly-connected subnet(s).
737c478bd9Sstevel@tonic-gate *
747c478bd9Sstevel@tonic-gate * rwhod -m <ttl> causes rwhod to send IP multicast datagrams with a
757c478bd9Sstevel@tonic-gate * time-to-live of <ttl>, via a SINGLE interface rather
767c478bd9Sstevel@tonic-gate * than all interfaces. <ttl> must be between 0 and
777c478bd9Sstevel@tonic-gate * MAX_MULTICAST_SCOPE, defined below. Note that "-m 1"
787c478bd9Sstevel@tonic-gate * is different than "-m", in that "-m 1" specifies
797c478bd9Sstevel@tonic-gate * transmission on one interface only.
807c478bd9Sstevel@tonic-gate *
817c478bd9Sstevel@tonic-gate * When "-m" is used without a <ttl> argument, the program accepts multicast
827c478bd9Sstevel@tonic-gate * rwhod reports from all multicast-capable interfaces. If a <ttl> argument
837c478bd9Sstevel@tonic-gate * is given, it accepts multicast reports from only one interface, the one
847c478bd9Sstevel@tonic-gate * on which reports are sent (which may be controlled via the host's routing
857c478bd9Sstevel@tonic-gate * table). Regardless of the "-m" option, the program accepts broadcast or
867c478bd9Sstevel@tonic-gate * unicast reports from all interfaces. Thus, this program will hear the
877c478bd9Sstevel@tonic-gate * reports of old, non-multicasting rwhods, but, if multicasting is used,
887c478bd9Sstevel@tonic-gate * those old rwhods won't hear the reports generated by this program.
897c478bd9Sstevel@tonic-gate *
907c478bd9Sstevel@tonic-gate * -- Steve Deering, Stanford University, February 1989
917c478bd9Sstevel@tonic-gate */
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate #define NO_MULTICAST 0 /* multicast modes */
947c478bd9Sstevel@tonic-gate #define PER_INTERFACE_MULTICAST 1
957c478bd9Sstevel@tonic-gate #define SCOPED_MULTICAST 2
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate #define MAX_MULTICAST_SCOPE 32 /* "site-wide", by convention */
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate #define INADDR_WHOD_GROUP (ulong_t)0xe0000103 /* 224.0.1.3 */
1007c478bd9Sstevel@tonic-gate /* (belongs in protocols/rwhod.h) */
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate static int multicast_mode = NO_MULTICAST;
1037c478bd9Sstevel@tonic-gate static int multicast_scope;
1047c478bd9Sstevel@tonic-gate static struct sockaddr_in multicast_addr = { AF_INET };
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate * Alarm interval. Don't forget to change the down time check in ruptime
1097c478bd9Sstevel@tonic-gate * if this is changed.
1107c478bd9Sstevel@tonic-gate */
1117c478bd9Sstevel@tonic-gate #define AL_INTERVAL (3 * 60)
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate static struct sockaddr_in sin = { AF_INET };
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate static char myname[MAXHOSTNAMELEN];
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate * We communicate with each neighbor in
1197c478bd9Sstevel@tonic-gate * a list constructed at the time we're
1207c478bd9Sstevel@tonic-gate * started up. Neighbors are currently
1217c478bd9Sstevel@tonic-gate * directly connected via a hardware interface.
1227c478bd9Sstevel@tonic-gate */
1237c478bd9Sstevel@tonic-gate struct neighbor {
1247c478bd9Sstevel@tonic-gate struct neighbor *n_next;
1257c478bd9Sstevel@tonic-gate char *n_name; /* interface name */
1267c478bd9Sstevel@tonic-gate char *n_addr; /* who to send to */
1277c478bd9Sstevel@tonic-gate int n_addrlen; /* size of address */
1287c478bd9Sstevel@tonic-gate ulong_t n_subnet; /* AF_INET subnet */
1297c478bd9Sstevel@tonic-gate uint_t n_flags; /* should forward?, interface flags */
1307c478bd9Sstevel@tonic-gate };
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate static struct neighbor *neighbors;
1337c478bd9Sstevel@tonic-gate static struct whod mywd;
1347c478bd9Sstevel@tonic-gate static struct servent *sp;
1357c478bd9Sstevel@tonic-gate static int s;
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate #define WHDRSIZE (sizeof (mywd) - sizeof (mywd.wd_we))
1387c478bd9Sstevel@tonic-gate #define RWHODIR "/var/spool/rwho"
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate static void onalrm(void);
1417c478bd9Sstevel@tonic-gate static void getkmem(void);
1427c478bd9Sstevel@tonic-gate static boolean_t configure(int);
1437c478bd9Sstevel@tonic-gate static int verify(const struct whod *);
1447c478bd9Sstevel@tonic-gate
145*740638c8Sbw int
main(int argc,char * argv[])1467c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate struct sockaddr_in from;
1497c478bd9Sstevel@tonic-gate struct stat st;
1507c478bd9Sstevel@tonic-gate char path[64];
1517c478bd9Sstevel@tonic-gate struct hostent *hp;
1527c478bd9Sstevel@tonic-gate int on = 1;
1537c478bd9Sstevel@tonic-gate char *cp;
1547c478bd9Sstevel@tonic-gate struct stat sb;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate if (getuid()) {
1577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "in.rwhod: not super user\n");
1587c478bd9Sstevel@tonic-gate exit(1);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate sp = getservbyname("who", "udp");
1617c478bd9Sstevel@tonic-gate if (sp == NULL) {
1627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "in.rwhod: udp/who: unknown service\n");
1637c478bd9Sstevel@tonic-gate exit(1);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate argv++;
1667c478bd9Sstevel@tonic-gate argc--;
1677c478bd9Sstevel@tonic-gate while (argc > 0 && *argv[0] == '-') {
1687c478bd9Sstevel@tonic-gate if (strcmp(*argv, "-m") == 0) {
1697c478bd9Sstevel@tonic-gate if (argc > 1 && isdigit(*(argv + 1)[0])) {
1707c478bd9Sstevel@tonic-gate argv++;
1717c478bd9Sstevel@tonic-gate argc--;
1727c478bd9Sstevel@tonic-gate multicast_mode = SCOPED_MULTICAST;
1737c478bd9Sstevel@tonic-gate multicast_scope = atoi(*argv);
1747c478bd9Sstevel@tonic-gate if (multicast_scope > MAX_MULTICAST_SCOPE) {
1757c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1767c478bd9Sstevel@tonic-gate "in.rwhod: "
1777c478bd9Sstevel@tonic-gate "ttl must not exceed %u\n",
1787c478bd9Sstevel@tonic-gate MAX_MULTICAST_SCOPE);
1797c478bd9Sstevel@tonic-gate exit(1);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate } else {
1827c478bd9Sstevel@tonic-gate multicast_mode = PER_INTERFACE_MULTICAST;
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate } else {
1857c478bd9Sstevel@tonic-gate goto usage;
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate argv++;
1887c478bd9Sstevel@tonic-gate argc--;
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate if (argc > 0)
1917c478bd9Sstevel@tonic-gate goto usage;
1927c478bd9Sstevel@tonic-gate if (chdir(RWHODIR) < 0) {
1937c478bd9Sstevel@tonic-gate perror(RWHODIR);
1947c478bd9Sstevel@tonic-gate exit(1);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate #ifndef DEBUG
1977c478bd9Sstevel@tonic-gate if (fork())
1987c478bd9Sstevel@tonic-gate exit(0);
1997c478bd9Sstevel@tonic-gate /* CSTYLED */
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate (void) close(0);
2027c478bd9Sstevel@tonic-gate (void) close(1);
2037c478bd9Sstevel@tonic-gate (void) close(2);
2047c478bd9Sstevel@tonic-gate (void) open("/", 0);
2057c478bd9Sstevel@tonic-gate (void) dup2(0, 1);
2067c478bd9Sstevel@tonic-gate (void) dup2(0, 2);
2077c478bd9Sstevel@tonic-gate (void) setsid();
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate #endif
2107c478bd9Sstevel@tonic-gate (void) sigset(SIGHUP, (void (*)())getkmem);
2117c478bd9Sstevel@tonic-gate openlog("in.rwhod", LOG_PID, LOG_DAEMON);
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate * Establish host name as returned by system.
2147c478bd9Sstevel@tonic-gate */
2157c478bd9Sstevel@tonic-gate if (gethostname(myname, sizeof (myname) - 1) < 0) {
2167c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "main: gethostname: %m");
2177c478bd9Sstevel@tonic-gate exit(1);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate if ((cp = index(myname, '.')) != NULL)
2207c478bd9Sstevel@tonic-gate *cp = '\0';
2217c478bd9Sstevel@tonic-gate (void) strlcpy(mywd.wd_hostname, myname, sizeof (mywd.wd_hostname));
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate if (stat(UTMPX_FILE, &sb) < 0) {
2247c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "main: stat: %s: %m", UTMPX_FILE);
2257c478bd9Sstevel@tonic-gate exit(1);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate getkmem();
2287c478bd9Sstevel@tonic-gate if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
2297c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "main: socket: %m");
2307c478bd9Sstevel@tonic-gate exit(1);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
2337c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "main: setsockopt SO_BROADCAST: %m");
2347c478bd9Sstevel@tonic-gate exit(1);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate hp = gethostbyname(myname);
2377c478bd9Sstevel@tonic-gate if (hp == NULL) {
2387c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "main: %s: don't know my own name\n", myname);
2397c478bd9Sstevel@tonic-gate exit(1);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate sin.sin_family = hp->h_addrtype;
2427c478bd9Sstevel@tonic-gate sin.sin_port = sp->s_port;
2437c478bd9Sstevel@tonic-gate if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
2447c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "main: bind: %m");
2457c478bd9Sstevel@tonic-gate exit(1);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate if (!configure(s))
2487c478bd9Sstevel@tonic-gate exit(1);
2497c478bd9Sstevel@tonic-gate (void) sigset(SIGALRM, (void (*)())onalrm);
2507c478bd9Sstevel@tonic-gate onalrm();
2517c478bd9Sstevel@tonic-gate for (;;) {
2527c478bd9Sstevel@tonic-gate struct whod wd;
2537c478bd9Sstevel@tonic-gate int cc, whod;
2547c478bd9Sstevel@tonic-gate socklen_t len = sizeof (from);
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate cc = recvfrom(s, &wd, sizeof (struct whod), 0,
2577c478bd9Sstevel@tonic-gate (struct sockaddr *)&from, &len);
2587c478bd9Sstevel@tonic-gate if (cc <= 0) {
2597c478bd9Sstevel@tonic-gate if (cc < 0 && errno != EINTR)
2607c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, "main: recvfrom: %m");
2617c478bd9Sstevel@tonic-gate continue;
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate if (from.sin_port != sp->s_port) {
2647c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, "main: %d: bad from port",
2657c478bd9Sstevel@tonic-gate ntohs(from.sin_port));
2667c478bd9Sstevel@tonic-gate continue;
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate #ifdef notdef
2697c478bd9Sstevel@tonic-gate if (gethostbyname(wd.wd_hostname) == 0) {
2707c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, "main: %s: unknown host",
2717c478bd9Sstevel@tonic-gate wd.wd_hostname);
2727c478bd9Sstevel@tonic-gate continue;
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate #endif
2757c478bd9Sstevel@tonic-gate if (wd.wd_vers != WHODVERSION)
2767c478bd9Sstevel@tonic-gate continue;
2777c478bd9Sstevel@tonic-gate if (wd.wd_type != WHODTYPE_STATUS)
2787c478bd9Sstevel@tonic-gate continue;
2797c478bd9Sstevel@tonic-gate if (!verify(&wd)) {
2807c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, "main: malformed host name from %x",
2817c478bd9Sstevel@tonic-gate from.sin_addr.s_addr);
2827c478bd9Sstevel@tonic-gate continue;
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate (void) sprintf(path, "whod.%s", wd.wd_hostname);
2857c478bd9Sstevel@tonic-gate /*
2867c478bd9Sstevel@tonic-gate * Rather than truncating and growing the file each time,
2877c478bd9Sstevel@tonic-gate * use ftruncate if size is less than previous size.
2887c478bd9Sstevel@tonic-gate */
2897c478bd9Sstevel@tonic-gate whod = open(path, O_WRONLY | O_CREAT, 0644);
2907c478bd9Sstevel@tonic-gate if (whod < 0) {
2917c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, "main: open: %s: %m", path);
2927c478bd9Sstevel@tonic-gate continue;
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
2957c478bd9Sstevel@tonic-gate /* CSTYLED */
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate int i, n = (cc - WHDRSIZE)/sizeof (struct whoent);
2987c478bd9Sstevel@tonic-gate struct whoent *we;
2997c478bd9Sstevel@tonic-gate
3007c478bd9Sstevel@tonic-gate /* undo header byte swapping before writing to file */
3017c478bd9Sstevel@tonic-gate wd.wd_sendtime = ntohl(wd.wd_sendtime);
3027c478bd9Sstevel@tonic-gate for (i = 0; i < 3; i++)
3037c478bd9Sstevel@tonic-gate wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
3047c478bd9Sstevel@tonic-gate wd.wd_boottime = ntohl(wd.wd_boottime);
3057c478bd9Sstevel@tonic-gate we = wd.wd_we;
3067c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) {
3077c478bd9Sstevel@tonic-gate we->we_idle = ntohl(we->we_idle);
3087c478bd9Sstevel@tonic-gate we->we_utmp.out_time =
3097c478bd9Sstevel@tonic-gate ntohl(we->we_utmp.out_time);
3107c478bd9Sstevel@tonic-gate we++;
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate #endif
3147c478bd9Sstevel@tonic-gate (void) time((time_t *)&wd.wd_recvtime);
3157c478bd9Sstevel@tonic-gate (void) write(whod, &wd, cc);
3167c478bd9Sstevel@tonic-gate if (fstat(whod, &st) < 0 || st.st_size > cc)
3177c478bd9Sstevel@tonic-gate (void) ftruncate(whod, cc);
3187c478bd9Sstevel@tonic-gate (void) close(whod);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate /* NOTREACHED */
3217c478bd9Sstevel@tonic-gate usage:
3227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "usage: in.rwhod [ -m [ ttl ] ]\n");
323*740638c8Sbw return (1);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate * Check out host name for unprintables
3287c478bd9Sstevel@tonic-gate * and other funnies before allowing a file
3297c478bd9Sstevel@tonic-gate * to be created. Sorry, but blanks aren't allowed.
3307c478bd9Sstevel@tonic-gate */
3317c478bd9Sstevel@tonic-gate static int
verify(const struct whod * wd)3327c478bd9Sstevel@tonic-gate verify(const struct whod *wd)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate int size = 0;
3357c478bd9Sstevel@tonic-gate const char *name = wd->wd_hostname;
3367c478bd9Sstevel@tonic-gate
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate * We shouldn't assume the name is NUL terminated, so bound the
3397c478bd9Sstevel@tonic-gate * checks at the size of the whod structures wd_hostname field.
3407c478bd9Sstevel@tonic-gate */
3417c478bd9Sstevel@tonic-gate while ((size < sizeof (wd->wd_hostname)) &&
3427c478bd9Sstevel@tonic-gate (*name != '\0')) {
3437c478bd9Sstevel@tonic-gate if (*name == '/' || !isascii(*name) ||
3447c478bd9Sstevel@tonic-gate !(isalnum(*name) || ispunct(*name)))
3457c478bd9Sstevel@tonic-gate return (0);
3467c478bd9Sstevel@tonic-gate name++, size++;
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate * Fail the verification if NULL name or it wasn't NUL terminated.
3507c478bd9Sstevel@tonic-gate */
3517c478bd9Sstevel@tonic-gate return ((size > 0) && (size < sizeof (wd->wd_hostname)));
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate
3547c478bd9Sstevel@tonic-gate static int utmpxtime;
3557c478bd9Sstevel@tonic-gate static int utmpxent;
3567c478bd9Sstevel@tonic-gate static int alarmcount;
3577c478bd9Sstevel@tonic-gate struct utmpx *utmpx;
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate static void
onalrm(void)3607c478bd9Sstevel@tonic-gate onalrm(void)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate int i;
3637c478bd9Sstevel@tonic-gate struct stat stb;
3647c478bd9Sstevel@tonic-gate int utmpxsize = 0;
3657c478bd9Sstevel@tonic-gate int entries;
3667c478bd9Sstevel@tonic-gate struct utmpx *utp;
3677c478bd9Sstevel@tonic-gate struct utmpx *utmpxbegin;
3687c478bd9Sstevel@tonic-gate struct whoent *we = mywd.wd_we, *wlast;
3697c478bd9Sstevel@tonic-gate int cc, cnt;
3707c478bd9Sstevel@tonic-gate double avenrun[3];
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate time_t now = time(0);
3737c478bd9Sstevel@tonic-gate struct neighbor *np;
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate if (alarmcount % 10 == 0)
3767c478bd9Sstevel@tonic-gate getkmem();
3777c478bd9Sstevel@tonic-gate alarmcount++;
3787c478bd9Sstevel@tonic-gate (void) stat(UTMPX_FILE, &stb);
3797c478bd9Sstevel@tonic-gate entries = stb.st_size / sizeof (struct futmpx);
3807c478bd9Sstevel@tonic-gate if ((stb.st_mtime != utmpxtime) || (entries > utmpxent)) {
3817c478bd9Sstevel@tonic-gate utmpxtime = stb.st_mtime;
3827c478bd9Sstevel@tonic-gate if (entries > utmpxent) {
3837c478bd9Sstevel@tonic-gate utmpxent = entries;
3847c478bd9Sstevel@tonic-gate utmpxsize = utmpxent * sizeof (struct utmpx);
3857c478bd9Sstevel@tonic-gate utmpx = realloc(utmpx, utmpxsize);
3867c478bd9Sstevel@tonic-gate if (utmpx == NULL) {
3877c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "onalrm: realloc: %m");
3887c478bd9Sstevel@tonic-gate utmpxsize = 0;
3897c478bd9Sstevel@tonic-gate goto done;
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate utmpxbegin = utmpx;
3937c478bd9Sstevel@tonic-gate setutxent();
3947c478bd9Sstevel@tonic-gate cnt = 0;
3957c478bd9Sstevel@tonic-gate while (cnt++ < utmpxent && (utp = getutxent()) != NULL)
3967c478bd9Sstevel@tonic-gate (void) memcpy(utmpxbegin++, utp, sizeof (struct utmpx));
3977c478bd9Sstevel@tonic-gate endutxent();
3987c478bd9Sstevel@tonic-gate wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
3997c478bd9Sstevel@tonic-gate for (i = 0; i < utmpxent; i++) {
4007c478bd9Sstevel@tonic-gate if (utmpx[i].ut_name[0] &&
4017c478bd9Sstevel@tonic-gate utmpx[i].ut_type == USER_PROCESS) {
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate * XXX - utmpx name and line lengths should
4047c478bd9Sstevel@tonic-gate * be here
4057c478bd9Sstevel@tonic-gate */
4067c478bd9Sstevel@tonic-gate bcopy(utmpx[i].ut_line, we->we_utmp.out_line,
4077c478bd9Sstevel@tonic-gate sizeof (we->we_utmp.out_line));
4087c478bd9Sstevel@tonic-gate bcopy(utmpx[i].ut_name, we->we_utmp.out_name,
4097c478bd9Sstevel@tonic-gate sizeof (we->we_utmp.out_name));
4107c478bd9Sstevel@tonic-gate we->we_utmp.out_time =
4117c478bd9Sstevel@tonic-gate htonl(utmpx[i].ut_xtime);
4127c478bd9Sstevel@tonic-gate if (we >= wlast)
4137c478bd9Sstevel@tonic-gate break;
4147c478bd9Sstevel@tonic-gate we++;
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate utmpxent = we - mywd.wd_we;
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate
4207c478bd9Sstevel@tonic-gate /*
4217c478bd9Sstevel@tonic-gate * The test on utmpxent looks silly---after all, if no one is
4227c478bd9Sstevel@tonic-gate * logged on, why worry about efficiency?---but is useful on
4237c478bd9Sstevel@tonic-gate * (e.g.) compute servers.
4247c478bd9Sstevel@tonic-gate */
4257c478bd9Sstevel@tonic-gate if (utmpxent > 0 && chdir("/dev") == -1) {
4267c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "onalrm: chdir /dev: %m");
4277c478bd9Sstevel@tonic-gate exit(1);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate we = mywd.wd_we;
4307c478bd9Sstevel@tonic-gate for (i = 0; i < utmpxent; i++) {
4317c478bd9Sstevel@tonic-gate if (stat(we->we_utmp.out_line, &stb) >= 0)
4327c478bd9Sstevel@tonic-gate we->we_idle = htonl(now - stb.st_atime);
4337c478bd9Sstevel@tonic-gate we++;
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate if (getloadavg(avenrun, 3) == -1) {
4367c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "onalrm: getloadavg: %m");
4377c478bd9Sstevel@tonic-gate exit(1);
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate
4407c478bd9Sstevel@tonic-gate for (i = 0; i < 3; i++)
4417c478bd9Sstevel@tonic-gate mywd.wd_loadav[i] = htonl((ulong_t)(avenrun[i] * 100));
4427c478bd9Sstevel@tonic-gate cc = (char *)we - (char *)&mywd;
4437c478bd9Sstevel@tonic-gate mywd.wd_sendtime = htonl(time(0));
4447c478bd9Sstevel@tonic-gate mywd.wd_vers = WHODVERSION;
4457c478bd9Sstevel@tonic-gate mywd.wd_type = WHODTYPE_STATUS;
4467c478bd9Sstevel@tonic-gate if (multicast_mode == SCOPED_MULTICAST) {
4477c478bd9Sstevel@tonic-gate (void) sendto(s, &mywd, cc, 0,
4487c478bd9Sstevel@tonic-gate (struct sockaddr *)&multicast_addr,
4497c478bd9Sstevel@tonic-gate sizeof (multicast_addr));
4507c478bd9Sstevel@tonic-gate } else for (np = neighbors; np != NULL; np = np->n_next) {
4517c478bd9Sstevel@tonic-gate if (multicast_mode == PER_INTERFACE_MULTICAST &&
4527c478bd9Sstevel@tonic-gate np->n_flags & IFF_MULTICAST) {
4537c478bd9Sstevel@tonic-gate /*
4547c478bd9Sstevel@tonic-gate * Select the outgoing interface for the multicast.
4557c478bd9Sstevel@tonic-gate */
4567c478bd9Sstevel@tonic-gate if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
4577c478bd9Sstevel@tonic-gate &(((struct sockaddr_in *)np->n_addr)->sin_addr),
4587c478bd9Sstevel@tonic-gate sizeof (struct in_addr)) < 0) {
4597c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
4607c478bd9Sstevel@tonic-gate "onalrm: setsockopt IP_MULTICAST_IF: %m");
4617c478bd9Sstevel@tonic-gate exit(1);
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate (void) sendto(s, &mywd, cc, 0,
4647c478bd9Sstevel@tonic-gate (struct sockaddr *)&multicast_addr,
4657c478bd9Sstevel@tonic-gate sizeof (multicast_addr));
4667c478bd9Sstevel@tonic-gate } else {
4677c478bd9Sstevel@tonic-gate (void) sendto(s, &mywd, cc, 0,
4687c478bd9Sstevel@tonic-gate (struct sockaddr *)np->n_addr, np->n_addrlen);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate if (utmpxent > 0 && chdir(RWHODIR) == -1) {
4727c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "onalrm: chdir %s: %m", RWHODIR);
4737c478bd9Sstevel@tonic-gate exit(1);
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate done:
4767c478bd9Sstevel@tonic-gate (void) alarm(AL_INTERVAL);
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate static void
getkmem(void)4807c478bd9Sstevel@tonic-gate getkmem(void)
4817c478bd9Sstevel@tonic-gate {
4827c478bd9Sstevel@tonic-gate struct utmpx *utmpx, utmpx_id;
4837c478bd9Sstevel@tonic-gate
4847c478bd9Sstevel@tonic-gate utmpx_id.ut_type = BOOT_TIME;
4857c478bd9Sstevel@tonic-gate if ((utmpx = getutxid(&utmpx_id)) != NULL)
4867c478bd9Sstevel@tonic-gate mywd.wd_boottime = utmpx->ut_xtime;
4877c478bd9Sstevel@tonic-gate endutxent();
4887c478bd9Sstevel@tonic-gate mywd.wd_boottime = htonl(mywd.wd_boottime);
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate
4917c478bd9Sstevel@tonic-gate /*
4927c478bd9Sstevel@tonic-gate * Figure out device configuration and select
4937c478bd9Sstevel@tonic-gate * networks which deserve status information.
4947c478bd9Sstevel@tonic-gate */
4957c478bd9Sstevel@tonic-gate static boolean_t
configure(int s)4967c478bd9Sstevel@tonic-gate configure(int s)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate char *buf;
4997c478bd9Sstevel@tonic-gate struct ifconf ifc;
5007c478bd9Sstevel@tonic-gate struct ifreq ifreq, *ifr;
5017c478bd9Sstevel@tonic-gate struct sockaddr_in *sin;
5027c478bd9Sstevel@tonic-gate struct neighbor *np;
5037c478bd9Sstevel@tonic-gate struct neighbor *np2;
5047c478bd9Sstevel@tonic-gate int n;
5057c478bd9Sstevel@tonic-gate int numifs;
5067c478bd9Sstevel@tonic-gate unsigned bufsize;
5077c478bd9Sstevel@tonic-gate
5087c478bd9Sstevel@tonic-gate if (multicast_mode == SCOPED_MULTICAST) {
5097c478bd9Sstevel@tonic-gate struct ip_mreq mreq;
5107c478bd9Sstevel@tonic-gate unsigned char ttl;
5117c478bd9Sstevel@tonic-gate
5127c478bd9Sstevel@tonic-gate mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
5137c478bd9Sstevel@tonic-gate mreq.imr_interface.s_addr = htonl(INADDR_ANY);
5147c478bd9Sstevel@tonic-gate if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
5157c478bd9Sstevel@tonic-gate sizeof (mreq)) < 0) {
5167c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
5177c478bd9Sstevel@tonic-gate "configure: setsockopt IP_ADD_MEMBERSHIP: %m");
5187c478bd9Sstevel@tonic-gate return (B_FALSE);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate ttl = multicast_scope;
5217c478bd9Sstevel@tonic-gate if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
5227c478bd9Sstevel@tonic-gate sizeof (ttl)) < 0) {
5237c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
5247c478bd9Sstevel@tonic-gate "configure: setsockopt IP_MULTICAST_TTL: %m");
5257c478bd9Sstevel@tonic-gate return (B_FALSE);
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP);
5287c478bd9Sstevel@tonic-gate multicast_addr.sin_port = sp->s_port;
5297c478bd9Sstevel@tonic-gate return (B_TRUE);
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) {
5337c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "configure: ioctl SIOCGIFNUM: %m");
5347c478bd9Sstevel@tonic-gate return (B_FALSE);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate bufsize = numifs * sizeof (struct ifreq);
5377c478bd9Sstevel@tonic-gate buf = malloc(bufsize);
5387c478bd9Sstevel@tonic-gate if (buf == NULL) {
5397c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "configure: malloc: %m");
5407c478bd9Sstevel@tonic-gate return (B_FALSE);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate ifc.ifc_len = bufsize;
5437c478bd9Sstevel@tonic-gate ifc.ifc_buf = buf;
5447c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
5457c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
5467c478bd9Sstevel@tonic-gate "configure: ioctl (get interface configuration): %m");
5477c478bd9Sstevel@tonic-gate (void) free(buf);
5487c478bd9Sstevel@tonic-gate return (B_FALSE);
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate ifr = ifc.ifc_req;
5517c478bd9Sstevel@tonic-gate for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
5527c478bd9Sstevel@tonic-gate /* Skip all logical interfaces */
5537c478bd9Sstevel@tonic-gate if (index(ifr->ifr_name, ':') != NULL)
5547c478bd9Sstevel@tonic-gate continue;
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate for (np = neighbors; np != NULL; np = np->n_next) {
5577c478bd9Sstevel@tonic-gate if (np->n_name &&
5587c478bd9Sstevel@tonic-gate strcmp(ifr->ifr_name, np->n_name) == 0)
5597c478bd9Sstevel@tonic-gate break;
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate if (np != NULL)
5627c478bd9Sstevel@tonic-gate continue;
5637c478bd9Sstevel@tonic-gate ifreq = *ifr;
5647c478bd9Sstevel@tonic-gate np = (struct neighbor *)malloc(sizeof (*np));
5657c478bd9Sstevel@tonic-gate if (np == NULL)
5667c478bd9Sstevel@tonic-gate continue;
5677c478bd9Sstevel@tonic-gate np->n_name = malloc(strlen(ifr->ifr_name) + 1);
5687c478bd9Sstevel@tonic-gate if (np->n_name == NULL) {
5697c478bd9Sstevel@tonic-gate free(np);
5707c478bd9Sstevel@tonic-gate continue;
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate (void) strcpy(np->n_name, ifr->ifr_name);
5737c478bd9Sstevel@tonic-gate np->n_addrlen = sizeof (ifr->ifr_addr);
5747c478bd9Sstevel@tonic-gate np->n_addr = malloc(np->n_addrlen);
5757c478bd9Sstevel@tonic-gate if (np->n_addr == NULL) {
5767c478bd9Sstevel@tonic-gate free(np->n_name);
5777c478bd9Sstevel@tonic-gate free(np);
5787c478bd9Sstevel@tonic-gate continue;
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate bcopy(&ifr->ifr_addr, np->n_addr, np->n_addrlen);
5817c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
5827c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
5837c478bd9Sstevel@tonic-gate "configure: ioctl (get interface flags): %m");
5847c478bd9Sstevel@tonic-gate free(np->n_addr);
5857c478bd9Sstevel@tonic-gate free(np->n_name);
5867c478bd9Sstevel@tonic-gate free(np);
5877c478bd9Sstevel@tonic-gate continue;
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate np->n_flags = ifreq.ifr_flags;
5907c478bd9Sstevel@tonic-gate if (((struct sockaddr_in *)np->n_addr)->sin_family == AF_INET &&
5917c478bd9Sstevel@tonic-gate ioctl(s, SIOCGIFNETMASK, (char *)&ifreq) >= 0) {
5927c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)np->n_addr;
5937c478bd9Sstevel@tonic-gate
5947c478bd9Sstevel@tonic-gate np->n_subnet = sin->sin_addr.s_addr &
5957c478bd9Sstevel@tonic-gate ((struct sockaddr_in *)&ifreq.ifr_addr)->
5967c478bd9Sstevel@tonic-gate sin_addr.s_addr;
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate if (multicast_mode == PER_INTERFACE_MULTICAST &&
5997c478bd9Sstevel@tonic-gate (np->n_flags & IFF_UP) &&
6007c478bd9Sstevel@tonic-gate (np->n_flags & IFF_MULTICAST) &&
6017c478bd9Sstevel@tonic-gate !(np->n_flags & IFF_LOOPBACK)) {
6027c478bd9Sstevel@tonic-gate struct ip_mreq mreq;
6037c478bd9Sstevel@tonic-gate
6047c478bd9Sstevel@tonic-gate /*
6057c478bd9Sstevel@tonic-gate * Skip interfaces that have matching subnets i.e.
6067c478bd9Sstevel@tonic-gate * (addr & netmask) are identical.
6077c478bd9Sstevel@tonic-gate * Such interfaces are connected to the same
6087c478bd9Sstevel@tonic-gate * physical wire.
6097c478bd9Sstevel@tonic-gate */
6107c478bd9Sstevel@tonic-gate for (np2 = neighbors; np2 != NULL; np2 = np2->n_next) {
6117c478bd9Sstevel@tonic-gate
6127c478bd9Sstevel@tonic-gate if (!(np->n_flags & IFF_POINTOPOINT) &&
6137c478bd9Sstevel@tonic-gate !(np2->n_flags & IFF_POINTOPOINT) &&
6147c478bd9Sstevel@tonic-gate (np->n_subnet == np2->n_subnet)) {
6157c478bd9Sstevel@tonic-gate free(np->n_addr);
6167c478bd9Sstevel@tonic-gate free(np->n_name);
6177c478bd9Sstevel@tonic-gate free(np);
6187c478bd9Sstevel@tonic-gate break;
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate }
6217c478bd9Sstevel@tonic-gate if (np2 != NULL)
6227c478bd9Sstevel@tonic-gate continue;
6237c478bd9Sstevel@tonic-gate
6247c478bd9Sstevel@tonic-gate mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
6257c478bd9Sstevel@tonic-gate mreq.imr_interface.s_addr =
6267c478bd9Sstevel@tonic-gate ((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr;
6277c478bd9Sstevel@tonic-gate if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
6287c478bd9Sstevel@tonic-gate sizeof (mreq)) < 0) {
6297c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
6307c478bd9Sstevel@tonic-gate "configure: "
6317c478bd9Sstevel@tonic-gate "setsockopt IP_ADD_MEMBERSHIP: %m");
6327c478bd9Sstevel@tonic-gate free(np->n_addr);
6337c478bd9Sstevel@tonic-gate free(np->n_name);
6347c478bd9Sstevel@tonic-gate free(np);
6357c478bd9Sstevel@tonic-gate continue;
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate multicast_addr.sin_addr.s_addr =
6387c478bd9Sstevel@tonic-gate htonl(INADDR_WHOD_GROUP);
6397c478bd9Sstevel@tonic-gate multicast_addr.sin_port = sp->s_port;
6407c478bd9Sstevel@tonic-gate np->n_next = neighbors;
6417c478bd9Sstevel@tonic-gate neighbors = np;
6427c478bd9Sstevel@tonic-gate continue;
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate if ((np->n_flags & IFF_UP) == 0 ||
6457c478bd9Sstevel@tonic-gate (np->n_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
6467c478bd9Sstevel@tonic-gate free(np->n_addr);
6477c478bd9Sstevel@tonic-gate free(np->n_name);
6487c478bd9Sstevel@tonic-gate free(np);
6497c478bd9Sstevel@tonic-gate continue;
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate if (np->n_flags & IFF_POINTOPOINT) {
6527c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
6537c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
6547c478bd9Sstevel@tonic-gate "configure: ioctl (get dstaddr): %m");
6557c478bd9Sstevel@tonic-gate free(np->n_addr);
6567c478bd9Sstevel@tonic-gate free(np->n_name);
6577c478bd9Sstevel@tonic-gate free(np);
6587c478bd9Sstevel@tonic-gate continue;
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate /* we assume addresses are all the same size */
6617c478bd9Sstevel@tonic-gate bcopy(&ifreq.ifr_dstaddr, np->n_addr, np->n_addrlen);
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate if (np->n_flags & IFF_BROADCAST) {
6647c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
6657c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
6667c478bd9Sstevel@tonic-gate "configure: ioctl (get broadaddr): %m");
6677c478bd9Sstevel@tonic-gate free(np->n_addr);
6687c478bd9Sstevel@tonic-gate free(np->n_name);
6697c478bd9Sstevel@tonic-gate free(np);
6707c478bd9Sstevel@tonic-gate continue;
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate /* we assume addresses are all the same size */
6737c478bd9Sstevel@tonic-gate bcopy(&ifreq.ifr_broadaddr, np->n_addr, np->n_addrlen);
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate /* gag, wish we could get rid of Internet dependencies */
6767c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)np->n_addr;
6777c478bd9Sstevel@tonic-gate sin->sin_port = sp->s_port;
6787c478bd9Sstevel@tonic-gate
6797c478bd9Sstevel@tonic-gate /*
6807c478bd9Sstevel@tonic-gate * Avoid adding duplicate broadcast and pt-pt destinations
6817c478bd9Sstevel@tonic-gate * to the list.
6827c478bd9Sstevel@tonic-gate */
6837c478bd9Sstevel@tonic-gate for (np2 = neighbors; np2 != NULL; np2 = np2->n_next) {
6847c478bd9Sstevel@tonic-gate struct sockaddr_in *sin2;
6857c478bd9Sstevel@tonic-gate
6867c478bd9Sstevel@tonic-gate sin2 = (struct sockaddr_in *)np2->n_addr;
6877c478bd9Sstevel@tonic-gate if (sin2->sin_addr.s_addr == sin->sin_addr.s_addr) {
6887c478bd9Sstevel@tonic-gate free(np->n_addr);
6897c478bd9Sstevel@tonic-gate free(np->n_name);
6907c478bd9Sstevel@tonic-gate free(np);
6917c478bd9Sstevel@tonic-gate break;
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate if (np2 != NULL)
6957c478bd9Sstevel@tonic-gate continue;
6967c478bd9Sstevel@tonic-gate
6977c478bd9Sstevel@tonic-gate np->n_next = neighbors;
6987c478bd9Sstevel@tonic-gate neighbors = np;
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate (void) free(buf);
7017c478bd9Sstevel@tonic-gate return (B_TRUE);
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate
7047c478bd9Sstevel@tonic-gate #ifdef DEBUG
7057c478bd9Sstevel@tonic-gate static char *interval(uint_t, char *);
7067c478bd9Sstevel@tonic-gate
7077c478bd9Sstevel@tonic-gate /* ARGSUSED */
7087c478bd9Sstevel@tonic-gate static ssize_t
sendto(int s,const void * buf,size_t cc,int flags,const struct sockaddr * to,socklen_t tolen)7097c478bd9Sstevel@tonic-gate sendto(int s, const void *buf, size_t cc, int flags, const struct sockaddr *to,
7107c478bd9Sstevel@tonic-gate socklen_t tolen)
7117c478bd9Sstevel@tonic-gate {
7127c478bd9Sstevel@tonic-gate struct whod *w = (struct whod *)buf;
7137c478bd9Sstevel@tonic-gate struct whoent *we;
7147c478bd9Sstevel@tonic-gate struct sockaddr_in *sin = (struct sockaddr_in *)to;
7157c478bd9Sstevel@tonic-gate int nsz;
7167c478bd9Sstevel@tonic-gate
7177c478bd9Sstevel@tonic-gate (void) printf("sendto %x.%d\n", ntohl(sin->sin_addr.s_addr),
7187c478bd9Sstevel@tonic-gate ntohs(sin->sin_port));
7197c478bd9Sstevel@tonic-gate (void) printf("hostname %s %s\n", w->wd_hostname,
7207c478bd9Sstevel@tonic-gate interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up"));
7217c478bd9Sstevel@tonic-gate (void) printf("load %4.2f, %4.2f, %4.2f\n",
7227c478bd9Sstevel@tonic-gate ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
7237c478bd9Sstevel@tonic-gate ntohl(w->wd_loadav[2]) / 100.0);
7247c478bd9Sstevel@tonic-gate cc -= WHDRSIZE;
7257c478bd9Sstevel@tonic-gate for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
7267c478bd9Sstevel@tonic-gate time_t t = ntohl(we->we_utmp.out_time);
7277c478bd9Sstevel@tonic-gate
7287c478bd9Sstevel@tonic-gate nsz = sizeof (we->we_utmp.out_name);
7297c478bd9Sstevel@tonic-gate (void) printf("%-*.*s %s:%s %.12s",
7307c478bd9Sstevel@tonic-gate nsz,
7317c478bd9Sstevel@tonic-gate nsz,
7327c478bd9Sstevel@tonic-gate we->we_utmp.out_name,
7337c478bd9Sstevel@tonic-gate w->wd_hostname,
7347c478bd9Sstevel@tonic-gate we->we_utmp.out_line,
7357c478bd9Sstevel@tonic-gate ctime(&t)+4);
7367c478bd9Sstevel@tonic-gate we->we_idle = ntohl(we->we_idle) / 60;
7377c478bd9Sstevel@tonic-gate if (we->we_idle) {
7387c478bd9Sstevel@tonic-gate if (we->we_idle >= 100*60)
7397c478bd9Sstevel@tonic-gate we->we_idle = 100*60 - 1;
7407c478bd9Sstevel@tonic-gate if (we->we_idle >= 60)
7417c478bd9Sstevel@tonic-gate (void) printf(" %2d", we->we_idle / 60);
7427c478bd9Sstevel@tonic-gate else
7437c478bd9Sstevel@tonic-gate (void) printf(" ");
7447c478bd9Sstevel@tonic-gate (void) printf(":%02d", we->we_idle % 60);
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate (void) printf("\n");
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate return (0);
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate
7517c478bd9Sstevel@tonic-gate static char *
interval(uint_t time,char * updown)7527c478bd9Sstevel@tonic-gate interval(uint_t time, char *updown)
7537c478bd9Sstevel@tonic-gate {
7547c478bd9Sstevel@tonic-gate static char resbuf[32];
7557c478bd9Sstevel@tonic-gate int days, hours, minutes;
7567c478bd9Sstevel@tonic-gate
7577c478bd9Sstevel@tonic-gate if (time > 3*30*24*60*60) {
7587c478bd9Sstevel@tonic-gate (void) sprintf(resbuf, " %s ??:??", updown);
7597c478bd9Sstevel@tonic-gate return (resbuf);
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate minutes = (time + 59) / 60; /* round to minutes */
7627c478bd9Sstevel@tonic-gate hours = minutes / 60;
7637c478bd9Sstevel@tonic-gate minutes %= 60;
7647c478bd9Sstevel@tonic-gate days = hours / 24;
7657c478bd9Sstevel@tonic-gate hours %= 24;
7667c478bd9Sstevel@tonic-gate if (days > 0) {
7677c478bd9Sstevel@tonic-gate (void) sprintf(resbuf, "%s %2d+%02d:%02d",
7687c478bd9Sstevel@tonic-gate updown, days, hours, minutes);
7697c478bd9Sstevel@tonic-gate } else {
7707c478bd9Sstevel@tonic-gate (void) sprintf(resbuf, "%s %2d:%02d",
7717c478bd9Sstevel@tonic-gate updown, hours, minutes);
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate return (resbuf);
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate #endif
776