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