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