826 lines
20 KiB
C
826 lines
20 KiB
C
/*
|
|
* Copyright (c) 1983 The Regents of the University of California.
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms are permitted
|
|
* provided that the above copyright notice and this paragraph are
|
|
* duplicated in all such forms and that any documentation,
|
|
* advertising materials, and other materials related to such
|
|
* distribution and use acknowledge that the software was developed
|
|
* by the University of California, Berkeley. The name of the
|
|
* University may not be used to endorse or promote products derived
|
|
* from this software without specific prior written permission.
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
*/
|
|
|
|
#ifndef lint
|
|
char copyright[] =
|
|
"@(#) Copyright (c) 1983 The Regents of the University of California.\n\
|
|
All rights reserved.\n";
|
|
#endif /* not lint */
|
|
|
|
#ifndef lint
|
|
static char sccsid[] = "@(#)rwhod.c 5.11 (Berkeley) 8/25/88 plus MULTICAST 1.2";
|
|
#endif /* not lint */
|
|
|
|
#ifdef sgi
|
|
#define _BSD_SIGNALS 1
|
|
|
|
#include <sys/param.h>
|
|
#include <sys/times.h>
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/file.h>
|
|
|
|
#include <net/if.h>
|
|
#include <netinet/in.h>
|
|
|
|
#include <nlist.h>
|
|
#include <stdio.h>
|
|
#include <signal.h>
|
|
#include <errno.h>
|
|
#include <utmp.h>
|
|
#include <ctype.h>
|
|
#include <netdb.h>
|
|
#include <syslog.h>
|
|
#include <cap_net.h>
|
|
#ifndef sgi
|
|
#include <unistd.h>
|
|
#endif
|
|
#include <protocols/rwhod.h>
|
|
#ifdef sgi
|
|
#include <paths.h>
|
|
#include <sys/sysmp.h>
|
|
#endif
|
|
|
|
/*
|
|
#define DEBUG
|
|
#define sendto dbg_sendto
|
|
#define syslog dbg_syslog
|
|
*/
|
|
|
|
/*
|
|
* This version of Berkeley's rwhod has been modified to use IP multicast
|
|
* datagrams, under control of a new command-line option:
|
|
*
|
|
* rwhod -m causes rwhod to use IP multicast (instead of
|
|
* broadcast or unicast) on all interfaces that have
|
|
* the IFF_MULTICAST flag set in their "ifnet" structs
|
|
* (excluding the loopback interface). The multicast
|
|
* reports are sent with a time-to-live of 1, to prevent
|
|
* forwarding beyond the directly-connected subnet(s).
|
|
*
|
|
* rwhod -m <ttl> causes rwhod to send IP multicast datagrams with a
|
|
* time-to-live of <ttl>, via a SINGLE interface rather
|
|
* than all interfaces. <ttl> must be between 0 and
|
|
* MAX_MULTICAST_SCOPE, defined below. Note that "-m 1"
|
|
* is different than "-m", in that "-m 1" specifies
|
|
* transmission on one interface only.
|
|
*
|
|
* When "-m" is used without a <ttl> argument, the program accepts multicast
|
|
* rwhod reports from all multicast-capable interfaces. If a <ttl> argument
|
|
* is given, it accepts multicast reports from only one interface, the one
|
|
* on which reports are sent (which may be controlled via the host's routing
|
|
* table). Regardless of the "-m" option, the program accepts broadcast or
|
|
* unicast reports from all interfaces. Thus, this program will hear the
|
|
* reports of old, non-multicasting rwhods, but, if multicasting is used,
|
|
* those old rwhods won't hear the reports generated by this program.
|
|
*
|
|
* -- Steve Deering, Stanford University, February 1989
|
|
*/
|
|
|
|
#define NO_MULTICAST 0 /* multicast modes */
|
|
#define PER_INTERFACE_MULTICAST 1
|
|
#define SCOPED_MULTICAST 2
|
|
|
|
#define MAX_MULTICAST_SCOPE 32 /* "site-wide", by convention */
|
|
|
|
int multicast_mode = NO_MULTICAST;
|
|
int multicast_scope;
|
|
struct sockaddr_in multicast_addr = { AF_INET };
|
|
|
|
/*
|
|
* Alarm interval. Don't forget to change the down time check in ruptime
|
|
* if this is changed.
|
|
*/
|
|
#define AL_INTERVAL (3 * 60)
|
|
|
|
struct sockaddr_in sin = { AF_INET };
|
|
|
|
extern errno;
|
|
|
|
char myname[32];
|
|
|
|
#ifndef sgi
|
|
struct nlist nl[] = {
|
|
#define NL_AVENRUN 0
|
|
{ "_avenrun" },
|
|
#define NL_BOOTTIME 1
|
|
{ "_boottime" },
|
|
0
|
|
};
|
|
#endif
|
|
|
|
|
|
/*
|
|
* We communicate with each neighbor in
|
|
* a list constructed at the time we're
|
|
* started up. Neighbors are currently
|
|
* directly connected via a hardware interface.
|
|
*/
|
|
struct neighbor {
|
|
struct neighbor *n_next;
|
|
char *n_name; /* interface name */
|
|
char *n_addr; /* who to send to */
|
|
int n_addrlen; /* size of address */
|
|
int n_flags; /* should forward?, interface flags */
|
|
};
|
|
|
|
struct neighbor *neighbors;
|
|
struct whod mywd;
|
|
struct servent *sp;
|
|
int s, utmpf, kmemf = -1;
|
|
|
|
#define WHDRSIZE (sizeof (mywd) - sizeof (mywd.wd_we))
|
|
#ifndef sgi
|
|
#define RWHODIR "/var/spool/rwho"
|
|
#endif
|
|
|
|
#ifdef sgi
|
|
void onalrm();
|
|
#else
|
|
int onalrm();
|
|
#endif
|
|
char *strcpy(), *malloc();
|
|
#ifndef sgi
|
|
long lseek();
|
|
int getkmem();
|
|
#else
|
|
void getkmem();
|
|
#endif
|
|
struct in_addr inet_makeaddr();
|
|
|
|
main(argc, argv)
|
|
int argc;
|
|
char *argv[];
|
|
{
|
|
struct sockaddr_in from;
|
|
struct stat st;
|
|
char path[64];
|
|
int on = 1;
|
|
char *cp;
|
|
extern char *index();
|
|
#ifdef sgi
|
|
extern char *strerror();
|
|
#endif
|
|
|
|
if (getuid()) {
|
|
fprintf(stderr, "rwhod: not super user\n");
|
|
exit(1);
|
|
}
|
|
argv++; argc--;
|
|
while (argc > 0 && *argv[0] == '-') {
|
|
if (strcmp(*argv, "-m") == 0) {
|
|
if (argc > 1 && isdigit(*(argv + 1)[0])) {
|
|
argv++, argc--;
|
|
multicast_mode = SCOPED_MULTICAST;
|
|
multicast_scope = atoi(*argv);
|
|
if (multicast_scope > MAX_MULTICAST_SCOPE) {
|
|
fprintf(stderr,
|
|
"rwhod: ttl must not exceed %u\n",
|
|
MAX_MULTICAST_SCOPE);
|
|
exit(1);
|
|
}
|
|
}
|
|
else multicast_mode = PER_INTERFACE_MULTICAST;
|
|
}
|
|
else goto usage;
|
|
argv++, argc--;
|
|
}
|
|
if (argc > 0) {
|
|
usage: fprintf(stderr, "usage: rwhod [ -m [ ttl ] ]\n");
|
|
exit(1);
|
|
}
|
|
#ifndef DEBUG
|
|
#ifdef sgi
|
|
if (_daemonize(0, -1, -1, -1) < 0)
|
|
exit(1);
|
|
#else
|
|
if (fork())
|
|
exit(0);
|
|
{ int s;
|
|
for (s = 0; s < 10; s++)
|
|
(void) close(s);
|
|
(void) open("/", 0);
|
|
(void) dup2(0, 1);
|
|
(void) dup2(0, 2);
|
|
s = open("/dev/tty", 2);
|
|
if (s >= 0) {
|
|
ioctl(s, TIOCNOTTY, 0);
|
|
(void) close(s);
|
|
}
|
|
}
|
|
#endif /* sgi */
|
|
#endif
|
|
#ifdef sgi
|
|
if (chdir(_PATH_RWHODIR) < 0) {
|
|
(void)fprintf(stderr, "rwhod: %s: %s\n",
|
|
_PATH_RWHODIR, strerror(errno));
|
|
exit(1);
|
|
}
|
|
#else
|
|
if (chdir(RWHODIR) < 0) {
|
|
perror(RWHODIR);
|
|
exit(1);
|
|
}
|
|
#endif
|
|
(void) signal(SIGHUP, getkmem);
|
|
openlog("rwhod", LOG_PID, LOG_DAEMON);
|
|
/*
|
|
* Establish host name as returned by system.
|
|
*/
|
|
if (gethostname(myname, sizeof (myname) - 1) < 0) {
|
|
syslog(LOG_ERR, "gethostname: %m");
|
|
exit(1);
|
|
}
|
|
#ifndef sgi /* prevent name collision if have multiple domains on net. */
|
|
if ((cp = index(myname, '.')) != NULL)
|
|
*cp = '\0';
|
|
#endif
|
|
strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
|
|
#ifdef sgi
|
|
utmpf = open(UTMP_FILE, O_RDONLY);
|
|
#else /* !sgi */
|
|
utmpf = open("/etc/utmp", O_RDONLY);
|
|
if (utmpf < 0) {
|
|
(void) close(creat("/etc/utmp", 0644));
|
|
utmpf = open("/etc/utmp", O_RDONLY);
|
|
}
|
|
#endif
|
|
if (utmpf < 0) {
|
|
#ifdef sgi
|
|
syslog(LOG_ERR, "%s: %m", UTMP_FILE);
|
|
#else
|
|
syslog(LOG_ERR, "/etc/utmp: %m");
|
|
#endif
|
|
exit(1);
|
|
}
|
|
getkmem();
|
|
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
|
syslog(LOG_ERR, "socket: %m");
|
|
exit(1);
|
|
}
|
|
if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
|
|
syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
|
|
exit(1);
|
|
}
|
|
if ((sp = getservbyname("who", "udp")) == 0) {
|
|
syslog(LOG_ERR, "udp/who: unknown service");
|
|
exit(1);
|
|
}
|
|
#ifdef sgi
|
|
sin.sin_family = AF_INET;
|
|
#endif
|
|
sin.sin_port = sp->s_port;
|
|
if (cap_bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
|
|
syslog(LOG_ERR, "bind: %m");
|
|
exit(1);
|
|
}
|
|
if (!configure(s))
|
|
exit(1);
|
|
signal(SIGALRM, onalrm);
|
|
onalrm();
|
|
for (;;) {
|
|
struct whod wd;
|
|
int cc, whod, len = sizeof (from);
|
|
|
|
cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
|
|
&from, &len);
|
|
if (cc <= 0) {
|
|
if (cc < 0 && errno != EINTR)
|
|
syslog(LOG_WARNING, "recv: %m");
|
|
continue;
|
|
}
|
|
if (from.sin_port != sp->s_port) {
|
|
syslog(LOG_WARNING, "%d: bad from port",
|
|
ntohs(from.sin_port));
|
|
continue;
|
|
}
|
|
#ifdef notdef
|
|
if (gethostbyname(wd.wd_hostname) == 0) {
|
|
syslog(LOG_WARNING, "%s: unknown host",
|
|
wd.wd_hostname);
|
|
continue;
|
|
}
|
|
#endif
|
|
if (wd.wd_vers != WHODVERSION)
|
|
continue;
|
|
if (wd.wd_type != WHODTYPE_STATUS)
|
|
continue;
|
|
if (!verify(wd.wd_hostname)) {
|
|
syslog(LOG_WARNING, "malformed host name from %x",
|
|
from.sin_addr);
|
|
continue;
|
|
}
|
|
(void) sprintf(path, "whod.%s", wd.wd_hostname);
|
|
/*
|
|
* Rather than truncating and growing the file each time,
|
|
* use ftruncate if size is less than previous size.
|
|
*/
|
|
whod = open(path, O_WRONLY | O_CREAT, 0644);
|
|
if (whod < 0) {
|
|
syslog(LOG_WARNING, "%s: %m", path);
|
|
continue;
|
|
}
|
|
#if vax || pdp11 || mips && !defined(sgi)
|
|
{
|
|
int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
|
|
struct whoent *we;
|
|
|
|
/* undo header byte swapping before writing to file */
|
|
wd.wd_sendtime = ntohl(wd.wd_sendtime);
|
|
for (i = 0; i < 3; i++)
|
|
wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
|
|
wd.wd_boottime = ntohl(wd.wd_boottime);
|
|
we = wd.wd_we;
|
|
for (i = 0; i < n; i++) {
|
|
we->we_idle = ntohl(we->we_idle);
|
|
we->we_utmp.out_time =
|
|
ntohl(we->we_utmp.out_time);
|
|
we++;
|
|
}
|
|
}
|
|
#endif
|
|
#ifdef sgi
|
|
(void) time((time_t *)&wd.wd_recvtime);
|
|
#else
|
|
(void) time(&wd.wd_recvtime);
|
|
#endif
|
|
(void) write(whod, (char *)&wd, cc);
|
|
if (fstat(whod, &st) < 0 || st.st_size > cc)
|
|
ftruncate(whod, cc);
|
|
(void) close(whod);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Check out host name for unprintables
|
|
* and other funnies before allowing a file
|
|
* to be created. Sorry, but blanks aren't allowed.
|
|
*/
|
|
verify(name)
|
|
register char *name;
|
|
{
|
|
register int size = 0;
|
|
|
|
while (*name) {
|
|
if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
|
|
return (0);
|
|
name++, size++;
|
|
}
|
|
return (size > 0);
|
|
}
|
|
|
|
int utmptime;
|
|
int utmpent;
|
|
int utmpsize = 0;
|
|
struct utmp *utmp;
|
|
int alarmcount;
|
|
|
|
#ifdef sgi
|
|
void
|
|
#endif
|
|
onalrm()
|
|
{
|
|
register int i;
|
|
struct stat stb;
|
|
register struct whoent *we = mywd.wd_we, *wlast;
|
|
int cc;
|
|
#ifdef sgi
|
|
static int avenrun[3];
|
|
#else
|
|
double avenrun[3];
|
|
#endif
|
|
time_t now = time(0);
|
|
register struct neighbor *np;
|
|
|
|
if (alarmcount % 10 == 0)
|
|
getkmem();
|
|
alarmcount++;
|
|
(void) fstat(utmpf, &stb);
|
|
if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
|
|
utmptime = stb.st_mtime;
|
|
#ifdef sgi
|
|
setutent();
|
|
utmpent = 0;
|
|
wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
|
|
/*
|
|
* Memory smash alert. There may be more utmp entries
|
|
* than the rwho packet can hold.
|
|
*/
|
|
while (((utmp = getutent()) != NULL) && (we <= wlast)) {
|
|
if (utmp->ut_name[0] == '\0'
|
|
|| USER_PROCESS != utmp->ut_type)
|
|
continue;
|
|
bcopy(utmp->ut_line, we->we_utmp.out_line,
|
|
sizeof(utmp->ut_line));
|
|
bcopy(utmp->ut_name, we->we_utmp.out_name,
|
|
sizeof(utmp->ut_name));
|
|
we->we_utmp.out_time = htonl(utmp->ut_time);
|
|
we++;
|
|
}
|
|
endutent();
|
|
#else
|
|
if (stb.st_size > utmpsize) {
|
|
utmpsize = stb.st_size + 10 * sizeof(struct utmp);
|
|
if (utmp)
|
|
utmp = (struct utmp *)realloc(utmp, utmpsize);
|
|
else
|
|
utmp = (struct utmp *)malloc(utmpsize);
|
|
if (! utmp) {
|
|
fprintf(stderr, "rwhod: malloc failed\n");
|
|
utmpsize = 0;
|
|
goto done;
|
|
}
|
|
}
|
|
(void) lseek(utmpf, (long)0, L_SET);
|
|
cc = read(utmpf, (char *)utmp, stb.st_size);
|
|
if (cc < 0) {
|
|
perror("/etc/utmp");
|
|
goto done;
|
|
}
|
|
wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
|
|
utmpent = cc / sizeof (struct utmp);
|
|
for (i = 0; i < utmpent; i++)
|
|
if (utmp[i].ut_name[0]) {
|
|
bcopy(utmp[i].ut_line, we->we_utmp.out_line,
|
|
sizeof (utmp[i].ut_line));
|
|
bcopy(utmp[i].ut_name, we->we_utmp.out_name,
|
|
sizeof (utmp[i].ut_name));
|
|
we->we_utmp.out_time = htonl(utmp[i].ut_time);
|
|
if (we >= wlast)
|
|
break;
|
|
we++;
|
|
}
|
|
#endif
|
|
utmpent = we - mywd.wd_we;
|
|
}
|
|
|
|
/*
|
|
* The test on utmpent looks silly---after all, if no one is
|
|
* logged on, why worry about efficiency?---but is useful on
|
|
* (e.g.) compute servers.
|
|
*/
|
|
#ifdef sgi
|
|
if (utmpent && chdir(_PATH_DEV)) {
|
|
syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
|
|
exit(1);
|
|
}
|
|
#else
|
|
if (utmpent && chdir("/dev")) {
|
|
syslog(LOG_ERR, "chdir(/dev): %m");
|
|
exit(1);
|
|
}
|
|
#endif
|
|
we = mywd.wd_we;
|
|
for (i = 0; i < utmpent; i++) {
|
|
if (stat(we->we_utmp.out_line, &stb) >= 0)
|
|
we->we_idle = htonl(now - stb.st_atime);
|
|
we++;
|
|
}
|
|
#ifdef sgi
|
|
if (lseek(kmemf, (off_t)(sysmp(MP_KERNADDR, MPKA_AVENRUN) & 0x7fffffff), L_SET) >= 0) {
|
|
(void) read(kmemf, (char *)avenrun, sizeof (avenrun));
|
|
}
|
|
#else
|
|
(void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET);
|
|
(void) read(kmemf, (char *)avenrun, sizeof (avenrun));
|
|
#endif
|
|
#ifdef sgi
|
|
for (i = 0; i < 3; i++)
|
|
mywd.wd_loadav[i] = htonl((u_long)((avenrun[i] * 100)/1024));
|
|
#else
|
|
for (i = 0; i < 3; i++)
|
|
mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
|
|
#endif
|
|
cc = (char *)we - (char *)&mywd;
|
|
mywd.wd_sendtime = htonl(time(0));
|
|
mywd.wd_vers = WHODVERSION;
|
|
mywd.wd_type = WHODTYPE_STATUS;
|
|
if (multicast_mode == SCOPED_MULTICAST) {
|
|
(void) sendto(s, (char *)&mywd, cc, 0,
|
|
&multicast_addr, sizeof(multicast_addr));
|
|
}
|
|
else for (np = neighbors; np != NULL; np = np->n_next) {
|
|
if (multicast_mode == PER_INTERFACE_MULTICAST &&
|
|
np->n_flags & IFF_MULTICAST) {
|
|
/*
|
|
* Select the outgoing interface for the multicast.
|
|
*/
|
|
if (cap_setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
|
|
&(((struct sockaddr_in *)np->n_addr)->sin_addr),
|
|
sizeof(struct in_addr)) < 0) {
|
|
syslog(LOG_ERR,
|
|
"setsockopt IP_MULTICAST_IF: %m");
|
|
exit(1);
|
|
}
|
|
(void) sendto(s, (char *)&mywd, cc, 0,
|
|
&multicast_addr, sizeof(multicast_addr));
|
|
} else (void) sendto(s, (char *)&mywd, cc, 0,
|
|
np->n_addr, np->n_addrlen);
|
|
}
|
|
#ifdef sgi
|
|
if (utmpent && chdir(_PATH_RWHODIR)) {
|
|
syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
|
|
exit(1);
|
|
}
|
|
#else
|
|
if (utmpent && chdir(RWHODIR)) {
|
|
syslog(LOG_ERR, "chdir(%s): %m", RWHODIR);
|
|
exit(1);
|
|
}
|
|
#endif
|
|
done:
|
|
(void) alarm(AL_INTERVAL);
|
|
}
|
|
|
|
#ifdef sgi
|
|
void
|
|
#endif
|
|
getkmem()
|
|
{
|
|
static ino_t vmunixino;
|
|
static time_t vmunixctime;
|
|
struct stat sb;
|
|
#ifdef sgi
|
|
struct tms tms;
|
|
|
|
#define UPTIME() (time(0) - times(&tms)/HZ)
|
|
if (stat(_PATH_UNIX, &sb) < 0) {
|
|
#else
|
|
|
|
if (stat("/vmunix", &sb) < 0) {
|
|
#endif
|
|
if (vmunixctime)
|
|
return;
|
|
} else {
|
|
if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
|
|
return;
|
|
vmunixctime = sb.st_ctime;
|
|
vmunixino= sb.st_ino;
|
|
}
|
|
if (kmemf >= 0)
|
|
(void) close(kmemf);
|
|
#ifdef sgi
|
|
kmemf = -1;
|
|
#endif
|
|
loop:
|
|
|
|
#ifndef sgi
|
|
if (nlist("/vmunix", nl)) {
|
|
syslog(LOG_WARNING, "/vmunix namelist botch");
|
|
sleep(300);
|
|
goto loop;
|
|
}
|
|
#endif
|
|
|
|
#ifdef sgi
|
|
kmemf = open(_PATH_KMEM, O_RDONLY);
|
|
#else
|
|
kmemf = open("/dev/kmem", O_RDONLY);
|
|
#endif
|
|
if (kmemf < 0) {
|
|
#ifdef sgi
|
|
syslog(LOG_ERR, "%s: %m", _PATH_KMEM);
|
|
#else
|
|
syslog(LOG_ERR, "/dev/kmem: %m");
|
|
#endif
|
|
exit(1);
|
|
}
|
|
#ifdef sgi
|
|
mywd.wd_boottime = UPTIME();
|
|
#else
|
|
(void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET);
|
|
(void) read(kmemf, (char *)&mywd.wd_boottime,
|
|
sizeof (mywd.wd_boottime));
|
|
#endif
|
|
mywd.wd_boottime = htonl(mywd.wd_boottime);
|
|
}
|
|
|
|
/*
|
|
* Figure out device configuration and select
|
|
* networks which deserve status information.
|
|
*/
|
|
configure(s)
|
|
int s;
|
|
{
|
|
char buf[BUFSIZ];
|
|
struct ifconf ifc;
|
|
struct ifreq ifreq, *ifr;
|
|
struct sockaddr_in *sin;
|
|
register struct neighbor *np;
|
|
int n;
|
|
|
|
if (multicast_mode == SCOPED_MULTICAST) {
|
|
struct ip_mreq mreq;
|
|
unsigned char ttl;
|
|
|
|
mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
|
|
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
|
|
if (cap_setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
|
|
&mreq, sizeof(mreq)) < 0) {
|
|
syslog(LOG_ERR,
|
|
"setsockopt IP_ADD_MEMBERSHIP: %m");
|
|
return(0);
|
|
}
|
|
ttl = multicast_scope;
|
|
if (cap_setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
|
|
&ttl, sizeof(ttl)) < 0) {
|
|
syslog(LOG_ERR,
|
|
"setsockopt IP_MULTICAST_TTL: %m");
|
|
return(0);
|
|
}
|
|
multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP);
|
|
multicast_addr.sin_port = sp->s_port;
|
|
return(1);
|
|
}
|
|
|
|
ifc.ifc_len = sizeof (buf);
|
|
ifc.ifc_buf = buf;
|
|
if (cap_network_ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
|
|
syslog(LOG_ERR, "ioctl (get interface configuration)");
|
|
return (0);
|
|
}
|
|
ifr = ifc.ifc_req;
|
|
for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
|
|
if (ifr->ifr_addr.sa_family != AF_INET)
|
|
continue;
|
|
for (np = neighbors; np != NULL; np = np->n_next)
|
|
if (np->n_name &&
|
|
strcmp(ifr->ifr_name, np->n_name) == 0)
|
|
break;
|
|
if (np != NULL)
|
|
continue;
|
|
ifreq = *ifr;
|
|
np = (struct neighbor *)malloc(sizeof (*np));
|
|
if (np == NULL)
|
|
continue;
|
|
np->n_name = malloc(strlen(ifr->ifr_name) + 1);
|
|
if (np->n_name == NULL) {
|
|
free((char *)np);
|
|
continue;
|
|
}
|
|
strcpy(np->n_name, ifr->ifr_name);
|
|
np->n_addrlen = sizeof (ifr->ifr_addr);
|
|
np->n_addr = malloc(np->n_addrlen);
|
|
if (np->n_addr == NULL) {
|
|
free(np->n_name);
|
|
free((char *)np);
|
|
continue;
|
|
}
|
|
bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
|
|
if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
|
|
syslog(LOG_ERR, "ioctl (get interface flags)");
|
|
free((char *)np);
|
|
continue;
|
|
}
|
|
if (multicast_mode == PER_INTERFACE_MULTICAST &&
|
|
(ifreq.ifr_flags & IFF_UP) &&
|
|
(ifreq.ifr_flags & IFF_MULTICAST) &&
|
|
!(ifreq.ifr_flags & IFF_LOOPBACK)) {
|
|
struct ip_mreq mreq;
|
|
|
|
mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
|
|
mreq.imr_interface.s_addr =
|
|
((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr;
|
|
if (cap_setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
|
|
&mreq, sizeof(mreq)) < 0) {
|
|
syslog(LOG_ERR,
|
|
"setsockopt IP_ADD_MEMBERSHIP: %m");
|
|
free(np->n_addr);
|
|
free(np->n_name);
|
|
free((char *)np);
|
|
continue;
|
|
}
|
|
multicast_addr.sin_addr.s_addr
|
|
= htonl(INADDR_WHOD_GROUP);
|
|
multicast_addr.sin_port = sp->s_port;
|
|
np->n_flags = ifreq.ifr_flags;
|
|
np->n_next = neighbors;
|
|
neighbors = np;
|
|
continue;
|
|
}
|
|
if ((ifreq.ifr_flags & IFF_UP) == 0 ||
|
|
(ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
|
|
free((char *)np);
|
|
continue;
|
|
}
|
|
np->n_flags = ifreq.ifr_flags;
|
|
if (np->n_flags & IFF_POINTOPOINT) {
|
|
if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
|
|
syslog(LOG_ERR, "ioctl (get dstaddr)");
|
|
free((char *)np);
|
|
continue;
|
|
}
|
|
/* we assume addresses are all the same size */
|
|
bcopy((char *)&ifreq.ifr_dstaddr,
|
|
np->n_addr, np->n_addrlen);
|
|
}
|
|
if (np->n_flags & IFF_BROADCAST) {
|
|
if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
|
|
syslog(LOG_ERR, "ioctl (get broadaddr)");
|
|
free((char *)np);
|
|
continue;
|
|
}
|
|
/* we assume addresses are all the same size */
|
|
bcopy((char *)&ifreq.ifr_broadaddr,
|
|
np->n_addr, np->n_addrlen);
|
|
}
|
|
/* gag, wish we could get rid of Internet dependencies */
|
|
sin = (struct sockaddr_in *)np->n_addr;
|
|
sin->sin_port = sp->s_port;
|
|
np->n_next = neighbors;
|
|
neighbors = np;
|
|
}
|
|
return (1);
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
dbg_sendto(s, buf, cc, flags, to, tolen)
|
|
int s;
|
|
char *buf;
|
|
int cc, flags;
|
|
char *to;
|
|
int tolen;
|
|
{
|
|
register struct whod *w = (struct whod *)buf;
|
|
register struct whoent *we;
|
|
struct sockaddr_in *sin = (struct sockaddr_in *)to;
|
|
char *interval();
|
|
|
|
printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
|
|
printf("hostname %s %s\n", w->wd_hostname,
|
|
interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up"));
|
|
printf("load %4.2f, %4.2f, %4.2f\n",
|
|
ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
|
|
ntohl(w->wd_loadav[2]) / 100.0);
|
|
cc -= WHDRSIZE;
|
|
for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
|
|
time_t t = ntohl(we->we_utmp.out_time);
|
|
printf("%-8.8s %s:%s %.12s",
|
|
we->we_utmp.out_name,
|
|
w->wd_hostname, we->we_utmp.out_line,
|
|
ctime(&t)+4);
|
|
we->we_idle = ntohl(we->we_idle) / 60;
|
|
if (we->we_idle) {
|
|
if (we->we_idle >= 100*60)
|
|
we->we_idle = 100*60 - 1;
|
|
if (we->we_idle >= 60)
|
|
printf(" %2d", we->we_idle / 60);
|
|
else
|
|
printf(" ");
|
|
printf(":%02d", we->we_idle % 60);
|
|
}
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
char *
|
|
interval(time, updown)
|
|
int time;
|
|
char *updown;
|
|
{
|
|
static char resbuf[32];
|
|
int days, hours, minutes;
|
|
|
|
if (time < 0 || time > 3*30*24*60*60) {
|
|
(void) sprintf(resbuf, " %s ??:??", updown);
|
|
return (resbuf);
|
|
}
|
|
minutes = (time + 59) / 60; /* round to minutes */
|
|
hours = minutes / 60; minutes %= 60;
|
|
days = hours / 24; hours %= 24;
|
|
if (days)
|
|
(void) sprintf(resbuf, "%s %2d+%02d:%02d",
|
|
updown, days, hours, minutes);
|
|
else
|
|
(void) sprintf(resbuf, "%s %2d:%02d",
|
|
updown, hours, minutes);
|
|
return (resbuf);
|
|
}
|
|
|
|
dbg_syslog(level, fmt)
|
|
int level;
|
|
char *fmt;
|
|
{
|
|
perror(fmt);
|
|
}
|
|
#endif
|