1994-09-08 00:26:13 +00:00
|
|
|
/*
|
|
|
|
* The mrouted program is covered by the license in the accompanying file
|
|
|
|
* named "LICENSE". Use of the mrouted program represents acceptance of
|
|
|
|
* the terms and conditions listed in that file.
|
|
|
|
*
|
|
|
|
* The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
|
|
|
|
* Leland Stanford Junior University.
|
|
|
|
*
|
1999-01-20 07:55:57 +00:00
|
|
|
*
|
|
|
|
* main.c,v 3.8.4.29 1998/03/01 01:49:00 fenner Exp
|
1994-09-08 00:26:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Written by Steve Deering, Stanford University, February 1989.
|
|
|
|
*
|
|
|
|
* (An earlier version of DVMRP was implemented by David Waitzman of
|
|
|
|
* BBN STC by extending Berkeley's routed program. Some of Waitzman's
|
|
|
|
* extensions have been incorporated into mrouted, but none of the
|
|
|
|
* original routed code has been adopted.)
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1997-09-30 06:15:23 +00:00
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1999-08-28 01:35:59 +00:00
|
|
|
"$FreeBSD$";
|
1997-09-30 06:15:23 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <err.h>
|
1994-09-08 00:26:13 +00:00
|
|
|
#include "defs.h"
|
1995-06-28 17:58:49 +00:00
|
|
|
#ifdef __STDC__
|
|
|
|
#include <stdarg.h>
|
|
|
|
#else
|
1995-06-13 18:05:16 +00:00
|
|
|
#include <varargs.h>
|
1995-06-28 17:58:49 +00:00
|
|
|
#endif
|
|
|
|
#include <fcntl.h>
|
2000-12-09 09:35:55 +00:00
|
|
|
#include <paths.h>
|
1995-06-13 18:05:16 +00:00
|
|
|
|
|
|
|
#ifdef SNMP
|
|
|
|
#include "snmp.h"
|
|
|
|
#endif
|
1994-09-08 00:26:13 +00:00
|
|
|
|
|
|
|
extern char *configfilename;
|
1996-01-06 21:10:30 +00:00
|
|
|
char versionstring[100];
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1995-06-13 18:05:16 +00:00
|
|
|
static char pidfilename[] = _PATH_MROUTED_PID;
|
|
|
|
static char dumpfilename[] = _PATH_MROUTED_DUMP;
|
|
|
|
static char cachefilename[] = _PATH_MROUTED_CACHE;
|
|
|
|
static char genidfilename[] = _PATH_MROUTED_GENID;
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
static int haveterminal = 1;
|
|
|
|
int did_final_init = 0;
|
|
|
|
|
|
|
|
static int sighandled = 0;
|
|
|
|
#define GOT_SIGINT 0x01
|
|
|
|
#define GOT_SIGHUP 0x02
|
|
|
|
#define GOT_SIGUSR1 0x04
|
|
|
|
#define GOT_SIGUSR2 0x08
|
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
int cache_lifetime = DEFAULT_CACHE_LIFETIME;
|
1999-01-20 07:55:57 +00:00
|
|
|
int prune_lifetime = AVERAGE_PRUNE_LIFETIME;
|
1994-09-08 00:26:13 +00:00
|
|
|
|
|
|
|
int debug = 0;
|
1999-01-20 07:55:57 +00:00
|
|
|
char *progname;
|
|
|
|
time_t mrouted_init_time;
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1996-01-06 21:10:30 +00:00
|
|
|
#ifdef SNMP
|
|
|
|
#define NHANDLERS 34
|
|
|
|
#else
|
1995-06-13 18:05:16 +00:00
|
|
|
#define NHANDLERS 2
|
1996-01-06 21:10:30 +00:00
|
|
|
#endif
|
1995-06-13 18:05:16 +00:00
|
|
|
|
|
|
|
static struct ihandler {
|
|
|
|
int fd; /* File descriptor */
|
1995-06-28 17:58:49 +00:00
|
|
|
ihfunc_t func; /* Function to call with &fd_set */
|
1995-06-13 18:05:16 +00:00
|
|
|
} ihandlers[NHANDLERS];
|
|
|
|
static int nhandlers = 0;
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
static struct debugname {
|
|
|
|
char *name;
|
|
|
|
int level;
|
|
|
|
int nchars;
|
|
|
|
} debugnames[] = {
|
|
|
|
{ "packet", DEBUG_PKT, 2 },
|
|
|
|
{ "pkt", DEBUG_PKT, 3 },
|
|
|
|
{ "pruning", DEBUG_PRUNE, 1 },
|
|
|
|
{ "prunes", DEBUG_PRUNE, 1 },
|
|
|
|
{ "routing", DEBUG_ROUTE, 1 },
|
|
|
|
{ "routes", DEBUG_ROUTE, 1 },
|
|
|
|
{ "route_detail", DEBUG_RTDETAIL, 6 },
|
|
|
|
{ "rtdetail", DEBUG_RTDETAIL, 2 },
|
|
|
|
{ "peers", DEBUG_PEER, 2 },
|
|
|
|
{ "neighbors", DEBUG_PEER, 1 },
|
|
|
|
{ "cache", DEBUG_CACHE, 1 },
|
|
|
|
{ "timeout", DEBUG_TIMEOUT, 1 },
|
|
|
|
{ "callout", DEBUG_TIMEOUT, 2 },
|
|
|
|
{ "interface", DEBUG_IF, 2 },
|
|
|
|
{ "vif", DEBUG_IF, 1 },
|
|
|
|
{ "membership", DEBUG_MEMBER, 1 },
|
|
|
|
{ "groups", DEBUG_MEMBER, 1 },
|
|
|
|
{ "traceroute", DEBUG_TRACE, 2 },
|
|
|
|
{ "mtrace", DEBUG_TRACE, 2 },
|
|
|
|
{ "igmp", DEBUG_IGMP, 1 },
|
|
|
|
{ "icmp", DEBUG_ICMP, 2 },
|
|
|
|
{ "rsrr", DEBUG_RSRR, 2 },
|
|
|
|
{ "3", 0xffffffff, 1 } /* compat. */
|
|
|
|
};
|
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
/*
|
|
|
|
* Forward declarations.
|
|
|
|
*/
|
1999-01-20 07:55:57 +00:00
|
|
|
static void final_init __P((void *));
|
|
|
|
static void fasttimer __P((void *));
|
|
|
|
static void timer __P((void *));
|
|
|
|
static void dump __P((void));
|
|
|
|
static void dump_version __P((FILE *));
|
|
|
|
static void fdump __P((void));
|
|
|
|
static void cdump __P((void));
|
|
|
|
static void restart __P((void));
|
|
|
|
static void handler __P((int));
|
1995-06-28 17:58:49 +00:00
|
|
|
static void cleanup __P((void));
|
1996-01-06 21:10:30 +00:00
|
|
|
static void resetlogging __P((void *));
|
1997-09-30 06:15:23 +00:00
|
|
|
static void usage __P((void));
|
1995-06-28 17:58:49 +00:00
|
|
|
|
|
|
|
/* To shut up gcc -Wstrict-prototypes */
|
|
|
|
int main __P((int argc, char **argv));
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1995-06-13 18:05:16 +00:00
|
|
|
int
|
|
|
|
register_input_handler(fd, func)
|
|
|
|
int fd;
|
1995-06-28 17:58:49 +00:00
|
|
|
ihfunc_t func;
|
1995-06-13 18:05:16 +00:00
|
|
|
{
|
|
|
|
if (nhandlers >= NHANDLERS)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ihandlers[nhandlers].fd = fd;
|
|
|
|
ihandlers[nhandlers++].func = func;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1995-06-28 17:58:49 +00:00
|
|
|
int
|
|
|
|
main(argc, argv)
|
1994-09-08 00:26:13 +00:00
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
register int recvlen;
|
|
|
|
int dummy;
|
|
|
|
FILE *fp;
|
1999-01-20 07:55:57 +00:00
|
|
|
struct timeval tv, difftime, curtime, lasttime, *timeout;
|
1995-06-28 17:58:49 +00:00
|
|
|
u_int32 prev_genid;
|
1995-06-13 18:05:16 +00:00
|
|
|
int vers;
|
|
|
|
fd_set rfds, readers;
|
1999-01-20 07:55:57 +00:00
|
|
|
int nfds, n, i, secs;
|
|
|
|
extern char todaysversion[];
|
|
|
|
struct sigaction sa;
|
1995-06-13 18:05:16 +00:00
|
|
|
#ifdef SNMP
|
1996-01-06 21:10:30 +00:00
|
|
|
struct timeval timeout, *tvp = &timeout;
|
|
|
|
struct timeval sched, *svp = &sched, now, *nvp = &now;
|
|
|
|
int index, block;
|
1995-06-13 18:05:16 +00:00
|
|
|
#endif
|
1994-09-08 00:26:13 +00:00
|
|
|
|
|
|
|
setlinebuf(stderr);
|
|
|
|
|
1997-09-30 06:15:23 +00:00
|
|
|
if (geteuid() != 0)
|
|
|
|
errx(1, "must be root");
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
progname = strrchr(argv[0], '/');
|
|
|
|
if (progname)
|
|
|
|
progname++;
|
|
|
|
else
|
|
|
|
progname = argv[0];
|
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
argv++, argc--;
|
|
|
|
while (argc > 0 && *argv[0] == '-') {
|
|
|
|
if (strcmp(*argv, "-d") == 0) {
|
1999-01-20 07:55:57 +00:00
|
|
|
if (argc > 1 && *(argv + 1)[0] != '-') {
|
|
|
|
char *p,*q;
|
|
|
|
int i, len;
|
|
|
|
struct debugname *d;
|
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
argv++, argc--;
|
1999-01-20 07:55:57 +00:00
|
|
|
debug = 0;
|
|
|
|
p = *argv; q = NULL;
|
|
|
|
while (p) {
|
|
|
|
q = strchr(p, ',');
|
|
|
|
if (q)
|
|
|
|
*q++ = '\0';
|
|
|
|
len = strlen(p);
|
|
|
|
for (i = 0, d = debugnames;
|
|
|
|
i < sizeof(debugnames) / sizeof(debugnames[0]);
|
|
|
|
i++, d++)
|
|
|
|
if (len >= d->nchars && strncmp(d->name, p, len) == 0)
|
|
|
|
break;
|
|
|
|
if (i == sizeof(debugnames) / sizeof(debugnames[0])) {
|
|
|
|
int j = 0xffffffff;
|
|
|
|
int k = 0;
|
|
|
|
fprintf(stderr, "Valid debug levels: ");
|
|
|
|
for (i = 0, d = debugnames;
|
|
|
|
i < sizeof(debugnames) / sizeof(debugnames[0]);
|
|
|
|
i++, d++) {
|
|
|
|
if ((j & d->level) == d->level) {
|
|
|
|
if (k++)
|
|
|
|
putc(',', stderr);
|
|
|
|
fputs(d->name, stderr);
|
|
|
|
j &= ~d->level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
putc('\n', stderr);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
debug |= d->level;
|
|
|
|
p = q;
|
|
|
|
}
|
1994-09-08 00:26:13 +00:00
|
|
|
} else
|
|
|
|
debug = DEFAULT_DEBUG;
|
|
|
|
} else if (strcmp(*argv, "-c") == 0) {
|
|
|
|
if (argc > 1) {
|
|
|
|
argv++, argc--;
|
|
|
|
configfilename = *argv;
|
|
|
|
} else
|
1997-09-30 06:15:23 +00:00
|
|
|
usage();
|
1994-09-08 00:26:13 +00:00
|
|
|
} else if (strcmp(*argv, "-p") == 0) {
|
1999-01-20 07:55:57 +00:00
|
|
|
log(LOG_WARNING, 0, "disabling pruning is no longer supported");
|
1996-01-06 21:10:30 +00:00
|
|
|
#ifdef SNMP
|
|
|
|
} else if (strcmp(*argv, "-P") == 0) {
|
|
|
|
if (argc > 1 && isdigit(*(argv + 1)[0])) {
|
|
|
|
argv++, argc--;
|
|
|
|
dest_port = atoi(*argv);
|
|
|
|
} else
|
|
|
|
dest_port = DEFAULT_PORT;
|
|
|
|
#endif
|
1994-09-08 00:26:13 +00:00
|
|
|
} else
|
1997-09-30 06:15:23 +00:00
|
|
|
usage();
|
1994-09-08 00:26:13 +00:00
|
|
|
argv++, argc--;
|
|
|
|
}
|
|
|
|
|
1997-09-30 06:15:23 +00:00
|
|
|
if (argc > 0)
|
|
|
|
usage();
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
if (debug != 0) {
|
|
|
|
struct debugname *d;
|
|
|
|
char c;
|
|
|
|
int tmpd = debug;
|
|
|
|
|
|
|
|
fprintf(stderr, "debug level 0x%x ", debug);
|
|
|
|
c = '(';
|
|
|
|
for (d = debugnames; d < debugnames +
|
|
|
|
sizeof(debugnames) / sizeof(debugnames[0]); d++) {
|
|
|
|
if ((tmpd & d->level) == d->level) {
|
|
|
|
tmpd &= ~d->level;
|
|
|
|
fprintf(stderr, "%c%s", c, d->name);
|
|
|
|
c = ',';
|
|
|
|
}
|
1994-09-08 00:26:13 +00:00
|
|
|
}
|
1999-01-20 07:55:57 +00:00
|
|
|
fprintf(stderr, ")\n");
|
1994-09-08 00:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LOG_DAEMON
|
|
|
|
(void)openlog("mrouted", LOG_PID, LOG_DAEMON);
|
|
|
|
(void)setlogmask(LOG_UPTO(LOG_NOTICE));
|
|
|
|
#else
|
|
|
|
(void)openlog("mrouted", LOG_PID);
|
|
|
|
#endif
|
1999-01-20 07:55:57 +00:00
|
|
|
sprintf(versionstring, "mrouted version %s", todaysversion);
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
log(LOG_DEBUG, 0, "%s starting", versionstring);
|
1996-01-06 21:10:30 +00:00
|
|
|
|
1995-06-13 18:05:16 +00:00
|
|
|
#ifdef SYSV
|
|
|
|
srand48(time(NULL));
|
|
|
|
#endif
|
1994-09-08 00:26:13 +00:00
|
|
|
|
|
|
|
/*
|
1995-06-13 18:05:16 +00:00
|
|
|
* Get generation id
|
1994-09-08 00:26:13 +00:00
|
|
|
*/
|
1995-06-13 18:05:16 +00:00
|
|
|
gettimeofday(&tv, 0);
|
1994-09-08 00:26:13 +00:00
|
|
|
dvmrp_genid = tv.tv_sec;
|
|
|
|
|
|
|
|
fp = fopen(genidfilename, "r");
|
|
|
|
if (fp != NULL) {
|
|
|
|
fscanf(fp, "%d", &prev_genid);
|
|
|
|
if (prev_genid == dvmrp_genid)
|
|
|
|
dvmrp_genid++;
|
|
|
|
(void) fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fopen(genidfilename, "w");
|
|
|
|
if (fp != NULL) {
|
|
|
|
fprintf(fp, "%d", dvmrp_genid);
|
|
|
|
(void) fclose(fp);
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
/* Start up the log rate-limiter */
|
|
|
|
resetlogging(NULL);
|
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
callout_init();
|
|
|
|
init_igmp();
|
1999-01-20 07:55:57 +00:00
|
|
|
init_icmp();
|
|
|
|
init_ipip();
|
1996-01-06 21:10:30 +00:00
|
|
|
init_routes();
|
|
|
|
init_ktable();
|
1995-06-13 18:05:16 +00:00
|
|
|
#ifndef OLD_KERNEL
|
1999-01-20 07:55:57 +00:00
|
|
|
/*
|
|
|
|
* Unfortunately, you can't k_get_version() unless you've
|
|
|
|
* k_init_dvmrp()'d. Now that we want to move the
|
|
|
|
* k_init_dvmrp() to later in the initialization sequence,
|
|
|
|
* we have to do the disgusting hack of initializing,
|
|
|
|
* getting the version, then stopping the kernel multicast
|
|
|
|
* forwarding.
|
|
|
|
*/
|
|
|
|
k_init_dvmrp();
|
1995-06-13 18:05:16 +00:00
|
|
|
vers = k_get_version();
|
1999-01-20 07:55:57 +00:00
|
|
|
k_stop_dvmrp();
|
1995-06-28 17:58:49 +00:00
|
|
|
/*XXX
|
|
|
|
* This function must change whenever the kernel version changes
|
|
|
|
*/
|
|
|
|
if ((((vers >> 8) & 0xff) != 3) ||
|
|
|
|
((vers & 0xff) != 5))
|
1995-06-13 18:05:16 +00:00
|
|
|
log(LOG_ERR, 0, "kernel (v%d.%d)/mrouted (v%d.%d) version mismatch",
|
|
|
|
(vers >> 8) & 0xff, vers & 0xff,
|
|
|
|
PROTOCOL_VERSION, MROUTED_VERSION);
|
|
|
|
#endif
|
|
|
|
|
1996-01-06 21:10:30 +00:00
|
|
|
#ifdef SNMP
|
|
|
|
if (i = snmp_init())
|
|
|
|
return i;
|
|
|
|
|
|
|
|
gettimeofday(nvp, 0);
|
|
|
|
if (nvp->tv_usec < 500000L){
|
|
|
|
svp->tv_usec = nvp->tv_usec + 500000L;
|
|
|
|
svp->tv_sec = nvp->tv_sec;
|
|
|
|
} else {
|
|
|
|
svp->tv_usec = nvp->tv_usec - 500000L;
|
|
|
|
svp->tv_sec = nvp->tv_sec + 1;
|
|
|
|
}
|
|
|
|
#endif /* SNMP */
|
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
init_vifs();
|
1996-01-06 21:10:30 +00:00
|
|
|
|
1995-06-13 18:05:16 +00:00
|
|
|
#ifdef RSRR
|
|
|
|
rsrr_init();
|
|
|
|
#endif /* RSRR */
|
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
sa.sa_handler = handler;
|
|
|
|
sa.sa_flags = 0; /* Interrupt system calls */
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigaction(SIGHUP, &sa, NULL);
|
|
|
|
sigaction(SIGTERM, &sa, NULL);
|
|
|
|
sigaction(SIGINT, &sa, NULL);
|
|
|
|
sigaction(SIGUSR1, &sa, NULL);
|
|
|
|
sigaction(SIGUSR2, &sa, NULL);
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1995-06-13 18:05:16 +00:00
|
|
|
FD_ZERO(&readers);
|
|
|
|
FD_SET(igmp_socket, &readers);
|
|
|
|
nfds = igmp_socket + 1;
|
|
|
|
for (i = 0; i < nhandlers; i++) {
|
|
|
|
FD_SET(ihandlers[i].fd, &readers);
|
|
|
|
if (ihandlers[i].fd >= nfds)
|
|
|
|
nfds = ihandlers[i].fd + 1;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
IF_DEBUG(DEBUG_IF)
|
|
|
|
dump_vifs(stderr);
|
|
|
|
IF_DEBUG(DEBUG_ROUTE)
|
|
|
|
dump_routes(stderr);
|
1996-01-06 21:10:30 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
/* schedule first timer interrupt */
|
|
|
|
timer_setTimer(1, fasttimer, NULL);
|
|
|
|
timer_setTimer(TIMER_INTERVAL, timer, NULL);
|
1996-01-06 21:10:30 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
if (debug == 0) {
|
|
|
|
/*
|
|
|
|
* Detach from the terminal
|
|
|
|
*/
|
|
|
|
int t;
|
1996-01-06 21:10:30 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
haveterminal = 0;
|
|
|
|
if (fork()) exit(0);
|
|
|
|
(void)close(0);
|
|
|
|
(void)close(1);
|
|
|
|
(void)close(2);
|
|
|
|
(void)open("/", 0);
|
|
|
|
(void)dup2(0, 1);
|
|
|
|
(void)dup2(0, 2);
|
|
|
|
#if defined(SYSV) || defined(linux)
|
|
|
|
(void)setpgrp();
|
|
|
|
#else
|
|
|
|
#ifdef TIOCNOTTY
|
2000-12-09 09:35:55 +00:00
|
|
|
t = open(_PATH_TTY, 2);
|
1999-01-20 07:55:57 +00:00
|
|
|
if (t >= 0) {
|
|
|
|
(void)ioctl(t, TIOCNOTTY, (char *)0);
|
|
|
|
(void)close(t);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (setsid() < 0)
|
|
|
|
perror("setsid");
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fopen(pidfilename, "w");
|
|
|
|
if (fp != NULL) {
|
|
|
|
fprintf(fp, "%d\n", (int)getpid());
|
|
|
|
(void) fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX HACK
|
|
|
|
* This will cause black holes for the first few seconds after startup,
|
|
|
|
* since we are exchanging routes but not actually forwarding.
|
|
|
|
* However, it eliminates much of the startup transient.
|
|
|
|
*
|
|
|
|
* It's possible that we can set a flag which says not to report any
|
|
|
|
* routes (just accept reports) until this timer fires, and then
|
|
|
|
* do a report_to_all_neighbors(ALL_ROUTES) immediately before
|
|
|
|
* turning on DVMRP.
|
|
|
|
*/
|
|
|
|
timer_setTimer(10, final_init, NULL);
|
1994-09-08 00:26:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Main receive loop.
|
|
|
|
*/
|
|
|
|
dummy = 0;
|
1999-01-20 07:55:57 +00:00
|
|
|
difftime.tv_usec = 0;
|
|
|
|
gettimeofday(&curtime, NULL);
|
|
|
|
lasttime = curtime;
|
1994-09-08 00:26:13 +00:00
|
|
|
for(;;) {
|
1995-06-13 18:05:16 +00:00
|
|
|
bcopy((char *)&readers, (char *)&rfds, sizeof(rfds));
|
1999-01-20 07:55:57 +00:00
|
|
|
secs = timer_nextTimer();
|
|
|
|
if (secs == -1)
|
|
|
|
timeout = NULL;
|
|
|
|
else {
|
|
|
|
timeout = &tv;
|
|
|
|
timeout->tv_sec = secs;
|
|
|
|
timeout->tv_usec = 0;
|
|
|
|
}
|
1995-06-13 18:05:16 +00:00
|
|
|
#ifdef SNMP
|
1999-01-20 07:55:57 +00:00
|
|
|
THIS IS BROKEN
|
1996-01-06 21:10:30 +00:00
|
|
|
if (nvp->tv_sec > svp->tv_sec
|
|
|
|
|| (nvp->tv_sec == svp->tv_sec && nvp->tv_usec > svp->tv_usec)){
|
|
|
|
alarmTimer(nvp);
|
|
|
|
eventTimer(nvp);
|
|
|
|
if (nvp->tv_usec < 500000L){
|
|
|
|
svp->tv_usec = nvp->tv_usec + 500000L;
|
|
|
|
svp->tv_sec = nvp->tv_sec;
|
|
|
|
} else {
|
|
|
|
svp->tv_usec = nvp->tv_usec - 500000L;
|
|
|
|
svp->tv_sec = nvp->tv_sec + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tvp = &timeout;
|
|
|
|
tvp->tv_sec = 0;
|
|
|
|
tvp->tv_usec = 500000L;
|
|
|
|
|
|
|
|
block = 0;
|
|
|
|
snmp_select_info(&nfds, &rfds, tvp, &block);
|
|
|
|
if (block == 1)
|
|
|
|
tvp = NULL; /* block without timeout */
|
|
|
|
if ((n = select(nfds, &rfds, NULL, NULL, tvp)) < 0)
|
1995-06-13 18:05:16 +00:00
|
|
|
#endif
|
1999-01-20 07:55:57 +00:00
|
|
|
if (sighandled) {
|
|
|
|
if (sighandled & GOT_SIGINT) {
|
|
|
|
sighandled &= ~GOT_SIGINT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sighandled & GOT_SIGHUP) {
|
|
|
|
sighandled &= ~GOT_SIGHUP;
|
|
|
|
restart();
|
|
|
|
}
|
|
|
|
if (sighandled & GOT_SIGUSR1) {
|
|
|
|
sighandled &= ~GOT_SIGUSR1;
|
|
|
|
fdump();
|
|
|
|
}
|
|
|
|
if (sighandled & GOT_SIGUSR2) {
|
|
|
|
sighandled &= ~GOT_SIGUSR2;
|
|
|
|
cdump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((n = select(nfds, &rfds, NULL, NULL, timeout)) < 0) {
|
|
|
|
if (errno != EINTR)
|
1995-06-13 18:05:16 +00:00
|
|
|
log(LOG_WARNING, errno, "select failed");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
if (n > 0) {
|
|
|
|
if (FD_ISSET(igmp_socket, &rfds)) {
|
|
|
|
recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
|
|
|
|
0, NULL, &dummy);
|
|
|
|
if (recvlen < 0) {
|
|
|
|
if (errno != EINTR) log(LOG_ERR, errno, "recvfrom");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
accept_igmp(recvlen);
|
1995-06-28 17:58:49 +00:00
|
|
|
}
|
1995-06-13 18:05:16 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
for (i = 0; i < nhandlers; i++) {
|
|
|
|
if (FD_ISSET(ihandlers[i].fd, &rfds)) {
|
|
|
|
(*ihandlers[i].func)(ihandlers[i].fd, &rfds);
|
|
|
|
}
|
1995-06-13 18:05:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SNMP
|
1999-01-20 07:55:57 +00:00
|
|
|
THIS IS BROKEN
|
1996-01-06 21:10:30 +00:00
|
|
|
snmp_read(&rfds);
|
|
|
|
snmp_timeout(); /* poll */
|
1995-06-13 18:05:16 +00:00
|
|
|
#endif
|
1999-01-20 07:55:57 +00:00
|
|
|
/*
|
|
|
|
* Handle timeout queue.
|
|
|
|
*
|
|
|
|
* If select + packet processing took more than 1 second,
|
|
|
|
* or if there is a timeout pending, age the timeout queue.
|
|
|
|
*
|
|
|
|
* If not, collect usec in difftime to make sure that the
|
|
|
|
* time doesn't drift too badly.
|
|
|
|
*
|
|
|
|
* If the timeout handlers took more than 1 second,
|
|
|
|
* age the timeout queue again. XXX This introduces the
|
|
|
|
* potential for infinite loops!
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
/*
|
|
|
|
* If the select timed out, then there's no other
|
|
|
|
* activity to account for and we don't need to
|
|
|
|
* call gettimeofday.
|
|
|
|
*/
|
|
|
|
if (n == 0) {
|
|
|
|
curtime.tv_sec = lasttime.tv_sec + secs;
|
|
|
|
curtime.tv_usec = lasttime.tv_usec;
|
|
|
|
n = -1; /* don't do this next time through the loop */
|
|
|
|
} else
|
|
|
|
gettimeofday(&curtime, NULL);
|
|
|
|
difftime.tv_sec = curtime.tv_sec - lasttime.tv_sec;
|
|
|
|
difftime.tv_usec += curtime.tv_usec - lasttime.tv_usec;
|
2000-03-26 14:44:34 +00:00
|
|
|
while (difftime.tv_usec >= 1000000) {
|
1999-01-20 07:55:57 +00:00
|
|
|
difftime.tv_sec++;
|
|
|
|
difftime.tv_usec -= 1000000;
|
|
|
|
}
|
|
|
|
if (difftime.tv_usec < 0) {
|
|
|
|
difftime.tv_sec--;
|
|
|
|
difftime.tv_usec += 1000000;
|
|
|
|
}
|
|
|
|
lasttime = curtime;
|
|
|
|
if (secs == 0 || difftime.tv_sec > 0)
|
|
|
|
age_callout_queue(difftime.tv_sec);
|
|
|
|
secs = -1;
|
|
|
|
} while (difftime.tv_sec > 0);
|
1995-06-13 18:05:16 +00:00
|
|
|
}
|
1999-01-20 07:55:57 +00:00
|
|
|
log(LOG_NOTICE, 0, "%s exiting", versionstring);
|
|
|
|
cleanup();
|
|
|
|
exit(0);
|
1994-09-08 00:26:13 +00:00
|
|
|
}
|
|
|
|
|
1997-09-30 06:15:23 +00:00
|
|
|
static void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"usage: mrouted [-p] [-c configfile] [-d [debug_level]]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
static void
|
|
|
|
final_init(i)
|
|
|
|
void *i;
|
|
|
|
{
|
|
|
|
char *s = (char *)i;
|
|
|
|
|
|
|
|
log(LOG_NOTICE, 0, "%s%s", versionstring, s ? s : "");
|
|
|
|
if (s)
|
|
|
|
free(s);
|
|
|
|
|
|
|
|
k_init_dvmrp(); /* enable DVMRP routing in kernel */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Install the vifs in the kernel as late as possible in the
|
|
|
|
* initialization sequence.
|
|
|
|
*/
|
|
|
|
init_installvifs();
|
|
|
|
|
|
|
|
time(&mrouted_init_time);
|
|
|
|
did_final_init = 1;
|
|
|
|
}
|
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
/*
|
1995-06-13 18:05:16 +00:00
|
|
|
* routine invoked every second. Its main goal is to cycle through
|
1994-09-08 00:26:13 +00:00
|
|
|
* the routing table and send partial updates to all neighbors at a
|
|
|
|
* rate that will cause the entire table to be sent in ROUTE_REPORT_INTERVAL
|
|
|
|
* seconds. Also, every TIMER_INTERVAL seconds it calls timer() to
|
|
|
|
* do all the other time-based processing.
|
|
|
|
*/
|
1995-06-13 18:05:16 +00:00
|
|
|
static void
|
1995-06-28 17:58:49 +00:00
|
|
|
fasttimer(i)
|
1999-01-20 07:55:57 +00:00
|
|
|
void *i;
|
1994-09-08 00:26:13 +00:00
|
|
|
{
|
|
|
|
static unsigned int tlast;
|
|
|
|
static unsigned int nsent;
|
|
|
|
register unsigned int t = tlast + 1;
|
|
|
|
register int n;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if we're in the last second, send everything that's left.
|
|
|
|
* otherwise send at least the fraction we should have sent by now.
|
|
|
|
*/
|
|
|
|
if (t >= ROUTE_REPORT_INTERVAL) {
|
|
|
|
register int nleft = nroutes - nsent;
|
|
|
|
while (nleft > 0) {
|
|
|
|
if ((n = report_next_chunk()) <= 0)
|
|
|
|
break;
|
|
|
|
nleft -= n;
|
|
|
|
}
|
|
|
|
tlast = 0;
|
|
|
|
nsent = 0;
|
|
|
|
} else {
|
|
|
|
register unsigned int ncum = nroutes * t / ROUTE_REPORT_INTERVAL;
|
|
|
|
while (nsent < ncum) {
|
|
|
|
if ((n = report_next_chunk()) <= 0)
|
|
|
|
break;
|
|
|
|
nsent += n;
|
|
|
|
}
|
|
|
|
tlast = t;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
timer_setTimer(1, fasttimer, NULL);
|
1994-09-08 00:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The 'virtual_time' variable is initialized to a value that will cause the
|
|
|
|
* first invocation of timer() to send a probe or route report to all vifs
|
|
|
|
* and send group membership queries to all subnets for which this router is
|
|
|
|
* querier. This first invocation occurs approximately TIMER_INTERVAL seconds
|
|
|
|
* after the router starts up. Note that probes for neighbors and queries
|
|
|
|
* for group memberships are also sent at start-up time, as part of initial-
|
|
|
|
* ization. This repetition after a short interval is desirable for quickly
|
|
|
|
* building up topology and membership information in the presence of possible
|
|
|
|
* packet loss.
|
|
|
|
*
|
|
|
|
* 'virtual_time' advances at a rate that is only a crude approximation of
|
|
|
|
* real time, because it does not take into account any time spent processing,
|
|
|
|
* and because the timer intervals are sometimes shrunk by a random amount to
|
|
|
|
* avoid unwanted synchronization with other routers.
|
|
|
|
*/
|
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
u_long virtual_time = 0;
|
1994-09-08 00:26:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Timer routine. Performs periodic neighbor probing, route reporting, and
|
|
|
|
* group querying duties, and drives various timers in routing entries and
|
|
|
|
* virtual interface data structures.
|
|
|
|
*/
|
1995-06-13 18:05:16 +00:00
|
|
|
static void
|
1999-01-20 07:55:57 +00:00
|
|
|
timer(i)
|
|
|
|
void *i;
|
1994-09-08 00:26:13 +00:00
|
|
|
{
|
|
|
|
age_routes(); /* Advance the timers in the route entries */
|
|
|
|
age_vifs(); /* Advance the timers for neighbors */
|
|
|
|
age_table_entry(); /* Advance the timers for the cache entries */
|
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
if (virtual_time % IGMP_QUERY_INTERVAL == 0) {
|
1994-09-08 00:26:13 +00:00
|
|
|
/*
|
|
|
|
* Time to query the local group memberships on all subnets
|
|
|
|
* for which this router is the elected querier.
|
|
|
|
*/
|
|
|
|
query_groups();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virtual_time % NEIGHBOR_PROBE_INTERVAL == 0) {
|
|
|
|
/*
|
|
|
|
* Time to send a probe on all vifs from which no neighbors have
|
|
|
|
* been heard. Also, check if any inoperative interfaces have now
|
|
|
|
* come up. (If they have, they will also be probed as part of
|
|
|
|
* their initialization.)
|
|
|
|
*/
|
|
|
|
probe_for_neighbors();
|
|
|
|
|
|
|
|
if (vifs_down)
|
|
|
|
check_vif_state();
|
|
|
|
}
|
|
|
|
|
|
|
|
delay_change_reports = FALSE;
|
|
|
|
if (routes_changed) {
|
|
|
|
/*
|
|
|
|
* Some routes have changed since the last timer interrupt, but
|
|
|
|
* have not been reported yet. Report the changed routes to all
|
|
|
|
* neighbors.
|
|
|
|
*/
|
|
|
|
report_to_all_neighbors(CHANGED_ROUTES);
|
|
|
|
}
|
|
|
|
|
1995-06-13 18:05:16 +00:00
|
|
|
#ifdef SNMP
|
1996-01-06 21:10:30 +00:00
|
|
|
sync_timer();
|
1995-06-13 18:05:16 +00:00
|
|
|
#endif
|
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
/*
|
|
|
|
* Advance virtual time
|
|
|
|
*/
|
|
|
|
virtual_time += TIMER_INTERVAL;
|
1999-01-20 07:55:57 +00:00
|
|
|
timer_setTimer(TIMER_INTERVAL, timer, NULL);
|
1994-09-08 00:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-06-13 18:05:16 +00:00
|
|
|
static void
|
|
|
|
cleanup()
|
1994-09-08 00:26:13 +00:00
|
|
|
{
|
1995-06-13 18:05:16 +00:00
|
|
|
static in_cleanup = 0;
|
|
|
|
|
|
|
|
if (!in_cleanup) {
|
|
|
|
in_cleanup++;
|
|
|
|
#ifdef RSRR
|
|
|
|
rsrr_clean();
|
|
|
|
#endif /* RSRR */
|
1995-06-28 17:58:49 +00:00
|
|
|
expire_all_routes();
|
|
|
|
report_to_all_neighbors(ALL_ROUTES);
|
1999-01-20 07:55:57 +00:00
|
|
|
if (did_final_init)
|
|
|
|
k_stop_dvmrp();
|
1995-06-13 18:05:16 +00:00
|
|
|
}
|
1994-09-08 00:26:13 +00:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
/*
|
|
|
|
* Signal handler. Take note of the fact that the signal arrived
|
|
|
|
* so that the main loop can take care of it.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
handler(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
|
|
|
switch (sig) {
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
sighandled |= GOT_SIGINT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGHUP:
|
|
|
|
sighandled |= GOT_SIGHUP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGUSR1:
|
|
|
|
sighandled |= GOT_SIGUSR1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGUSR2:
|
|
|
|
sighandled |= GOT_SIGUSR2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1994-09-08 00:26:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Dump internal data structures to stderr.
|
|
|
|
*/
|
1995-06-13 18:05:16 +00:00
|
|
|
static void
|
1999-01-20 07:55:57 +00:00
|
|
|
dump()
|
1994-09-08 00:26:13 +00:00
|
|
|
{
|
|
|
|
dump_vifs(stderr);
|
|
|
|
dump_routes(stderr);
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
static void
|
|
|
|
dump_version(fp)
|
|
|
|
FILE *fp;
|
|
|
|
{
|
|
|
|
time_t t;
|
|
|
|
|
|
|
|
time(&t);
|
|
|
|
fprintf(fp, "%s ", versionstring);
|
|
|
|
if (did_final_init)
|
|
|
|
fprintf(fp, "up %s",
|
|
|
|
scaletime(t - mrouted_init_time));
|
|
|
|
else
|
|
|
|
fprintf(fp, "(not yet initialized)");
|
|
|
|
fprintf(fp, " %s\n", ctime(&t));
|
|
|
|
}
|
1994-09-08 00:26:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Dump internal data structures to a file.
|
|
|
|
*/
|
1995-06-13 18:05:16 +00:00
|
|
|
static void
|
1999-01-20 07:55:57 +00:00
|
|
|
fdump()
|
1994-09-08 00:26:13 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp = fopen(dumpfilename, "w");
|
|
|
|
if (fp != NULL) {
|
1999-01-20 07:55:57 +00:00
|
|
|
dump_version(fp);
|
1994-09-08 00:26:13 +00:00
|
|
|
dump_vifs(fp);
|
|
|
|
dump_routes(fp);
|
|
|
|
(void) fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dump local cache contents to a file.
|
|
|
|
*/
|
1995-06-13 18:05:16 +00:00
|
|
|
static void
|
1999-01-20 07:55:57 +00:00
|
|
|
cdump()
|
1994-09-08 00:26:13 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp = fopen(cachefilename, "w");
|
|
|
|
if (fp != NULL) {
|
1999-01-20 07:55:57 +00:00
|
|
|
dump_version(fp);
|
1995-06-13 18:05:16 +00:00
|
|
|
dump_cache(fp);
|
1994-09-08 00:26:13 +00:00
|
|
|
(void) fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Restart mrouted
|
|
|
|
*/
|
1995-06-13 18:05:16 +00:00
|
|
|
static void
|
1999-01-20 07:55:57 +00:00
|
|
|
restart()
|
1994-09-08 00:26:13 +00:00
|
|
|
{
|
1999-01-20 07:55:57 +00:00
|
|
|
char *s;
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
s = (char *)malloc(sizeof(" restart"));
|
|
|
|
if (s == NULL)
|
|
|
|
log(LOG_ERR, 0, "out of memory");
|
|
|
|
strcpy(s, " restart");
|
1994-09-08 00:26:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* reset all the entries
|
|
|
|
*/
|
|
|
|
free_all_prunes();
|
|
|
|
free_all_routes();
|
1999-01-20 07:55:57 +00:00
|
|
|
free_all_callouts();
|
1994-09-08 00:26:13 +00:00
|
|
|
stop_all_vifs();
|
|
|
|
k_stop_dvmrp();
|
1995-06-13 18:05:16 +00:00
|
|
|
close(igmp_socket);
|
|
|
|
close(udp_socket);
|
1999-01-20 07:55:57 +00:00
|
|
|
did_final_init = 0;
|
1994-09-08 00:26:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* start processing again
|
|
|
|
*/
|
|
|
|
dvmrp_genid++;
|
|
|
|
|
|
|
|
init_igmp();
|
|
|
|
init_routes();
|
|
|
|
init_ktable();
|
|
|
|
init_vifs();
|
1999-01-20 07:55:57 +00:00
|
|
|
/*XXX Schedule final_init() as main does? */
|
|
|
|
final_init(s);
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
/* schedule timer interrupts */
|
|
|
|
timer_setTimer(1, fasttimer, NULL);
|
|
|
|
timer_setTimer(TIMER_INTERVAL, timer, NULL);
|
1994-09-08 00:26:13 +00:00
|
|
|
}
|
|
|
|
|
1996-01-06 21:10:30 +00:00
|
|
|
#define LOG_MAX_MSGS 20 /* if > 20/minute then shut up for a while */
|
|
|
|
#define LOG_SHUT_UP 600 /* shut up for 10 minutes */
|
|
|
|
static int log_nmsgs = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
resetlogging(arg)
|
|
|
|
void *arg;
|
|
|
|
{
|
|
|
|
int nxttime = 60;
|
|
|
|
void *narg = NULL;
|
|
|
|
|
|
|
|
if (arg == NULL && log_nmsgs > LOG_MAX_MSGS) {
|
|
|
|
nxttime = LOG_SHUT_UP;
|
|
|
|
narg = (void *)&log_nmsgs; /* just need some valid void * */
|
|
|
|
syslog(LOG_WARNING, "logging too fast, shutting up for %d minutes",
|
|
|
|
LOG_SHUT_UP / 60);
|
|
|
|
} else {
|
|
|
|
log_nmsgs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
timer_setTimer(nxttime, resetlogging, narg);
|
|
|
|
}
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
char *
|
|
|
|
scaletime(t)
|
|
|
|
u_long t;
|
|
|
|
{
|
|
|
|
#define SCALETIMEBUFLEN 20
|
|
|
|
static char buf1[20];
|
|
|
|
static char buf2[20];
|
|
|
|
static char *buf = buf1;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
if (buf == buf1)
|
|
|
|
buf = buf2;
|
|
|
|
else
|
|
|
|
buf = buf1;
|
|
|
|
|
|
|
|
/* XXX snprintf */
|
|
|
|
sprintf(p, "%2ld:%02ld:%02ld", t / 3600, (t % 3600) / 60, t % 60);
|
|
|
|
p[SCALETIMEBUFLEN - 1] = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RINGBUFFER
|
|
|
|
#define NLOGMSGS 10000
|
|
|
|
#define LOGMSGSIZE 200
|
|
|
|
char *logmsg[NLOGMSGS];
|
|
|
|
static int logmsgno = 0;
|
|
|
|
|
|
|
|
void
|
|
|
|
printringbuf()
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
f = fopen("/var/tmp/mrouted.log", "a");
|
|
|
|
if (f == NULL) {
|
|
|
|
log(LOG_ERR, errno, "can't open /var/tmp/mrouted.log");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
fprintf(f, "--------------------------------------------\n");
|
|
|
|
|
|
|
|
i = (logmsgno + 1) % NLOGMSGS;
|
|
|
|
|
|
|
|
while (i != logmsgno) {
|
|
|
|
if (*logmsg[i]) {
|
|
|
|
fprintf(f, "%s\n", logmsg[i]);
|
|
|
|
*logmsg[i] = '\0';
|
|
|
|
}
|
|
|
|
i = (i + 1) % NLOGMSGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
/*
|
|
|
|
* Log errors and other messages to the system log daemon and to stderr,
|
|
|
|
* according to the severity of the message and the current debug level.
|
|
|
|
* For errors of severity LOG_ERR or worse, terminate the program.
|
|
|
|
*/
|
1995-06-28 17:58:49 +00:00
|
|
|
#ifdef __STDC__
|
|
|
|
void
|
|
|
|
log(int severity, int syserr, char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
static char fmt[211] = "warning - ";
|
|
|
|
char *msg;
|
|
|
|
struct timeval now;
|
1998-06-29 17:38:35 +00:00
|
|
|
time_t now_sec;
|
1995-06-28 17:58:49 +00:00
|
|
|
struct tm *thyme;
|
1999-01-20 07:55:57 +00:00
|
|
|
#ifdef RINGBUFFER
|
|
|
|
static int ringbufinit = 0;
|
|
|
|
#endif
|
1995-06-28 17:58:49 +00:00
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
#else
|
1995-06-13 18:05:16 +00:00
|
|
|
/*VARARGS3*/
|
|
|
|
void
|
|
|
|
log(severity, syserr, format, va_alist)
|
1994-09-08 00:26:13 +00:00
|
|
|
int severity, syserr;
|
|
|
|
char *format;
|
1995-06-13 18:05:16 +00:00
|
|
|
va_dcl
|
1994-09-08 00:26:13 +00:00
|
|
|
{
|
1995-06-13 18:05:16 +00:00
|
|
|
va_list ap;
|
1999-01-20 07:55:57 +00:00
|
|
|
static char fmt[311] = "warning - ";
|
1995-06-13 18:05:16 +00:00
|
|
|
char *msg;
|
|
|
|
char tbuf[20];
|
|
|
|
struct timeval now;
|
1998-06-29 17:38:35 +00:00
|
|
|
time_t now_sec;
|
1995-06-13 18:05:16 +00:00
|
|
|
struct tm *thyme;
|
1999-01-20 07:55:57 +00:00
|
|
|
#ifdef RINGBUFFER
|
|
|
|
static int ringbufinit = 0;
|
|
|
|
#endif
|
1995-06-13 18:05:16 +00:00
|
|
|
|
|
|
|
va_start(ap);
|
1995-06-28 17:58:49 +00:00
|
|
|
#endif
|
2000-10-09 06:08:00 +00:00
|
|
|
vsnprintf(&fmt[10], sizeof(fmt) - 10, format, ap);
|
1995-06-13 18:05:16 +00:00
|
|
|
va_end(ap);
|
|
|
|
msg = (severity == LOG_WARNING) ? fmt : &fmt[10];
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
#ifdef RINGBUFFER
|
|
|
|
if (!ringbufinit) {
|
|
|
|
int i;
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
for (i = 0; i < NLOGMSGS; i++) {
|
|
|
|
logmsg[i] = malloc(LOGMSGSIZE);
|
|
|
|
if (logmsg[i] == 0) {
|
|
|
|
syslog(LOG_ERR, "out of memory");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
*logmsg[i] = 0;
|
1996-01-06 21:10:30 +00:00
|
|
|
}
|
1999-01-20 07:55:57 +00:00
|
|
|
ringbufinit = 1;
|
|
|
|
}
|
|
|
|
gettimeofday(&now,NULL);
|
|
|
|
now_sec = now.tv_sec;
|
|
|
|
thyme = localtime(&now_sec);
|
2000-10-09 06:08:00 +00:00
|
|
|
snprintf(logmsg[logmsgno++], LOGMSGSIZE, "%02d:%02d:%02d.%03ld %s err %d",
|
1999-01-20 07:55:57 +00:00
|
|
|
thyme->tm_hour, thyme->tm_min, thyme->tm_sec,
|
|
|
|
now.tv_usec / 1000, msg, syserr);
|
|
|
|
logmsgno %= NLOGMSGS;
|
|
|
|
if (severity <= LOG_NOTICE)
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Log to stderr if we haven't forked yet and it's a warning or worse,
|
|
|
|
* or if we're debugging.
|
|
|
|
*/
|
|
|
|
if (haveterminal && (debug || severity <= LOG_WARNING)) {
|
|
|
|
gettimeofday(&now,NULL);
|
|
|
|
now_sec = now.tv_sec;
|
|
|
|
thyme = localtime(&now_sec);
|
|
|
|
if (!debug)
|
|
|
|
fprintf(stderr, "%s: ", progname);
|
|
|
|
fprintf(stderr, "%02d:%02d:%02d.%03ld %s", thyme->tm_hour,
|
|
|
|
thyme->tm_min, thyme->tm_sec, now.tv_usec / 1000, msg);
|
|
|
|
if (syserr == 0)
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
else if (syserr < sys_nerr)
|
|
|
|
fprintf(stderr, ": %s\n", sys_errlist[syserr]);
|
|
|
|
else
|
|
|
|
fprintf(stderr, ": errno %d\n", syserr);
|
|
|
|
}
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
/*
|
|
|
|
* Always log things that are worse than warnings, no matter what
|
|
|
|
* the log_nmsgs rate limiter says.
|
|
|
|
* Only count things worse than debugging in the rate limiter
|
|
|
|
* (since if you put daemon.debug in syslog.conf you probably
|
|
|
|
* actually want to log the debugging messages so they shouldn't
|
|
|
|
* be rate-limited)
|
|
|
|
*/
|
|
|
|
if ((severity < LOG_WARNING) || (log_nmsgs < LOG_MAX_MSGS)) {
|
|
|
|
if (severity < LOG_DEBUG)
|
|
|
|
log_nmsgs++;
|
|
|
|
if (syserr != 0) {
|
|
|
|
errno = syserr;
|
|
|
|
syslog(severity, "%s: %m", msg);
|
|
|
|
} else
|
|
|
|
syslog(severity, "%s", msg);
|
1994-09-08 00:26:13 +00:00
|
|
|
}
|
1999-01-20 07:55:57 +00:00
|
|
|
|
|
|
|
if (severity <= LOG_ERR) exit(-1);
|
1994-09-08 00:26:13 +00:00
|
|
|
}
|
1996-01-06 21:10:30 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_MFC
|
|
|
|
void
|
|
|
|
md_log(what, origin, mcastgrp)
|
|
|
|
int what;
|
|
|
|
u_int32 origin, mcastgrp;
|
|
|
|
{
|
|
|
|
static FILE *f = NULL;
|
|
|
|
struct timeval tv;
|
|
|
|
u_int32 buf[4];
|
|
|
|
|
|
|
|
if (!f) {
|
|
|
|
if ((f = fopen("/tmp/mrouted.clog", "w")) == NULL) {
|
|
|
|
log(LOG_ERR, errno, "open /tmp/mrouted.clog");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
buf[0] = tv.tv_sec;
|
|
|
|
buf[1] = what;
|
|
|
|
buf[2] = origin;
|
|
|
|
buf[3] = mcastgrp;
|
|
|
|
|
|
|
|
fwrite(buf, sizeof(u_int32), 4, f);
|
|
|
|
}
|
|
|
|
#endif
|