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