| 1 | /* |
| 2 | * Copyright (c) 1985 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This file includes significant work done at Cornell University by |
| 6 | * Bill Nesheim. That work included by permission. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. All advertising materials mentioning features or use of this software |
| 17 | * must display the following acknowledgement: |
| 18 | * This product includes software developed by the University of |
| 19 | * California, Berkeley and its contributors. |
| 20 | * 4. Neither the name of the University nor the names of its contributors |
| 21 | * may be used to endorse or promote products derived from this software |
| 22 | * without specific prior written permission. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | * SUCH DAMAGE. |
| 35 | */ |
| 36 | |
| 37 | #ifndef lint |
| 38 | char copyright[] = |
| 39 | "@(#) Copyright (c) 1985 The Regents of the University of California.\n\ |
| 40 | All rights reserved.\n"; |
| 41 | #endif /* not lint */ |
| 42 | |
| 43 | #ifndef lint |
| 44 | static char sccsid[] = "@(#)main.c 5.11 (Berkeley) 2/26/91"; |
| 45 | #endif /* not lint */ |
| 46 | |
| 47 | /* |
| 48 | * XNS Routing Information Protocol Daemon |
| 49 | */ |
| 50 | #include "defs.h" |
| 51 | #include <sys/time.h> |
| 52 | |
| 53 | #include <net/if.h> |
| 54 | |
| 55 | #include <errno.h> |
| 56 | #include <nlist.h> |
| 57 | #include <signal.h> |
| 58 | #include <paths.h> |
| 59 | |
| 60 | int supplier = -1; /* process should supply updates */ |
| 61 | extern int gateway; |
| 62 | |
| 63 | struct rip *msg = (struct rip *) &packet[sizeof (struct idp)]; |
| 64 | void hup(), fkexit(), timer(); |
| 65 | |
| 66 | main(argc, argv) |
| 67 | int argc; |
| 68 | char *argv[]; |
| 69 | { |
| 70 | int cc; |
| 71 | struct sockaddr from; |
| 72 | u_char retry; |
| 73 | |
| 74 | argv0 = argv; |
| 75 | argv++, argc--; |
| 76 | while (argc > 0 && **argv == '-') { |
| 77 | if (strcmp(*argv, "-s") == 0) { |
| 78 | supplier = 1; |
| 79 | argv++, argc--; |
| 80 | continue; |
| 81 | } |
| 82 | if (strcmp(*argv, "-q") == 0) { |
| 83 | supplier = 0; |
| 84 | argv++, argc--; |
| 85 | continue; |
| 86 | } |
| 87 | if (strcmp(*argv, "-R") == 0) { |
| 88 | noteremoterequests++; |
| 89 | argv++, argc--; |
| 90 | continue; |
| 91 | } |
| 92 | if (strcmp(*argv, "-t") == 0) { |
| 93 | tracepackets++; |
| 94 | argv++, argc--; |
| 95 | ftrace = stderr; |
| 96 | tracing = 1; |
| 97 | continue; |
| 98 | } |
| 99 | if (strcmp(*argv, "-g") == 0) { |
| 100 | gateway = 1; |
| 101 | argv++, argc--; |
| 102 | continue; |
| 103 | } |
| 104 | if (strcmp(*argv, "-l") == 0) { |
| 105 | gateway = -1; |
| 106 | argv++, argc--; |
| 107 | continue; |
| 108 | } |
| 109 | fprintf(stderr, |
| 110 | "usage: xnsrouted [ -s ] [ -q ] [ -t ] [ -g ] [ -l ]\n"); |
| 111 | exit(1); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | #ifndef DEBUG |
| 116 | if (!tracepackets) |
| 117 | daemon(0, 0); |
| 118 | #endif |
| 119 | openlog("XNSrouted", LOG_PID, LOG_DAEMON); |
| 120 | |
| 121 | ns_anynet.s_net[0] = -1; ns_anynet.s_net[1] = -1; |
| 122 | addr.sns_family = AF_NS; |
| 123 | addr.sns_len = sizeof(addr); |
| 124 | addr.sns_port = htons(IDPPORT_RIF); |
| 125 | s = getsocket(SOCK_DGRAM, 0, &addr); |
| 126 | if (s < 0) |
| 127 | exit(1); |
| 128 | /* |
| 129 | * Any extra argument is considered |
| 130 | * a tracing log file. |
| 131 | */ |
| 132 | if (argc > 0) |
| 133 | traceon(*argv); |
| 134 | /* |
| 135 | * Collect an initial view of the world by |
| 136 | * snooping in the kernel. Then, send a request packet on all |
| 137 | * directly connected networks to find out what |
| 138 | * everyone else thinks. |
| 139 | */ |
| 140 | rtinit(); |
| 141 | ifinit(); |
| 142 | if (supplier < 0) |
| 143 | supplier = 0; |
| 144 | /* request the state of the world */ |
| 145 | msg->rip_cmd = htons(RIPCMD_REQUEST); |
| 146 | msg->rip_nets[0].rip_dst = ns_anynet; |
| 147 | msg->rip_nets[0].rip_metric = htons(HOPCNT_INFINITY); |
| 148 | toall(sndmsg); |
| 149 | signal(SIGALRM, timer); |
| 150 | signal(SIGHUP, hup); |
| 151 | signal(SIGINT, hup); |
| 152 | signal(SIGEMT, fkexit); |
| 153 | timer(); |
| 154 | |
| 155 | |
| 156 | for (;;) |
| 157 | process(s); |
| 158 | |
| 159 | } |
| 160 | |
| 161 | process(fd) |
| 162 | int fd; |
| 163 | { |
| 164 | struct sockaddr from; |
| 165 | int fromlen = sizeof (from), cc, omask; |
| 166 | struct idp *idp = (struct idp *)packet; |
| 167 | |
| 168 | cc = recvfrom(fd, packet, sizeof (packet), 0, &from, &fromlen); |
| 169 | if (cc <= 0) { |
| 170 | if (cc < 0 && errno != EINTR) |
| 171 | syslog(LOG_ERR, "recvfrom: %m"); |
| 172 | return; |
| 173 | } |
| 174 | if (tracepackets > 1 && ftrace) { |
| 175 | fprintf(ftrace,"rcv %d bytes on %s ", cc, xns_ntoa(&idp->idp_dna)); |
| 176 | fprintf(ftrace," from %s\n", xns_ntoa(&idp->idp_sna)); |
| 177 | } |
| 178 | |
| 179 | if (noteremoterequests && !ns_neteqnn(idp->idp_sna.x_net, ns_zeronet) |
| 180 | && !ns_neteq(idp->idp_sna, idp->idp_dna)) |
| 181 | { |
| 182 | syslog(LOG_ERR, |
| 183 | "net of interface (%s) != net on ether (%s)!\n", |
| 184 | xns_nettoa(idp->idp_dna.x_net), |
| 185 | xns_nettoa(idp->idp_sna.x_net)); |
| 186 | } |
| 187 | |
| 188 | /* We get the IDP header in front of the RIF packet*/ |
| 189 | cc -= sizeof (struct idp); |
| 190 | #define mask(s) (1<<((s)-1)) |
| 191 | omask = sigblock(mask(SIGALRM)); |
| 192 | rip_input(&from, cc); |
| 193 | sigsetmask(omask); |
| 194 | } |
| 195 | |
| 196 | getsocket(type, proto, sns) |
| 197 | int type, proto; |
| 198 | struct sockaddr_ns *sns; |
| 199 | { |
| 200 | int domain = sns->sns_family; |
| 201 | int retry, s, on = 1; |
| 202 | |
| 203 | retry = 1; |
| 204 | while ((s = socket(domain, type, proto)) < 0 && retry) { |
| 205 | syslog(LOG_ERR, "socket: %m"); |
| 206 | sleep(5 * retry); |
| 207 | retry <<= 1; |
| 208 | } |
| 209 | if (retry == 0) |
| 210 | return (-1); |
| 211 | while (bind(s, (struct sockaddr *)sns, sizeof (*sns)) < 0 && retry) { |
| 212 | syslog(LOG_ERR, "bind: %m"); |
| 213 | sleep(5 * retry); |
| 214 | retry <<= 1; |
| 215 | } |
| 216 | if (retry == 0) |
| 217 | return (-1); |
| 218 | if (domain==AF_NS) { |
| 219 | struct idp idp; |
| 220 | if (setsockopt(s, 0, SO_HEADERS_ON_INPUT, &on, sizeof(on))) { |
| 221 | syslog(LOG_ERR, "setsockopt SEE HEADERS: %m"); |
| 222 | exit(1); |
| 223 | } |
| 224 | idp.idp_pt = NSPROTO_RI; |
| 225 | if (setsockopt(s, 0, SO_DEFAULT_HEADERS, &idp, sizeof(idp))) { |
| 226 | syslog(LOG_ERR, "setsockopt SET HEADER: %m"); |
| 227 | exit(1); |
| 228 | } |
| 229 | } |
| 230 | if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) { |
| 231 | syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); |
| 232 | exit(1); |
| 233 | } |
| 234 | return (s); |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Fork and exit on EMT-- for profiling. |
| 239 | */ |
| 240 | void |
| 241 | fkexit() |
| 242 | { |
| 243 | if (fork() == 0) |
| 244 | exit(0); |
| 245 | } |