Debian Codegraph @

ax25mond/ax25mond.c

raw symbols SCIP
1 /*
2 * ax25mond.c - Wrapper for offering AX.25 traffic to non-root processes
3 *
4 * Copyright (C) 1998-1999 Johann Hanne, DH3MB. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
19 * 02110-1301, USA.
20 */
21
22 /*--------------------------------------------------------------------------*/
23
24 #define CONFFILE "/etc/ax25/ax25mond.conf"
25 #define MAX_SOCKETS 5
26 #define MAX_CONNECTS 50
27
28 /*--------------------------------------------------------------------------*/
29
30 #include <string.h>
31 #include <stdio.h>
32 #include <errno.h>
33 #include <netdb.h>
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38 #include <syslog.h>
39 #include <sys/time.h>
40 #include <sys/un.h>
41 #include <sys/socket.h>
42 #include <sys/fcntl.h>
43 #include <sys/signal.h>
44 #include <sys/ioctl.h>
45 #include <net/if.h>
46 #include <netinet/if_ether.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49
50 #include <config.h>
51
52 /* For older kernels */
53 #ifndef PF_PACKET
54 #define PF_PACKET PF_INET
55 #endif
56
57 /*--------------------------------------------------------------------------*/
58
59 static union {
60 struct sockaddr sa;
61 struct sockaddr_in si;
62 struct sockaddr_un su;
63 } addr;
64
65 /*--------------------------------------------------------------------------*/
66
67 static int sock_list[MAX_SOCKETS];
68 static char sock_monmode[MAX_SOCKETS];
69 static char sock_filename[MAX_SOCKETS][100];
70 static int sock_num = 0;
71 static int conn_list[MAX_CONNECTS];
72 static struct sockaddr conn_addr[MAX_CONNECTS];
73 static socklen_t conn_addrlen[MAX_CONNECTS];
74 static char conn_monmode[MAX_CONNECTS];
75 static int conn_num = 0;
76 static int highest_sock_fd;
77 static int end = 0;
78
79 /*--------------------------------------------------------------------------*/
80
81 /* from buildsaddr.c */
82 static struct sockaddr *build_sockaddr(const char *name, int *addrlen)
83 {
84 char *host_name;
85 char *serv_name;
86 char buf[1024];
87
88 memset(&addr, 0, sizeof(addr));
89 *addrlen = 0;
90
91 host_name = strcpy(buf, name);
92 serv_name = strchr(buf, ':');
93 if (!serv_name)
94 return NULL;
95 *serv_name++ = 0;
96 if (!*host_name || !*serv_name)
97 return NULL;
98
99 if (!strcmp(host_name, "local") || !strcmp(host_name, "unix")) {
100 addr.su.sun_family = AF_UNIX;
101 *addr.su.sun_path = 0;
102 strcat(addr.su.sun_path, serv_name);
103 *addrlen = sizeof(struct sockaddr_un);
104 return &addr.sa;
105 }
106
107 addr.si.sin_family = AF_INET;
108
109 if (!strcmp(host_name, "*")) {
110 addr.si.sin_addr.s_addr = INADDR_ANY;
111 } else if (!strcmp(host_name, "loopback")) {
112 addr.si.sin_addr.s_addr = inet_addr("127.0.0.1");
113 } else {addr.si.sin_addr.s_addr = inet_addr(host_name);
114 if (addr.si.sin_addr.s_addr == -1) {
115 struct hostent *hp = gethostbyname(host_name);
116 endhostent();
117 if (!hp)
118 return NULL;
119 addr.si.sin_addr.s_addr =
120 ((struct in_addr *) (hp->h_addr))->s_addr;
121 }
122 }
123
124 if (isdigit(*serv_name & 0xff)) {
125 addr.si.sin_port = htons(atoi(serv_name));
126 } else {
127 struct servent *sp = getservbyname(serv_name, NULL);
128 endservent();
129 if (!sp)
130 return NULL;
131 addr.si.sin_port = sp->s_port;
132 }
133
134 *addrlen = sizeof(struct sockaddr_in);
135 return &addr.sa;
136 }
137
138 /*--------------------------------------------------------------------------*/
139
140 static void add_socket(char *sockname, char monmode)
141 {
142 struct sockaddr *saddr;
143 int saddrlen;
144
145 if (sock_num == MAX_SOCKETS - 1) {
146 fprintf(stderr,
147 "WARNING: Too many sockets defined - only %d are "
148 "allowed\n", MAX_SOCKETS);
149 return;
150 }
151
152 if (!(saddr = build_sockaddr(sockname, &saddrlen))) {
153 fprintf(stderr, "WARNING: Invalid socket name: \"%s\"\n",
154 sockname);
155 return;
156 }
157
158 if (saddr->sa_family == AF_UNIX)
159 strcpy(sock_filename[sock_num], strchr(sockname, ':') + 1);
160 else
161 sock_filename[sock_num][0] = 0;
162
163 sock_list[sock_num] = socket(saddr->sa_family, SOCK_STREAM, 0);
164 if (sock_list[sock_num] < 0) {
165 fprintf(stderr,
166 "WARNING: Error opening socket \"%s\": %s\n",
167 sockname, strerror(errno));
168 return;
169 }
170
171 if (bind(sock_list[sock_num], saddr, saddrlen) < 0) {
172 fprintf(stderr,
173 "WARNING: Error binding socket \"%s\": %s\n",
174 sockname, strerror(errno));
175 return;
176 }
177
178 if (listen(sock_list[sock_num], 5) < 0) {
179 fprintf(stderr,
180 "WARNING: Error listening on socket \"%s\": %s\n",
181 sockname, strerror(errno));
182 return;
183 }
184
185 fcntl(sock_list[sock_num], F_SETFL, O_NONBLOCK);
186
187 if (sock_list[sock_num] > highest_sock_fd)
188 highest_sock_fd = sock_list[sock_num];
189
190 sock_monmode[sock_num] = monmode;
191 sock_num++;
192 }
193
194 /*--------------------------------------------------------------------------*/
195
196 static void close_sockets(void)
197 {
198 int i;
199
200 for (i = 0; i < sock_num; i++) {
201 close(sock_list[i]);
202
203 if (sock_filename[i][0])
204 unlink(sock_filename[i]);
205 }
206 }
207
208 /*--------------------------------------------------------------------------*/
209
210 static void quit_handler(int dummy)
211 {
212 end = 1;
213 }
214
215 /*--------------------------------------------------------------------------*/
216
217 int main(int argc, char *argv[])
218 {
219 fd_set monavail;
220 int monrx_fd, monrxtx_fd;
221 struct sockaddr monfrom;
222 socklen_t monfromlen;
223 fd_set conn_request;
224 struct timeval tv;
225 char buf[500];
226 int size;
227 int i;
228 struct ifreq ifr;
229 FILE *conffile;
230 char confline[100];
231 char *mode;
232 char *sockname;
233
234 if (argc > 1) {
235 if (argc == 2 &&
236 (strcmp(argv[1], "-v") == 0
237 || strcmp(argv[1], "--version") == 0)) {
238 printf("%s: Version " VERSION "\n\n", argv[0]);
239 printf
240 ("Copyright (C) 1998-1999 Johann Hanne, DH3MB. All rights reserved.\n"
241 "This program is free software; you can redistribute it and/or modify\n"
242 "it under the terms of the GNU General Public License as published by\n"
243 "the Free Software Foundation; either version 2 of the License, or\n"
244 "(at your option) any later version.\n\n"
245 "This program is distributed in the hope that it will be useful,\n"
246 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
247 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
248 exit(1);
249 } else {
250 printf("Usage: %s [-v|--version]\n", argv[0]);
251 exit(0);
252 }
253 }
254 /* At first, read the configuration file */
255 if (!(conffile = fopen(CONFFILE, "r"))) {
256 fprintf(stderr, "Unable to open " CONFFILE ".\n");
257 exit(1);
258 }
259 while (fgets(confline, 100, conffile)) {
260 if (confline[0] == '#') /* Comment */
261 continue;
262
263 confline[strlen(confline) - 1] = 0; /* Cut the LF */
264
265 if (!(sockname = strchr(confline, ' '))) {
266 fprintf(stderr,
267 "WARNING: The following configuration line includes "
268 "one or more errors:\n%s\n", confline);
269 continue;
270 }
271
272 *(sockname++) = 0;
273 mode = confline;
274 if (strcasecmp(mode, "RX") == 0)
275 add_socket(sockname, 0);
276 else if (strcasecmp(mode, "RXTX") == 0)
277 add_socket(sockname, 1);
278 else
279 fprintf(stderr,
280 "WARNING: Mode \"%s\" not supported\n",
281 mode);
282
283 }
284 fclose(conffile);
285
286 if (sock_num == 0) {
287 fprintf(stderr, "FATAL: No usable socket found\n");
288 exit(1);
289 }
290 /* Fork into background */
291 if (fork())
292 exit(0);
293
294 /* Close stdout, stderr and stdin */
295 fclose(stdout);
296 fclose(stderr);
297 fclose(stdin);
298
299 /* Set some signal handlers */
300 signal(SIGPIPE, SIG_IGN);
301 signal(SIGTERM, quit_handler);
302
303 openlog("ax25mond", LOG_PID, LOG_DAEMON);
304
305 /* Open AX.25 socket for monitoring RX traffic only */
306 if ((monrx_fd =
307 socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_AX25))) < 0) {
308 syslog(LOG_ERR, "Error opening monitor socket: %s\n",
309 strerror(errno));
310 exit(1);
311 }
312 fcntl(monrx_fd, F_SETFL, O_NONBLOCK);
313
314 /* Open AX.25 socket for monitoring RX and TX traffic */
315 if ((monrxtx_fd =
316 socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ALL))) < 0) {
317 syslog(LOG_ERR, "Error opening monitor socket: %s\n",
318 strerror(errno));
319 exit(1);
320 }
321 fcntl(monrxtx_fd, F_SETFL, O_NONBLOCK);
322
323 while (!end) {
324 /* Look for incoming connects on all open sockets */
325 FD_ZERO(&conn_request);
326 for (i = 0; i < sock_num; i++)
327 FD_SET(sock_list[i], &conn_request);
328 tv.tv_sec = 0;
329 tv.tv_usec = 0;
330 select(highest_sock_fd + 1, &conn_request, NULL, NULL, &tv);
331 for (i = 0; i < sock_num; i++)
332 if (FD_ISSET(sock_list[i], &conn_request)) {
333 conn_list[conn_num] = accept(sock_list[i],
334 &conn_addr
335 [conn_num],
336 &conn_addrlen
337 [conn_num]);
338 conn_monmode[conn_num] = sock_monmode[i];
339 conn_num++;
340 }
341 /* Check if there is new data on the RX-only monitor socket */
342 FD_ZERO(&monavail);
343 FD_SET(monrx_fd, &monavail);
344 tv.tv_sec = 0;
345 tv.tv_usec = 10;
346 select(monrx_fd + 1, &monavail, NULL, NULL, &tv);
347 if (FD_ISSET(monrx_fd, &monavail)) {
348 monfromlen = sizeof(monfrom);
349 size =
350 recvfrom(monrx_fd, buf, sizeof(buf), 0,
351 &monfrom, &monfromlen);
352 /* Send the packet to all connected sockets */
353 for (i = 0; i < conn_num; i++) {
354 if (conn_monmode[i] == 0)
355 if (send
356 (conn_list[i], buf, size,
357 0) < 0)
358 if (errno != EAGAIN) {
359 syslog(LOG_ERR,
360 "Error sending monitor data: %s\n",
361 strerror
362 (errno));
363 close(conn_list
364 [i]);
365 conn_list[i] =
366 conn_list
367 [conn_num - 1];
368 conn_num--;
369 }
370 }
371 }
372 /* Check if there is new data on the RX+TX monitor socket */
373 FD_ZERO(&monavail);
374 FD_SET(monrxtx_fd, &monavail);
375 tv.tv_sec = 0;
376 tv.tv_usec = 10;
377 select(monrxtx_fd + 1, &monavail, NULL, NULL, &tv);
378 if (FD_ISSET(monrxtx_fd, &monavail)) {
379 monfromlen = sizeof(monfrom);
380 size =
381 recvfrom(monrxtx_fd, buf, sizeof(buf), 0,
382 &monfrom, &monfromlen);
383 /* Check, if we have received a AX.25-packet */
384 strcpy(ifr.ifr_name, monfrom.sa_data);
385 ioctl(monrxtx_fd, SIOCGIFHWADDR, &ifr);
386 if (ifr.ifr_hwaddr.sa_family == AF_AX25)
387 /* Send the packet to all connected sockets */
388 for (i = 0; i < conn_num; i++) {
389 if (conn_monmode[i] == 1)
390 if (send
391 (conn_list[i], buf,
392 size, 0) < 0)
393 if (errno !=
394 EAGAIN) {
395 syslog
396 (LOG_ERR,
397 "Error sending monitor data: %s\n",
398 strerror
399 (errno));
400 close
401 (conn_list
402 [i]);
403 conn_list
404 [i] =
405 conn_list
406 [conn_num
407 - 1];
408 conn_num--;
409 }
410 }
411 }
412
413 }
414
415 for (i = 0; i < conn_num; i++)
416 close(conn_list[i]);
417 close_sockets();
418 close(monrx_fd);
419 close(monrxtx_fd);
420 closelog();
421 return 0;
422 }