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