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 35 static 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 41 static 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 113 int multicast_mode = NO_MULTICAST; 114 int multicast_scope; 115 struct 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 123 char 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 */ 130 struct 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 138 struct neighbor *neighbors; 139 struct whod mywd; 140 struct servent *sp; 141 int s, utmpf; 142 143 #define WHDRSIZE (sizeof(mywd) - sizeof(mywd.wd_we)) 144 145 void run_as __P((uid_t *, gid_t *)); 146 int configure __P((int)); 147 void getboottime __P((int)); 148 void onalrm __P((int)); 149 void quit __P((char *)); 150 void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *)); 151 int verify __P((char *, int)); 152 #ifdef DEBUG 153 char *interval __P((int, char *)); 154 void Sendto __P((int, const void *, size_t, int, 155 const struct sockaddr *, int)); 156 #define sendto Sendto 157 #endif 158 159 int 160 main(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) { 200 usage: 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(myname) - 1); 227 utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644); 228 if (utmpf < 0) { 229 syslog(LOG_ERR, "%s: %m", _PATH_UTMP); 230 exit(1); 231 } 232 getboottime(0); 233 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 234 syslog(LOG_ERR, "socket: %m"); 235 exit(1); 236 } 237 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) { 238 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 239 exit(1); 240 } 241 memset(&sin, 0, sizeof(sin)); 242 sin.sin_len = sizeof(sin); 243 sin.sin_family = AF_INET; 244 sin.sin_port = sp->s_port; 245 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 246 syslog(LOG_ERR, "bind: %m"); 247 exit(1); 248 } 249 setgid(unpriv_gid); 250 setgroups(1, &unpriv_gid); /* XXX BOGUS groups[0] = egid */ 251 setuid(unpriv_uid); 252 if (!configure(s)) 253 exit(1); 254 signal(SIGALRM, onalrm); 255 onalrm(0); 256 for (;;) { 257 struct whod wd; 258 int cc, whod, len = sizeof(from); 259 260 cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0, 261 (struct sockaddr *)&from, &len); 262 if (cc <= 0) { 263 if (cc < 0 && errno != EINTR) 264 syslog(LOG_WARNING, "recv: %m"); 265 continue; 266 } 267 if (from.sin_port != sp->s_port) { 268 syslog(LOG_WARNING, "%d: bad from port", 269 ntohs(from.sin_port)); 270 continue; 271 } 272 if (wd.wd_vers != WHODVERSION) 273 continue; 274 if (wd.wd_type != WHODTYPE_STATUS) 275 continue; 276 if (!verify(wd.wd_hostname, sizeof wd.wd_hostname)) { 277 syslog(LOG_WARNING, "malformed host name from %x", 278 from.sin_addr); 279 continue; 280 } 281 (void) sprintf(path, "whod.%s", wd.wd_hostname); 282 /* 283 * Rather than truncating and growing the file each time, 284 * use ftruncate if size is less than previous size. 285 */ 286 whod = open(path, O_WRONLY | O_CREAT, 0644); 287 if (whod < 0) { 288 syslog(LOG_WARNING, "%s: %m", path); 289 continue; 290 } 291 #if ENDIAN != BIG_ENDIAN 292 { 293 int i, n = (cc - WHDRSIZE)/sizeof(struct whoent); 294 struct whoent *we; 295 296 /* undo header byte swapping before writing to file */ 297 wd.wd_sendtime = ntohl(wd.wd_sendtime); 298 for (i = 0; i < 3; i++) 299 wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 300 wd.wd_boottime = ntohl(wd.wd_boottime); 301 we = wd.wd_we; 302 for (i = 0; i < n; i++) { 303 we->we_idle = ntohl(we->we_idle); 304 we->we_utmp.out_time = 305 ntohl(we->we_utmp.out_time); 306 we++; 307 } 308 } 309 #endif 310 (void) time((time_t *)&wd.wd_recvtime); 311 (void) write(whod, (char *)&wd, cc); 312 if (fstat(whod, &st) < 0 || st.st_size > cc) 313 ftruncate(whod, cc); 314 (void) close(whod); 315 } 316 } 317 318 319 void 320 run_as(uid, gid) 321 uid_t *uid; 322 gid_t *gid; 323 { 324 struct passwd *pw; 325 struct group *gr; 326 327 pw = getpwnam(UNPRIV_USER); 328 if (!pw) { 329 syslog(LOG_ERR, "getpwnam(%s): %m", UNPRIV_USER); 330 exit(1); 331 } 332 *uid = pw->pw_uid; 333 334 gr = getgrnam(UNPRIV_GROUP); 335 if (!gr) { 336 syslog(LOG_ERR, "getgrnam(%s): %m", UNPRIV_GROUP); 337 exit(1); 338 } 339 *gid = gr->gr_gid; 340 } 341 342 /* 343 * Check out host name for unprintables 344 * and other funnies before allowing a file 345 * to be created. Sorry, but blanks aren't allowed. 346 */ 347 int 348 verify(name, maxlen) 349 register char *name; 350 register int maxlen; 351 { 352 register int size = 0; 353 354 while (*name && size < maxlen) { 355 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 356 return (0); 357 name++, size++; 358 } 359 *name = '\0'; 360 return (size > 0); 361 } 362 363 int utmptime; 364 int utmpent; 365 int utmpsize = 0; 366 struct utmp *utmp; 367 int alarmcount; 368 369 void 370 onalrm(signo) 371 int signo; 372 { 373 register struct neighbor *np; 374 register struct whoent *we = mywd.wd_we, *wlast; 375 register int i; 376 struct stat stb; 377 double avenrun[3]; 378 time_t now; 379 int cc; 380 381 now = time(NULL); 382 if (alarmcount % 10 == 0) 383 getboottime(0); 384 alarmcount++; 385 (void) fstat(utmpf, &stb); 386 if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) { 387 utmptime = stb.st_mtime; 388 if (stb.st_size > utmpsize) { 389 utmpsize = stb.st_size + 10 * sizeof(struct utmp); 390 if (utmp) 391 utmp = (struct utmp *)realloc(utmp, utmpsize); 392 else 393 utmp = (struct utmp *)malloc(utmpsize); 394 if (! utmp) { 395 fprintf(stderr, "rwhod: malloc failed\n"); 396 utmpsize = 0; 397 goto done; 398 } 399 } 400 (void) lseek(utmpf, (off_t)0, L_SET); 401 cc = read(utmpf, (char *)utmp, stb.st_size); 402 if (cc < 0) { 403 fprintf(stderr, "rwhod: %s: %s\n", 404 _PATH_UTMP, strerror(errno)); 405 goto done; 406 } 407 wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1]; 408 utmpent = cc / sizeof(struct utmp); 409 for (i = 0; i < utmpent; i++) 410 if (utmp[i].ut_name[0]) { 411 memcpy(we->we_utmp.out_line, utmp[i].ut_line, 412 sizeof(utmp[i].ut_line)); 413 memcpy(we->we_utmp.out_name, utmp[i].ut_name, 414 sizeof(utmp[i].ut_name)); 415 we->we_utmp.out_time = htonl(utmp[i].ut_time); 416 if (we >= wlast) 417 break; 418 we++; 419 } 420 utmpent = we - mywd.wd_we; 421 } 422 423 /* 424 * The test on utmpent looks silly---after all, if no one is 425 * logged on, why worry about efficiency?---but is useful on 426 * (e.g.) compute servers. 427 */ 428 if (utmpent && chdir(_PATH_DEV)) { 429 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV); 430 exit(1); 431 } 432 we = mywd.wd_we; 433 for (i = 0; i < utmpent; i++) { 434 if (stat(we->we_utmp.out_line, &stb) >= 0) 435 we->we_idle = htonl(now - stb.st_atime); 436 we++; 437 } 438 (void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); 439 for (i = 0; i < 3; i++) 440 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 441 cc = (char *)we - (char *)&mywd; 442 mywd.wd_sendtime = htonl(time(0)); 443 mywd.wd_vers = WHODVERSION; 444 mywd.wd_type = WHODTYPE_STATUS; 445 if (multicast_mode == SCOPED_MULTICAST) { 446 (void) sendto(s, (char *)&mywd, cc, 0, 447 (struct sockaddr *)&multicast_addr, 448 sizeof(multicast_addr)); 449 } 450 else for (np = neighbors; np != NULL; np = np->n_next) { 451 if (multicast_mode == PER_INTERFACE_MULTICAST && 452 np->n_flags & IFF_MULTICAST) { 453 /* 454 * Select the outgoing interface for the multicast. 455 */ 456 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, 457 &(((struct sockaddr_in *)np->n_addr)->sin_addr), 458 sizeof(struct in_addr)) < 0) { 459 syslog(LOG_ERR, 460 "setsockopt IP_MULTICAST_IF: %m"); 461 exit(1); 462 } 463 (void) sendto(s, (char *)&mywd, cc, 0, 464 (struct sockaddr *)&multicast_addr, 465 sizeof(multicast_addr)); 466 } else (void) sendto(s, (char *)&mywd, cc, 0, 467 np->n_addr, np->n_addrlen); 468 } 469 if (utmpent && chdir(_PATH_RWHODIR)) { 470 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR); 471 exit(1); 472 } 473 done: 474 (void) alarm(AL_INTERVAL); 475 } 476 477 void 478 getboottime(signo) 479 int signo; 480 { 481 int mib[2]; 482 size_t size; 483 struct timeval tm; 484 485 mib[0] = CTL_KERN; 486 mib[1] = KERN_BOOTTIME; 487 size = sizeof(tm); 488 if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) { 489 syslog(LOG_ERR, "cannot get boottime: %m"); 490 exit(1); 491 } 492 mywd.wd_boottime = htonl(tm.tv_sec); 493 } 494 495 void 496 quit(msg) 497 char *msg; 498 { 499 syslog(LOG_ERR, msg); 500 exit(1); 501 } 502 503 #define ROUNDUP(a) \ 504 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 505 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 506 507 void 508 rt_xaddrs(cp, cplim, rtinfo) 509 register caddr_t cp, cplim; 510 register struct rt_addrinfo *rtinfo; 511 { 512 register struct sockaddr *sa; 513 register int i; 514 515 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info)); 516 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 517 if ((rtinfo->rti_addrs & (1 << i)) == 0) 518 continue; 519 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp; 520 ADVANCE(cp, sa); 521 } 522 } 523 524 /* 525 * Figure out device configuration and select 526 * networks which deserve status information. 527 */ 528 int 529 configure(s) 530 int s; 531 { 532 register struct neighbor *np; 533 register struct if_msghdr *ifm; 534 register struct ifa_msghdr *ifam; 535 struct sockaddr_dl *sdl; 536 size_t needed; 537 int mib[6], flags = 0, len; 538 char *buf, *lim, *next; 539 struct rt_addrinfo info; 540 541 if (multicast_mode != NO_MULTICAST) { 542 multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP); 543 multicast_addr.sin_port = sp->s_port; 544 } 545 546 if (multicast_mode == SCOPED_MULTICAST) { 547 struct ip_mreq mreq; 548 unsigned char ttl; 549 550 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP); 551 mreq.imr_interface.s_addr = htonl(INADDR_ANY); 552 if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, 553 &mreq, sizeof(mreq)) < 0) { 554 syslog(LOG_ERR, 555 "setsockopt IP_ADD_MEMBERSHIP: %m"); 556 return(0); 557 } 558 ttl = multicast_scope; 559 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, 560 &ttl, sizeof(ttl)) < 0) { 561 syslog(LOG_ERR, 562 "setsockopt IP_MULTICAST_TTL: %m"); 563 return(0); 564 } 565 return(1); 566 } 567 568 mib[0] = CTL_NET; 569 mib[1] = PF_ROUTE; 570 mib[2] = 0; 571 mib[3] = AF_INET; 572 mib[4] = NET_RT_IFLIST; 573 mib[5] = 0; 574 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 575 quit("route-sysctl-estimate"); 576 if ((buf = malloc(needed)) == NULL) 577 quit("malloc"); 578 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 579 quit("actual retrieval of interface table"); 580 lim = buf + needed; 581 582 sdl = NULL; /* XXX just to keep gcc -Wall happy */ 583 for (next = buf; next < lim; next += ifm->ifm_msglen) { 584 ifm = (struct if_msghdr *)next; 585 if (ifm->ifm_type == RTM_IFINFO) { 586 sdl = (struct sockaddr_dl *)(ifm + 1); 587 flags = ifm->ifm_flags; 588 continue; 589 } 590 if ((flags & IFF_UP) == 0 || 591 (flags & (((multicast_mode == PER_INTERFACE_MULTICAST) ? 592 IFF_MULTICAST : 0) | 593 IFF_BROADCAST|IFF_POINTOPOINT)) == 0) 594 continue; 595 if (ifm->ifm_type != RTM_NEWADDR) 596 quit("out of sync parsing NET_RT_IFLIST"); 597 ifam = (struct ifa_msghdr *)ifm; 598 info.rti_addrs = ifam->ifam_addrs; 599 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam, 600 &info); 601 /* gag, wish we could get rid of Internet dependencies */ 602 #define dstaddr info.rti_info[RTAX_BRD] 603 #define ifaddr info.rti_info[RTAX_IFA] 604 #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr 605 #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port 606 if (dstaddr == 0 || dstaddr->sa_family != AF_INET) 607 continue; 608 PORT_SA(dstaddr) = sp->s_port; 609 for (np = neighbors; np != NULL; np = np->n_next) 610 if (memcmp(sdl->sdl_data, np->n_name, 611 sdl->sdl_nlen) == 0 && 612 IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr)) 613 break; 614 if (np != NULL) 615 continue; 616 len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1; 617 np = (struct neighbor *)malloc(len); 618 if (np == NULL) 619 quit("malloc of neighbor structure"); 620 memset(np, 0, len); 621 np->n_flags = flags; 622 np->n_addr = (struct sockaddr *)(np + 1); 623 np->n_addrlen = dstaddr->sa_len; 624 np->n_name = np->n_addrlen + (char *)np->n_addr; 625 memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen); 626 memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen); 627 if (multicast_mode == PER_INTERFACE_MULTICAST && 628 (flags & IFF_MULTICAST) && 629 !(flags & IFF_LOOPBACK)) { 630 struct ip_mreq mreq; 631 632 memcpy((char *)np->n_addr, (char *)ifaddr, 633 np->n_addrlen); 634 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP); 635 mreq.imr_interface.s_addr = 636 ((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr; 637 if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, 638 &mreq, sizeof(mreq)) < 0) { 639 syslog(LOG_ERR, 640 "setsockopt IP_ADD_MEMBERSHIP: %m"); 641 #if 0 642 /* Fall back to broadcast on this if. */ 643 np->n_flags &= ~IFF_MULTICAST; 644 #else 645 free((char *)np); 646 continue; 647 #endif 648 } 649 } 650 np->n_next = neighbors; 651 neighbors = np; 652 } 653 free(buf); 654 return (1); 655 } 656 657 #ifdef DEBUG 658 void 659 Sendto(s, buf, cc, flags, to, tolen) 660 int s; 661 const void *buf; 662 size_t cc; 663 int flags; 664 const struct sockaddr *to; 665 int tolen; 666 { 667 register struct whod *w = (struct whod *)buf; 668 register struct whoent *we; 669 struct sockaddr_in *sin = (struct sockaddr_in *)to; 670 671 printf("sendto %x.%d\n", ntohl(sin->sin_addr.s_addr), 672 ntohs(sin->sin_port)); 673 printf("hostname %s %s\n", w->wd_hostname, 674 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 675 printf("load %4.2f, %4.2f, %4.2f\n", 676 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 677 ntohl(w->wd_loadav[2]) / 100.0); 678 cc -= WHDRSIZE; 679 for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) { 680 time_t t = ntohl(we->we_utmp.out_time); 681 printf("%-8.8s %s:%s %.12s", 682 we->we_utmp.out_name, 683 w->wd_hostname, we->we_utmp.out_line, 684 ctime(&t)+4); 685 we->we_idle = ntohl(we->we_idle) / 60; 686 if (we->we_idle) { 687 if (we->we_idle >= 100*60) 688 we->we_idle = 100*60 - 1; 689 if (we->we_idle >= 60) 690 printf(" %2d", we->we_idle / 60); 691 else 692 printf(" "); 693 printf(":%02d", we->we_idle % 60); 694 } 695 printf("\n"); 696 } 697 } 698 699 char * 700 interval(time, updown) 701 int time; 702 char *updown; 703 { 704 static char resbuf[32]; 705 int days, hours, minutes; 706 707 if (time < 0 || time > 3*30*24*60*60) { 708 (void) sprintf(resbuf, " %s ??:??", updown); 709 return (resbuf); 710 } 711 minutes = (time + 59) / 60; /* round to minutes */ 712 hours = minutes / 60; minutes %= 60; 713 days = hours / 24; hours %= 24; 714 if (days) 715 (void) sprintf(resbuf, "%s %2d+%02d:%02d", 716 updown, days, hours, minutes); 717 else 718 (void) sprintf(resbuf, "%s %2d:%02d", 719 updown, hours, minutes); 720 return (resbuf); 721 } 722 #endif 723