Debian Codegraph @

tcpip/rip98d.c

raw
1
2 #ifdef HAVE_CONFIG_H
3 #include <config.h>
4 #endif
5 #include <scm-version.h>
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <netdb.h>
12 #include <syslog.h>
13 #include <signal.h>
14 #include <sys/time.h>
15 #include <time.h>
16
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
19
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <net/if.h>
24 #include <net/route.h>
25
26 #include <netax25/daemon.h>
27
28
29 #include "../pathnames.h"
30 #include "rip98d.h"
31
32 struct dest_struct dest_list[50];
33
34 int dest_count;
35
36 int debug = FALSE;
37 int route_restrict = FALSE;
38 int logging = FALSE;
39
40 struct route_struct *first_route;
41
42 static void terminate(int sig)
43 {
44 if (logging) {
45 syslog(LOG_INFO, "terminating on SIGTERM\n");
46 closelog();
47 }
48
49 exit(0);
50 }
51
52 unsigned int mask2bits(unsigned int mask)
53 {
54 mask = ~mask;
55
56 if (!mask)
57 return 32;
58
59 return __builtin_clz(mask);
60 }
61
62 unsigned int bits2mask(unsigned int bits)
63 {
64 if (!bits)
65 return 0;
66
67 return ~0 << (sizeof(unsigned int) * 8 - bits);
68 }
69
70 static unsigned int hex2int(char *buffer)
71 {
72 return strtoul(buffer, NULL, 16);
73 }
74
75 static int read_routes(void)
76 {
77 struct route_struct *route, *temp;
78 char buffer[1023], iface[16], net_addr[64], gate_addr[64], mask_addr[64];
79 int n, refcnt, use, metric, mss, window;
80 unsigned int iflags;
81 struct in_addr address;
82 unsigned long int netmask;
83 unsigned long int network;
84 FILE *fp;
85
86 if (first_route != NULL) {
87 route = first_route;
88
89 while (route != NULL) {
90 temp = route->next;
91 free(route);
92 route = temp;
93 }
94
95 first_route = NULL;
96 }
97
98 fp = fopen(PROC_IP_ROUTE_FILE, "r");
99 if (fp == NULL) {
100 if (logging)
101 syslog(LOG_ERR, "error cannot open %s\n", PROC_IP_ROUTE_FILE);
102 return FALSE;
103 }
104
105 while (fgets(buffer, 1023, fp) != NULL) {
106 n = sscanf(buffer, "%s %s %s %X %d %d %d %s %d %d\n",
107 iface, net_addr, gate_addr, &iflags, &refcnt, &use,
108 &metric, mask_addr, &mss, &window);
109
110 if (n != 10)
111 continue;
112
113 address.s_addr = hex2int(net_addr);
114 netmask = mask2bits(ntohl(hex2int(mask_addr)));
115
116 network = inet_netof(address);
117
118 if (network == 0 || network == 127) {
119 if (debug && logging)
120 syslog(LOG_DEBUG, "rejecting route to %s/%ld - should not be propagated\n", inet_ntoa(address), netmask);
121 continue;
122 }
123
124 if (route_restrict) {
125 if (inet_netof(address) != 44) {
126 if (debug && logging)
127 syslog(LOG_DEBUG, "rejecting route to %s/%ld - not ampr.org\n", inet_ntoa(address), netmask);
128 continue;
129 }
130 }
131
132 route = malloc(sizeof(struct route_struct));
133 if (route == NULL) {
134 if (logging)
135 syslog(LOG_ERR, "out of memory !\n");
136 return FALSE;
137 }
138
139 route->addr = address;
140 route->bits = netmask;
141 route->metric = metric;
142 route->action = (iflags & RTF_DYNAMIC) ? ORIG_ROUTE : FIXED_ROUTE;
143
144 route->next = first_route;
145 first_route = route;
146 }
147
148 fclose(fp);
149
150 return TRUE;
151 }
152
153 static int load_dests(void)
154 {
155 struct hostent *host;
156 char buffer[255], *s;
157 FILE *fp;
158
159 fp = fopen(CONF_RIP98D_FILE, "r");
160 if (fp == NULL) {
161 fprintf(stderr, "rip98d: cannot open config file\n");
162 return FALSE;
163 }
164
165 while (fgets(buffer, 255, fp) != NULL) {
166 s = strchr(buffer, '\n');
167 if (s != NULL) *s = '\0';
168
169 host = gethostbyname(buffer);
170 if (host == NULL) {
171 fprintf(stderr, "rip98d: cannot resolve name %s\n", buffer);
172 fclose(fp);
173 return FALSE;
174 }
175
176 memcpy(&dest_list[dest_count].dest_addr, host->h_addr,
177 host->h_length);
178 dest_count++;
179 }
180
181 fclose(fp);
182
183 if (dest_count == 0)
184 return FALSE;
185
186 return TRUE;
187 }
188
189 int main(int argc, char **argv)
190 {
191 int s, i;
192 struct sockaddr_in loc_addr;
193 struct timeval timeout;
194 time_t timenow, timelast = 0;
195 int interval = 3600;
196 fd_set fdset;
197
198 if (!load_dests()) {
199 fprintf(stderr, "rip98d: no destination routers defined\n");
200 return 1;
201 }
202
203 while ((i = getopt(argc, argv, "dlrt:v")) != -1) {
204 switch (i) {
205 case 'd':
206 debug = TRUE;
207 break;
208 case 'l':
209 logging = TRUE;
210 break;
211 case 'r':
212 route_restrict = TRUE;
213 break;
214 case 't':
215 interval = atoi(optarg) * 60;
216 if (interval < 60 || interval > 7200) {
217 fprintf(stderr, "rip98d: invalid time interval\n");
218 return 1;
219 }
220 break;
221 case 'v':
222 printf("rip98d: %s\n", FULL_VER);
223 return 0;
224 case ':':
225 fprintf(stderr, "rip98d: invalid time interval\n");
226 return 1;
227 case '?':
228 fprintf(stderr, "usage: rip98d [-d] [-l] [-r] [-t interval] [-v]\n");
229 return 1;
230 }
231 }
232
233 signal(SIGTERM, terminate);
234
235 s = socket(AF_INET, SOCK_DGRAM, 0);
236 if (s < 0) {
237 perror("rip98d: socket");
238 return 1;
239 }
240
241 memset(&loc_addr, 0, sizeof(loc_addr));
242 loc_addr.sin_family = AF_INET;
243 loc_addr.sin_addr.s_addr = htonl(INADDR_ANY);
244 loc_addr.sin_port = htons(RIP_PORT);
245
246 if (bind(s, &loc_addr, sizeof(loc_addr)) < 0) {
247 perror("rip98d: bind");
248 close(s);
249 return 1;
250 }
251
252 if (!daemon_start(FALSE)) {
253 fprintf(stderr, "rip98d: cannot become a daemon\n");
254 close(s);
255 return 1;
256 }
257
258 if (logging) {
259 openlog("rip98d", LOG_PID, LOG_DAEMON);
260 syslog(LOG_INFO, "starting");
261 }
262
263 for (;;) {
264 FD_ZERO(&fdset);
265 FD_SET(s, &fdset);
266
267 timeout.tv_sec = 60;
268 timeout.tv_usec = 0;
269
270 select(s + 1, &fdset, NULL, NULL, &timeout);
271
272 if (!read_routes()) {
273 if (logging)
274 closelog();
275 return 1;
276 }
277
278 if (FD_ISSET(s, &fdset))
279 receive_routes(s);
280
281 time(&timenow);
282
283 if ((timenow - timelast) > interval) {
284 timelast = timenow;
285 if (first_route != NULL)
286 transmit_routes(s);
287 }
288 }
289 }