Debian Codegraph @

ax25ipd/config.c

raw
1 /* config.c config file manipulation routines
2 *
3 * Copyright 1991, Michael Westerhof, Sun Microsystems, Inc.
4 * This software may be freely used, distributed, or modified, providing
5 * this header is not removed.
6 *
7 */
8
9 /*
10 * Modifications made for dual port TNC's
11 * by Michael Durrant VE3PNX and D. Jeff Dionne February 4, 1995
12 */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <limits.h>
17 #include <memory.h>
18 #include <netdb.h>
19 #include <fcntl.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <syslog.h>
27
28 #include "ax25ipd.h"
29
30 #include "../pathnames.h"
31
32 /* Initialize the config table */
33 void config_init(void)
34 {
35 int i;
36
37 *ttydevice = '\0';
38 for (i = 0; i < 7; i++)
39 mycallsign[i] = '\0';
40 for (i = 0; i < 7; i++)
41 myalias[i] = '\0';
42 for (i = 0; i < 7; i++)
43 mycallsign2[i] = '\0';
44 for (i = 0; i < 7; i++)
45 myalias2[i] = '\0';
46 digi = 1;
47 ttyspeed = 9600;
48 loglevel = 0;
49 bc_interval = 0;
50 bc_text[0] = '\0';
51 bc_every = 0;
52 my_udp = htons(0);
53 udp_mode = 0;
54 ip_mode = 0;
55 dual_port = 0;
56
57 stats.kiss_in = 0;
58 stats.kiss_toobig = 0;
59 stats.kiss_badtype = 0;
60 stats.kiss_tooshort = 0;
61 stats.kiss_not_for_me = 0;
62 stats.kiss_i_am_dest = 0;
63 stats.kiss_no_ip_addr = 0;
64 stats.kiss_out = 0;
65 stats.kiss_beacon_outs = 0;
66 stats.udp_in = 0;
67 stats.udp_out = 0;
68 stats.ip_in = 0;
69 stats.ip_out = 0;
70 stats.ip_failed_crc = 0;
71 stats.ip_tooshort = 0;
72 stats.ip_not_for_me = 0;
73 stats.ip_i_am_dest = 0;
74 }
75
76 /* Open and read the config file */
77
78 void config_read(char *f)
79 {
80 FILE *cf;
81 char buf[256], cbuf[256];
82 int errflag, e, lineno;
83 char *fname;
84
85 if (f==NULL || strlen(f) == 0)
86 fname=CONF_AX25IPD_FILE;
87 else
88 fname=f;
89
90 cf = fopen(fname, "r");
91 if (cf == NULL) {
92 fprintf(stderr,
93 "Config file %s not found or could not be opened\n",
94 fname);
95 exit(1);
96 }
97
98 errflag = 0;
99 lineno = 0;
100 while (fgets(buf, 255, cf) != NULL) {
101 strcpy(cbuf, buf);
102 lineno++;
103 e = parse_line(buf);
104 if (e < 0) {
105 fprintf(stderr, "Config error at line %d: ",
106 lineno);
107 if (e == -1)
108 fprintf(stderr, "Missing argument\n");
109 else if (e == -2)
110 fprintf(stderr, "Bad callsign format\n");
111 else if (e == -3)
112 fprintf(stderr, "Bad option - on/off\n");
113 else if (e == -4)
114 fprintf(stderr, "Bad option - tnc/digi\n");
115 else if (e == -5)
116 fprintf(stderr, "Host not known\n");
117 else if (e == -6)
118 fprintf(stderr, "Unknown command\n");
119 else if (e == -7)
120 fprintf(stderr, "Text string too long\n");
121 else if (e == -8)
122 fprintf(stderr,
123 "Bad option - every/after\n");
124 else if (e == -9)
125 fprintf(stderr, "Bad option - ip/udp\n");
126 else
127 fprintf(stderr, "Unknown error\n");
128 fprintf(stderr, "%s", cbuf);
129 errflag++;
130 }
131 }
132 if (errflag)
133 exit(1);
134
135 if (strlen(ttydevice) == 0) {
136 fprintf(stderr, "No device specified in config file\n");
137 exit(1);
138 }
139
140 if ((udp_mode == 0) && (ip_mode == 0)) {
141 fprintf(stderr, "Must specify ip and/or udp sockets\n");
142 exit(1);
143 }
144
145 if (digi) {
146 if (mycallsign[0] == '\0') {
147 fprintf(stderr, "No mycall line in config file\n");
148 exit(1);
149 }
150 }
151 if ((digi) && (dual_port)) {
152 if (mycallsign2[0] == '\0') {
153 fprintf(stderr,
154 "No mycall2 line in config file\n");
155 exit(1);
156 }
157 }
158 fclose(cf);
159 }
160
161 /* Process each line from the config file. The return value is encoded. */
162 int parse_line(char *buf)
163 {
164 char *p, *q;
165 unsigned char tcall[7], tip[4];
166 struct hostent *he;
167 int i, j, uport;
168 unsigned int flags;
169
170 p = strtok(buf, " \t\n\r");
171
172 if (p == NULL)
173 return 0;
174 if (*p == '#')
175 return 0;
176
177 if (strcmp(p, "mycall") == 0) {
178 q = strtok(NULL, " \t\n\r");
179 if (q == NULL)
180 return -1;
181 if (a_to_call(q, mycallsign) != 0)
182 return -2;
183 return 0;
184
185 } else if (strcmp(p, "mycall2") == 0) {
186 q = strtok(NULL, " \t\n\r");
187 if (q == NULL)
188 return -1;
189 if (a_to_call(q, mycallsign2) != 0)
190 return -2;
191 return 0;
192
193 } else if (strcmp(p, "myalias") == 0) {
194 q = strtok(NULL, " \t\n\r");
195 if (q == NULL)
196 return -1;
197 if (a_to_call(q, myalias) != 0)
198 return -2;
199 dual_port = 1;
200 if (mycallsign2[0] == '\0') {
201 dual_port = 0;
202 }
203 return 0;
204
205 } else if (strcmp(p, "myalias2") == 0) {
206 q = strtok(NULL, " \t\n\r");
207 if (q == NULL)
208 return -1;
209 if (a_to_call(q, myalias2) != 0)
210 return -2;
211 return 0;
212
213 } else if (strcmp(p, "device") == 0) {
214 /* already set? i.e. with commandline option, which overwrites
215 * the preconfigured setting. useful for systems with unix98
216 * style pty's
217 */
218 if (*ttydevice)
219 return 0;
220 q = strtok(NULL, " \t\n\r");
221 if (q == NULL)
222 return -1;
223 strncpy(ttydevice, q, sizeof(ttydevice)-1);
224 ttydevice[sizeof(ttydevice)-1] = 0;
225 return 0;
226
227 } else if (strcmp(p, "mode") == 0) {
228 q = strtok(NULL, " \t\n\r");
229 if (q == NULL)
230 return -1;
231 if (strcmp(q, "digi") == 0)
232 digi = 1;
233 else if (strcmp(q, "tnc") == 0)
234 digi = 0;
235 else
236 return -4;
237 return 0;
238
239 } else if (strcmp(p, "speed") == 0) {
240 q = strtok(NULL, " \t\n\r");
241 if (q == NULL)
242 return -1;
243 ttyspeed = atoi(q);
244 return 0;
245
246 } else if (strcmp(p, "socket") == 0) {
247 q = strtok(NULL, " \t\n\r");
248 if (q == NULL)
249 return -1;
250 if (strcmp(q, "ip") == 0) {
251 ip_mode = 1;
252 } else if (strcmp(q, "udp") == 0) {
253 udp_mode = 1;
254 my_udp = htons(DEFAULT_UDP_PORT);
255 q = strtok(NULL, " \t\n\r");
256 if (q != NULL) {
257 i = atoi(q);
258 if (i > 0)
259 my_udp = htons(i);
260 }
261 } else
262 return -9;
263 return 0;
264
265 } else if (strcmp(p, "beacon") == 0) {
266 q = strtok(NULL, " \t\n\r");
267 if (q == NULL)
268 return -1;
269
270 if (strcmp(q, "every") == 0)
271 bc_every = 1;
272 else if (strcmp(q, "after") == 0)
273 bc_every = 0;
274 else
275 return -8;
276
277 q = strtok(NULL, " \t\n\r");
278 if (q == NULL)
279 return -1;
280 bc_interval = atoi(q);
281 return 0;
282
283 /* This next one is a hack!!!!!! watch out!!!! */
284 } else if (strcmp(p, "btext") == 0) {
285 q = p + strlen(p) + 1;
286 if (strlen(q) < 2)
287 return -1; /* line ends with a \n */
288 if (strlen(q) > sizeof(bc_text))
289 return -7;
290 q[strlen(q) - 1] = '\0';
291 strncpy(bc_text, q, sizeof(bc_text)-1);
292 bc_text[sizeof(bc_text)-1] = 0;
293 return 0;
294
295 } else if (strcmp(p, "loglevel") == 0) {
296 q = strtok(NULL, " \t\n\r");
297 if (q == NULL)
298 return -1;
299 loglevel = atoi(q);
300 return 0;
301
302 } else if (strcmp(p, "route") == 0) {
303 uport = 0;
304 flags = 0;
305
306 q = strtok(NULL, " \t\n\r");
307 if (q == NULL)
308 return -1;
309
310 if (a_to_call(q, tcall) != 0)
311 return -2;
312
313 q = strtok(NULL, " \t\n\r");
314 if (q == NULL)
315 return -1;
316 he = gethostbyname(q);
317 if (he != NULL) {
318 memcpy(tip, he->h_addr_list[0], 4);
319 } else { /* maybe user specified a numeric addr? */
320 j = inet_addr(q);
321 if (j == -1)
322 return -5; /* if -1, bad deal! */
323 memcpy(tip, &j, 4);
324 }
325
326 if (my_udp)
327 uport = ntohs(my_udp);
328 while ((q = strtok(NULL, " \t\n\r")) != NULL) {
329 if (strcmp(q, "udp") == 0) {
330 /* uport == 0 should never happen.
331 * re-use setting. It costed me a long time
332 * to realize that "udp" in ax25ipd.conf
333 * is not enough. It used the wrong port;
334 * 93 is standard, I configured socket udp 93,
335 * but ax25ipd talked to partners on that
336 * strange 10093. Even more, ax25ipd.conf's
337 * examples for axip did not help - and the
338 * manual is far from complete.
339 */
340 if (uport == 0)
341 uport = DEFAULT_UDP_PORT;
342 q = strtok(NULL, " \t\n\r");
343 if (q != NULL) {
344 i = atoi(q);
345 if (i > 0)
346 uport = i;
347 }
348 } else {
349 /* Test for broadcast flag */
350 if (strchr(q, 'b')) {
351 flags |= AXRT_BCAST;
352 }
353
354 /* Test for Default flag */
355 if (strchr(q, 'd')) {
356 flags |= AXRT_DEFAULT;
357 }
358 }
359 }
360 route_add(tip, tcall, uport, flags);
361 return 0;
362
363 } else if (strcmp(p, "broadcast") == 0) {
364
365 while ((q = strtok(NULL, " \t\n\r")) != NULL) {
366 if (a_to_call(q, tcall) != 0)
367 return -2;
368 bcast_add(tcall);
369 }
370 return 0;
371
372 } else if (strcmp(p, "param") == 0) {
373 q = strtok(NULL, " \t\n\r");
374 if (q == NULL)
375 return -1;
376 i = atoi(q);
377 q = strtok(NULL, " \t\n\r");
378 if (q == NULL)
379 return -1;
380 j = atoi(q);
381 param_add(i, j);
382 return 0;
383 }
384 return -999;
385 }
386
387 /* Convert ascii callsign to internal format */
388 int a_to_call(char *text, unsigned char *tcall)
389 {
390 int i;
391 int ssid;
392 unsigned char c;
393
394 if (strlen(text) == 0)
395 return -1;
396
397 ssid = 0;
398 for (i = 0; i < 6; i++) {
399 tcall[i] = (' ' << 1);
400 }
401 tcall[6] = '\0';
402
403 for (i = 0; i < strlen(text); i++) {
404 c = text[i];
405 if (c == '-') {
406 ssid = atoi(&text[i + 1]);
407 if (ssid > 15)
408 return -1;
409 tcall[6] = (ssid << 1);
410 return 0;
411 }
412 if (islower(c))
413 c = toupper(c);
414 if (i > 5)
415 return -1;
416 tcall[i] = (c << 1);
417 }
418 return 0;
419 }
420
421 /* Convert internal callsign to printable format */
422 char *call_to_a(unsigned char *tcall)
423 {
424 int i;
425 int ssid;
426 char *tptr;
427 static char t[10];
428
429 for (i = 0, tptr = t; i < 6; i++) {
430 if (tcall[i] == (' ' << 1))
431 break;
432 *tptr = tcall[i] >> 1;
433 tptr++;
434 }
435
436 ssid = (tcall[6] >> 1) & 0x0f;
437 if (ssid > 0) {
438 *tptr = '-';
439 tptr++;
440 if (ssid > 9) {
441 *tptr = '1';
442 tptr++;
443 ssid -= 10;
444 }
445 *tptr = '0' + ssid;
446 tptr++;
447 }
448
449 *tptr = '\0';
450 return &t[0];
451 }
452
453 /* print the configuration data out */
454 void dump_config(void)
455 {
456 LOGL1("\nCurrent configuration:\n");
457 if (ip_mode)
458 LOGL1(" socket ip\n");
459 if (udp_mode)
460 LOGL1(" socket udp on port %d\n", ntohs(my_udp));
461 LOGL1(" mode %s\n", digi ? "digi" : "tnc");
462 LOGL1(" device %s\n", ttydevice);
463 LOGL1(" speed %d\n", ttyspeed);
464 if (digi)
465 LOGL1(" mycall %s\n", call_to_a(mycallsign));
466 if (digi && myalias[0])
467 LOGL1(" myalias %s\n", call_to_a(myalias));
468 if (bc_interval > 0) {
469 LOGL1(" beacon %s %d\n", bc_every ? "every" : "after",
470 bc_interval);
471 LOGL1(" btext %s\n", bc_text);
472 }
473 LOGL1(" loglevel %d\n", loglevel);
474 (void) fflush(stdout);
475 }