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