Debian Codegraph @

ax25rtd/ax25rtd.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1996 Joerg Reuter ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 */
20
21 /*
22 * This daemon tries to learn AX.25, ARP, IP route entries by listening
23 * to the AX.25 traffic. It caches up to 256 entries (in "FIFO" mode)
24 * and saves the cache on demand or at shutdown in /var/ax25/ax25rtd/ip_route
25 * and /var/ax25/ax25rtd/ax25_route. The configuration file is
26 * /etc/ax25/ax25rtd.conf, you can almost everything configure
27 * there. See ax25rtcl.c for runtime maintenance.
28 *
29 */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <signal.h>
39 #include <errno.h>
40 #include <syslog.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <sys/socket.h>
45 #include <sys/un.h>
46 #include <netinet/in.h>
47 #ifdef __GLIBC__
48 #include <net/ethernet.h>
49 #else
50 #include <linux/if_ether.h>
51 #endif
52
53 #include <netax25/ax25.h>
54 #include <netrose/rose.h>
55 #include <netax25/axconfig.h>
56 #include <netax25/axlib.h>
57
58 #include "../pathnames.h"
59 #include "ax25rtd.h"
60
61 config *Config = NULL;
62
63 int reload = 0;
64
65 ip_rt_entry *ip_routes;
66 int ip_routes_cnt;
67 int ip_maxroutes = IP_MAXROUTES;
68
69 ax25_rt_entry *ax25_routes;
70 int ax25_routes_cnt;
71 int ax25_maxroutes = AX25_MAXROUTES;
72
73 char ip_encaps_dev[32] = "";
74 char iproute2_table[32] = "";
75
76 config *dev_get_config(char *dev)
77 {
78 config *config;
79
80 for (config = Config; config; config = config->next)
81 if (!strcmp(config->dev, dev))
82 return config;
83
84 return port_get_config(dev);
85 }
86
87 config *port_get_config(char *port)
88 {
89 config *config;
90
91 for (config = Config; config; config = config->next)
92 if (!strcmp(config->port, port))
93 return config;
94 return NULL;
95 }
96
97 static void sig_reload(int d)
98 {
99 reload = 1;
100 signal(SIGHUP, sig_reload);
101 }
102
103 static void sig_debug(int d)
104 {
105 fprintf(stderr, "config:\n");
106 dump_config(2);
107 fprintf(stderr, "ip-routes:\n");
108 dump_ip_routes(2, 0);
109 fprintf(stderr, "ax25-routes:\n");
110 dump_ax25_routes(2, 0);
111 signal(SIGUSR1, sig_debug);
112 }
113
114 static void sig_term(int d)
115 {
116 save_cache();
117 daemon_shutdown(0);
118 }
119
120 void daemon_shutdown(int reason)
121 {
122 unlink(DATA_AX25ROUTED_CTL_SOCK);
123 exit(reason);
124 }
125
126 #define FD_MAX(fd) {fd_max = (fd > fd_max? fd : fd_max); FD_SET(fd, &read_fds);}
127
128 int main(int argc, char **argv)
129 {
130 char buf[256];
131 int size, s, cntrl_s, cntrl_fd;
132 socklen_t cntrl_len;
133 struct sockaddr_un cntrl_addr;
134 fd_set read_fds, write_fds;
135 int fd_max;
136
137 if (ax25_config_load_ports() == 0) {
138 fprintf(stderr, "ax25rtd: no AX.25 port configured\n");
139 return 1;
140 }
141
142 load_config();
143 load_cache();
144
145 if (fork())
146 return 0;
147
148 s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_AX25));
149 if (s == -1) {
150 perror("AX.25 socket");
151 return 1;
152 }
153
154 cntrl_s = socket(AF_UNIX, SOCK_STREAM, 0);
155 if (cntrl_s < 0) {
156 perror("Control socket");
157 return 1;
158 }
159
160 unlink(DATA_AX25ROUTED_CTL_SOCK);
161
162 cntrl_addr.sun_family = AF_UNIX;
163 strcpy(cntrl_addr.sun_path, DATA_AX25ROUTED_CTL_SOCK);
164 cntrl_len =
165 sizeof(cntrl_addr.sun_family) +
166 strlen(DATA_AX25ROUTED_CTL_SOCK);
167
168 if (bind(cntrl_s, (struct sockaddr *) &cntrl_addr, cntrl_len) < 0) {
169 perror("bind Control socket");
170 daemon_shutdown(1);
171 }
172
173 chmod(DATA_AX25ROUTED_CTL_SOCK, 0600);
174 listen(cntrl_s, 1);
175
176 signal(SIGUSR1, sig_debug);
177 signal(SIGHUP, sig_reload);
178 signal(SIGTERM, sig_term);
179
180 cntrl_fd = -1;
181
182 for (;;) {
183 fd_max = 0;
184 FD_ZERO(&read_fds);
185 FD_ZERO(&write_fds);
186 FD_MAX(s);
187 if (cntrl_fd > 0) {
188 FD_MAX(cntrl_fd);
189 FD_SET(cntrl_fd, &write_fds);
190 } else {
191 FD_MAX(cntrl_s);
192 }
193
194 if (select(fd_max + 1, &read_fds, NULL, &write_fds, NULL) < 0) {
195 if (errno == EINTR) /* woops! */
196 continue;
197
198 if (!FD_ISSET(cntrl_fd, &write_fds)) {
199 perror("select");
200 save_cache();
201 daemon_shutdown(1);
202 } else {
203 close(cntrl_fd);
204 cntrl_fd = -1;
205 continue;
206 }
207 }
208
209 if (cntrl_fd > 0) {
210 if (FD_ISSET(cntrl_fd, &read_fds)) {
211 size = read(cntrl_fd, buf, sizeof(buf));
212 if (size > 0) {
213 buf[size] = '\0';
214 interpret_command(cntrl_fd, buf);
215 } else {
216 close(cntrl_fd);
217 cntrl_fd = -1;
218 }
219 }
220 } else if (FD_ISSET(cntrl_s, &read_fds)) {
221 cntrl_fd = accept(cntrl_s,
222 (struct sockaddr *)&cntrl_addr,
223 &cntrl_len);
224 if (cntrl_fd < 0) {
225 perror("accept Control");
226 save_cache();
227 daemon_shutdown(1);
228 }
229 }
230
231 if (reload)
232 reload_config();
233
234 if (FD_ISSET(s, &read_fds))
235 ax25_receive(s);
236 }
237
238 return 0; /* what ?! */
239 }