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