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 const 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 #if 0 42 static char sccsid[] = "@(#)rwhod.c 8.1 (Berkeley) 6/6/93"; 43 #endif 44 static const char rcsid[] = 45 "$Id$"; 46 #endif /* not lint */ 47 48 #include <sys/param.h> 49 #include <sys/socket.h> 50 #include <sys/stat.h> 51 #include <sys/signal.h> 52 #include <sys/ioctl.h> 53 #include <sys/sysctl.h> 54 55 #include <net/if.h> 56 #include <net/if_dl.h> 57 #include <net/route.h> 58 #include <netinet/in.h> 59 #include <protocols/rwhod.h> 60 61 #include <ctype.h> 62 #include <err.h> 63 #include <errno.h> 64 #include <fcntl.h> 65 #include <netdb.h> 66 #include <paths.h> 67 #include <stdio.h> 68 #include <stdlib.h> 69 #include <string.h> 70 #include <syslog.h> 71 #include <unistd.h> 72 #include <utmp.h> 73 #include <pwd.h> 74 #include <grp.h> 75 76 /* 77 * This version of Berkeley's rwhod has been modified to use IP multicast 78 * datagrams, under control of a new command-line option: 79 * 80 * rwhod -m causes rwhod to use IP multicast (instead of 81 * broadcast or unicast) on all interfaces that have 82 * the IFF_MULTICAST flag set in their "ifnet" structs 83 * (excluding the loopback interface). The multicast 84 * reports are sent with a time-to-live of 1, to prevent 85 * forwarding beyond the directly-connected subnet(s). 86 * 87 * rwhod -m <ttl> causes rwhod to send IP multicast datagrams with a 88 * time-to-live of <ttl>, via a SINGLE interface rather 89 * than all interfaces. <ttl> must be between 0 and 90 * MAX_MULTICAST_SCOPE, defined below. Note that "-m 1" 91 * is different than "-m", in that "-m 1" specifies 92 * transmission on one interface only. 93 * 94 * When "-m" is used without a <ttl> argument, the program accepts multicast 95 * rwhod reports from all multicast-capable interfaces. If a <ttl> argument 96 * is given, it accepts multicast reports from only one interface, the one 97 * on which reports are sent (which may be controlled via the host's routing 98 * table). Regardless of the "-m" option, the program accepts broadcast or 99 * unicast reports from all interfaces. Thus, this program will hear the 100 * reports of old, non-multicasting rwhods, but, if multicasting is used, 101 * those old rwhods won't hear the reports generated by this program. 102 * 103 * -- Steve Deering, Stanford University, February 1989 104 */ 105 106 #define UNPRIV_USER "daemon" 107 #define UNPRIV_GROUP "daemon" 108 109 #define NO_MULTICAST 0 /* multicast modes */ 110 #define PER_INTERFACE_MULTICAST 1 111 #define SCOPED_MULTICAST 2 112 113 #define MAX_MULTICAST_SCOPE 32 /* "site-wide", by convention */ 114 115 #define INADDR_WHOD_GROUP (u_long)0xe0000103 /* 224.0.1.3 */ 116 /* (belongs in protocols/rwhod.h) */ 117 118 int multicast_mode = NO_MULTICAST; 119 int multicast_scope; 120 struct sockaddr_in multicast_addr = { sizeof multicast_addr, AF_INET }; 121 122 /* 123 * Alarm interval. Don't forget to change the down time check in ruptime 124 * if this is changed. 125 */ 126 #define AL_INTERVAL (3 * 60) 127 128 char myname[MAXHOSTNAMELEN]; 129 130 /* 131 * We communicate with each neighbor in a list constructed at the time we're 132 * started up. Neighbors are currently directly connected via a hardware 133 * interface. 134 */ 135 struct neighbor { 136 struct neighbor *n_next; 137 char *n_name; /* interface name */ 138 struct sockaddr *n_addr; /* who to send to */ 139 int n_addrlen; /* size of address */ 140 int n_flags; /* should forward?, interface flags */ 141 }; 142 143 struct neighbor *neighbors; 144 struct whod mywd; 145 struct servent *sp; 146 int s, utmpf; 147 148 #define WHDRSIZE (sizeof(mywd) - sizeof(mywd.wd_we)) 149 150 void run_as __P((uid_t *, gid_t *)); 151 int configure __P((int)); 152 void getboottime __P((int)); 153 void onalrm __P((int)); 154 void quit __P((char *)); 155 void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *)); 156 int verify __P((char *, int)); 157 static void usage __P((void)); 158 #ifdef DEBUG 159 char *interval __P((int, char *)); 160 void Sendto __P((int, const void *, size_t, int, 161 const struct sockaddr *, int)); 162 #define sendto Sendto 163 #endif 164 165 int 166 main(argc, argv) 167 int argc; 168 char *argv[]; 169 { 170 struct sockaddr_in from; 171 struct stat st; 172 char path[64]; 173 int on = 1; 174 char *cp; 175 struct sockaddr_in sin; 176 uid_t unpriv_uid; 177 gid_t unpriv_gid; 178 179 if (getuid()) 180 errx(1, "not super user"); 181 182 run_as(&unpriv_uid, &unpriv_gid); 183 184 argv++; argc--; 185 while (argc > 0 && *argv[0] == '-') { 186 if (strcmp(*argv, "-m") == 0) { 187 if (argc > 1 && isdigit(*(argv + 1)[0])) { 188 argv++, argc--; 189 multicast_mode = SCOPED_MULTICAST; 190 multicast_scope = atoi(*argv); 191 if (multicast_scope > MAX_MULTICAST_SCOPE) 192 errx(1, "ttl must not exceed %u", 193 MAX_MULTICAST_SCOPE); 194 } 195 else multicast_mode = PER_INTERFACE_MULTICAST; 196 } 197 else 198 usage(); 199 argv++, argc--; 200 } 201 if (argc > 0) 202 usage(); 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) snprintf(path, sizeof 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 static void 320 usage() 321 { 322 fprintf(stderr, "usage: rwhod [-m [ttl]]\n"); 323 exit(1); 324 } 325 326 void 327 run_as(uid, gid) 328 uid_t *uid; 329 gid_t *gid; 330 { 331 struct passwd *pw; 332 struct group *gr; 333 334 pw = getpwnam(UNPRIV_USER); 335 if (!pw) { 336 syslog(LOG_ERR, "getpwnam(%s): %m", UNPRIV_USER); 337 exit(1); 338 } 339 *uid = pw->pw_uid; 340 341 gr = getgrnam(UNPRIV_GROUP); 342 if (!gr) { 343 syslog(LOG_ERR, "getgrnam(%s): %m", UNPRIV_GROUP); 344 exit(1); 345 } 346 *gid = gr->gr_gid; 347 } 348 349 /* 350 * Check out host name for unprintables 351 * and other funnies before allowing a file 352 * to be created. Sorry, but blanks aren't allowed. 353 */ 354 int 355 verify(name, maxlen) 356 register char *name; 357 register int maxlen; 358 { 359 register int size = 0; 360 361 while (*name && size < maxlen - 1) { 362 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 363 return (0); 364 name++, size++; 365 } 366 *name = '\0'; 367 return (size > 0); 368 } 369 370 int utmptime; 371 int utmpent; 372 int utmpsize = 0; 373 struct utmp *utmp; 374 int alarmcount; 375 376 void 377 onalrm(signo) 378 int signo; 379 { 380 register struct neighbor *np; 381 register struct whoent *we = mywd.wd_we, *wlast; 382 register int i; 383 struct stat stb; 384 double avenrun[3]; 385 time_t now; 386 int cc; 387 388 now = time(NULL); 389 if (alarmcount % 10 == 0) 390 getboottime(0); 391 alarmcount++; 392 (void) fstat(utmpf, &stb); 393 if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) { 394 utmptime = stb.st_mtime; 395 if (stb.st_size > utmpsize) { 396 utmpsize = stb.st_size + 10 * sizeof(struct utmp); 397 if (utmp) 398 utmp = (struct utmp *)realloc(utmp, utmpsize); 399 else 400 utmp = (struct utmp *)malloc(utmpsize); 401 if (! utmp) { 402 syslog(LOG_WARNING, "malloc failed"); 403 utmpsize = 0; 404 goto done; 405 } 406 } 407 (void) lseek(utmpf, (off_t)0, L_SET); 408 cc = read(utmpf, (char *)utmp, stb.st_size); 409 if (cc < 0) { 410 syslog(LOG_ERR, "read(%s): %m", _PATH_UTMP); 411 goto done; 412 } 413 wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1]; 414 utmpent = cc / sizeof(struct utmp); 415 for (i = 0; i < utmpent; i++) 416 if (utmp[i].ut_name[0]) { 417 memcpy(we->we_utmp.out_line, utmp[i].ut_line, 418 sizeof(utmp[i].ut_line)); 419 memcpy(we->we_utmp.out_name, utmp[i].ut_name, 420 sizeof(utmp[i].ut_name)); 421 we->we_utmp.out_time = htonl(utmp[i].ut_time); 422 if (we >= wlast) 423 break; 424 we++; 425 } 426 utmpent = we - mywd.wd_we; 427 } 428 429 /* 430 * The test on utmpent looks silly---after all, if no one is 431 * logged on, why worry about efficiency?---but is useful on 432 * (e.g.) compute servers. 433 */ 434 if (utmpent && chdir(_PATH_DEV)) { 435 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV); 436 exit(1); 437 } 438 we = mywd.wd_we; 439 for (i = 0; i < utmpent; i++) { 440 if (stat(we->we_utmp.out_line, &stb) >= 0) 441 we->we_idle = htonl(now - stb.st_atime); 442 we++; 443 } 444 (void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); 445 for (i = 0; i < 3; i++) 446 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 447 cc = (char *)we - (char *)&mywd; 448 mywd.wd_sendtime = htonl(time(0)); 449 mywd.wd_vers = WHODVERSION; 450 mywd.wd_type = WHODTYPE_STATUS; 451 if (multicast_mode == SCOPED_MULTICAST) { 452 (void) sendto(s, (char *)&mywd, cc, 0, 453 (struct sockaddr *)&multicast_addr, 454 sizeof(multicast_addr)); 455 } 456 else for (np = neighbors; np != NULL; np = np->n_next) { 457 if (multicast_mode == PER_INTERFACE_MULTICAST && 458 np->n_flags & IFF_MULTICAST) { 459 /* 460 * Select the outgoing interface for the multicast. 461 */ 462 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, 463 &(((struct sockaddr_in *)np->n_addr)->sin_addr), 464 sizeof(struct in_addr)) < 0) { 465 syslog(LOG_ERR, 466 "setsockopt IP_MULTICAST_IF: %m"); 467 exit(1); 468 } 469 (void) sendto(s, (char *)&mywd, cc, 0, 470 (struct sockaddr *)&multicast_addr, 471 sizeof(multicast_addr)); 472 } else (void) sendto(s, (char *)&mywd, cc, 0, 473 np->n_addr, np->n_addrlen); 474 } 475 if (utmpent && chdir(_PATH_RWHODIR)) { 476 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR); 477 exit(1); 478 } 479 done: 480 (void) alarm(AL_INTERVAL); 481 } 482 483 void 484 getboottime(signo) 485 int signo; 486 { 487 int mib[2]; 488 size_t size; 489 struct timeval tm; 490 491 mib[0] = CTL_KERN; 492 mib[1] = KERN_BOOTTIME; 493 size = sizeof(tm); 494 if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) { 495 syslog(LOG_ERR, "cannot get boottime: %m"); 496 exit(1); 497 } 498 mywd.wd_boottime = htonl(tm.tv_sec); 499 } 500 501 void 502 quit(msg) 503 char *msg; 504 { 505 syslog(LOG_ERR, msg); 506 exit(1); 507 } 508 509 #define ROUNDUP(a) \ 510 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 511 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 512 513 void 514 rt_xaddrs(cp, cplim, rtinfo) 515 register caddr_t cp, cplim; 516 register struct rt_addrinfo *rtinfo; 517 { 518 register struct sockaddr *sa; 519 register int i; 520 521 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info)); 522 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 523 if ((rtinfo->rti_addrs & (1 << i)) == 0) 524 continue; 525 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp; 526 ADVANCE(cp, sa); 527 } 528 } 529 530 /* 531 * Figure out device configuration and select 532 * networks which deserve status information. 533 */ 534 int 535 configure(s) 536 int s; 537 { 538 register struct neighbor *np; 539 register struct if_msghdr *ifm; 540 register struct ifa_msghdr *ifam; 541 struct sockaddr_dl *sdl; 542 size_t needed; 543 int mib[6], flags = 0, len; 544 char *buf, *lim, *next; 545 struct rt_addrinfo info; 546 547 if (multicast_mode != NO_MULTICAST) { 548 multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP); 549 multicast_addr.sin_port = sp->s_port; 550 } 551 552 if (multicast_mode == SCOPED_MULTICAST) { 553 struct ip_mreq mreq; 554 unsigned char ttl; 555 556 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP); 557 mreq.imr_interface.s_addr = htonl(INADDR_ANY); 558 if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, 559 &mreq, sizeof(mreq)) < 0) { 560 syslog(LOG_ERR, 561 "setsockopt IP_ADD_MEMBERSHIP: %m"); 562 return(0); 563 } 564 ttl = multicast_scope; 565 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, 566 &ttl, sizeof(ttl)) < 0) { 567 syslog(LOG_ERR, 568 "setsockopt IP_MULTICAST_TTL: %m"); 569 return(0); 570 } 571 return(1); 572 } 573 574 mib[0] = CTL_NET; 575 mib[1] = PF_ROUTE; 576 mib[2] = 0; 577 mib[3] = AF_INET; 578 mib[4] = NET_RT_IFLIST; 579 mib[5] = 0; 580 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 581 quit("route-sysctl-estimate"); 582 if ((buf = malloc(needed)) == NULL) 583 quit("malloc"); 584 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 585 quit("actual retrieval of interface table"); 586 lim = buf + needed; 587 588 sdl = NULL; /* XXX just to keep gcc -Wall happy */ 589 for (next = buf; next < lim; next += ifm->ifm_msglen) { 590 ifm = (struct if_msghdr *)next; 591 if (ifm->ifm_type == RTM_IFINFO) { 592 sdl = (struct sockaddr_dl *)(ifm + 1); 593 flags = ifm->ifm_flags; 594 continue; 595 } 596 if ((flags & IFF_UP) == 0 || 597 (flags & (((multicast_mode == PER_INTERFACE_MULTICAST) ? 598 IFF_MULTICAST : 0) | 599 IFF_BROADCAST|IFF_POINTOPOINT)) == 0) 600 continue; 601 if (ifm->ifm_type != RTM_NEWADDR) 602 quit("out of sync parsing NET_RT_IFLIST"); 603 ifam = (struct ifa_msghdr *)ifm; 604 info.rti_addrs = ifam->ifam_addrs; 605 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam, 606 &info); 607 /* gag, wish we could get rid of Internet dependencies */ 608 #define dstaddr info.rti_info[RTAX_BRD] 609 #define ifaddr info.rti_info[RTAX_IFA] 610 #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr 611 #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port 612 if (dstaddr == 0 || dstaddr->sa_family != AF_INET) 613 continue; 614 PORT_SA(dstaddr) = sp->s_port; 615 for (np = neighbors; np != NULL; np = np->n_next) 616 if (memcmp(sdl->sdl_data, np->n_name, 617 sdl->sdl_nlen) == 0 && 618 IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr)) 619 break; 620 if (np != NULL) 621 continue; 622 len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1; 623 np = (struct neighbor *)malloc(len); 624 if (np == NULL) 625 quit("malloc of neighbor structure"); 626 memset(np, 0, len); 627 np->n_flags = flags; 628 np->n_addr = (struct sockaddr *)(np + 1); 629 np->n_addrlen = dstaddr->sa_len; 630 np->n_name = np->n_addrlen + (char *)np->n_addr; 631 memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen); 632 memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen); 633 if (multicast_mode == PER_INTERFACE_MULTICAST && 634 (flags & IFF_MULTICAST) && 635 !(flags & IFF_LOOPBACK)) { 636 struct ip_mreq mreq; 637 638 memcpy((char *)np->n_addr, (char *)ifaddr, 639 np->n_addrlen); 640 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP); 641 mreq.imr_interface.s_addr = 642 ((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr; 643 if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, 644 &mreq, sizeof(mreq)) < 0) { 645 syslog(LOG_ERR, 646 "setsockopt IP_ADD_MEMBERSHIP: %m"); 647 #if 0 648 /* Fall back to broadcast on this if. */ 649 np->n_flags &= ~IFF_MULTICAST; 650 #else 651 free((char *)np); 652 continue; 653 #endif 654 } 655 } 656 np->n_next = neighbors; 657 neighbors = np; 658 } 659 free(buf); 660 return (1); 661 } 662 663 #ifdef DEBUG 664 void 665 Sendto(s, buf, cc, flags, to, tolen) 666 int s; 667 const void *buf; 668 size_t cc; 669 int flags; 670 const struct sockaddr *to; 671 int tolen; 672 { 673 register struct whod *w = (struct whod *)buf; 674 register struct whoent *we; 675 struct sockaddr_in *sin = (struct sockaddr_in *)to; 676 677 printf("sendto %x.%d\n", ntohl(sin->sin_addr.s_addr), 678 ntohs(sin->sin_port)); 679 printf("hostname %s %s\n", w->wd_hostname, 680 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 681 printf("load %4.2f, %4.2f, %4.2f\n", 682 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 683 ntohl(w->wd_loadav[2]) / 100.0); 684 cc -= WHDRSIZE; 685 for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) { 686 time_t t = ntohl(we->we_utmp.out_time); 687 printf("%-8.8s %s:%s %.12s", 688 we->we_utmp.out_name, 689 w->wd_hostname, we->we_utmp.out_line, 690 ctime(&t)+4); 691 we->we_idle = ntohl(we->we_idle) / 60; 692 if (we->we_idle) { 693 if (we->we_idle >= 100*60) 694 we->we_idle = 100*60 - 1; 695 if (we->we_idle >= 60) 696 printf(" %2d", we->we_idle / 60); 697 else 698 printf(" "); 699 printf(":%02d", we->we_idle % 60); 700 } 701 printf("\n"); 702 } 703 } 704 705 char * 706 interval(time, updown) 707 int time; 708 char *updown; 709 { 710 static char resbuf[32]; 711 int days, hours, minutes; 712 713 if (time < 0 || time > 3*30*24*60*60) { 714 (void) sprintf(resbuf, " %s ??:??", updown); 715 return (resbuf); 716 } 717 minutes = (time + 59) / 60; /* round to minutes */ 718 hours = minutes / 60; minutes %= 60; 719 days = hours / 24; hours %= 24; 720 if (days) 721 (void) sprintf(resbuf, "%s %2d+%02d:%02d", 722 updown, days, hours, minutes); 723 else 724 (void) sprintf(resbuf, "%s %2d:%02d", 725 updown, hours, minutes); 726 return (resbuf); 727 } 728 #endif 729