rework interface metrics to invert perspective; other misc. fixes
[unix-history] / usr / src / sbin / routed / main.c
index a68a3a0..666af15 100644 (file)
@@ -1,6 +1,18 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1983 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)main.c     4.11 (Berkeley) %G%";
-#endif
+static char sccsid[] = "@(#)main.c     5.11 (Berkeley) %G%";
+#endif not lint
 
 /*
  * Routing Table Management Daemon
 
 /*
  * Routing Table Management Daemon
@@ -12,14 +24,15 @@ static char sccsid[] = "@(#)main.c  4.11 (Berkeley) %G%";
 #include <net/if.h>
 
 #include <errno.h>
 #include <net/if.h>
 
 #include <errno.h>
-#include <nlist.h>
 #include <signal.h>
 #include <signal.h>
+#include <syslog.h>
 
 int    supplier = -1;          /* process should supply updates */
 
 int    supplier = -1;          /* process should supply updates */
-extern int gateway;
+int    gateway = 0;            /* 1 if we are a gateway to parts beyond */
+int    debug = 0;
 
 struct rip *msg = (struct rip *)packet;
 
 struct rip *msg = (struct rip *)packet;
-int    hup();
+int    hup(), rtdeleteall();
 
 main(argc, argv)
        int argc;
 
 main(argc, argv)
        int argc;
@@ -28,11 +41,10 @@ main(argc, argv)
        int cc;
        struct sockaddr from;
        u_char retry;
        int cc;
        struct sockaddr from;
        u_char retry;
-#ifdef COMPAT
-       int snoroute;
-#endif
        
        argv0 = argv;
        
        argv0 = argv;
+       openlog("routed", LOG_PID | LOG_ODELAY, LOG_DAEMON);
+       setlogmask(LOG_WARNING);
        sp = getservbyname("router", "udp");
        if (sp == NULL) {
                fprintf(stderr, "routed: router/udp: unknown service\n");
        sp = getservbyname("router", "udp");
        if (sp == NULL) {
                fprintf(stderr, "routed: router/udp: unknown service\n");
@@ -43,14 +55,6 @@ main(argc, argv)
        s = getsocket(AF_INET, SOCK_DGRAM, &addr);
        if (s < 0)
                exit(1);
        s = getsocket(AF_INET, SOCK_DGRAM, &addr);
        if (s < 0)
                exit(1);
-#ifdef COMPAT
-       bzero(&addr, sizeof (addr));
-       addr.sin_family = AF_INET;
-       addr.sin_port = htons(ntohs(sp->s_port) + 1);
-       snoroute = getsocket(AF_INET, SOCK_DGRAM, &addr);
-       if (snoroute < 0)
-               exit(1);
-#endif
        argv++, argc--;
        while (argc > 0 && **argv == '-') {
                if (strcmp(*argv, "-s") == 0) {
        argv++, argc--;
        while (argc > 0 && **argv == '-') {
                if (strcmp(*argv, "-s") == 0) {
@@ -65,35 +69,33 @@ main(argc, argv)
                }
                if (strcmp(*argv, "-t") == 0) {
                        tracepackets++;
                }
                if (strcmp(*argv, "-t") == 0) {
                        tracepackets++;
+                       setlogmask(LOG_DEBUG);
                        argv++, argc--;
                        continue;
                }
                        argv++, argc--;
                        continue;
                }
-               if (strcmp(*argv, "-g") == 0) {
-                       gateway = 1;
+               if (strcmp(*argv, "-d") == 0) {
+                       debug++;
+                       setlogmask(LOG_DEBUG);
                        argv++, argc--;
                        continue;
                }
                        argv++, argc--;
                        continue;
                }
-               if (strcmp(*argv, "-l") == 0) {
-                       gateway = -1;
+               if (strcmp(*argv, "-g") == 0) {
+                       gateway = 1;
                        argv++, argc--;
                        continue;
                }
                fprintf(stderr,
                        argv++, argc--;
                        continue;
                }
                fprintf(stderr,
-                       "usage: routed [ -s ] [ -q ] [ -t ] [ -g ] [ -l ]\n");
+                       "usage: routed [ -s ] [ -q ] [ -t ] [ -g ]\n");
                exit(1);
        }
                exit(1);
        }
-#ifndef DEBUG
-       if (!tracepackets) {
+       if (tracepackets == 0 && debug == 0) {
                int t;
 
                if (fork())
                        exit(0);
                for (t = 0; t < 20; t++)
                        if (t != s)
                int t;
 
                if (fork())
                        exit(0);
                for (t = 0; t < 20; t++)
                        if (t != s)
-#ifdef COMPAT
-                               if (t != snoroute)
-#endif
-                               (void) close(cc);
+                               (void) close(t);
                (void) open("/", 0);
                (void) dup2(0, 1);
                (void) dup2(0, 2);
                (void) open("/", 0);
                (void) dup2(0, 1);
                (void) dup2(0, 2);
@@ -103,7 +105,6 @@ main(argc, argv)
                        (void) close(t);
                }
        }
                        (void) close(t);
                }
        }
-#endif
        /*
         * Any extra argument is considered
         * a tracing log file.
        /*
         * Any extra argument is considered
         * a tracing log file.
@@ -112,14 +113,16 @@ main(argc, argv)
                traceon(*argv);
        /*
         * Collect an initial view of the world by
                traceon(*argv);
        /*
         * Collect an initial view of the world by
-        * snooping in the kernel and the gateway kludge
+        * checking the interface configuration and the gateway kludge
         * file.  Then, send a request packet on all
         * directly connected networks to find out what
         * everyone else thinks.
         */
        rtinit();
         * file.  Then, send a request packet on all
         * directly connected networks to find out what
         * everyone else thinks.
         */
        rtinit();
-       gwkludge();
        ifinit();
        ifinit();
+       gwkludge();
+       if (gateway > 0)
+               rtdefault();
        if (supplier < 0)
                supplier = 0;
        msg->rip_cmd = RIPCMD_REQUEST;
        if (supplier < 0)
                supplier = 0;
        msg->rip_cmd = RIPCMD_REQUEST;
@@ -131,6 +134,8 @@ main(argc, argv)
        toall(sendmsg);
        signal(SIGALRM, timer);
        signal(SIGHUP, hup);
        toall(sendmsg);
        signal(SIGALRM, timer);
        signal(SIGHUP, hup);
+       signal(SIGTERM, hup);
+       signal(SIGINT, rtdeleteall);
        timer();
 
        for (;;) {
        timer();
 
        for (;;) {
@@ -138,18 +143,11 @@ main(argc, argv)
                register int n;
 
                ibits = 1 << s;
                register int n;
 
                ibits = 1 << s;
-#ifdef COMPAT
-               ibits |= 1 << snoroute;
-#endif
                n = select(20, &ibits, 0, 0, 0);
                if (n < 0)
                        continue;
                if (ibits & (1 << s))
                        process(s);
                n = select(20, &ibits, 0, 0, 0);
                if (n < 0)
                        continue;
                if (ibits & (1 << s))
                        process(s);
-#ifdef COMPAT
-               if (ibits & (1 << snoroute))
-                       process(snoroute);
-#endif
                /* handle ICMP redirects */
        }
 }
                /* handle ICMP redirects */
        }
 }
@@ -168,8 +166,7 @@ process(fd)
        }
        if (fromlen != sizeof (struct sockaddr_in))
                return;
        }
        if (fromlen != sizeof (struct sockaddr_in))
                return;
-#define        mask(s) (1<<((s)-1))
-       omask = sigblock(mask(SIGALRM));
+       omask = sigblock(sigmask(SIGALRM));
        rip_input(&from, cc);
        sigsetmask(omask);
 }
        rip_input(&from, cc);
        sigsetmask(omask);
 }
@@ -178,22 +175,26 @@ getsocket(domain, type, sin)
        int domain, type;
        struct sockaddr_in *sin;
 {
        int domain, type;
        struct sockaddr_in *sin;
 {
-       int retry, s;
+       int s, on = 1;
 
 
-       retry = 1;
-       while ((s = socket(domain, type, 0, 0)) < 0 && retry) {
+       if ((s = socket(domain, type, 0)) < 0) {
                perror("socket");
                perror("socket");
-               sleep(5 * retry);
-               retry <<= 1;
+               syslog(LOG_ERR, "socket: %m");
+               return (-1);
        }
        }
-       if (retry == 0)
+       if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
+               syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
+               close(s);
                return (-1);
                return (-1);
-       while (bind(s, sin, sizeof (*sin), 0) < 0 && retry) {
-               perror("bind");
-               sleep(5 * retry);
-               retry <<= 1;
        }
        }
-       if (retry == 0)
+       on = 48*1024;
+       if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &on, sizeof (on)) < 0)
+               syslog(LOG_ERR, "setsockopt SO_RCVBUF: %m");
+       if (bind(s, sin, sizeof (*sin), 0) < 0) {
+               perror("bind");
+               syslog(LOG_ERR, "bind: %m");
+               close(s);
                return (-1);
                return (-1);
+       }
        return (s);
 }
        return (s);
 }