rwhod.c revision 202206
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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#ifndef lint 31static const char copyright[] = 32"@(#) Copyright (c) 1983, 1993\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34#endif /* not lint */ 35 36#if 0 37#ifndef lint 38static char sccsid[] = "@(#)rwhod.c 8.1 (Berkeley) 6/6/93"; 39#endif /* not lint */ 40#endif 41 42#include <sys/cdefs.h> 43__FBSDID("$FreeBSD: head/usr.sbin/rwhod/rwhod.c 202206 2010-01-13 18:17:53Z ed $"); 44 45#include <sys/param.h> 46#include <sys/socket.h> 47#include <sys/stat.h> 48#include <sys/signal.h> 49#include <sys/ioctl.h> 50#include <sys/sysctl.h> 51 52#include <net/if.h> 53#include <net/if_dl.h> 54#include <net/route.h> 55#include <netinet/in.h> 56#include <arpa/inet.h> 57#include <protocols/rwhod.h> 58 59#include <ctype.h> 60#include <err.h> 61#include <errno.h> 62#include <fcntl.h> 63#include <netdb.h> 64#include <paths.h> 65#include <stdio.h> 66#include <stdlib.h> 67#include <string.h> 68#include <syslog.h> 69#include <timeconv.h> 70#include <unistd.h> 71#include <utmpx.h> 72#include <pwd.h> 73#include <grp.h> 74 75/* 76 * This version of Berkeley's rwhod has been modified to use IP multicast 77 * datagrams, under control of a new command-line option: 78 * 79 * rwhod -m causes rwhod to use IP multicast (instead of 80 * broadcast or unicast) on all interfaces that have 81 * the IFF_MULTICAST flag set in their "ifnet" structs 82 * (excluding the loopback interface). The multicast 83 * reports are sent with a time-to-live of 1, to prevent 84 * forwarding beyond the directly-connected subnet(s). 85 * 86 * rwhod -m <ttl> causes rwhod to send IP multicast datagrams with a 87 * time-to-live of <ttl>, via a SINGLE interface rather 88 * than all interfaces. <ttl> must be between 0 and 89 * MAX_MULTICAST_SCOPE, defined below. Note that "-m 1" 90 * is different than "-m", in that "-m 1" specifies 91 * transmission on one interface only. 92 * 93 * When "-m" is used without a <ttl> argument, the program accepts multicast 94 * rwhod reports from all multicast-capable interfaces. If a <ttl> argument 95 * is given, it accepts multicast reports from only one interface, the one 96 * on which reports are sent (which may be controlled via the host's routing 97 * table). Regardless of the "-m" option, the program accepts broadcast or 98 * unicast reports from all interfaces. Thus, this program will hear the 99 * reports of old, non-multicasting rwhods, but, if multicasting is used, 100 * those old rwhods won't hear the reports generated by this program. 101 * 102 * -- Steve Deering, Stanford University, February 1989 103 */ 104 105#define UNPRIV_USER "daemon" 106#define UNPRIV_GROUP "daemon" 107 108#define NO_MULTICAST 0 /* multicast modes */ 109#define PER_INTERFACE_MULTICAST 1 110#define SCOPED_MULTICAST 2 111 112#define MAX_MULTICAST_SCOPE 32 /* "site-wide", by convention */ 113 114#define INADDR_WHOD_GROUP (u_long)0xe0000103 /* 224.0.1.3 */ 115 /* (belongs in protocols/rwhod.h) */ 116 117int insecure_mode; 118int quiet_mode; 119int iff_flag = IFF_POINTOPOINT; 120int multicast_mode = NO_MULTICAST; 121int multicast_scope; 122struct sockaddr_in multicast_addr = 123 { sizeof multicast_addr, AF_INET, 0, { 0 }, { 0 } }; 124 125/* 126 * Alarm interval. Don't forget to change the down time check in ruptime 127 * if this is changed. 128 */ 129#define AL_INTERVAL (3 * 60) 130 131char myname[MAXHOSTNAMELEN]; 132 133/* 134 * We communicate with each neighbor in a list constructed at the time we're 135 * started up. Neighbors are currently directly connected via a hardware 136 * interface. 137 */ 138struct neighbor { 139 struct neighbor *n_next; 140 char *n_name; /* interface name */ 141 struct sockaddr *n_addr; /* who to send to */ 142 int n_addrlen; /* size of address */ 143 int n_flags; /* should forward?, interface flags */ 144}; 145 146struct neighbor *neighbors; 147struct whod mywd; 148struct servent *sp; 149int s; 150 151#define WHDRSIZE (int)(sizeof(mywd) - sizeof(mywd.wd_we)) 152 153void run_as(uid_t *, gid_t *); 154int configure(int); 155void getboottime(int); 156void onalrm(int); 157void quit(const char *); 158void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *); 159int verify(char *, int); 160static void usage(void); 161#ifdef DEBUG 162char *interval(int, char *); 163void Sendto(int, const void *, size_t, int, const struct sockaddr *, int); 164#define sendto Sendto 165#endif 166 167int 168main(int argc, 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 soin; 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 if (strcmp(*argv, "-i") == 0) 198 insecure_mode = 1; 199 else if (strcmp(*argv, "-l") == 0) 200 quiet_mode = 1; 201 else if (strcmp(*argv, "-p") == 0) 202 iff_flag = 0; 203 else 204 usage(); 205 argv++, argc--; 206 } 207 if (argc > 0) 208 usage(); 209#ifndef DEBUG 210 daemon(1, 0); 211#endif 212 (void) signal(SIGHUP, getboottime); 213 openlog("rwhod", LOG_PID, LOG_DAEMON); 214 sp = getservbyname("who", "udp"); 215 if (sp == NULL) { 216 syslog(LOG_ERR, "who/udp: unknown service"); 217 exit(1); 218 } 219 if (chdir(_PATH_RWHODIR) < 0) { 220 syslog(LOG_ERR, "%s: %m", _PATH_RWHODIR); 221 exit(1); 222 } 223 /* 224 * Establish host name as returned by system. 225 */ 226 if (gethostname(myname, sizeof(myname) - 1) < 0) { 227 syslog(LOG_ERR, "gethostname: %m"); 228 exit(1); 229 } 230 if ((cp = index(myname, '.')) != NULL) 231 *cp = '\0'; 232 strncpy(mywd.wd_hostname, myname, sizeof(mywd.wd_hostname) - 1); 233 mywd.wd_hostname[sizeof(mywd.wd_hostname) - 1] = '\0'; 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(&soin, 0, sizeof(soin)); 244 soin.sin_len = sizeof(soin); 245 soin.sin_family = AF_INET; 246 soin.sin_port = sp->s_port; 247 if (bind(s, (struct sockaddr *)&soin, sizeof(soin)) < 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 socklen_t len = sizeof(from); 263 int cc, whod; 264 time_t t; 265 266 cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0, 267 (struct sockaddr *)&from, &len); 268 if (cc <= 0) { 269 if (cc < 0 && errno != EINTR) 270 syslog(LOG_WARNING, "recv: %m"); 271 continue; 272 } 273 if (from.sin_port != sp->s_port && !insecure_mode) { 274 syslog(LOG_WARNING, "%d: bad source port from %s", 275 ntohs(from.sin_port), inet_ntoa(from.sin_addr)); 276 continue; 277 } 278 if (cc < WHDRSIZE) { 279 syslog(LOG_WARNING, "short packet from %s", 280 inet_ntoa(from.sin_addr)); 281 continue; 282 } 283 if (wd.wd_vers != WHODVERSION) 284 continue; 285 if (wd.wd_type != WHODTYPE_STATUS) 286 continue; 287 if (!verify(wd.wd_hostname, sizeof wd.wd_hostname)) { 288 syslog(LOG_WARNING, "malformed host name from %s", 289 inet_ntoa(from.sin_addr)); 290 continue; 291 } 292 (void) snprintf(path, sizeof path, "whod.%s", wd.wd_hostname); 293 /* 294 * Rather than truncating and growing the file each time, 295 * use ftruncate if size is less than previous size. 296 */ 297 whod = open(path, O_WRONLY | O_CREAT, 0644); 298 if (whod < 0) { 299 syslog(LOG_WARNING, "%s: %m", path); 300 continue; 301 } 302#if ENDIAN != BIG_ENDIAN 303 { 304 int i, n = (cc - WHDRSIZE)/sizeof(struct whoent); 305 struct whoent *we; 306 307 /* undo header byte swapping before writing to file */ 308 wd.wd_sendtime = ntohl(wd.wd_sendtime); 309 for (i = 0; i < 3; i++) 310 wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 311 wd.wd_boottime = ntohl(wd.wd_boottime); 312 we = wd.wd_we; 313 for (i = 0; i < n; i++) { 314 we->we_idle = ntohl(we->we_idle); 315 we->we_utmp.out_time = 316 ntohl(we->we_utmp.out_time); 317 we++; 318 } 319 } 320#endif 321 (void) time(&t); 322 wd.wd_recvtime = _time_to_int(t); 323 (void) write(whod, (char *)&wd, cc); 324 if (fstat(whod, &st) < 0 || st.st_size > cc) 325 ftruncate(whod, cc); 326 (void) close(whod); 327 } 328} 329 330static void 331usage() 332{ 333 fprintf(stderr, "usage: rwhod [-i] [-p] [-l] [-m [ttl]]\n"); 334 exit(1); 335} 336 337void 338run_as(uid, gid) 339 uid_t *uid; 340 gid_t *gid; 341{ 342 struct passwd *pw; 343 struct group *gr; 344 345 pw = getpwnam(UNPRIV_USER); 346 if (!pw) { 347 syslog(LOG_ERR, "getpwnam(%s): %m", UNPRIV_USER); 348 exit(1); 349 } 350 *uid = pw->pw_uid; 351 352 gr = getgrnam(UNPRIV_GROUP); 353 if (!gr) { 354 syslog(LOG_ERR, "getgrnam(%s): %m", UNPRIV_GROUP); 355 exit(1); 356 } 357 *gid = gr->gr_gid; 358} 359 360/* 361 * Check out host name for unprintables 362 * and other funnies before allowing a file 363 * to be created. Sorry, but blanks aren't allowed. 364 */ 365int 366verify(name, maxlen) 367 register char *name; 368 register int maxlen; 369{ 370 register int size = 0; 371 372 while (*name && size < maxlen - 1) { 373 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 374 return (0); 375 name++, size++; 376 } 377 *name = '\0'; 378 return (size > 0); 379} 380 381void 382onalrm(int signo __unused) 383{ 384 struct neighbor *np; 385 struct whoent *we = mywd.wd_we, *wend; 386 struct stat stb; 387 struct utmpx *ut; 388 static int alarmcount = 0; 389 double avenrun[3]; 390 time_t now; 391 int i, cc; 392 393 now = time(NULL); 394 if (alarmcount % 10 == 0) 395 getboottime(0); 396 alarmcount++; 397 wend = &mywd.wd_we[1024 / sizeof(struct whoent)]; 398 setutxent(); 399 while ((ut = getutxent()) != NULL && we < wend) { 400 if (ut->ut_type != USER_PROCESS) 401 continue; 402 strncpy(we->we_utmp.out_line, ut->ut_line, 403 sizeof(we->we_utmp.out_line)); 404 strncpy(we->we_utmp.out_name, ut->ut_user, 405 sizeof(we->we_utmp.out_name)); 406 we->we_utmp.out_time = 407 htonl(_time_to_time32(ut->ut_tv.tv_sec)); 408 we++; 409 } 410 endutxent(); 411 412 if (chdir(_PATH_DEV)) { 413 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV); 414 exit(1); 415 } 416 wend = we; 417 for (we = mywd.wd_we; we < wend; we++) { 418 if (stat(we->we_utmp.out_line, &stb) >= 0) 419 we->we_idle = htonl(now - stb.st_atime); 420 we++; 421 } 422 (void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); 423 for (i = 0; i < 3; i++) 424 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 425 cc = (char *)wend - (char *)&mywd; 426 mywd.wd_sendtime = htonl(_time_to_time32(time(NULL))); 427 mywd.wd_vers = WHODVERSION; 428 mywd.wd_type = WHODTYPE_STATUS; 429 if (multicast_mode == SCOPED_MULTICAST) { 430 (void) sendto(s, (char *)&mywd, cc, 0, 431 (struct sockaddr *)&multicast_addr, 432 sizeof(multicast_addr)); 433 } 434 else for (np = neighbors; np != NULL; np = np->n_next) { 435 if (multicast_mode == PER_INTERFACE_MULTICAST && 436 np->n_flags & IFF_MULTICAST) { 437 /* 438 * Select the outgoing interface for the multicast. 439 */ 440 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, 441 &(((struct sockaddr_in *)np->n_addr)->sin_addr), 442 sizeof(struct in_addr)) < 0) { 443 syslog(LOG_ERR, 444 "setsockopt IP_MULTICAST_IF: %m"); 445 exit(1); 446 } 447 (void) sendto(s, (char *)&mywd, cc, 0, 448 (struct sockaddr *)&multicast_addr, 449 sizeof(multicast_addr)); 450 } else (void) sendto(s, (char *)&mywd, cc, 0, 451 np->n_addr, np->n_addrlen); 452 } 453 if (chdir(_PATH_RWHODIR)) { 454 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR); 455 exit(1); 456 } 457 (void) alarm(AL_INTERVAL); 458} 459 460void 461getboottime(signo) 462 int signo __unused; 463{ 464 int mib[2]; 465 size_t size; 466 struct timeval tm; 467 468 mib[0] = CTL_KERN; 469 mib[1] = KERN_BOOTTIME; 470 size = sizeof(tm); 471 if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) { 472 syslog(LOG_ERR, "cannot get boottime: %m"); 473 exit(1); 474 } 475 mywd.wd_boottime = htonl(_time_to_time32(tm.tv_sec)); 476} 477 478void 479quit(msg) 480 const char *msg; 481{ 482 syslog(LOG_ERR, "%s", msg); 483 exit(1); 484} 485 486void 487rt_xaddrs(cp, cplim, rtinfo) 488 register caddr_t cp, cplim; 489 register struct rt_addrinfo *rtinfo; 490{ 491 register struct sockaddr *sa; 492 register int i; 493 494 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info)); 495 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 496 if ((rtinfo->rti_addrs & (1 << i)) == 0) 497 continue; 498 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp; 499 cp += SA_SIZE(sa); 500 } 501} 502 503/* 504 * Figure out device configuration and select 505 * networks which deserve status information. 506 */ 507int 508configure(so) 509 int so; 510{ 511 register struct neighbor *np; 512 register struct if_msghdr *ifm; 513 register struct ifa_msghdr *ifam; 514 struct sockaddr_dl *sdl; 515 size_t needed; 516 int mib[6], flags = 0, len; 517 char *buf, *lim, *next; 518 struct rt_addrinfo info; 519 520 if (multicast_mode != NO_MULTICAST) { 521 multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP); 522 multicast_addr.sin_port = sp->s_port; 523 } 524 525 if (multicast_mode == SCOPED_MULTICAST) { 526 struct ip_mreq mreq; 527 unsigned char ttl; 528 529 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP); 530 mreq.imr_interface.s_addr = htonl(INADDR_ANY); 531 if (setsockopt(so, IPPROTO_IP, IP_ADD_MEMBERSHIP, 532 &mreq, sizeof(mreq)) < 0) { 533 syslog(LOG_ERR, 534 "setsockopt IP_ADD_MEMBERSHIP: %m"); 535 return(0); 536 } 537 ttl = multicast_scope; 538 if (setsockopt(so, IPPROTO_IP, IP_MULTICAST_TTL, 539 &ttl, sizeof(ttl)) < 0) { 540 syslog(LOG_ERR, 541 "setsockopt IP_MULTICAST_TTL: %m"); 542 return(0); 543 } 544 return(1); 545 } 546 547 mib[0] = CTL_NET; 548 mib[1] = PF_ROUTE; 549 mib[2] = 0; 550 mib[3] = AF_INET; 551 mib[4] = NET_RT_IFLIST; 552 mib[5] = 0; 553 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 554 quit("route-sysctl-estimate"); 555 if ((buf = malloc(needed)) == NULL) 556 quit("malloc"); 557 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 558 quit("actual retrieval of interface table"); 559 lim = buf + needed; 560 561 sdl = NULL; /* XXX just to keep gcc -Wall happy */ 562 for (next = buf; next < lim; next += ifm->ifm_msglen) { 563 ifm = (struct if_msghdr *)next; 564 if (ifm->ifm_type == RTM_IFINFO) { 565 sdl = (struct sockaddr_dl *)(ifm + 1); 566 flags = ifm->ifm_flags; 567 continue; 568 } 569 if ((flags & IFF_UP) == 0 || 570 (flags & (((multicast_mode == PER_INTERFACE_MULTICAST) ? 571 IFF_MULTICAST : 0) | 572 IFF_BROADCAST|iff_flag)) == 0) 573 continue; 574 if (ifm->ifm_type != RTM_NEWADDR) 575 quit("out of sync parsing NET_RT_IFLIST"); 576 ifam = (struct ifa_msghdr *)ifm; 577 info.rti_addrs = ifam->ifam_addrs; 578 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam, 579 &info); 580 /* gag, wish we could get rid of Internet dependencies */ 581#define dstaddr info.rti_info[RTAX_BRD] 582#define ifaddr info.rti_info[RTAX_IFA] 583#define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr 584#define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port 585 if (dstaddr == 0 || dstaddr->sa_family != AF_INET) 586 continue; 587 PORT_SA(dstaddr) = sp->s_port; 588 for (np = neighbors; np != NULL; np = np->n_next) 589 if (memcmp(sdl->sdl_data, np->n_name, 590 sdl->sdl_nlen) == 0 && 591 IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr)) 592 break; 593 if (np != NULL) 594 continue; 595 len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1; 596 np = (struct neighbor *)malloc(len); 597 if (np == NULL) 598 quit("malloc of neighbor structure"); 599 memset(np, 0, len); 600 np->n_flags = flags; 601 np->n_addr = (struct sockaddr *)(np + 1); 602 np->n_addrlen = dstaddr->sa_len; 603 np->n_name = np->n_addrlen + (char *)np->n_addr; 604 memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen); 605 memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen); 606 if (multicast_mode == PER_INTERFACE_MULTICAST && 607 (flags & IFF_MULTICAST) && 608 !(flags & IFF_LOOPBACK)) { 609 struct ip_mreq mreq; 610 611 memcpy((char *)np->n_addr, (char *)ifaddr, 612 np->n_addrlen); 613 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP); 614 mreq.imr_interface.s_addr = 615 ((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr; 616 if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, 617 &mreq, sizeof(mreq)) < 0) { 618 syslog(LOG_ERR, 619 "setsockopt IP_ADD_MEMBERSHIP: %m"); 620#if 0 621 /* Fall back to broadcast on this if. */ 622 np->n_flags &= ~IFF_MULTICAST; 623#else 624 free((char *)np); 625 continue; 626#endif 627 } 628 } 629 np->n_next = neighbors; 630 neighbors = np; 631 } 632 free(buf); 633 return (1); 634} 635 636#ifdef DEBUG 637void 638Sendto(s, buf, cc, flags, to, tolen) 639 int s; 640 const void *buf; 641 size_t cc; 642 int flags; 643 const struct sockaddr *to; 644 int tolen; 645{ 646 register struct whod *w = (struct whod *)buf; 647 register struct whoent *we; 648 struct sockaddr_in *sin = (struct sockaddr_in *)to; 649 650 printf("sendto %x.%d\n", ntohl(sin->sin_addr.s_addr), 651 ntohs(sin->sin_port)); 652 printf("hostname %s %s\n", w->wd_hostname, 653 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 654 printf("load %4.2f, %4.2f, %4.2f\n", 655 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 656 ntohl(w->wd_loadav[2]) / 100.0); 657 cc -= WHDRSIZE; 658 for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) { 659 time_t t = _time32_to_time(ntohl(we->we_utmp.out_time)); 660 printf("%-8.8s %s:%s %.12s", 661 we->we_utmp.out_name, 662 w->wd_hostname, we->we_utmp.out_line, 663 ctime(&t)+4); 664 we->we_idle = ntohl(we->we_idle) / 60; 665 if (we->we_idle) { 666 if (we->we_idle >= 100*60) 667 we->we_idle = 100*60 - 1; 668 if (we->we_idle >= 60) 669 printf(" %2d", we->we_idle / 60); 670 else 671 printf(" "); 672 printf(":%02d", we->we_idle % 60); 673 } 674 printf("\n"); 675 } 676} 677 678char * 679interval(time, updown) 680 int time; 681 char *updown; 682{ 683 static char resbuf[32]; 684 int days, hours, minutes; 685 686 if (time < 0 || time > 3*30*24*60*60) { 687 (void) sprintf(resbuf, " %s ??:??", updown); 688 return (resbuf); 689 } 690 minutes = (time + 59) / 60; /* round to minutes */ 691 hours = minutes / 60; minutes %= 60; 692 days = hours / 24; hours %= 24; 693 if (days) 694 (void) sprintf(resbuf, "%s %2d+%02d:%02d", 695 updown, days, hours, minutes); 696 else 697 (void) sprintf(resbuf, "%s %2d:%02d", 698 updown, hours, minutes); 699 return (resbuf); 700} 701#endif 702