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