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