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 #if 0 41 #ifndef lint 42 static char sccsid[] = "@(#)rwhod.c 8.1 (Berkeley) 6/6/93"; 43 #endif /* not lint */ 44 #endif 45 46 #include <sys/cdefs.h> 47 __FBSDID("$FreeBSD$"); 48 49 #include <sys/param.h> 50 #include <sys/socket.h> 51 #include <sys/stat.h> 52 #include <sys/signal.h> 53 #include <sys/ioctl.h> 54 #include <sys/sysctl.h> 55 56 #include <net/if.h> 57 #include <net/if_dl.h> 58 #include <net/route.h> 59 #include <netinet/in.h> 60 #include <arpa/inet.h> 61 #include <protocols/rwhod.h> 62 63 #include <ctype.h> 64 #include <err.h> 65 #include <errno.h> 66 #include <fcntl.h> 67 #include <netdb.h> 68 #include <paths.h> 69 #include <stdio.h> 70 #include <stdlib.h> 71 #include <string.h> 72 #include <syslog.h> 73 #include <timeconv.h> 74 #include <unistd.h> 75 #include <utmp.h> 76 #include <pwd.h> 77 #include <grp.h> 78 79 /* 80 * This version of Berkeley's rwhod has been modified to use IP multicast 81 * datagrams, under control of a new command-line option: 82 * 83 * rwhod -m causes rwhod to use IP multicast (instead of 84 * broadcast or unicast) on all interfaces that have 85 * the IFF_MULTICAST flag set in their "ifnet" structs 86 * (excluding the loopback interface). The multicast 87 * reports are sent with a time-to-live of 1, to prevent 88 * forwarding beyond the directly-connected subnet(s). 89 * 90 * rwhod -m <ttl> causes rwhod to send IP multicast datagrams with a 91 * time-to-live of <ttl>, via a SINGLE interface rather 92 * than all interfaces. <ttl> must be between 0 and 93 * MAX_MULTICAST_SCOPE, defined below. Note that "-m 1" 94 * is different than "-m", in that "-m 1" specifies 95 * transmission on one interface only. 96 * 97 * When "-m" is used without a <ttl> argument, the program accepts multicast 98 * rwhod reports from all multicast-capable interfaces. If a <ttl> argument 99 * is given, it accepts multicast reports from only one interface, the one 100 * on which reports are sent (which may be controlled via the host's routing 101 * table). Regardless of the "-m" option, the program accepts broadcast or 102 * unicast reports from all interfaces. Thus, this program will hear the 103 * reports of old, non-multicasting rwhods, but, if multicasting is used, 104 * those old rwhods won't hear the reports generated by this program. 105 * 106 * -- Steve Deering, Stanford University, February 1989 107 */ 108 109 #define UNPRIV_USER "daemon" 110 #define UNPRIV_GROUP "daemon" 111 112 #define NO_MULTICAST 0 /* multicast modes */ 113 #define PER_INTERFACE_MULTICAST 1 114 #define SCOPED_MULTICAST 2 115 116 #define MAX_MULTICAST_SCOPE 32 /* "site-wide", by convention */ 117 118 #define INADDR_WHOD_GROUP (u_long)0xe0000103 /* 224.0.1.3 */ 119 /* (belongs in protocols/rwhod.h) */ 120 121 int insecure_mode; 122 int quiet_mode; 123 int iff_flag = IFF_POINTOPOINT; 124 int multicast_mode = NO_MULTICAST; 125 int multicast_scope; 126 struct sockaddr_in multicast_addr = 127 { sizeof multicast_addr, AF_INET, 0, { 0 }, { 0 } }; 128 129 /* 130 * Alarm interval. Don't forget to change the down time check in ruptime 131 * if this is changed. 132 */ 133 #define AL_INTERVAL (3 * 60) 134 135 char myname[MAXHOSTNAMELEN]; 136 137 /* 138 * We communicate with each neighbor in a list constructed at the time we're 139 * started up. Neighbors are currently directly connected via a hardware 140 * interface. 141 */ 142 struct neighbor { 143 struct neighbor *n_next; 144 char *n_name; /* interface name */ 145 struct sockaddr *n_addr; /* who to send to */ 146 int n_addrlen; /* size of address */ 147 int n_flags; /* should forward?, interface flags */ 148 }; 149 150 struct neighbor *neighbors; 151 struct whod mywd; 152 struct servent *sp; 153 int s, utmpf; 154 155 #define WHDRSIZE (int)(sizeof(mywd) - sizeof(mywd.wd_we)) 156 157 void run_as(uid_t *, gid_t *); 158 int configure(int); 159 void getboottime(int); 160 void onalrm(int); 161 void quit(const char *); 162 void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *); 163 int verify(char *, int); 164 static void usage(void); 165 #ifdef DEBUG 166 char *interval(int, char *); 167 void Sendto(int, const void *, size_t, int, 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, "who/udp: 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 void 529 rt_xaddrs(cp, cplim, rtinfo) 530 register caddr_t cp, cplim; 531 register struct rt_addrinfo *rtinfo; 532 { 533 register struct sockaddr *sa; 534 register int i; 535 536 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info)); 537 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 538 if ((rtinfo->rti_addrs & (1 << i)) == 0) 539 continue; 540 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp; 541 cp += SA_SIZE(sa); 542 } 543 } 544 545 /* 546 * Figure out device configuration and select 547 * networks which deserve status information. 548 */ 549 int 550 configure(so) 551 int so; 552 { 553 register struct neighbor *np; 554 register struct if_msghdr *ifm; 555 register struct ifa_msghdr *ifam; 556 struct sockaddr_dl *sdl; 557 size_t needed; 558 int mib[6], flags = 0, len; 559 char *buf, *lim, *next; 560 struct rt_addrinfo info; 561 562 if (multicast_mode != NO_MULTICAST) { 563 multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP); 564 multicast_addr.sin_port = sp->s_port; 565 } 566 567 if (multicast_mode == SCOPED_MULTICAST) { 568 struct ip_mreq mreq; 569 unsigned char ttl; 570 571 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP); 572 mreq.imr_interface.s_addr = htonl(INADDR_ANY); 573 if (setsockopt(so, IPPROTO_IP, IP_ADD_MEMBERSHIP, 574 &mreq, sizeof(mreq)) < 0) { 575 syslog(LOG_ERR, 576 "setsockopt IP_ADD_MEMBERSHIP: %m"); 577 return(0); 578 } 579 ttl = multicast_scope; 580 if (setsockopt(so, IPPROTO_IP, IP_MULTICAST_TTL, 581 &ttl, sizeof(ttl)) < 0) { 582 syslog(LOG_ERR, 583 "setsockopt IP_MULTICAST_TTL: %m"); 584 return(0); 585 } 586 return(1); 587 } 588 589 mib[0] = CTL_NET; 590 mib[1] = PF_ROUTE; 591 mib[2] = 0; 592 mib[3] = AF_INET; 593 mib[4] = NET_RT_IFLIST; 594 mib[5] = 0; 595 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 596 quit("route-sysctl-estimate"); 597 if ((buf = malloc(needed)) == NULL) 598 quit("malloc"); 599 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 600 quit("actual retrieval of interface table"); 601 lim = buf + needed; 602 603 sdl = NULL; /* XXX just to keep gcc -Wall happy */ 604 for (next = buf; next < lim; next += ifm->ifm_msglen) { 605 ifm = (struct if_msghdr *)next; 606 if (ifm->ifm_type == RTM_IFINFO) { 607 sdl = (struct sockaddr_dl *)(ifm + 1); 608 flags = ifm->ifm_flags; 609 continue; 610 } 611 if ((flags & IFF_UP) == 0 || 612 (flags & (((multicast_mode == PER_INTERFACE_MULTICAST) ? 613 IFF_MULTICAST : 0) | 614 IFF_BROADCAST|iff_flag)) == 0) 615 continue; 616 if (ifm->ifm_type != RTM_NEWADDR) 617 quit("out of sync parsing NET_RT_IFLIST"); 618 ifam = (struct ifa_msghdr *)ifm; 619 info.rti_addrs = ifam->ifam_addrs; 620 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam, 621 &info); 622 /* gag, wish we could get rid of Internet dependencies */ 623 #define dstaddr info.rti_info[RTAX_BRD] 624 #define ifaddr info.rti_info[RTAX_IFA] 625 #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr 626 #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port 627 if (dstaddr == 0 || dstaddr->sa_family != AF_INET) 628 continue; 629 PORT_SA(dstaddr) = sp->s_port; 630 for (np = neighbors; np != NULL; np = np->n_next) 631 if (memcmp(sdl->sdl_data, np->n_name, 632 sdl->sdl_nlen) == 0 && 633 IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr)) 634 break; 635 if (np != NULL) 636 continue; 637 len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1; 638 np = (struct neighbor *)malloc(len); 639 if (np == NULL) 640 quit("malloc of neighbor structure"); 641 memset(np, 0, len); 642 np->n_flags = flags; 643 np->n_addr = (struct sockaddr *)(np + 1); 644 np->n_addrlen = dstaddr->sa_len; 645 np->n_name = np->n_addrlen + (char *)np->n_addr; 646 memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen); 647 memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen); 648 if (multicast_mode == PER_INTERFACE_MULTICAST && 649 (flags & IFF_MULTICAST) && 650 !(flags & IFF_LOOPBACK)) { 651 struct ip_mreq mreq; 652 653 memcpy((char *)np->n_addr, (char *)ifaddr, 654 np->n_addrlen); 655 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP); 656 mreq.imr_interface.s_addr = 657 ((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr; 658 if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, 659 &mreq, sizeof(mreq)) < 0) { 660 syslog(LOG_ERR, 661 "setsockopt IP_ADD_MEMBERSHIP: %m"); 662 #if 0 663 /* Fall back to broadcast on this if. */ 664 np->n_flags &= ~IFF_MULTICAST; 665 #else 666 free((char *)np); 667 continue; 668 #endif 669 } 670 } 671 np->n_next = neighbors; 672 neighbors = np; 673 } 674 free(buf); 675 return (1); 676 } 677 678 #ifdef DEBUG 679 void 680 Sendto(s, buf, cc, flags, to, tolen) 681 int s; 682 const void *buf; 683 size_t cc; 684 int flags; 685 const struct sockaddr *to; 686 int tolen; 687 { 688 register struct whod *w = (struct whod *)buf; 689 register struct whoent *we; 690 struct sockaddr_in *sin = (struct sockaddr_in *)to; 691 692 printf("sendto %x.%d\n", ntohl(sin->sin_addr.s_addr), 693 ntohs(sin->sin_port)); 694 printf("hostname %s %s\n", w->wd_hostname, 695 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 696 printf("load %4.2f, %4.2f, %4.2f\n", 697 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 698 ntohl(w->wd_loadav[2]) / 100.0); 699 cc -= WHDRSIZE; 700 for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) { 701 time_t t = _time32_to_time(ntohl(we->we_utmp.out_time)); 702 printf("%-8.8s %s:%s %.12s", 703 we->we_utmp.out_name, 704 w->wd_hostname, we->we_utmp.out_line, 705 ctime(&t)+4); 706 we->we_idle = ntohl(we->we_idle) / 60; 707 if (we->we_idle) { 708 if (we->we_idle >= 100*60) 709 we->we_idle = 100*60 - 1; 710 if (we->we_idle >= 60) 711 printf(" %2d", we->we_idle / 60); 712 else 713 printf(" "); 714 printf(":%02d", we->we_idle % 60); 715 } 716 printf("\n"); 717 } 718 } 719 720 char * 721 interval(time, updown) 722 int time; 723 char *updown; 724 { 725 static char resbuf[32]; 726 int days, hours, minutes; 727 728 if (time < 0 || time > 3*30*24*60*60) { 729 (void) sprintf(resbuf, " %s ??:??", updown); 730 return (resbuf); 731 } 732 minutes = (time + 59) / 60; /* round to minutes */ 733 hours = minutes / 60; minutes %= 60; 734 days = hours / 24; hours %= 24; 735 if (days) 736 (void) sprintf(resbuf, "%s %2d+%02d:%02d", 737 updown, days, hours, minutes); 738 else 739 (void) sprintf(resbuf, "%s %2d:%02d", 740 updown, hours, minutes); 741 return (resbuf); 742 } 743 #endif 744