add stdout tracing of all packets
[unix-history] / usr / src / sbin / routed / main.c
CommitLineData
fdd0ed12 1#ifndef lint
d4b6a849 2static char sccsid[] = "@(#)main.c 4.3 %G%";
fdd0ed12
SL
3#endif
4
5/*
6 * Routing Table Management Daemon
7 */
7fe7fe74 8#include "defs.h"
fdd0ed12
SL
9#include <sys/ioctl.h>
10#include <net/if.h>
11#include <errno.h>
12#include <nlist.h>
13#include <signal.h>
14#include <time.h>
15
16int supplier = -1; /* process should supply updates */
17
18struct rip *msg = (struct rip *)packet;
19
20main(argc, argv)
21 int argc;
22 char *argv[];
23{
24 int cc;
25 struct sockaddr from;
7fe7fe74
SL
26 u_char retry;
27#ifdef COMPAT
28 int snoroute;
29#endif
fdd0ed12
SL
30
31 argv0 = argv;
fdd0ed12 32 sp = getservbyname("router", "udp");
7fe7fe74
SL
33 if (sp == NULL) {
34 fprintf(stderr, "routed: router/udp: unknown service\n");
fdd0ed12
SL
35 exit(1);
36 }
7fe7fe74
SL
37 addr.sin_family = AF_INET;
38 addr.sin_port = sp->s_port;
39 s = getsocket(AF_INET, SOCK_DGRAM, &addr);
40 if (s < 0)
41 exit(1);
42#ifdef COMPAT
43 bzero(&addr, sizeof (addr));
44 addr.sin_family = AF_INET;
45 addr.sin_port = htons(ntohs(sp->s_port) + 1);
46 snoroute = getsocket(AF_INET, SOCK_DGRAM, &addr);
47 if (snoroute < 0)
48 exit(1);
49#endif
fdd0ed12
SL
50 argv++, argc--;
51 while (argc > 0 && **argv == '-') {
d4b6a849 52 if (strcmp(*argv, "-s") == 0) {
fdd0ed12
SL
53 supplier = 1;
54 argv++, argc--;
55 continue;
56 }
d4b6a849 57 if (strcmp(*argv, "-q") == 0) {
fdd0ed12
SL
58 supplier = 0;
59 argv++, argc--;
60 continue;
61 }
d4b6a849
SL
62 if (strcmp(*argv, "-t") == 0) {
63 tracepackets++;
64 argv++, argc--;
65 continue;
66 }
67 fprintf(stderr, "usage: routed [ -s ] [ -q ] [ -t ]\n");
fdd0ed12
SL
68 exit(1);
69 }
d4b6a849
SL
70#ifndef DEBUG
71 if (!tracepackets) {
72 int t;
73
74 if (fork())
75 exit(0);
76 for (cc = 0; cc < 10; cc++)
77 (void) close(cc);
78 (void) open("/", 0);
79 (void) dup2(0, 1);
80 (void) dup2(0, 2);
81 t = open("/dev/tty", 2);
82 if (t >= 0) {
83 ioctl(t, TIOCNOTTY, (char *)0);
84 (void) close(t);
85 }
86 }
87#endif
7fe7fe74
SL
88 /*
89 * Any extra argument is considered
90 * a tracing log file.
91 */
92 if (argc > 0)
93 traceon(*argv);
fdd0ed12
SL
94 /*
95 * Collect an initial view of the world by
96 * snooping in the kernel and the gateway kludge
97 * file. Then, send a request packet on all
98 * directly connected networks to find out what
99 * everyone else thinks.
100 */
101 rtinit();
102 gwkludge();
103 ifinit();
104 if (supplier < 0)
105 supplier = 0;
106 msg->rip_cmd = RIPCMD_REQUEST;
107 msg->rip_nets[0].rip_dst.sa_family = AF_UNSPEC;
108 msg->rip_nets[0].rip_metric = HOPCNT_INFINITY;
109 toall(sendmsg);
110 sigset(SIGALRM, timer);
111 timer();
112
fdd0ed12
SL
113 for (;;) {
114 int ibits;
115 register int n;
116
7fe7fe74
SL
117 ibits = 1 << s;
118#ifdef COMPAT
119 ibits |= 1 << snoroute;
120#endif
121 n = select(20, &ibits, 0, 0, 0);
fdd0ed12
SL
122 if (n < 0)
123 continue;
124 if (ibits & (1 << s))
125 process(s);
7fe7fe74 126#ifdef COMPAT
fdd0ed12
SL
127 if (ibits & (1 << snoroute))
128 process(snoroute);
7fe7fe74
SL
129#endif
130 /* handle ICMP redirects */
fdd0ed12
SL
131 }
132}
133
134process(fd)
135 int fd;
136{
fdd0ed12 137 struct sockaddr from;
7fe7fe74 138 int fromlen = sizeof (from), cc;
fdd0ed12 139
7fe7fe74 140 cc = recvfrom(fd, packet, sizeof (packet), 0, &from, &fromlen);
fdd0ed12
SL
141 if (cc <= 0) {
142 if (cc < 0 && errno != EINTR)
7fe7fe74 143 perror("recvfrom");
fdd0ed12
SL
144 return;
145 }
7fe7fe74
SL
146 if (fromlen != sizeof (struct sockaddr_in))
147 return;
fdd0ed12
SL
148 sighold(SIGALRM);
149 rip_input(&from, cc);
150 sigrelse(SIGALRM);
151}
7fe7fe74
SL
152
153getsocket(domain, type, sin)
154 int domain, type;
155 struct sockaddr_in *sin;
156{
157 int retry, s;
158
159 retry = 1;
160 while ((s = socket(domain, type, 0, 0)) < 0 && retry) {
161 perror("socket");
162 sleep(5 * retry);
163 retry <<= 1;
164 }
165 if (retry == 0)
166 return (-1);
167 while (bind(s, sin, sizeof (*sin), 0) < 0 && retry) {
168 perror("bind");
169 sleep(5 * retry);
170 retry <<= 1;
171 }
172 if (retry == 0)
173 return (-1);
174 return (s);
175}