summaryrefslogtreecommitdiffstats
path: root/axconfig.c
blob: 756b8ef421dd8a8a4ebc8065475b3356fcf66bba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#define _LINUX_STRING_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <config.h>

#include <sys/types.h>
#include <sys/ioctl.h>

#include <net/if.h>
#include <net/if_arp.h>
#ifdef HAVE_NETAX25_AX25_H
#include <netax25/ax25.h>
#else
#include "kernel_ax25.h"
#endif
#ifdef HAVE_NETROSE_ROSE_H
#include <netrose/rose.h>
#else
#include "kernel_rose.h"
#endif
#include "axlib.h"
#include "pathnames.h"
#include "axconfig.h"

#define _PATH_PROCNET_DEV           "/proc/net/dev"

typedef struct _axport
{
	struct _axport *Next;
	char *Name;
	char *Call;
	char *Device;
	int  Baud;
	int  Window;
	int  Paclen;
	char *Description;
} AX_Port;

static AX_Port *ax25_ports     = NULL;
static AX_Port *ax25_port_tail = NULL;

typedef struct _axiface
{
	struct _axiface *Next;
	char *Name;
	char *Call;
	char *Device;
} AX_Iface;

static AX_Iface *ax25_ifaces     = NULL;

static int ax25_hw_cmp(unsigned char *callsign, unsigned char *hw_addr)
{
	ax25_address call;

	ax25_aton_entry(callsign, call.ax25_call);
	
	return ax25_cmp(&call, (ax25_address *)hw_addr) == 0;
}

static AX_Port *ax25_port_ptr(char *name)
{
	AX_Port *p = ax25_ports;

	if (name == NULL)
		return p;

	while (p != NULL) {
		if (strcasecmp(p->Name, name) == 0)
			return p;

		p = p->Next;
	}

	return NULL;
}

char *ax25_config_get_next(char *name)
{
	AX_Port *p;
	
	if (ax25_ports == NULL)
		return NULL;
		
	if (name == NULL)
		return ax25_ports->Name;
	
	if ((p = ax25_port_ptr(name)) == NULL)
		return NULL;
			
	p = p->Next;

	if (p == NULL)
		return NULL;
		
	return p->Name;
}

char *ax25_config_get_name(char *device)
{
	AX_Port *p = ax25_ports;

	while (p != NULL) {
		if (strcmp(p->Device, device) == 0)
			return p->Name;

		p = p->Next;
	}

	return NULL;
}

char *ax25_config_get_addr(char *name)
{
	AX_Port *p = ax25_port_ptr(name);

	if (p == NULL)
		return NULL;

	return p->Call;
}

char *ax25_config_get_dev(char *name)
{
	AX_Port *p = ax25_port_ptr(name);

	if (p == NULL)
		return NULL;

	return p->Device;
}

char *ax25_config_get_port(ax25_address *callsign)
{
	AX_Port *p = ax25_ports;
	ax25_address addr;

	if (ax25_cmp(callsign, &null_ax25_address) == 0)
		return "*";
		
	while (p != NULL) {
		ax25_aton_entry(p->Call, (char *)&addr);

		if (ax25_cmp(callsign, &addr) == 0)
			return p->Name;

		p = p->Next;
	}

	return NULL;
}

int ax25_config_get_window(char *name)
{
	AX_Port *p = ax25_port_ptr(name);

	if (p == NULL)
		return 0;

	return p->Window;
}

int ax25_config_get_paclen(char *name)
{
	AX_Port *p = ax25_port_ptr(name);

	if (p == NULL)
		return 0;

	return p->Paclen;
}

int ax25_config_get_baud(char *name)
{
	AX_Port *p = ax25_port_ptr(name);

	if (p == NULL)
		return 0;

	return p->Baud;
}

char *ax25_config_get_desc(char *name)
{
	AX_Port *p = ax25_port_ptr(name);

	if (p == NULL)
		return NULL;

	return p->Description;
}

static void free_ax25_ports() {
	AX_Port *axp;
	for (axp = ax25_ports; axp; ) {
		AX_Port *q = axp->Next;
		if (axp->Name) free(axp->Name);
		if (axp->Call) free(axp->Call);
		if (axp->Device) free(axp->Device);
		if (axp->Description) free(axp->Description);
		free(axp);
		axp = q;
	}
	ax25_ports = ax25_port_tail = NULL;
}

static void free_ax25_ifaces() {
	AX_Iface *axif;
	for (axif = ax25_ifaces; axif; ) {
		AX_Iface *q = axif->Next;
		if (axif->Name) free(axif->Name);
		if (axif->Device) free(axif->Device);
		if (axif->Call) free(axif->Call);
		free(axif);
		axif = q;
	}
	ax25_ifaces = NULL;
}

static int test_and_add_ax25_iface(int fd, char *name, struct ifreq *ifr) {
	AX_Iface *axif;

	if (!name)
		return 0;
	if (!strcmp(name, "lo")) return 0;
	if (!ifr)
		return 0;

	strncpy(ifr->ifr_name, name, IFNAMSIZ-1);
	ifr->ifr_name[IFNAMSIZ-1] = 0;

	if (ioctl(fd, SIOCGIFFLAGS, ifr) < 0) {
		fprintf(stderr, "axconfig: SIOCGIFFLAGS: %s\n", strerror(errno));
		return -1;
	}

	if (!(ifr->ifr_flags & IFF_UP)) return 0;

	if (ioctl(fd, SIOCGIFHWADDR, ifr) < 0) {
		fprintf(stderr, "axconfig: SIOCGIFHWADDR: %s\n", strerror(errno));
		return -1;
	}

	if (ifr->ifr_hwaddr.sa_family != ARPHRD_AX25) return 0;

	if ((axif = (AX_Iface *)malloc(sizeof(AX_Iface))) == NULL) {
		fprintf(stderr, "axconfig: out of memory!\n");
		return -1;
	}
	if (!(axif->Name = strdup(name))) {
		fprintf(stderr, "axconfig: out of memory!\n");
		free(axif);
		return -1;
	}
	if (!(axif->Device = strdup(ifr->ifr_name))) {
		fprintf(stderr, "axconfig: out of memory!\n");
		free(axif->Name);
		free(axif);
		return -1;
	}
	if (!(axif->Call = strdup(ifr->ifr_hwaddr.sa_data))) {
		free(axif->Name);
		free(axif->Device);
		free(axif);
		fprintf(stderr, "axconfig: out of memory!\n");
		return -1;
	}
	axif->Next = ax25_ifaces;
	ax25_ifaces = axif;

	return 1;
}

static char *proc_get_iface_name(char *line) {
	char *p;

	if (!(p = strchr(line, ':')))
		return 0; // should never hapen
	*p = 0;
	while (*line && isspace(*line & 0xff))
		line++;
	if (!*line)
		return 0; // should never hapen
	return line;
}

static int get_ax25_ifaces(void) {
	FILE *fp;
	struct ifreq ifr;
	int fd;
	int ret = -1;

	free_ax25_ifaces();
	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		fprintf(stderr, "axconfig: unable to open socket (%s)\n", strerror(errno));
		goto out;
	}

#ifdef	FIND_ALL_INTERFACES /* will be default soon, after tracing a kernel error */
	if ((fp = fopen(_PATH_PROCNET_DEV, "r"))) {
		/* Because ifc.ifc_req does not show interfaces without
		 * IP-Address assigned, we use the device list via /proc.
		 * This concept was inspired by net-tools / ifconfig
		 */
		char buf[512];
		int i = 0;
		ret = 0;
		while (fgets(buf, sizeof(buf), fp)) {
			/* skip proc header */
			if (i < 2) {
				i++;
				continue;
			}
			if (test_and_add_ax25_iface(fd, proc_get_iface_name(buf), &ifr) > 0)
				ret++;
		}
		fclose(fp);
	} else {
#else
	{
#endif
		struct ifconf ifc;
		struct ifreq *ifrp;
		char buffer[1024];
		int n = 0;
		ifc.ifc_len = sizeof(buffer);
		ifc.ifc_buf = buffer;
	
		if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
			fprintf(stderr, "axconfig: SIOCGIFCONF: %s\n", strerror(errno));
			goto out;
		}
		ret = 0;

		for (ifrp = ifc.ifc_req, n = ifc.ifc_len / sizeof(struct ifreq); --n >= 0; ifrp++)
			if (test_and_add_ax25_iface(fd, ifrp->ifr_name, &ifr) > 0)
				ret++;
	}

out:
	close(fd);
	return ret;
}

static int ax25_config_init_port(int lineno, char *line)
{
	AX_Port *p;
	AX_Iface *axif = NULL;
	char *name, *call, *baud, *paclen, *window, *desc, *dev = NULL;
	int found = 0;

	name   = strtok(line, " \t");
	call   = strtok(NULL, " \t");
	baud   = strtok(NULL, " \t");
	paclen = strtok(NULL, " \t");
	window = strtok(NULL, " \t");
	desc   = strtok(NULL, "");

	if (name == NULL   || call == NULL   || baud == NULL ||
	    paclen == NULL || window == NULL || desc == NULL) {
		fprintf(stderr, "axconfig: unable to parse line %d of axports file\n", lineno);
		return -1;
	}

	for (p = ax25_ports; p != NULL; p = p->Next) {
		if (strcasecmp(name, p->Name) == 0) {
			fprintf(stderr, "axconfig: duplicate port name %s in line %d of axports file\n", name, lineno);
			return -1;
		}
		/* dl9sau: why? */
		if (strcasecmp(call, p->Call) == 0) {
			fprintf(stderr, "axconfig: duplicate callsign %s in line %d of axports file\n", call, lineno);
			return -1;
		}
	}

	if (atoi(baud) < 0) {
		fprintf(stderr, "axconfig: invalid baud rate setting %s in line %d of axports file\n", baud, lineno);
		return -1;
	}

	if (atoi(paclen) <= 0) {
		fprintf(stderr, "axconfig: invalid packet size setting %s in line %d of axports file\n", paclen, lineno);
		return -1;
	}

	if (atoi(window) <= 0) {
		fprintf(stderr, "axconfig: invalid window size setting %s in line %d of axports file\n", window, lineno);
		return -1;
	}

	strupr(call);

	for (axif = ax25_ifaces; axif; axif = axif->Next) {
		if (!strcmp(axif->Name, name)) {
			found = 1;
			dev = axif->Device;
			/* exact match */
			if (ax25_hw_cmp(call, axif->Call))
				found = 2;
			break;
		}
	}

	if (found < 2) {
		fprintf(stderr, "axconfig: port %s not active\n", name);
		return -1;
	}

	if ((p = (AX_Port *)malloc(sizeof(AX_Port))) == NULL) {
		fprintf(stderr, "axconfig: out of memory!\n");
		return -1;
	}

	if (!(p->Name        = strdup(name))) {
		fprintf(stderr, "axconfig: out of memory!\n");
		free(p);
		return -1;
	}
	if (!(p->Call        = strdup(call))) {
		fprintf(stderr, "axconfig: out of memory!\n");
		free(p->Name);
		free(p);
		return -1;
	}
	if (!(p->Device      = strdup(dev))) {
		fprintf(stderr, "axconfig: out of memory!\n");
		free(p->Name);
		free(p->Call);
		free(p);
		return -1;
	}
	if (!(p->Description = strdup(desc))) {
		fprintf(stderr, "axconfig: out of memory!\n");
		free(p->Name);
		free(p->Call);
		free(p->Description);
		free(p);
		return -1;
	}
	p->Baud        = atoi(baud);
	p->Window      = atoi(window);
	p->Paclen      = atoi(paclen);

	if (ax25_ports == NULL)
		ax25_ports = p;
	else
		ax25_port_tail->Next = p;

	ax25_port_tail = p;

	p->Next = NULL;

	return 0;
}

int ax25_config_load_ports(void)
{
	FILE *fp;
	char buffer[256], *s;
	int lineno = 1;
	int n = 0;

	/* in case of a "reload" */
	free_ax25_ports();

	if (get_ax25_ifaces() <= 0)
		goto out;

	if ((fp = fopen(CONF_AXPORTS_FILE, "r")) == NULL) {
		fprintf(stderr, "axconfig: unable to open axports file %s (%s)\n", CONF_AXPORTS_FILE, strerror(errno));
		goto out;
	}

	while (fgets(buffer, 255, fp) != NULL) {
		if ((s = strchr(buffer, '\n')) != NULL)
			*s = '\0';

		if (strlen(buffer) > 0 && *buffer != '#')
			if (!ax25_config_init_port(lineno, buffer))
				n++;

		lineno++;
	}

	fclose(fp);

	if (ax25_ports == NULL)
		n = 0;

out:
	free_ax25_ifaces();
	return n;
}