1/* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#ifndef lint 31static const char copyright[] = 32"@(#) Copyright (c) 1983, 1993\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34#endif /* not lint */ 35 36#if 0 37#ifndef lint 38static char sccsid[] = "@(#)rwhod.c 8.1 (Berkeley) 6/6/93"; 39#endif /* not lint */ 40#endif 41 42#include <sys/cdefs.h> 43__FBSDID("$FreeBSD: src/usr.sbin/rwhod/rwhod.c,v 1.23 2005/06/03 17:38:33 ssouhlal Exp $"); 44 45#include <sys/param.h> 46#include <sys/socket.h> 47#include <sys/stat.h> 48#include <sys/signal.h> 49#include <sys/ioctl.h> 50#include <sys/sysctl.h> 51 52#include <net/if.h> 53#include <net/if_dl.h> 54#include <netinet/in.h> 55#include <arpa/inet.h> 56#include <protocols/rwhod.h> 57 58#include <ctype.h> 59#include <err.h> 60#include <errno.h> 61#include <fcntl.h> 62#include <netdb.h> 63#include <paths.h> 64#include <stdio.h> 65#include <stdlib.h> 66#include <string.h> 67#include <syslog.h> 68#include <timeconv.h> 69#include <unistd.h> 70#ifdef __APPLE__ 71#include <utmpx.h> 72#include <net/route.h> 73#else 74#include <utmp.h> 75#endif 76#include <pwd.h> 77#include <grp.h> 78 79/* 80 * This version of Berkeley's rwhod has been modified to use IP multicast 81 * datagrams, under control of a new command-line option: 82 * 83 * rwhod -m causes rwhod to use IP multicast (instead of 84 * broadcast or unicast) on all interfaces that have 85 * the IFF_MULTICAST flag set in their "ifnet" structs 86 * (excluding the loopback interface). The multicast 87 * reports are sent with a time-to-live of 1, to prevent 88 * forwarding beyond the directly-connected subnet(s). 89 * 90 * rwhod -m <ttl> causes rwhod to send IP multicast datagrams with a 91 * time-to-live of <ttl>, via a SINGLE interface rather 92 * than all interfaces. <ttl> must be between 0 and 93 * MAX_MULTICAST_SCOPE, defined below. Note that "-m 1" 94 * is different than "-m", in that "-m 1" specifies 95 * transmission on one interface only. 96 * 97 * When "-m" is used without a <ttl> argument, the program accepts multicast 98 * rwhod reports from all multicast-capable interfaces. If a <ttl> argument 99 * is given, it accepts multicast reports from only one interface, the one 100 * on which reports are sent (which may be controlled via the host's routing 101 * table). Regardless of the "-m" option, the program accepts broadcast or 102 * unicast reports from all interfaces. Thus, this program will hear the 103 * reports of old, non-multicasting rwhods, but, if multicasting is used, 104 * those old rwhods won't hear the reports generated by this program. 105 * 106 * -- Steve Deering, Stanford University, February 1989 107 */ 108 109#define UNPRIV_USER "daemon" 110#define UNPRIV_GROUP "daemon" 111 112#define NO_MULTICAST 0 /* multicast modes */ 113#define PER_INTERFACE_MULTICAST 1 114#define SCOPED_MULTICAST 2 115 116#define MAX_MULTICAST_SCOPE 32 /* "site-wide", by convention */ 117 118#define INADDR_WHOD_GROUP (u_long)0xe0000103 /* 224.0.1.3 */ 119 /* (belongs in protocols/rwhod.h) */ 120 121int insecure_mode; 122int quiet_mode; 123int iff_flag = IFF_POINTOPOINT; 124int multicast_mode = NO_MULTICAST; 125int multicast_scope; 126struct sockaddr_in multicast_addr = 127 { sizeof multicast_addr, AF_INET, 0, { 0 }, { 0 } }; 128 129/* 130 * Alarm interval. Don't forget to change the down time check in ruptime 131 * if this is changed. 132 */ 133#define AL_INTERVAL (3 * 60) 134 135char myname[MAXHOSTNAMELEN]; 136 137/* 138 * We communicate with each neighbor in a list constructed at the time we're 139 * started up. Neighbors are currently directly connected via a hardware 140 * interface. 141 */ 142struct neighbor { 143 struct neighbor *n_next; 144 char *n_name; /* interface name */ 145 struct sockaddr *n_addr; /* who to send to */ 146 int n_addrlen; /* size of address */ 147 int n_flags; /* should forward?, interface flags */ 148}; 149 150struct neighbor *neighbors; 151struct whod mywd; 152struct servent *sp; 153int s, utmpf; 154 155#define WHDRSIZE (int)(sizeof(mywd) - sizeof(mywd.wd_we)) 156 157void run_as(uid_t *, gid_t *); 158int configure(int); 159void getboottime(int); 160void onalrm(int); 161void quit(const char *); 162void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *); 163int verify(char *, int); 164static void usage(void); 165#ifdef DEBUG 166char *interval(int, char *); 167void Sendto(int, const void *, size_t, int, const struct sockaddr *, int); 168#define sendto Sendto 169#endif 170 171int 172main(int argc, char *argv[]) 173{ 174 struct sockaddr_in from; 175 struct stat st; 176 char path[64]; 177 int on = 1; 178 char *cp; 179 struct sockaddr_in soin; 180 uid_t unpriv_uid; 181 gid_t unpriv_gid; 182 183 if (getuid()) 184 errx(1, "not super user"); 185 186 run_as(&unpriv_uid, &unpriv_gid); 187 188 argv++; argc--; 189 while (argc > 0 && *argv[0] == '-') { 190 if (strcmp(*argv, "-m") == 0) { 191 if (argc > 1 && isdigit(*(argv + 1)[0])) { 192 argv++, argc--; 193 multicast_mode = SCOPED_MULTICAST; 194 multicast_scope = atoi(*argv); 195 if (multicast_scope > MAX_MULTICAST_SCOPE) 196 errx(1, "ttl must not exceed %u", 197 MAX_MULTICAST_SCOPE); 198 } 199 else multicast_mode = PER_INTERFACE_MULTICAST; 200 } 201 else if (strcmp(*argv, "-i") == 0) 202 insecure_mode = 1; 203 else if (strcmp(*argv, "-l") == 0) 204 quiet_mode = 1; 205 else if (strcmp(*argv, "-p") == 0) 206 iff_flag = 0; 207 else 208 usage(); 209 argv++, argc--; 210 } 211 if (argc > 0) 212 usage(); 213#ifndef DEBUG 214 daemon(1, 0); 215#endif 216 (void) signal(SIGHUP, getboottime); 217 openlog("rwhod", LOG_PID, LOG_DAEMON); 218 sp = getservbyname("who", "udp"); 219 if (sp == NULL) { 220 syslog(LOG_ERR, "who/udp: unknown service"); 221 exit(1); 222 } 223 if (chdir(_PATH_RWHODIR) < 0) { 224 syslog(LOG_ERR, "%s: %m", _PATH_RWHODIR); 225 exit(1); 226 } 227 /* 228 * Establish host name as returned by system. 229 */ 230 if (gethostname(myname, sizeof(myname) - 1) < 0) { 231 syslog(LOG_ERR, "gethostname: %m"); 232 exit(1); 233 } 234 if ((cp = index(myname, '.')) != NULL) 235 *cp = '\0'; 236 strncpy(mywd.wd_hostname, myname, sizeof(mywd.wd_hostname) - 1); 237 mywd.wd_hostname[sizeof(mywd.wd_hostname) - 1] = '\0'; 238#ifdef __APPLE__ 239 utmpf = open(_PATH_UTMPX, O_RDONLY|O_CREAT, 0644); 240#else 241 utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644); 242#endif 243 if (utmpf < 0) { 244#ifdef __APPLE__ 245 syslog(LOG_ERR, "%s: %m", _PATH_UTMPX); 246#else 247 syslog(LOG_ERR, "%s: %m", _PATH_UTMP); 248#endif 249 exit(1); 250 } 251 getboottime(0); 252 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 253 syslog(LOG_ERR, "socket: %m"); 254 exit(1); 255 } 256 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) { 257 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 258 exit(1); 259 } 260 memset(&soin, 0, sizeof(soin)); 261 soin.sin_len = sizeof(soin); 262 soin.sin_family = AF_INET; 263 soin.sin_port = sp->s_port; 264 if (bind(s, (struct sockaddr *)&soin, sizeof(soin)) < 0) { 265 syslog(LOG_ERR, "bind: %m"); 266 exit(1); 267 } 268 setgid(unpriv_gid); 269 setgroups(1, &unpriv_gid); /* XXX BOGUS groups[0] = egid */ 270 setuid(unpriv_uid); 271 if (!configure(s)) 272 exit(1); 273 if (!quiet_mode) { 274 signal(SIGALRM, onalrm); 275 onalrm(0); 276 } 277 for (;;) { 278 struct whod wd; 279 socklen_t len = sizeof(from); 280 int cc, whod; 281 time_t t; 282 283 cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0, 284 (struct sockaddr *)&from, &len); 285 if (cc <= 0) { 286 if (cc < 0 && errno != EINTR) 287 syslog(LOG_WARNING, "recv: %m"); 288 continue; 289 } 290 if (from.sin_port != sp->s_port && !insecure_mode) { 291 syslog(LOG_WARNING, "%d: bad source port from %s", 292 ntohs(from.sin_port), inet_ntoa(from.sin_addr)); 293 continue; 294 } 295 if (cc < WHDRSIZE) { 296 syslog(LOG_WARNING, "short packet from %s", 297 inet_ntoa(from.sin_addr)); 298 continue; 299 } 300 if (wd.wd_vers != WHODVERSION) 301 continue; 302 if (wd.wd_type != WHODTYPE_STATUS) 303 continue; 304 if (!verify(wd.wd_hostname, sizeof wd.wd_hostname)) { 305 syslog(LOG_WARNING, "malformed host name from %s", 306 inet_ntoa(from.sin_addr)); 307 continue; 308 } 309 (void) snprintf(path, sizeof path, "whod.%s", wd.wd_hostname); 310 /* 311 * Rather than truncating and growing the file each time, 312 * use ftruncate if size is less than previous size. 313 */ 314 whod = open(path, O_WRONLY | O_CREAT, 0644); 315 if (whod < 0) { 316 syslog(LOG_WARNING, "%s: %m", path); 317 continue; 318 } 319#if ENDIAN != BIG_ENDIAN 320 { 321 int i, n = (cc - WHDRSIZE)/sizeof(struct whoent); 322 struct whoent *we; 323 324 /* undo header byte swapping before writing to file */ 325 wd.wd_sendtime = ntohl(wd.wd_sendtime); 326 for (i = 0; i < 3; i++) 327 wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 328 wd.wd_boottime = ntohl(wd.wd_boottime); 329 we = wd.wd_we; 330 for (i = 0; i < n; i++) { 331 we->we_idle = ntohl(we->we_idle); 332 we->we_utmp.out_time = 333 ntohl(we->we_utmp.out_time); 334 we++; 335 } 336 } 337#endif 338 (void) time(&t); 339 wd.wd_recvtime = _time_to_int(t); 340 (void) write(whod, (char *)&wd, cc); 341 if (fstat(whod, &st) < 0 || st.st_size > cc) 342 ftruncate(whod, cc); 343 (void) close(whod); 344 } 345} 346 347static void 348usage() 349{ 350 fprintf(stderr, "usage: rwhod [-i] [-p] [-l] [-m [ttl]]\n"); 351 exit(1); 352} 353 354void 355run_as(uid, gid) 356 uid_t *uid; 357 gid_t *gid; 358{ 359 struct passwd *pw; 360 struct group *gr; 361 362 pw = getpwnam(UNPRIV_USER); 363 if (!pw) { 364 syslog(LOG_ERR, "getpwnam(%s): %m", UNPRIV_USER); 365 exit(1); 366 } 367 *uid = pw->pw_uid; 368 369 gr = getgrnam(UNPRIV_GROUP); 370 if (!gr) { 371 syslog(LOG_ERR, "getgrnam(%s): %m", UNPRIV_GROUP); 372 exit(1); 373 } 374 *gid = gr->gr_gid; 375} 376 377/* 378 * Check out host name for unprintables 379 * and other funnies before allowing a file 380 * to be created. Sorry, but blanks aren't allowed. 381 */ 382int 383verify(name, maxlen) 384 register char *name; 385 register int maxlen; 386{ 387 register int size = 0; 388 389 while (*name && size < maxlen - 1) { 390 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 391 return (0); 392 name++, size++; 393 } 394 *name = '\0'; 395 return (size > 0); 396} 397 398int utmptime; 399int utmpent; 400int utmpsize = 0; 401#ifndef __APPLE__ 402struct utmp *utmp; 403#endif 404int alarmcount; 405 406void 407onalrm(signo) 408 int signo __unused; 409{ 410 register struct neighbor *np; 411 register struct whoent *we = mywd.wd_we, *wlast; 412 register int i; 413 struct stat stb; 414 double avenrun[3]; 415 time_t now; 416 int cc; 417#ifdef __APPLE__ 418 struct utmpx *u; 419#endif 420 421 now = time(NULL); 422 if (alarmcount % 10 == 0) 423 getboottime(0); 424 alarmcount++; 425 (void) fstat(utmpf, &stb); 426 if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) { 427 utmptime = stb.st_mtime; 428#ifdef __APPLE__ 429 utmpsize = stb.st_size > utmpsize; 430 wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1]; 431 utmpent = 0; 432 setutxent(); 433 while ((u = getutxent()) != NULL) 434 if (u->ut_user[0] && u->ut_type == USER_PROCESS) { 435 memcpy(we->we_utmp.out_line, u->ut_line, 436 sizeof(we->we_utmp.out_line)); 437 memcpy(we->we_utmp.out_name, u->ut_user, 438 sizeof(we->we_utmp.out_name)); 439 we->we_utmp.out_time = htonl(u->ut_tv.tv_sec); 440 if (++we > wlast) 441 break; 442 } 443 endutxent(); 444#else 445 if (stb.st_size > utmpsize) { 446 utmpsize = stb.st_size + 10 * sizeof(struct utmp); 447 utmp = (struct utmp *)reallocf(utmp, utmpsize); 448 if (utmp == NULL) { 449 syslog(LOG_WARNING, "malloc failed"); 450 utmpsize = 0; 451 goto done; 452 } 453 } 454 (void) lseek(utmpf, (off_t)0, L_SET); 455 cc = read(utmpf, (char *)utmp, stb.st_size); 456 if (cc < 0) { 457 syslog(LOG_ERR, "read(%s): %m", _PATH_UTMP); 458 goto done; 459 } 460 wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1]; 461 utmpent = cc / sizeof(struct utmp); 462 for (i = 0; i < utmpent; i++) 463 if (utmp[i].ut_name[0]) { 464 memcpy(we->we_utmp.out_line, utmp[i].ut_line, 465 sizeof(utmp[i].ut_line)); 466 memcpy(we->we_utmp.out_name, utmp[i].ut_name, 467 sizeof(utmp[i].ut_name)); 468 we->we_utmp.out_time = htonl(utmp[i].ut_time); 469 if (we >= wlast) 470 break; 471 we++; 472 } 473#endif 474 utmpent = we - mywd.wd_we; 475 } 476 477 /* 478 * The test on utmpent looks silly---after all, if no one is 479 * logged on, why worry about efficiency?---but is useful on 480 * (e.g.) compute servers. 481 */ 482 if (utmpent && chdir(_PATH_DEV)) { 483 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV); 484 exit(1); 485 } 486 we = mywd.wd_we; 487 for (i = 0; i < utmpent; i++) { 488 if (stat(we->we_utmp.out_line, &stb) >= 0) 489 we->we_idle = htonl(now - stb.st_atime); 490 we++; 491 } 492 (void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); 493 for (i = 0; i < 3; i++) 494 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 495 cc = (char *)we - (char *)&mywd; 496 mywd.wd_sendtime = htonl(_time_to_time32(time(NULL))); 497 mywd.wd_vers = WHODVERSION; 498 mywd.wd_type = WHODTYPE_STATUS; 499 if (multicast_mode == SCOPED_MULTICAST) { 500 (void) sendto(s, (char *)&mywd, cc, 0, 501 (struct sockaddr *)&multicast_addr, 502 sizeof(multicast_addr)); 503 } 504 else for (np = neighbors; np != NULL; np = np->n_next) { 505 if (multicast_mode == PER_INTERFACE_MULTICAST && 506 np->n_flags & IFF_MULTICAST) { 507 /* 508 * Select the outgoing interface for the multicast. 509 */ 510 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, 511 &(((struct sockaddr_in *)np->n_addr)->sin_addr), 512 sizeof(struct in_addr)) < 0) { 513 syslog(LOG_ERR, 514 "setsockopt IP_MULTICAST_IF: %m"); 515 exit(1); 516 } 517 (void) sendto(s, (char *)&mywd, cc, 0, 518 (struct sockaddr *)&multicast_addr, 519 sizeof(multicast_addr)); 520 } else (void) sendto(s, (char *)&mywd, cc, 0, 521 np->n_addr, np->n_addrlen); 522 } 523 if (utmpent && chdir(_PATH_RWHODIR)) { 524 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR); 525 exit(1); 526 } 527#ifndef __APPLE__ 528done: 529#endif 530 (void) alarm(AL_INTERVAL); 531} 532 533void 534getboottime(signo) 535 int signo __unused; 536{ 537 int mib[2]; 538 size_t size; 539 struct timeval tm; 540 541 mib[0] = CTL_KERN; 542 mib[1] = KERN_BOOTTIME; 543 size = sizeof(tm); 544 if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) { 545 syslog(LOG_ERR, "cannot get boottime: %m"); 546 exit(1); 547 } 548 mywd.wd_boottime = htonl(_time_to_time32(tm.tv_sec)); 549} 550 551void 552quit(msg) 553 const char *msg; 554{ 555 syslog(LOG_ERR, "%s", msg); 556 exit(1); 557} 558 559#ifdef __APPLE__ 560/* 561 * This macro returns the size of a struct sockaddr when passed 562 * through a routing socket. Basically we round up sa_len to 563 * a multiple of sizeof(long), with a minimum of sizeof(long). 564 * The check for a NULL pointer is just a convenience, probably never used. 565 * The case sa_len == 0 should only apply to empty structures. 566 */ 567#define SA_SIZE(sa) \ 568 ( (!(sa) || ((struct sockaddr *)(sa))->sa_len == 0) ? \ 569 sizeof(long) : \ 570 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(long) - 1) ) ) 571#endif 572 573void 574rt_xaddrs(cp, cplim, rtinfo) 575 register caddr_t cp, cplim; 576 register struct rt_addrinfo *rtinfo; 577{ 578 register struct sockaddr *sa; 579 register int i; 580 581 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info)); 582 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 583 if ((rtinfo->rti_addrs & (1 << i)) == 0) 584 continue; 585 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp; 586 cp += SA_SIZE(sa); 587 } 588} 589 590/* 591 * Figure out device configuration and select 592 * networks which deserve status information. 593 */ 594int 595configure(so) 596 int so; 597{ 598 register struct neighbor *np; 599 register struct if_msghdr *ifm; 600 register struct ifa_msghdr *ifam; 601 struct sockaddr_dl *sdl; 602 size_t needed; 603 int mib[6], flags = 0, len; 604 char *buf, *lim, *next; 605 struct rt_addrinfo info; 606 607 if (multicast_mode != NO_MULTICAST) { 608 multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP); 609 multicast_addr.sin_port = sp->s_port; 610 } 611 612 if (multicast_mode == SCOPED_MULTICAST) { 613 struct ip_mreq mreq; 614 unsigned char ttl; 615 616 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP); 617 mreq.imr_interface.s_addr = htonl(INADDR_ANY); 618 if (setsockopt(so, IPPROTO_IP, IP_ADD_MEMBERSHIP, 619 &mreq, sizeof(mreq)) < 0) { 620 syslog(LOG_ERR, 621 "setsockopt IP_ADD_MEMBERSHIP: %m"); 622 return(0); 623 } 624 ttl = multicast_scope; 625 if (setsockopt(so, IPPROTO_IP, IP_MULTICAST_TTL, 626 &ttl, sizeof(ttl)) < 0) { 627 syslog(LOG_ERR, 628 "setsockopt IP_MULTICAST_TTL: %m"); 629 return(0); 630 } 631 return(1); 632 } 633 634 mib[0] = CTL_NET; 635 mib[1] = PF_ROUTE; 636 mib[2] = 0; 637 mib[3] = AF_INET; 638 mib[4] = NET_RT_IFLIST; 639 mib[5] = 0; 640 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 641 quit("route-sysctl-estimate"); 642 if ((buf = malloc(needed)) == NULL) 643 quit("malloc"); 644 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 645 quit("actual retrieval of interface table"); 646 lim = buf + needed; 647 648 sdl = NULL; /* XXX just to keep gcc -Wall happy */ 649 for (next = buf; next < lim; next += ifm->ifm_msglen) { 650 ifm = (struct if_msghdr *)next; 651 if (ifm->ifm_type == RTM_IFINFO) { 652 sdl = (struct sockaddr_dl *)(ifm + 1); 653 flags = ifm->ifm_flags; 654 continue; 655 } 656 if ((flags & IFF_UP) == 0 || 657 (flags & (((multicast_mode == PER_INTERFACE_MULTICAST) ? 658 IFF_MULTICAST : 0) | 659 IFF_BROADCAST|iff_flag)) == 0) 660 continue; 661 if (ifm->ifm_type != RTM_NEWADDR) 662 quit("out of sync parsing NET_RT_IFLIST"); 663 ifam = (struct ifa_msghdr *)ifm; 664 info.rti_addrs = ifam->ifam_addrs; 665 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam, 666 &info); 667 /* gag, wish we could get rid of Internet dependencies */ 668#define dstaddr info.rti_info[RTAX_BRD] 669#define ifaddr info.rti_info[RTAX_IFA] 670#define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr 671#define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port 672 if (dstaddr == 0 || dstaddr->sa_family != AF_INET) 673 continue; 674 PORT_SA(dstaddr) = sp->s_port; 675 for (np = neighbors; np != NULL; np = np->n_next) 676 if (memcmp(sdl->sdl_data, np->n_name, 677 sdl->sdl_nlen) == 0 && 678 IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr)) 679 break; 680 if (np != NULL) 681 continue; 682 len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1; 683 np = (struct neighbor *)malloc(len); 684 if (np == NULL) 685 quit("malloc of neighbor structure"); 686 memset(np, 0, len); 687 np->n_flags = flags; 688 np->n_addr = (struct sockaddr *)(np + 1); 689 np->n_addrlen = dstaddr->sa_len; 690 np->n_name = np->n_addrlen + (char *)np->n_addr; 691 memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen); 692 memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen); 693 if (multicast_mode == PER_INTERFACE_MULTICAST && 694 (flags & IFF_MULTICAST) && 695 !(flags & IFF_LOOPBACK)) { 696 struct ip_mreq mreq; 697 698 memcpy((char *)np->n_addr, (char *)ifaddr, 699 np->n_addrlen); 700 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP); 701 mreq.imr_interface.s_addr = 702 ((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr; 703 if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, 704 &mreq, sizeof(mreq)) < 0) { 705 syslog(LOG_ERR, 706 "setsockopt IP_ADD_MEMBERSHIP: %m"); 707#if 0 708 /* Fall back to broadcast on this if. */ 709 np->n_flags &= ~IFF_MULTICAST; 710#else 711 free((char *)np); 712 continue; 713#endif 714 } 715 } 716 np->n_next = neighbors; 717 neighbors = np; 718 } 719 free(buf); 720 return (1); 721} 722 723#ifdef DEBUG 724void 725Sendto(s, buf, cc, flags, to, tolen) 726 int s; 727 const void *buf; 728 size_t cc; 729 int flags; 730 const struct sockaddr *to; 731 int tolen; 732{ 733 register struct whod *w = (struct whod *)buf; 734 register struct whoent *we; 735 struct sockaddr_in *sin = (struct sockaddr_in *)to; 736 737 printf("sendto %x.%d\n", ntohl(sin->sin_addr.s_addr), 738 ntohs(sin->sin_port)); 739 printf("hostname %s %s\n", w->wd_hostname, 740 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 741 printf("load %4.2f, %4.2f, %4.2f\n", 742 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 743 ntohl(w->wd_loadav[2]) / 100.0); 744 cc -= WHDRSIZE; 745 for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) { 746 time_t t = _time32_to_time(ntohl(we->we_utmp.out_time)); 747 printf("%-8.8s %s:%s %.12s", 748 we->we_utmp.out_name, 749 w->wd_hostname, we->we_utmp.out_line, 750 ctime(&t)+4); 751 we->we_idle = ntohl(we->we_idle) / 60; 752 if (we->we_idle) { 753 if (we->we_idle >= 100*60) 754 we->we_idle = 100*60 - 1; 755 if (we->we_idle >= 60) 756 printf(" %2d", we->we_idle / 60); 757 else 758 printf(" "); 759 printf(":%02d", we->we_idle % 60); 760 } 761 printf("\n"); 762 } 763} 764 765char * 766interval(time, updown) 767 int time; 768 char *updown; 769{ 770 static char resbuf[32]; 771 int days, hours, minutes; 772 773 if (time < 0 || time > 3*30*24*60*60) { 774 (void) sprintf(resbuf, " %s ??:??", updown); 775 return (resbuf); 776 } 777 minutes = (time + 59) / 60; /* round to minutes */ 778 hours = minutes / 60; minutes %= 60; 779 days = hours / 24; hours %= 24; 780 if (days) 781 (void) sprintf(resbuf, "%s %2d+%02d:%02d", 782 updown, days, hours, minutes); 783 else 784 (void) sprintf(resbuf, "%s %2d:%02d", 785 updown, hours, minutes); 786 return (resbuf); 787} 788#endif 789