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