main.c revision 1.14 1 /* $NetBSD: main.c,v 1.14 1996/08/10 01:29:23 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 char copyright[] =
37 "@(#) Copyright (c) 1983, 1988, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
39 #if !defined(lint) && !defined(sgi)
40 #if 0
41 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/5/93";
42 #else
43 static char rcsid[] = "$NetBSD: main.c,v 1.14 1996/08/10 01:29:23 thorpej Exp $";
44 #endif
45 #endif /* not lint */
46
47 #include "defs.h"
48 #include "pathnames.h"
49 #ifdef sgi
50 #include "math.h"
51 #endif
52 #include <signal.h>
53 #include <fcntl.h>
54 #include <sys/file.h>
55
56 pid_t mypid;
57
58 naddr myaddr; /* system address */
59 char myname[MAXHOSTNAMELEN+1];
60
61 int supplier; /* supply or broadcast updates */
62 int supplier_set;
63 int ipforwarding = 1; /* kernel forwarding on */
64
65 int default_gateway; /* 1=advertise default */
66 int background = 1;
67 int ridhosts; /* 1=reduce host routes */
68 int mhome; /* 1=want multi-homed host route */
69 int advertise_mhome; /* 1=must continue adverising it */
70 int auth_ok = 1; /* 1=ignore auth if we do not care */
71
72 struct timeval epoch; /* when started */
73 struct timeval clk, prev_clk;
74 struct timeval now; /* current idea of time */
75 time_t now_stale;
76 time_t now_garbage;
77
78 struct timeval next_bcast; /* next general broadcast */
79 struct timeval no_flash = {EPOCH+SUPPLY_INTERVAL}; /* inhibit flash update */
80
81 fd_set fdbits;
82 int sock_max;
83 int rip_sock = -1; /* RIP socket */
84 struct interface *rip_sock_mcast; /* current multicast interface */
85 int rt_sock; /* routing socket */
86 int rt_sock_seqno;
87
88
89 static int get_rip_sock(naddr, int);
90 static void timevalsub(struct timeval *, struct timeval *, struct timeval *);
91
92 int
93 main(int argc,
94 char *argv[])
95 {
96 int n, mib[4], off;
97 size_t len;
98 char *p, *q;
99 struct timeval wtime, t2;
100 time_t dt;
101 fd_set ibits;
102 naddr p_addr, p_mask;
103 struct interface *ifp;
104 struct parm parm;
105 char *tracename = 0;
106
107
108 openlog("routed", LOG_PID | LOG_ODELAY, LOG_DAEMON);
109 ftrace = stdout;
110
111 gettimeofday(&clk, 0);
112 prev_clk = clk;
113 epoch = clk;
114 epoch.tv_sec -= EPOCH;
115 now.tv_sec = EPOCH;
116 now_stale = EPOCH - STALE_TIME;
117 now_garbage = EPOCH - GARBAGE_TIME;
118 wtime.tv_sec = 0;
119
120 (void)gethostname(myname, sizeof(myname)-1);
121 (void)gethost(myname, &myaddr);
122
123 while ((n = getopt(argc, argv, "sqdghmpAtT:F:P:")) != EOF) {
124 switch (n) {
125 case 's':
126 supplier = 1;
127 supplier_set = 1;
128 break;
129
130 case 'q':
131 supplier = 0;
132 supplier_set = 1;
133 break;
134
135 case 'd':
136 background = 0;
137 break;
138
139 case 'g':
140 bzero(&parm, sizeof(parm));
141 parm.parm_d_metric = 1;
142 p = check_parms(&parm);
143 if (p != 0)
144 msglog("bad -g: %s", p);
145 else
146 default_gateway = 1;
147 break;
148
149 case 'h': /* suppress extra host routes */
150 ridhosts = 1;
151 break;
152
153 case 'm': /* advertise host route */
154 mhome = 1; /* on multi-homed hosts */
155 break;
156
157 case 'A':
158 /* Ignore authentication if we do not care.
159 * Crazy as it is, that is what RFC 1723 requires.
160 */
161 auth_ok = 0;
162 break;
163
164 case 't':
165 new_tracelevel++;
166 break;
167
168 case 'T':
169 tracename = optarg;
170 break;
171
172 case 'F': /* minimal routes for SLIP */
173 n = HOPCNT_INFINITY-2;
174 p = strchr(optarg,',');
175 if (p && *p != '\0') {
176 n = (int)strtoul(p+1, &q, 0);
177 if (*q == '\0'
178 && n <= HOPCNT_INFINITY-1
179 && n >= 1)
180 *p = '\0';
181 }
182 if (!getnet(optarg, &p_addr, &p_mask)) {
183 msglog("bad network; \"-F %s\"",
184 optarg);
185 break;
186 }
187 bzero(&parm, sizeof(parm));
188 parm.parm_addr_h = ntohl(p_addr);
189 parm.parm_mask = p_mask;
190 parm.parm_d_metric = n;
191 p = check_parms(&parm);
192 if (p != 0)
193 msglog("bad -F: %s", p);
194 break;
195
196 case 'P':
197 /* handle arbirary, (usually) per-interface
198 * parameters.
199 */
200 p = parse_parms(optarg);
201 if (p != 0)
202 msglog("bad \"%s\" in \"%s\"",
203 p, optarg);
204 break;
205
206 default:
207 goto usage;
208 }
209 }
210 argc -= optind;
211 argv += optind;
212
213 if (tracename == 0 && argc >= 1) {
214 tracename = *argv++;
215 argc--;
216 }
217 if (argc != 0) {
218 usage:
219 logbad(0, "usage: routed [-sqdghmpAt] [-T /tracefile]"
220 " [-F net[,metric]] [-P parms]");
221 }
222 if (geteuid() != 0)
223 logbad(0, "requires UID 0");
224
225 mib[0] = CTL_NET;
226 mib[1] = PF_INET;
227 mib[2] = IPPROTO_IP;
228 mib[3] = IPCTL_FORWARDING;
229 len = sizeof(ipforwarding);
230 if (sysctl(mib, 4, &ipforwarding, &len, 0, 0) < 0)
231 LOGERR("sysctl(IPCTL_FORWARDING)");
232
233 if (!ipforwarding) {
234 if (supplier)
235 msglog("-s incompatible with ipforwarding=0");
236 if (default_gateway) {
237 msglog("-g incompatible with ipforwarding=0");
238 default_gateway = 0;
239 }
240 supplier = 0;
241 supplier_set = 1;
242 }
243 if (default_gateway) {
244 if (supplier_set && !supplier) {
245 msglog("-g and -q incompatible");
246 } else {
247 supplier = 1;
248 supplier_set = 1;
249 }
250 }
251
252
253 /* get into the background */
254 if (background) {
255 #ifdef sgi
256 if (0 > _daemonize(_DF_NOCHDIR,
257 new_tracelevel == 0 ? -1 : STDOUT_FILENO,
258 new_tracelevel == 0 ? -1 : STDERR_FILENO,
259 -1))
260 BADERR(0, "_daemonize()");
261 #else
262 if (daemon(1, 1) < 0)
263 BADERR(0,"daemon()");
264 #endif
265 }
266
267 mypid = getpid();
268 srandom((int)(clk.tv_sec ^ clk.tv_usec ^ mypid));
269
270 /* prepare socket connected to the kernel.
271 */
272 rt_sock = socket(AF_ROUTE, SOCK_RAW, 0);
273 if (rt_sock < 0)
274 BADERR(1,"rt_sock = socket()");
275 if (fcntl(rt_sock, F_SETFL, O_NONBLOCK) == -1)
276 logbad(1, "fcntl(rt_sock) O_NONBLOCK: %s", strerror(errno));
277 off = 0;
278 if (setsockopt(rt_sock, SOL_SOCKET,SO_USELOOPBACK,
279 &off,sizeof(off)) < 0)
280 LOGERR("setsockopt(SO_USELOOPBACK,0)");
281
282 /* prepare Router Discovery socket.
283 */
284 rdisc_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
285 if (rdisc_sock < 0)
286 BADERR(1,"rdisc_sock = socket()");
287 fix_sock(rdisc_sock,"rdisc_sock");
288
289 fix_select();
290
291
292 if (background && new_tracelevel == 0)
293 ftrace = 0;
294 if (tracename != 0) {
295 trace_on(tracename, 1);
296 if (new_tracelevel == 0) /* use stdout if file is bad */
297 new_tracelevel = 1;
298 }
299 set_tracelevel();
300
301 /* initialize radix tree */
302 rtinit();
303
304 /* Pick a random part of the second for our output to minimize
305 * collisions.
306 *
307 * Start broadcasting after hearing from other routers, and
308 * at a random time so a bunch of systems do not get synchronized
309 * after a power failure.
310 */
311 intvl_random(&next_bcast, EPOCH+MIN_WAITTIME, EPOCH+SUPPLY_INTERVAL);
312 age_timer.tv_usec = next_bcast.tv_usec;
313 age_timer.tv_sec = EPOCH+MIN_WAITTIME;
314 rdisc_timer = next_bcast;
315 ifinit_timer.tv_usec = next_bcast.tv_usec;
316
317 signal(SIGALRM, sigalrm);
318 signal(SIGHUP, sigterm);
319 signal(SIGTERM, sigterm);
320 signal(SIGINT, sigterm);
321 signal(SIGUSR1, sigtrace_on);
322 signal(SIGUSR2, sigtrace_off);
323
324 /* Collect an initial view of the world by checking the interface
325 * configuration and the kludge file.
326 */
327 gwkludge();
328 ifinit();
329 flush_kern();
330
331 /* Ask for routes */
332 rip_query();
333 if (!supplier)
334 rdisc_sol();
335
336 /* Loop forever, listening and broadcasting.
337 */
338 for (;;) {
339 prev_clk = clk;
340 gettimeofday(&clk, 0);
341 timevalsub(&t2, &clk, &prev_clk);
342 if (t2.tv_sec < 0
343 || t2.tv_sec > wtime.tv_sec + 5) {
344 /* Deal with time changes before other housekeeping to
345 * keep everything straight.
346 */
347 dt = t2.tv_sec;
348 if (dt > 0)
349 dt -= wtime.tv_sec;
350 trace_act("time changed by %d sec\n", dt);
351 epoch.tv_sec += dt;
352 }
353 timevalsub(&now, &clk, &epoch);
354 now_stale = now.tv_sec - STALE_TIME;
355 now_garbage = now.tv_sec - GARBAGE_TIME;
356
357 /* deal with interrupts that should affect tracing */
358 set_tracelevel();
359
360 if (stopint != 0) {
361 if (supplier) {
362 rip_bcast(0);
363 rdisc_adv();
364 }
365 trace_off("exiting with signal %d\n", stopint);
366 exit(stopint | 128);
367 }
368
369 /* look for new or dead interfaces */
370 timevalsub(&wtime, &ifinit_timer, &now);
371 if (wtime.tv_sec <= 0) {
372 wtime.tv_sec = 0;
373 ifinit();
374 rip_query();
375 continue;
376 }
377
378 /* If it is time, then broadcast our routes.
379 */
380 if (supplier || advertise_mhome) {
381 timevalsub(&t2, &next_bcast, &now);
382 if (t2.tv_sec <= 0) {
383 /* Synchronize the aging and broadcast
384 * timers to minimize awakenings
385 */
386 age(0);
387
388 rip_bcast(0);
389
390 /* It is desirable to send routing updates
391 * regularly. So schedule the next update
392 * 30 seconds after the previous one was
393 * secheduled, instead of 30 seconds after
394 * the previous update was finished.
395 * Even if we just started after discovering
396 * a 2nd interface or were otherwise delayed,
397 * pick a 30-second aniversary of the
398 * original broadcast time.
399 */
400 n = 1 + (0-t2.tv_sec)/SUPPLY_INTERVAL;
401 next_bcast.tv_sec += n*SUPPLY_INTERVAL;
402
403 continue;
404 }
405
406 if (timercmp(&t2, &wtime, <))
407 wtime = t2;
408 }
409
410 /* If we need a flash update, either do it now or
411 * set the delay to end when it is time.
412 *
413 * If we are within MIN_WAITTIME seconds of a full update,
414 * do not bother.
415 */
416 if (need_flash
417 && supplier
418 && no_flash.tv_sec+MIN_WAITTIME < next_bcast.tv_sec) {
419 /* accurate to the millisecond */
420 if (!timercmp(&no_flash, &now, >))
421 rip_bcast(1);
422 timevalsub(&t2, &no_flash, &now);
423 if (timercmp(&t2, &wtime, <))
424 wtime = t2;
425 }
426
427 /* trigger the main aging timer.
428 */
429 timevalsub(&t2, &age_timer, &now);
430 if (t2.tv_sec <= 0) {
431 age(0);
432 continue;
433 }
434 if (timercmp(&t2, &wtime, <))
435 wtime = t2;
436
437 /* update the kernel routing table
438 */
439 timevalsub(&t2, &need_kern, &now);
440 if (t2.tv_sec <= 0) {
441 age(0);
442 continue;
443 }
444 if (timercmp(&t2, &wtime, <))
445 wtime = t2;
446
447 /* take care of router discovery,
448 * but do it to the millisecond
449 */
450 if (!timercmp(&rdisc_timer, &now, >)) {
451 rdisc_age(0);
452 continue;
453 }
454 timevalsub(&t2, &rdisc_timer, &now);
455 if (timercmp(&t2, &wtime, <))
456 wtime = t2;
457
458
459 /* wait for input or a timer to expire.
460 */
461 trace_flush();
462 ibits = fdbits;
463 n = select(sock_max, &ibits, 0, 0, &wtime);
464 if (n <= 0) {
465 if (n < 0 && errno != EINTR && errno != EAGAIN)
466 BADERR(1,"select");
467 continue;
468 }
469
470 if (FD_ISSET(rt_sock, &ibits)) {
471 read_rt();
472 n--;
473 }
474 if (rdisc_sock >= 0 && FD_ISSET(rdisc_sock, &ibits)) {
475 read_d();
476 n--;
477 }
478 if (rip_sock >= 0 && FD_ISSET(rip_sock, &ibits)) {
479 read_rip(rip_sock, 0);
480 n--;
481 }
482
483 for (ifp = ifnet; n > 0 && 0 != ifp; ifp = ifp->int_next) {
484 if (ifp->int_rip_sock >= 0
485 && FD_ISSET(ifp->int_rip_sock, &ibits)) {
486 read_rip(ifp->int_rip_sock, ifp);
487 n--;
488 }
489 }
490 }
491 }
492
493
494 /* ARGSUSED */
495 void
496 sigalrm(int sig)
497 {
498 /* Historically, SIGALRM would cause the daemon to check for
499 * new and broken interfaces.
500 */
501 ifinit_timer.tv_sec = now.tv_sec;
502 trace_act("SIGALRM\n");
503 }
504
505
506 /* watch for fatal signals */
507 void
508 sigterm(int sig)
509 {
510 stopint = sig;
511 (void)signal(sig, SIG_DFL); /* catch it only once */
512 }
513
514
515 void
516 fix_select(void)
517 {
518 struct interface *ifp;
519
520
521 FD_ZERO(&fdbits);
522 sock_max = 0;
523
524 FD_SET(rt_sock, &fdbits);
525 if (sock_max <= rt_sock)
526 sock_max = rt_sock+1;
527 if (rip_sock >= 0) {
528 FD_SET(rip_sock, &fdbits);
529 if (sock_max <= rip_sock)
530 sock_max = rip_sock+1;
531 }
532 for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
533 if (ifp->int_rip_sock >= 0) {
534 FD_SET(ifp->int_rip_sock, &fdbits);
535 if (sock_max <= ifp->int_rip_sock)
536 sock_max = ifp->int_rip_sock+1;
537 }
538 }
539 if (rdisc_sock >= 0) {
540 FD_SET(rdisc_sock, &fdbits);
541 if (sock_max <= rdisc_sock)
542 sock_max = rdisc_sock+1;
543 }
544 }
545
546
547 void
548 fix_sock(int sock,
549 char *name)
550 {
551 int on;
552 #define MIN_SOCKBUF (4*1024)
553 static int rbuf;
554
555 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1)
556 logbad(1, "fcntl(%s) O_NONBLOCK: %s",
557 name, strerror(errno));
558 on = 1;
559 if (setsockopt(sock, SOL_SOCKET,SO_BROADCAST,
560 &on,sizeof(on)) < 0)
561 msglog("setsockopt(%s,SO_BROADCAST): %s",
562 name, strerror(errno));
563 if (rbuf >= MIN_SOCKBUF) {
564 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
565 &rbuf, sizeof(rbuf)) < 0)
566 msglog("setsockopt(%s,SO_RCVBUF=%d): %s",
567 name, rbuf, strerror(errno));
568 } else {
569 for (rbuf = 60*1024; ; rbuf -= 4096) {
570 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
571 &rbuf, sizeof(rbuf)) == 0) {
572 trace_act("RCVBUF=%d\n", rbuf);
573 break;
574 }
575 if (rbuf < MIN_SOCKBUF) {
576 msglog("setsockopt(%s,SO_RCVBUF = %d): %s",
577 name, rbuf, strerror(errno));
578 break;
579 }
580 }
581 }
582 }
583
584
585 /* get a rip socket
586 */
587 static int /* <0 or file descriptor */
588 get_rip_sock(naddr addr,
589 int serious) /* 1=failure to bind is serious */
590 {
591 struct sockaddr_in sin;
592 unsigned char ttl;
593 int s;
594
595
596 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
597 BADERR(1,"rip_sock = socket()");
598
599 bzero(&sin,sizeof(sin));
600 #ifdef _HAVE_SIN_LEN
601 sin.sin_len = sizeof(sin);
602 #endif
603 sin.sin_family = AF_INET;
604 sin.sin_port = htons(RIP_PORT);
605 sin.sin_addr.s_addr = addr;
606 if (bind(s, (struct sockaddr *)&sin,sizeof(sin)) < 0) {
607 if (serious)
608 BADERR(errno != EADDRINUSE, "bind(rip_sock)");
609 return -1;
610 }
611 fix_sock(s,"rip_sock");
612
613 ttl = 1;
614 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
615 &ttl, sizeof(ttl)) < 0)
616 DBGERR(1,"rip_sock setsockopt(IP_MULTICAST_TTL)");
617
618 return s;
619 }
620
621
622 /* turn off main RIP socket */
623 void
624 rip_off(void)
625 {
626 struct interface *ifp;
627 register naddr addr;
628
629
630 if (rip_sock >= 0 && !mhome) {
631 trace_act("turn off RIP\n");
632
633 (void)close(rip_sock);
634 rip_sock = -1;
635
636 /* get non-broadcast sockets to listen to queries.
637 */
638 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
639 if (ifp->int_rip_sock < 0
640 && !(ifp->int_state & IS_ALIAS)) {
641 addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
642 ? ifp->int_dstaddr
643 : ifp->int_addr);
644 ifp->int_rip_sock = get_rip_sock(addr, 0);
645 }
646 }
647
648 fix_select();
649
650 age(0);
651 }
652 }
653
654
655 /* turn on RIP multicast input via an interface
656 */
657 static void
658 rip_mcast_on(struct interface *ifp)
659 {
660 struct ip_mreq m;
661
662 if (!IS_RIP_IN_OFF(ifp->int_state)
663 && (ifp->int_if_flags & IFF_MULTICAST)
664 #ifdef MCAST_PPP_BUG
665 && !(ifp->int_if_flags & IFF_POINTOPOINT)
666 #endif
667 && !(ifp->int_state & IS_ALIAS)) {
668 m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
669 m.imr_interface.s_addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
670 ? ifp->int_dstaddr
671 : ifp->int_addr);
672 if (setsockopt(rip_sock,IPPROTO_IP, IP_ADD_MEMBERSHIP,
673 &m, sizeof(m)) < 0)
674 LOGERR("setsockopt(IP_ADD_MEMBERSHIP RIP)");
675 }
676 }
677
678
679 /* Prepare socket used for RIP.
680 */
681 void
682 rip_on(struct interface *ifp)
683 {
684 /* If the main RIP socket is already alive, only start receiving
685 * multicasts for this interface.
686 */
687 if (rip_sock >= 0) {
688 if (ifp != 0)
689 rip_mcast_on(ifp);
690 return;
691 }
692
693 /* If the main RIP socket is off, and it makes sense to turn it on,
694 * turn it on for all of the interfaces.
695 */
696 if (rip_interfaces > 0 && !rdisc_ok) {
697 trace_act("turn on RIP\n");
698
699 /* Close all of the query sockets so that we can open
700 * the main socket. SO_REUSEPORT is not a solution,
701 * since that would let two daemons bind to the broadcast
702 * socket.
703 */
704 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
705 if (ifp->int_rip_sock >= 0) {
706 (void)close(ifp->int_rip_sock);
707 ifp->int_rip_sock = -1;
708 }
709 }
710
711 rip_sock = get_rip_sock(INADDR_ANY, 1);
712 rip_sock_mcast = 0;
713
714 /* Do not advertise anything until we have heard something
715 */
716 if (next_bcast.tv_sec < now.tv_sec+MIN_WAITTIME)
717 next_bcast.tv_sec = now.tv_sec+MIN_WAITTIME;
718
719 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
720 if (!IS_RIP_IN_OFF(ifp->int_state))
721 ifp->int_state &= ~IS_RIP_QUERIED;
722 rip_mcast_on(ifp);
723 }
724
725 ifinit_timer.tv_sec = now.tv_sec;
726
727 fix_select();
728
729 } else if (ifp != 0
730 && ifp->int_rip_sock < 0
731 && !(ifp->int_state & IS_ALIAS)) {
732 /* RIP is off, so ensure there are sockets on which
733 * to listen for queries.
734 */
735 ifp->int_rip_sock = get_rip_sock(ifp->int_addr, 0);
736
737 fix_select();
738 }
739 }
740
741
742 /* get a random instant in an interval
743 */
744 void
745 intvl_random(struct timeval *tp, /* put value here */
746 u_long lo, /* value is after this second */
747 u_long hi) /* and before this */
748 {
749 tp->tv_sec = (time_t)(hi == lo
750 ? lo
751 : (lo + random() % ((hi - lo))));
752 tp->tv_usec = random() % 1000000;
753 }
754
755
756 void
757 timevaladd(struct timeval *t1,
758 struct timeval *t2)
759 {
760
761 t1->tv_sec += t2->tv_sec;
762 if ((t1->tv_usec += t2->tv_usec) > 1000000) {
763 t1->tv_sec++;
764 t1->tv_usec -= 1000000;
765 }
766 }
767
768
769 /* t1 = t2 - t3
770 */
771 static void
772 timevalsub(struct timeval *t1,
773 struct timeval *t2,
774 struct timeval *t3)
775 {
776 t1->tv_sec = t2->tv_sec - t3->tv_sec;
777 if ((t1->tv_usec = t2->tv_usec - t3->tv_usec) < 0) {
778 t1->tv_sec--;
779 t1->tv_usec += 1000000;
780 }
781 }
782
783
784 void
785 msglog(char *p, ...)
786 {
787 va_list args;
788
789 trace_flush();
790
791 va_start(args, p);
792 vsyslog(LOG_ERR, p, args);
793
794 if (ftrace != 0) {
795 if (ftrace == stdout)
796 (void)fputs("routed: ", ftrace);
797 (void)vfprintf(ftrace, p, args);
798 (void)fputc('\n', ftrace);
799 }
800 }
801
802
803 void
804 logbad(int dump, char *p, ...)
805 {
806 va_list args;
807
808 trace_flush();
809
810 va_start(args, p);
811 vsyslog(LOG_ERR, p, args);
812
813 (void)fputs("routed: ", stderr);
814 (void)vfprintf(stderr, p, args);
815 (void)fputs("; giving up\n",stderr);
816 (void)fflush(stderr);
817
818 if (dump)
819 abort();
820 exit(1);
821 }
822