summaryrefslogtreecommitdiffstats
path: root/netrom/netromr.c
blob: 90e775a66981ee197861a788d3fdd58b901aef18 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>

#include <config.h>

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

#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <net/ethernet.h>

#ifdef HAVE_NETAX25_AX25_H
#include <netax25/ax25.h>
#else
#include <netax25/kernel_ax25.h>
#endif
#ifdef HAVE_NETROM_NETROM_H
#include <netrom/netrom.h>
#else
#include <netax25/kernel_netrom.h>
#endif
#ifdef HAVE_NETROSE_ROSE_H
#include <netrose/rose.h>
#else 
#include <netax25/kernel_rose.h>
#endif

#include <netax25/axlib.h>
#include "../pathnames.h"

#include "netromd.h"

extern int compliant;

static int validmnemonic(char *mnemonic)
{
	if (compliant) {
		if (strspn(mnemonic, "#_&-/ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") == strlen(mnemonic))
			return TRUE;
	} else {
		if (strspn(mnemonic, "#_&-/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") == strlen(mnemonic))
			return TRUE;
	}

	return FALSE;
}

void receive_nodes(unsigned char *buffer, int length, ax25_address *neighbour, int index)
{
	struct nr_route_struct nr_node;
	char neigh_buffer[90], *portcall, *p;
	FILE *fp;
	int s;
	int quality, obs_count, qual, lock;
	char *addr, *callsign, *device;

	nr_node.type   = NETROM_NODE;
	/*nr_node.ndigis = 0;*/

	sprintf(neigh_buffer, "%s/obsolescence_count_initialiser", PROC_NR_SYSCTL_DIR);

	if ((fp = fopen(neigh_buffer, "r")) == NULL) {
		if (logging)
			syslog(LOG_ERR, "netromr: cannot open %s\n", neigh_buffer);
		return;
	}

	fgets(neigh_buffer, 90, fp);
	
	obs_count = atoi(neigh_buffer);

	fclose(fp);

	if ((s = socket(AF_NETROM, SOCK_SEQPACKET, 0)) < 0) {
		if (logging)
			syslog(LOG_ERR, "netromr: socket: %m");
		return;
	}

	if ((fp = fopen(PROC_NR_NEIGH_FILE, "r")) == NULL) {
		if (logging)
			syslog(LOG_ERR, "netromr: cannot open %s\n", PROC_NR_NEIGH_FILE);
		close(s);
		return;
	}

	fgets(neigh_buffer, 90, fp);
	
	portcall = ax25_ntoa(neighbour);

	quality = port_list[index].default_qual;

	while (fgets(neigh_buffer, 90, fp) != NULL) {
		addr     = strtok(neigh_buffer, " ");
		callsign = strtok(NULL, " ");
		device   = strtok(NULL, " ");
		qual     = atoi(strtok(NULL, " "));
		lock     = atoi(strtok(NULL, " "));

		if (strcmp(callsign, portcall) == 0 &&
		    strcmp(device, port_list[index].device) == 0 &&
		    lock == 1) {
			quality = qual;
			break;
		}
	}

	fclose(fp);

	nr_node.callsign    = *neighbour;
	memcpy(nr_node.mnemonic, buffer, MNEMONIC_LEN);
	nr_node.mnemonic[MNEMONIC_LEN] = '\0';

	if ((p = strchr(nr_node.mnemonic, ' ')) != NULL)
		*p = '\0';

	if (!validmnemonic(nr_node.mnemonic)) {
		if (debug && logging)
			syslog(LOG_DEBUG, "rejecting route, invalid mnemonic - %s\n", nr_node.mnemonic);
	} else {
		nr_node.neighbour   = *neighbour;
		strcpy(nr_node.device, port_list[index].device);
		nr_node.quality     = quality;
		nr_node.obs_count   = obs_count;

		if (ioctl(s, SIOCADDRT, &nr_node) == -1) {
			if (logging)
				syslog(LOG_ERR, "netromr: SIOCADDRT: %m");
			close(s);
			return;
		}
	}

	buffer += MNEMONIC_LEN;
	length -= MNEMONIC_LEN;

	while (length >= ROUTE_LEN) {
		/*
		 * Ensure that a) the route is not via me, and
		 *             b) it is better than my minimum acceptable quality
		 */
		if (ax25_cmp(&my_call, (ax25_address *)(buffer + 13)) != 0 &&
		    buffer[20] > port_list[index].worst_qual) {
			memcpy(&nr_node.callsign, buffer + 0, CALLSIGN_LEN);
			memcpy(nr_node.mnemonic,  buffer + 7, MNEMONIC_LEN);
			nr_node.mnemonic[MNEMONIC_LEN] = '\0';

			if ((p = strchr(nr_node.mnemonic, ' ')) != NULL)
				*p = '\0';

			if (!validmnemonic(nr_node.mnemonic)) {
				if (debug && logging)
					syslog(LOG_DEBUG, "rejecting route, invalid mnemonic - %s\n", nr_node.mnemonic);
			} else {
				nr_node.neighbour   = *neighbour;
				strcpy(nr_node.device, port_list[index].device);
				nr_node.quality     = ((quality * buffer[20]) + 128) / 256;
				nr_node.obs_count   = obs_count;

				if (ioctl(s, SIOCADDRT, &nr_node) == -1) {
					if (logging)
						syslog(LOG_ERR, "netromr: SIOCADDRT: %m");
					close(s);
					return;
				}
			}
		}

		buffer += ROUTE_LEN;
		length -= ROUTE_LEN;
	}

	close(s);
}