new subnets
[unix-history] / usr / src / sbin / routed / main.c
index 94fe5df..bccba1e 100644 (file)
@@ -1,5 +1,5 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)main.c     4.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)main.c     4.13 (Berkeley) %G%";
 #endif
 
 /*
 #endif
 
 /*
@@ -7,15 +7,20 @@ static char sccsid[] = "@(#)main.c    4.7 (Berkeley) %G%";
  */
 #include "defs.h"
 #include <sys/ioctl.h>
  */
 #include "defs.h"
 #include <sys/ioctl.h>
+#include <sys/time.h>
+
 #include <net/if.h>
 #include <net/if.h>
+
 #include <errno.h>
 #include <nlist.h>
 #include <signal.h>
 #include <errno.h>
 #include <nlist.h>
 #include <signal.h>
-#include <time.h>
+#include <syslog.h>
 
 int    supplier = -1;          /* process should supply updates */
 
 int    supplier = -1;          /* process should supply updates */
+int    gateway = 0;            /* 1 if we are a gateway to parts beyond */
 
 struct rip *msg = (struct rip *)packet;
 
 struct rip *msg = (struct rip *)packet;
+int    hup();
 
 main(argc, argv)
        int argc;
 
 main(argc, argv)
        int argc;
@@ -24,11 +29,9 @@ 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, 0);
        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");
@@ -39,14 +42,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) {
@@ -64,7 +59,13 @@ main(argc, argv)
                        argv++, argc--;
                        continue;
                }
                        argv++, argc--;
                        continue;
                }
-               fprintf(stderr, "usage: routed [ -s ] [ -q ] [ -t ]\n");
+               if (strcmp(*argv, "-g") == 0) {
+                       gateway = 1;
+                       argv++, argc--;
+                       continue;
+               }
+               fprintf(stderr,
+                       "usage: routed [ -s ] [ -q ] [ -t ] [ -g ]\n");
                exit(1);
        }
 #ifndef DEBUG
                exit(1);
        }
 #ifndef DEBUG
@@ -75,9 +76,6 @@ main(argc, argv)
                        exit(0);
                for (t = 0; t < 20; t++)
                        if (t != s)
                        exit(0);
                for (t = 0; t < 20; t++)
                        if (t != s)
-#ifdef COMPAT
-                               if (t != snoroute)
-#endif
                                (void) close(cc);
                (void) open("/", 0);
                (void) dup2(0, 1);
                                (void) close(cc);
                (void) open("/", 0);
                (void) dup2(0, 1);
@@ -97,7 +95,7 @@ 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.
         * file.  Then, send a request packet on all
         * directly connected networks to find out what
         * everyone else thinks.
@@ -105,6 +103,8 @@ main(argc, argv)
        rtinit();
        gwkludge();
        ifinit();
        rtinit();
        gwkludge();
        ifinit();
+       if (gateway > 0)
+               rtdefault();
        if (supplier < 0)
                supplier = 0;
        msg->rip_cmd = RIPCMD_REQUEST;
        if (supplier < 0)
                supplier = 0;
        msg->rip_cmd = RIPCMD_REQUEST;
@@ -115,6 +115,8 @@ main(argc, argv)
        msg->rip_nets[0].rip_metric = htonl(HOPCNT_INFINITY);
        toall(sendmsg);
        signal(SIGALRM, timer);
        msg->rip_nets[0].rip_metric = htonl(HOPCNT_INFINITY);
        toall(sendmsg);
        signal(SIGALRM, timer);
+       signal(SIGHUP, hup);
+       signal(SIGTERM, hup);
        timer();
 
        for (;;) {
        timer();
 
        for (;;) {
@@ -122,18 +124,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 */
        }
 }
@@ -162,7 +157,7 @@ getsocket(domain, type, sin)
        int domain, type;
        struct sockaddr_in *sin;
 {
        int domain, type;
        struct sockaddr_in *sin;
 {
-       int retry, s;
+       int retry, s, on = 1;
 
        retry = 1;
        while ((s = socket(domain, type, 0, 0)) < 0 && retry) {
 
        retry = 1;
        while ((s = socket(domain, type, 0, 0)) < 0 && retry) {
@@ -170,14 +165,22 @@ getsocket(domain, type, sin)
                sleep(5 * retry);
                retry <<= 1;
        }
                sleep(5 * retry);
                retry <<= 1;
        }
-       if (retry == 0)
+       if (retry == 0) {
+               syslog(LOG_ERR, "socket: %m");
                return (-1);
                return (-1);
+       }
+       if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
+               syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
+               exit(1);
+       }
        while (bind(s, sin, sizeof (*sin), 0) < 0 && retry) {
                perror("bind");
                sleep(5 * retry);
                retry <<= 1;
        }
        while (bind(s, sin, sizeof (*sin), 0) < 0 && retry) {
                perror("bind");
                sleep(5 * retry);
                retry <<= 1;
        }
-       if (retry == 0)
+       if (retry == 0) {
+               syslog(LOG_ERR, "bind: %m");
                return (-1);
                return (-1);
+       }
        return (s);
 }
        return (s);
 }