-
Notifications
You must be signed in to change notification settings - Fork 0
/
nrconfig.c
373 lines (295 loc) · 7.34 KB
/
nrconfig.c
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
#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>
#include <netax25/ax25.h>
#include <netrose/rose.h>
#include "pathnames.h"
#include <netax25/axlib.h>
#include <netax25/nrconfig.h>
typedef struct _nrport
{
struct _nrport *Next;
char *Name;
char *Call;
char *Alias;
char *Device;
int Paclen;
char *Description;
} NR_Port;
static NR_Port *nr_ports;
static NR_Port *nr_port_tail;
static int is_same_call(char *call1, char *call2)
{
if (!call1 || !call2)
return 0;
for (; *call1 && *call2; call1++, call2++) {
if (*call1 == '-' || *call2 == '-')
break;
if (tolower(*call1 & 0xff) != tolower(*call2 & 0xff))
return 0;
}
if (!*call1 && !*call2)
return 1;
if (!*call1 && !strcmp(call2, "-0"))
return 1;
if (!*call2 && !strcmp(call1, "-0"))
return 1;
return !strcmp(call1, call2) ? 1 : 0;
}
static NR_Port *nr_port_ptr(char *name)
{
NR_Port *p = nr_ports;
if (name == NULL)
return p;
while (p != NULL) {
if (p->Name != NULL && strcasecmp(name, p->Name) == 0)
return p;
p = p->Next;
}
return NULL;
}
char *nr_config_get_next(char *name)
{
NR_Port *p;
if (nr_ports == NULL)
return NULL;
if (name == NULL)
return nr_ports->Name;
if ((p = nr_port_ptr(name)) == NULL)
return NULL;
p = p->Next;
if (p == NULL)
return NULL;
return p->Name;
}
char *nr_config_get_name(char *device)
{
NR_Port *p = nr_ports;
while (p != NULL) {
if (p->Device != NULL && strcmp(device, p->Device) == 0)
return p->Name;
p = p->Next;
}
return NULL;
}
char *nr_config_get_addr(char *name)
{
NR_Port *p = nr_port_ptr(name);
if (p == NULL)
return NULL;
return p->Call;
}
char *nr_config_get_dev(char *name)
{
NR_Port *p = nr_port_ptr(name);
if (p == NULL)
return NULL;
return p->Device;
}
char *nr_config_get_port(ax25_address *callsign)
{
NR_Port *p = nr_ports;
ax25_address addr;
while (p != NULL) {
if (p->Call != NULL) {
ax25_aton_entry(p->Call, (char *)&addr);
if (ax25_cmp(callsign, &addr) == 0)
return p->Name;
}
p = p->Next;
}
return NULL;
}
char *nr_config_get_alias(char *name)
{
NR_Port *p = nr_port_ptr(name);
if (p == NULL)
return NULL;
return p->Alias;
}
int nr_config_get_paclen(char *name)
{
NR_Port *p = nr_port_ptr(name);
if (p == NULL)
return 0;
return p->Paclen;
}
char *nr_config_get_desc(char *name)
{
NR_Port *p = nr_port_ptr(name);
if (p == NULL)
return NULL;
return p->Description;
}
static int nr_config_init_port(int fd, int lineno, char *line, const char **ifcalls, const char **ifdevs)
{
NR_Port *p;
char *name, *call, *alias, *paclen, *desc;
const char *dev = NULL;
int found = 0;
name = strtok(line, " \t");
call = strtok(NULL, " \t");
alias = strtok(NULL, " \t");
paclen = strtok(NULL, " \t");
desc = strtok(NULL, "");
if (name == NULL || call == NULL || alias == NULL || paclen == NULL ||
desc == NULL) {
fprintf(stderr, "nrconfig: unable to parse line %d of config file\n", lineno);
return FALSE;
}
for (p = nr_ports; p != NULL; p = p->Next) {
if (p->Name != NULL && strcasecmp(name, p->Name) == 0) {
fprintf(stderr, "nrconfig: duplicate port name %s in line %d of config file\n", name, lineno);
return FALSE;
}
if (p->Call != NULL && is_same_call(call, p->Call)) {
fprintf(stderr, "nrconfig: duplicate callsign %s in line %d of config file\n", call, lineno);
return FALSE;
}
if (p->Alias != NULL && strcasecmp(alias, p->Alias) == 0) {
fprintf(stderr, "nrconfig: duplicate alias %s in line %d of config file\n", alias, lineno);
return FALSE;
}
}
if (atoi(paclen) <= 0) {
fprintf(stderr, "nrconfig: invalid packet size %s in line %d of config file\n", paclen, lineno);
return FALSE;
}
strupr(call);
strupr(alias);
char *cp;
if ((cp = strstr(call, "-0")) != NULL)
*cp = 0;
found = 0;
for (;ifcalls && *ifcalls; ++ifcalls, ++ifdevs) {
if (strcmp(call, *ifcalls) == 0) {
found = 1;
dev = *ifdevs;
break;
}
}
if (!found) {
#if 0 /* None of your business to complain about some port being down... */
fprintf(stderr, "nrconfig: port %s not active\n", name);
#endif
return FALSE;
}
if ((p = (NR_Port *)malloc(sizeof(NR_Port))) == NULL) {
fprintf(stderr, "nrconfig: out of memory!\n");
return FALSE;
}
p->Name = strdup(name);
p->Call = strdup(call);
p->Alias = strdup(alias);
p->Device = strdup(dev);
p->Paclen = atoi(paclen);
p->Description = strdup(desc);
if (nr_ports == NULL)
nr_ports = p;
else
nr_port_tail->Next = p;
nr_port_tail = p;
p->Next = NULL;
return TRUE;
}
int nr_config_load_ports(void)
{
FILE *fp = NULL;
char buffer[256], *s;
int fd = -1, lineno = 1, n = 0, i;
const char **calllist = NULL;
const char **devlist = NULL;
const char **pp;
int callcount = 0;
struct ifreq ifr;
/* Reliable listing of all network ports on Linux
is only available via reading /proc/net/dev ... */
if ((fd = socket(PF_FILE, SOCK_DGRAM, 0)) < 0) {
fprintf(stderr, "nrconfig: unable to open socket (%s)\n", strerror(errno));
goto cleanup;
}
if ((fp = fopen("/proc/net/dev", "r"))) {
/* Two header lines.. */
s = fgets(buffer, sizeof(buffer), fp);
s = fgets(buffer, sizeof(buffer), fp);
/* .. then network interface names */
while (!feof(fp)) {
if (!fgets(buffer, sizeof(buffer), fp))
break;
s = strchr(buffer, ':');
if (s) *s = 0;
s = buffer;
while (isspace(*s & 0xff)) ++s;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, s, IFNAMSIZ-1);
ifr.ifr_name[IFNAMSIZ-1] = 0;
if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
fprintf(stderr, "nrconfig: SIOCGIFHWADDR: %s\n", strerror(errno));
return FALSE;
}
if (ifr.ifr_hwaddr.sa_family != ARPHRD_NETROM)
continue;
/* store found interface callsigns */
/* ax25_ntoa() returns pointer to static buffer */
s = ax25_ntoa((void*)ifr.ifr_hwaddr.sa_data);
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
fprintf(stderr, "nrconfig: SIOCGIFFLAGS: %s\n", strerror(errno));
return FALSE;
}
if (!(ifr.ifr_flags & IFF_UP))
continue;
if ((pp = realloc(calllist, sizeof(char *) * (callcount+2))) == 0)
break;
calllist = pp;
if ((pp = realloc(devlist, sizeof(char *) * (callcount+2))) == 0)
break;
devlist = pp;
if ((calllist[callcount] = strdup(s)) != NULL) {
if ((devlist[callcount] = strdup(ifr.ifr_name)) != NULL) {
++callcount;
calllist[callcount] = NULL;
devlist [callcount] = NULL;
} else {
free((void*)calllist[callcount]);
calllist[callcount] = NULL;
devlist[callcount] = NULL;
}
}
}
fclose(fp);
fp = NULL;
}
if ((fp = fopen(CONF_NRPORTS_FILE, "r")) == NULL) {
fprintf(stderr, "nrconfig: unable to open nrports file %s (%s)\n", CONF_NRPORTS_FILE, strerror(errno));
goto cleanup;
}
while (fp && fgets(buffer, 255, fp)) {
if ((s = strchr(buffer, '\n')))
*s = '\0';
if (strlen(buffer) > 0 && *buffer != '#')
if (nr_config_init_port(fd, lineno, buffer, calllist, devlist))
n++;
lineno++;
}
cleanup:;
if (fd >= 0) close(fd);
if (fp) fclose(fp);
for (i = 0; calllist && calllist[i]; ++i) {
free((void*)calllist[i]);
if (devlist[i] != NULL)
free((void*)devlist[i]);
}
if (calllist) free(calllist);
if (devlist) free(devlist);
if (nr_ports == NULL)
return 0;
return n;
}