Home | History | Annotate | Line # | Download | only in mrouted
main.c revision 1.4
      1  1.1   brezak /*
      2  1.1   brezak  * The mrouted program is covered by the license in the accompanying file
      3  1.1   brezak  * named "LICENSE".  Use of the mrouted program represents acceptance of
      4  1.1   brezak  * the terms and conditions listed in that file.
      5  1.1   brezak  *
      6  1.1   brezak  * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
      7  1.1   brezak  * Leland Stanford Junior University.
      8  1.1   brezak  *
      9  1.1   brezak  *
     10  1.4  mycroft  * $Id: main.c,v 1.4 1995/06/01 02:25:57 mycroft Exp $
     11  1.1   brezak  */
     12  1.1   brezak 
     13  1.1   brezak /*
     14  1.1   brezak  * Written by Steve Deering, Stanford University, February 1989.
     15  1.1   brezak  *
     16  1.1   brezak  * (An earlier version of DVMRP was implemented by David Waitzman of
     17  1.1   brezak  *  BBN STC by extending Berkeley's routed program.  Some of Waitzman's
     18  1.1   brezak  *  extensions have been incorporated into mrouted, but none of the
     19  1.1   brezak  *  original routed code has been adopted.)
     20  1.1   brezak  */
     21  1.1   brezak 
     22  1.1   brezak 
     23  1.1   brezak #include "defs.h"
     24  1.4  mycroft #include <string.h>
     25  1.4  mycroft #include <varargs.h>
     26  1.4  mycroft 
     27  1.4  mycroft #ifdef SNMP
     28  1.4  mycroft #include "snmp.h"
     29  1.4  mycroft #endif
     30  1.1   brezak 
     31  1.1   brezak extern char *configfilename;
     32  1.1   brezak 
     33  1.1   brezak static char pidfilename[]  = _PATH_MROUTED_PID;
     34  1.1   brezak static char dumpfilename[] = _PATH_MROUTED_DUMP;
     35  1.4  mycroft static char cachefilename[] = _PATH_MROUTED_CACHE;
     36  1.4  mycroft static char genidfilename[] = _PATH_MROUTED_GENID;
     37  1.4  mycroft 
     38  1.4  mycroft int cache_lifetime 	= DEFAULT_CACHE_LIFETIME;
     39  1.4  mycroft int max_prune_lifetime 	= DEFAULT_CACHE_LIFETIME * 2;
     40  1.1   brezak 
     41  1.2   brezak int debug = 0;
     42  1.4  mycroft u_char pruning = 1;	/* Enable pruning by default */
     43  1.4  mycroft 
     44  1.4  mycroft #define NHANDLERS	2
     45  1.1   brezak 
     46  1.4  mycroft static struct ihandler {
     47  1.4  mycroft     int fd;			/* File descriptor		 */
     48  1.4  mycroft     void (*func)();		/* Function to call with &fd_set */
     49  1.4  mycroft } ihandlers[NHANDLERS];
     50  1.4  mycroft static int nhandlers = 0;
     51  1.1   brezak 
     52  1.1   brezak /*
     53  1.1   brezak  * Forward declarations.
     54  1.1   brezak  */
     55  1.2   brezak static void fasttimer();
     56  1.1   brezak static void timer();
     57  1.4  mycroft static void cleanup();
     58  1.4  mycroft static void done();
     59  1.1   brezak static void dump();
     60  1.1   brezak static void fdump();
     61  1.4  mycroft static void cdump();
     62  1.4  mycroft static void restart();
     63  1.4  mycroft 
     64  1.4  mycroft int
     65  1.4  mycroft register_input_handler(fd, func)
     66  1.4  mycroft     int fd;
     67  1.4  mycroft     void (*func)();
     68  1.4  mycroft {
     69  1.4  mycroft     if (nhandlers >= NHANDLERS)
     70  1.4  mycroft 	return -1;
     71  1.4  mycroft 
     72  1.4  mycroft     ihandlers[nhandlers].fd = fd;
     73  1.4  mycroft     ihandlers[nhandlers++].func = func;
     74  1.1   brezak 
     75  1.4  mycroft     return 0;
     76  1.4  mycroft }
     77  1.1   brezak 
     78  1.4  mycroft int main(argc, argv)
     79  1.1   brezak     int argc;
     80  1.1   brezak     char *argv[];
     81  1.1   brezak {
     82  1.1   brezak     register int recvlen;
     83  1.1   brezak     register int omask;
     84  1.1   brezak     int dummy;
     85  1.1   brezak     FILE *fp;
     86  1.1   brezak     extern uid_t geteuid();
     87  1.4  mycroft     struct timeval tv;
     88  1.4  mycroft     u_long prev_genid;
     89  1.4  mycroft     int vers;
     90  1.4  mycroft     fd_set rfds, readers;
     91  1.4  mycroft     int nfds, n, i;
     92  1.4  mycroft #ifdef SNMP
     93  1.4  mycroft     char *myname;
     94  1.4  mycroft     fd_set wfds;
     95  1.4  mycroft 
     96  1.4  mycroft 
     97  1.4  mycroft     if (myname = strrchr(argv[0], '/'))
     98  1.4  mycroft         myname++;
     99  1.4  mycroft     if (myname == NULL || *myname == 0)
    100  1.4  mycroft         myname = argv[0];
    101  1.4  mycroft     isodetailor (myname, 0);
    102  1.4  mycroft #endif
    103  1.1   brezak 
    104  1.4  mycroft #ifdef SYSV
    105  1.4  mycroft     setvbuf(stderr, NULL, _IOLBF, 0);
    106  1.4  mycroft #else
    107  1.1   brezak     setlinebuf(stderr);
    108  1.4  mycroft #endif
    109  1.1   brezak 
    110  1.1   brezak     if (geteuid() != 0) {
    111  1.4  mycroft 	fprintf(stderr, "must be root\n");
    112  1.1   brezak 	exit(1);
    113  1.1   brezak     }
    114  1.1   brezak 
    115  1.1   brezak     argv++, argc--;
    116  1.1   brezak     while (argc > 0 && *argv[0] == '-') {
    117  1.1   brezak 	if (strcmp(*argv, "-d") == 0) {
    118  1.1   brezak 	    if (argc > 1 && isdigit(*(argv + 1)[0])) {
    119  1.1   brezak 		argv++, argc--;
    120  1.1   brezak 		debug = atoi(*argv);
    121  1.1   brezak 	    } else
    122  1.1   brezak 		debug = DEFAULT_DEBUG;
    123  1.1   brezak 	} else if (strcmp(*argv, "-c") == 0) {
    124  1.1   brezak 	    if (argc > 1) {
    125  1.1   brezak 		argv++, argc--;
    126  1.1   brezak 		configfilename = *argv;
    127  1.1   brezak 	    } else
    128  1.1   brezak 		goto usage;
    129  1.4  mycroft 	} else if (strcmp(*argv, "-p") == 0) {
    130  1.4  mycroft 	    pruning = 0;
    131  1.1   brezak 	} else
    132  1.1   brezak 	    goto usage;
    133  1.1   brezak 	argv++, argc--;
    134  1.1   brezak     }
    135  1.1   brezak 
    136  1.1   brezak     if (argc > 0) {
    137  1.4  mycroft usage:	fprintf(stderr,
    138  1.4  mycroft 		"usage: mrouted [-p] [-c configfile] [-d [debug_level]]\n");
    139  1.1   brezak 	exit(1);
    140  1.1   brezak     }
    141  1.1   brezak 
    142  1.1   brezak     if (debug == 0) {
    143  1.1   brezak 	/*
    144  1.1   brezak 	 * Detach from the terminal
    145  1.1   brezak 	 */
    146  1.1   brezak 	int t;
    147  1.4  mycroft 
    148  1.1   brezak 	if (fork()) exit(0);
    149  1.1   brezak 	(void)close(0);
    150  1.1   brezak 	(void)close(1);
    151  1.1   brezak 	(void)close(2);
    152  1.1   brezak 	(void)open("/", 0);
    153  1.1   brezak 	(void)dup2(0, 1);
    154  1.1   brezak 	(void)dup2(0, 2);
    155  1.4  mycroft #ifdef TIOCNOTTY
    156  1.1   brezak 	t = open("/dev/tty", 2);
    157  1.1   brezak 	if (t >= 0) {
    158  1.1   brezak 	    (void)ioctl(t, TIOCNOTTY, (char *)0);
    159  1.1   brezak 	    (void)close(t);
    160  1.1   brezak 	}
    161  1.4  mycroft #else
    162  1.4  mycroft 	if (setsid() < 0)
    163  1.4  mycroft 	    perror("setsid");
    164  1.4  mycroft #endif
    165  1.1   brezak     }
    166  1.4  mycroft     else
    167  1.4  mycroft 	fprintf(stderr, "debug level %u\n", debug);
    168  1.1   brezak 
    169  1.1   brezak #ifdef LOG_DAEMON
    170  1.1   brezak     (void)openlog("mrouted", LOG_PID, LOG_DAEMON);
    171  1.1   brezak     (void)setlogmask(LOG_UPTO(LOG_NOTICE));
    172  1.1   brezak #else
    173  1.1   brezak     (void)openlog("mrouted", LOG_PID);
    174  1.1   brezak #endif
    175  1.1   brezak     log(LOG_NOTICE, 0, "mrouted version %d.%d",
    176  1.1   brezak 			PROTOCOL_VERSION, MROUTED_VERSION);
    177  1.1   brezak 
    178  1.4  mycroft #ifdef SYSV
    179  1.4  mycroft     srand48(time(NULL));
    180  1.4  mycroft #else
    181  1.4  mycroft     srandom(gethostid());
    182  1.4  mycroft #endif
    183  1.4  mycroft 
    184  1.4  mycroft     /*
    185  1.4  mycroft      * Get generation id
    186  1.4  mycroft      */
    187  1.4  mycroft     gettimeofday(&tv, 0);
    188  1.4  mycroft     dvmrp_genid = tv.tv_sec;
    189  1.4  mycroft 
    190  1.4  mycroft     fp = fopen(genidfilename, "r");
    191  1.4  mycroft     if (fp != NULL) {
    192  1.4  mycroft 	fscanf(fp, "%d", &prev_genid);
    193  1.4  mycroft 	if (prev_genid == dvmrp_genid)
    194  1.4  mycroft 	    dvmrp_genid++;
    195  1.4  mycroft 	(void) fclose(fp);
    196  1.4  mycroft     }
    197  1.4  mycroft 
    198  1.4  mycroft     fp = fopen(genidfilename, "w");
    199  1.1   brezak     if (fp != NULL) {
    200  1.4  mycroft 	fprintf(fp, "%d", dvmrp_genid);
    201  1.1   brezak 	(void) fclose(fp);
    202  1.1   brezak     }
    203  1.1   brezak 
    204  1.4  mycroft     callout_init();
    205  1.4  mycroft 
    206  1.4  mycroft #ifdef SNMP
    207  1.4  mycroft     snmp_init();
    208  1.4  mycroft #endif
    209  1.4  mycroft 
    210  1.1   brezak     init_igmp();
    211  1.1   brezak     k_init_dvmrp();		/* enable DVMRP routing in kernel */
    212  1.4  mycroft 
    213  1.4  mycroft #ifndef OLD_KERNEL
    214  1.4  mycroft     vers = k_get_version();
    215  1.4  mycroft     if ((((vers >> 8) & 0xff) != PROTOCOL_VERSION) ||
    216  1.4  mycroft 	 ((vers & 0xff) != MROUTED_VERSION))
    217  1.4  mycroft 	log(LOG_ERR, 0, "kernel (v%d.%d)/mrouted (v%d.%d) version mismatch",
    218  1.4  mycroft 		(vers >> 8) & 0xff, vers & 0xff,
    219  1.4  mycroft 		PROTOCOL_VERSION, MROUTED_VERSION);
    220  1.4  mycroft #endif
    221  1.4  mycroft 
    222  1.1   brezak     init_routes();
    223  1.4  mycroft     init_ktable();
    224  1.1   brezak     init_vifs();
    225  1.4  mycroft #ifdef RSRR
    226  1.4  mycroft     rsrr_init();
    227  1.4  mycroft #endif /* RSRR */
    228  1.4  mycroft 
    229  1.4  mycroft #if defined(__STDC__) || defined(__GNUC__)
    230  1.4  mycroft     /* Allow cleanup if unexpected exit.  Apparently some architectures
    231  1.4  mycroft      * have a kernel bug where closing the socket doesn't do an
    232  1.4  mycroft      * ip_mrouter_done(), so we attempt to do it on exit.
    233  1.4  mycroft      */
    234  1.4  mycroft     atexit(cleanup);
    235  1.4  mycroft #endif
    236  1.4  mycroft 
    237  1.4  mycroft     if (debug)
    238  1.4  mycroft 	fprintf(stderr, "pruning %s\n", pruning ? "on" : "off");
    239  1.4  mycroft 
    240  1.4  mycroft     fp = fopen(pidfilename, "w");
    241  1.4  mycroft     if (fp != NULL) {
    242  1.4  mycroft 	fprintf(fp, "%d\n", getpid());
    243  1.4  mycroft 	(void) fclose(fp);
    244  1.4  mycroft     }
    245  1.1   brezak 
    246  1.1   brezak     if (debug >= 2) dump();
    247  1.1   brezak 
    248  1.2   brezak     (void)signal(SIGALRM, fasttimer);
    249  1.4  mycroft 
    250  1.4  mycroft     (void)signal(SIGHUP,  restart);
    251  1.4  mycroft     (void)signal(SIGTERM, done);
    252  1.4  mycroft     (void)signal(SIGINT,  done);
    253  1.1   brezak     (void)signal(SIGUSR1, fdump);
    254  1.4  mycroft     (void)signal(SIGUSR2, cdump);
    255  1.1   brezak     if (debug != 0)
    256  1.1   brezak 	(void)signal(SIGQUIT, dump);
    257  1.1   brezak 
    258  1.4  mycroft     FD_ZERO(&readers);
    259  1.4  mycroft     FD_SET(igmp_socket, &readers);
    260  1.4  mycroft     nfds = igmp_socket + 1;
    261  1.4  mycroft     for (i = 0; i < nhandlers; i++) {
    262  1.4  mycroft 	FD_SET(ihandlers[i].fd, &readers);
    263  1.4  mycroft 	if (ihandlers[i].fd >= nfds)
    264  1.4  mycroft 	    nfds = ihandlers[i].fd + 1;
    265  1.4  mycroft     }
    266  1.4  mycroft 
    267  1.2   brezak     (void)alarm(1);	 /* schedule first timer interrupt */
    268  1.1   brezak 
    269  1.1   brezak     /*
    270  1.1   brezak      * Main receive loop.
    271  1.1   brezak      */
    272  1.1   brezak     dummy = 0;
    273  1.1   brezak     for(;;) {
    274  1.4  mycroft 	bcopy((char *)&readers, (char *)&rfds, sizeof(rfds));
    275  1.4  mycroft #ifdef SNMP
    276  1.4  mycroft         FD_ZERO(&wfds);
    277  1.4  mycroft 
    278  1.4  mycroft         if (smux_fd != NOTOK) {
    279  1.4  mycroft            if (rock_and_roll)
    280  1.4  mycroft               FD_SET(smux_fd, &rfds);
    281  1.4  mycroft            else
    282  1.4  mycroft               FD_SET(smux_fd, &wfds);
    283  1.4  mycroft            if (smux_fd >= nfds)
    284  1.4  mycroft               nfds = smux_fd + 1;
    285  1.4  mycroft         }
    286  1.4  mycroft 
    287  1.4  mycroft         if ((n = xselect(nfds, &rfds, &wfds, NULLFD, NOTOK))==NOTOK) {
    288  1.4  mycroft #else
    289  1.4  mycroft 	if ((n = select(nfds, &rfds, NULL, NULL, NULL)) < 0) {
    290  1.4  mycroft #endif
    291  1.4  mycroft             if (errno != EINTR) /* SIGALRM is expected */
    292  1.4  mycroft                 log(LOG_WARNING, errno, "select failed");
    293  1.4  mycroft             continue;
    294  1.4  mycroft         }
    295  1.4  mycroft 
    296  1.4  mycroft 	if (FD_ISSET(igmp_socket, &rfds)) {
    297  1.4  mycroft 	    recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
    298  1.4  mycroft 			       0, NULL, &dummy);
    299  1.4  mycroft 	    if (recvlen < 0) {
    300  1.4  mycroft 		if (errno != EINTR) log(LOG_ERR, errno, "recvfrom");
    301  1.4  mycroft 		continue;
    302  1.4  mycroft 	    }
    303  1.4  mycroft 	    omask = sigblock(sigmask(SIGALRM));
    304  1.4  mycroft 	    accept_igmp(recvlen);
    305  1.4  mycroft 	    (void)sigsetmask(omask);
    306  1.4  mycroft         }
    307  1.4  mycroft 
    308  1.4  mycroft 	for (i = 0; i < nhandlers; i++) {
    309  1.4  mycroft 	    if (FD_ISSET(ihandlers[i].fd, &rfds)) {
    310  1.4  mycroft 		(*ihandlers[i].func)(&rfds);
    311  1.4  mycroft 	    }
    312  1.1   brezak 	}
    313  1.4  mycroft 
    314  1.4  mycroft #ifdef SNMP
    315  1.4  mycroft         if (smux_fd != NOTOK) {
    316  1.4  mycroft             if (rock_and_roll) {
    317  1.4  mycroft 		if (FD_ISSET(smux_fd, &rfds))
    318  1.4  mycroft 		    doit_smux();
    319  1.4  mycroft 	    } else if (FD_ISSET(smux_fd, &wfds))
    320  1.4  mycroft                 start_smux();
    321  1.4  mycroft         }
    322  1.4  mycroft #endif
    323  1.1   brezak     }
    324  1.1   brezak }
    325  1.1   brezak 
    326  1.1   brezak 
    327  1.1   brezak /*
    328  1.4  mycroft  * routine invoked every second.  Its main goal is to cycle through
    329  1.2   brezak  * the routing table and send partial updates to all neighbors at a
    330  1.2   brezak  * rate that will cause the entire table to be sent in ROUTE_REPORT_INTERVAL
    331  1.2   brezak  * seconds.  Also, every TIMER_INTERVAL seconds it calls timer() to
    332  1.2   brezak  * do all the other time-based processing.
    333  1.2   brezak  */
    334  1.4  mycroft static void
    335  1.4  mycroft fasttimer()
    336  1.2   brezak {
    337  1.2   brezak     static unsigned int tlast;
    338  1.2   brezak     static unsigned int nsent;
    339  1.2   brezak     register unsigned int t = tlast + 1;
    340  1.2   brezak     register int n;
    341  1.2   brezak 
    342  1.2   brezak     /*
    343  1.2   brezak      * if we're in the last second, send everything that's left.
    344  1.2   brezak      * otherwise send at least the fraction we should have sent by now.
    345  1.2   brezak      */
    346  1.2   brezak     if (t >= ROUTE_REPORT_INTERVAL) {
    347  1.2   brezak 	register int nleft = nroutes - nsent;
    348  1.2   brezak 	while (nleft > 0) {
    349  1.2   brezak 	    if ((n = report_next_chunk()) <= 0)
    350  1.2   brezak 		break;
    351  1.2   brezak 	    nleft -= n;
    352  1.2   brezak 	}
    353  1.2   brezak 	tlast = 0;
    354  1.2   brezak 	nsent = 0;
    355  1.2   brezak     } else {
    356  1.2   brezak 	register unsigned int ncum = nroutes * t / ROUTE_REPORT_INTERVAL;
    357  1.2   brezak 	while (nsent < ncum) {
    358  1.2   brezak 	    if ((n = report_next_chunk()) <= 0)
    359  1.2   brezak 		break;
    360  1.2   brezak 	    nsent += n;
    361  1.2   brezak 	}
    362  1.2   brezak 	tlast = t;
    363  1.2   brezak     }
    364  1.2   brezak     if ((t % TIMER_INTERVAL) == 0)
    365  1.2   brezak 	timer();
    366  1.2   brezak 
    367  1.4  mycroft     age_callout_queue();/* Advance the timer for the callout queue
    368  1.4  mycroft 				for groups */
    369  1.2   brezak     alarm(1);
    370  1.2   brezak }
    371  1.2   brezak 
    372  1.2   brezak /*
    373  1.1   brezak  * The 'virtual_time' variable is initialized to a value that will cause the
    374  1.1   brezak  * first invocation of timer() to send a probe or route report to all vifs
    375  1.1   brezak  * and send group membership queries to all subnets for which this router is
    376  1.1   brezak  * querier.  This first invocation occurs approximately TIMER_INTERVAL seconds
    377  1.1   brezak  * after the router starts up.   Note that probes for neighbors and queries
    378  1.1   brezak  * for group memberships are also sent at start-up time, as part of initial-
    379  1.1   brezak  * ization.  This repetition after a short interval is desirable for quickly
    380  1.1   brezak  * building up topology and membership information in the presence of possible
    381  1.1   brezak  * packet loss.
    382  1.1   brezak  *
    383  1.1   brezak  * 'virtual_time' advances at a rate that is only a crude approximation of
    384  1.1   brezak  * real time, because it does not take into account any time spent processing,
    385  1.1   brezak  * and because the timer intervals are sometimes shrunk by a random amount to
    386  1.1   brezak  * avoid unwanted synchronization with other routers.
    387  1.1   brezak  */
    388  1.1   brezak 
    389  1.1   brezak static u_long virtual_time = 0;
    390  1.1   brezak 
    391  1.1   brezak 
    392  1.1   brezak /*
    393  1.1   brezak  * Timer routine.  Performs periodic neighbor probing, route reporting, and
    394  1.1   brezak  * group querying duties, and drives various timers in routing entries and
    395  1.1   brezak  * virtual interface data structures.
    396  1.1   brezak  */
    397  1.4  mycroft static void
    398  1.4  mycroft timer()
    399  1.1   brezak {
    400  1.1   brezak     age_routes();	/* Advance the timers in the route entries     */
    401  1.4  mycroft     age_vifs();		/* Advance the timers for neighbors */
    402  1.4  mycroft     age_table_entry();	/* Advance the timers for the cache entries */
    403  1.1   brezak 
    404  1.1   brezak     if (virtual_time % GROUP_QUERY_INTERVAL == 0) {
    405  1.1   brezak 	/*
    406  1.1   brezak 	 * Time to query the local group memberships on all subnets
    407  1.1   brezak 	 * for which this router is the elected querier.
    408  1.1   brezak 	 */
    409  1.1   brezak 	query_groups();
    410  1.1   brezak     }
    411  1.1   brezak 
    412  1.1   brezak     if (virtual_time % NEIGHBOR_PROBE_INTERVAL == 0) {
    413  1.1   brezak 	/*
    414  1.1   brezak 	 * Time to send a probe on all vifs from which no neighbors have
    415  1.1   brezak 	 * been heard.  Also, check if any inoperative interfaces have now
    416  1.1   brezak 	 * come up.  (If they have, they will also be probed as part of
    417  1.1   brezak 	 * their initialization.)
    418  1.1   brezak 	 */
    419  1.1   brezak 	probe_for_neighbors();
    420  1.1   brezak 
    421  1.1   brezak 	if (vifs_down)
    422  1.1   brezak 	    check_vif_state();
    423  1.1   brezak     }
    424  1.1   brezak 
    425  1.1   brezak     delay_change_reports = FALSE;
    426  1.2   brezak     if (routes_changed) {
    427  1.1   brezak 	/*
    428  1.1   brezak 	 * Some routes have changed since the last timer interrupt, but
    429  1.1   brezak 	 * have not been reported yet.  Report the changed routes to all
    430  1.1   brezak 	 * neighbors.
    431  1.1   brezak 	 */
    432  1.1   brezak 	report_to_all_neighbors(CHANGED_ROUTES);
    433  1.1   brezak     }
    434  1.1   brezak 
    435  1.4  mycroft #ifdef SNMP
    436  1.4  mycroft     if (smux_fd == NOTOK && !dont_bother_anymore
    437  1.4  mycroft 		 && virtual_time % SNMPD_RETRY_INTERVAL == 0) {
    438  1.4  mycroft 	/*
    439  1.4  mycroft 	 * Time to check for snmpd running.
    440  1.4  mycroft 	 */
    441  1.4  mycroft         try_smux_init();
    442  1.4  mycroft     }
    443  1.4  mycroft #endif
    444  1.4  mycroft 
    445  1.1   brezak     /*
    446  1.2   brezak      * Advance virtual time
    447  1.1   brezak      */
    448  1.1   brezak     virtual_time += TIMER_INTERVAL;
    449  1.1   brezak }
    450  1.1   brezak 
    451  1.1   brezak 
    452  1.1   brezak /*
    453  1.4  mycroft  * On termination, let everyone know we're going away.
    454  1.1   brezak  */
    455  1.4  mycroft static void
    456  1.4  mycroft done()
    457  1.1   brezak {
    458  1.4  mycroft     log(LOG_NOTICE, 0, "mrouted version %d.%d exiting",
    459  1.4  mycroft 			PROTOCOL_VERSION, MROUTED_VERSION);
    460  1.4  mycroft     cleanup();
    461  1.4  mycroft     _exit(1);
    462  1.4  mycroft }
    463  1.4  mycroft 
    464  1.4  mycroft static void
    465  1.4  mycroft cleanup()
    466  1.4  mycroft {
    467  1.4  mycroft     static in_cleanup = 0;
    468  1.4  mycroft 
    469  1.4  mycroft     if (!in_cleanup) {
    470  1.4  mycroft 	in_cleanup++;
    471  1.4  mycroft #ifdef RSRR
    472  1.4  mycroft 	rsrr_clean();
    473  1.4  mycroft #endif /* RSRR */
    474  1.4  mycroft 	expire_all_routes();
    475  1.4  mycroft 	report_to_all_neighbors(ALL_ROUTES);
    476  1.4  mycroft 	k_stop_dvmrp();
    477  1.4  mycroft     }
    478  1.1   brezak }
    479  1.1   brezak 
    480  1.1   brezak 
    481  1.1   brezak /*
    482  1.1   brezak  * Dump internal data structures to stderr.
    483  1.1   brezak  */
    484  1.4  mycroft static void
    485  1.4  mycroft dump()
    486  1.1   brezak {
    487  1.1   brezak     dump_vifs(stderr);
    488  1.1   brezak     dump_routes(stderr);
    489  1.1   brezak }
    490  1.1   brezak 
    491  1.1   brezak 
    492  1.1   brezak /*
    493  1.1   brezak  * Dump internal data structures to a file.
    494  1.1   brezak  */
    495  1.4  mycroft static void
    496  1.4  mycroft fdump()
    497  1.1   brezak {
    498  1.1   brezak     FILE *fp;
    499  1.1   brezak 
    500  1.1   brezak     fp = fopen(dumpfilename, "w");
    501  1.1   brezak     if (fp != NULL) {
    502  1.1   brezak 	dump_vifs(fp);
    503  1.1   brezak 	dump_routes(fp);
    504  1.1   brezak 	(void) fclose(fp);
    505  1.1   brezak     }
    506  1.1   brezak }
    507  1.1   brezak 
    508  1.1   brezak 
    509  1.1   brezak /*
    510  1.4  mycroft  * Dump local cache contents to a file.
    511  1.4  mycroft  */
    512  1.4  mycroft static void
    513  1.4  mycroft cdump()
    514  1.4  mycroft {
    515  1.4  mycroft     FILE *fp;
    516  1.4  mycroft 
    517  1.4  mycroft     fp = fopen(cachefilename, "w");
    518  1.4  mycroft     if (fp != NULL) {
    519  1.4  mycroft 	dump_cache(fp);
    520  1.4  mycroft 	(void) fclose(fp);
    521  1.4  mycroft     }
    522  1.4  mycroft }
    523  1.4  mycroft 
    524  1.4  mycroft 
    525  1.4  mycroft /*
    526  1.4  mycroft  * Restart mrouted
    527  1.4  mycroft  */
    528  1.4  mycroft static void
    529  1.4  mycroft restart()
    530  1.4  mycroft {
    531  1.4  mycroft     register int omask;
    532  1.4  mycroft 
    533  1.4  mycroft     log(LOG_NOTICE, 0, "mrouted version %d.%d restart",
    534  1.4  mycroft 			PROTOCOL_VERSION, MROUTED_VERSION);
    535  1.4  mycroft 
    536  1.4  mycroft     /*
    537  1.4  mycroft      * reset all the entries
    538  1.4  mycroft      */
    539  1.4  mycroft     omask = sigblock(sigmask(SIGALRM));
    540  1.4  mycroft     free_all_prunes();
    541  1.4  mycroft     free_all_routes();
    542  1.4  mycroft     stop_all_vifs();
    543  1.4  mycroft     k_stop_dvmrp();
    544  1.4  mycroft     close(igmp_socket);
    545  1.4  mycroft     close(udp_socket);
    546  1.4  mycroft 
    547  1.4  mycroft     /*
    548  1.4  mycroft      * start processing again
    549  1.4  mycroft      */
    550  1.4  mycroft     dvmrp_genid++;
    551  1.4  mycroft     pruning = 1;
    552  1.4  mycroft 
    553  1.4  mycroft     init_igmp();
    554  1.4  mycroft     k_init_dvmrp();		/* enable DVMRP routing in kernel */
    555  1.4  mycroft     init_routes();
    556  1.4  mycroft     init_ktable();
    557  1.4  mycroft     init_vifs();
    558  1.4  mycroft 
    559  1.4  mycroft     (void)sigsetmask(omask);
    560  1.4  mycroft }
    561  1.4  mycroft 
    562  1.4  mycroft 
    563  1.4  mycroft /*
    564  1.1   brezak  * Log errors and other messages to the system log daemon and to stderr,
    565  1.1   brezak  * according to the severity of the message and the current debug level.
    566  1.1   brezak  * For errors of severity LOG_ERR or worse, terminate the program.
    567  1.1   brezak  */
    568  1.4  mycroft /*VARARGS3*/
    569  1.4  mycroft void
    570  1.4  mycroft log(severity, syserr, format, va_alist)
    571  1.1   brezak     int severity, syserr;
    572  1.1   brezak     char *format;
    573  1.4  mycroft     va_dcl
    574  1.1   brezak {
    575  1.4  mycroft     va_list ap;
    576  1.4  mycroft     static char fmt[211] = "warning - ";
    577  1.4  mycroft     char *msg;
    578  1.4  mycroft     char tbuf[20];
    579  1.4  mycroft     struct timeval now;
    580  1.4  mycroft     struct tm *thyme;
    581  1.4  mycroft 
    582  1.4  mycroft     va_start(ap);
    583  1.4  mycroft     vsprintf(&fmt[10], format, ap);
    584  1.4  mycroft     va_end(ap);
    585  1.4  mycroft     msg = (severity == LOG_WARNING) ? fmt : &fmt[10];
    586  1.1   brezak 
    587  1.1   brezak     switch (debug) {
    588  1.1   brezak 	case 0: break;
    589  1.1   brezak 	case 1: if (severity > LOG_NOTICE) break;
    590  1.1   brezak 	case 2: if (severity > LOG_INFO  ) break;
    591  1.1   brezak 	default:
    592  1.4  mycroft 	    gettimeofday(&now,NULL);
    593  1.4  mycroft 	    thyme = localtime((time_t *)&now.tv_sec);
    594  1.4  mycroft 	    strftime(tbuf, sizeof(tbuf), "%X.%%03d ", thyme);
    595  1.4  mycroft 	    fprintf(stderr, tbuf, now.tv_usec / 1000);
    596  1.4  mycroft 	    fprintf(stderr, "%s", msg);
    597  1.1   brezak 	    if (syserr == 0)
    598  1.1   brezak 		fprintf(stderr, "\n");
    599  1.1   brezak 	    else
    600  1.3  mycroft 		fprintf(stderr, ": %s\n", strerror(syserr));
    601  1.1   brezak     }
    602  1.1   brezak 
    603  1.1   brezak     if (severity <= LOG_NOTICE) {
    604  1.1   brezak 	if (syserr != 0) {
    605  1.4  mycroft 	    errno = syserr;
    606  1.4  mycroft 	    syslog(severity, "%s: %m", msg);
    607  1.4  mycroft 	} else
    608  1.4  mycroft 	    syslog(severity, "%s", msg);
    609  1.1   brezak 
    610  1.1   brezak 	if (severity <= LOG_ERR) exit(-1);
    611  1.1   brezak     }
    612  1.1   brezak }
    613