summaryrefslogtreecommitdiffstats
path: root/netrom/netromt.c
blob: 3b37d5f2ed04ea006177fa90227cb1578d1cedf9 (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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <syslog.h>

#include <config.h>

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

#include <sys/socket.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 <netax25/axconfig.h>
#include <netax25/nrconfig.h>

#include "../pathnames.h"
#include "netromd.h"

static int build_header(unsigned char *message)
{
	message[0] = NODES_SIG;
	
	strcpy(message + 1, nr_config_get_alias(NULL));
	strncat(message + 1, "       ", MNEMONIC_LEN - strlen(message + 1));

	return 7;
}

static void build_mine(int s, struct full_sockaddr_ax25 *dest, int dlen, int localval, int pause)
{
	unsigned char message[100];
	char buffer[255], *port, *p;
	FILE *fp;
	int len;

	len = build_header(message);

	if ((fp = fopen(CONF_NRPORTS_FILE, "r")) == NULL) {
		if (logging)
			syslog(LOG_ERR, "netromt: cannot open nrports file\n");
		return;
	}

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

		if (strlen(buffer) == 0 || buffer[0] == '#')
			continue;

		port = strtok(buffer, " \t");

		if (nr_config_get_addr(port) == NULL)
			continue;

		if (strcmp(nr_config_get_addr(port), nr_config_get_addr(NULL)) == 0)
			continue;

		if (ax25_aton_entry(nr_config_get_addr(port), message + len) == -1) {
			if (logging)
				syslog(LOG_ERR, "netromt: invalid callsign in nrports\n");
			fclose(fp);
			return;
		}
		len += CALLSIGN_LEN;

		strcpy(message + len, nr_config_get_alias(port));
		strncat(message + len, "       ", MNEMONIC_LEN - strlen(message + len));
		len += MNEMONIC_LEN;

		ax25_aton_entry(nr_config_get_addr(NULL), message + len);
		len += CALLSIGN_LEN;

		message[len] = localval;
		len += QUALITY_LEN;
	}

	fclose(fp);
	
	if (sendto(s, message, len, 0, (struct sockaddr *)dest, dlen) == -1) {
		if (logging)
			syslog(LOG_ERR, "netromt: sendto: %m");
	}
	
	sleep(pause);
}

static void build_others(int s, int min_obs, struct full_sockaddr_ax25 *dest, int dlen, int port, int pause)
{
	unsigned char message[300];
	FILE *fpnodes, *fpneigh;
	char nodes_buffer[90];
	char neigh_buffer[90];
	char *callsign, *mnemonic, *neighbour;
	int  which, number, quality, neigh_no, obs_count;
	int  olen, len;

	if ((fpnodes = fopen(PROC_NR_NODES_FILE, "r")) == NULL) {
		if (logging)
			syslog(LOG_ERR, "netromt: cannot open %s\n", PROC_NR_NODES_FILE);
		return;
	}

	if ((fpneigh = fopen(PROC_NR_NEIGH_FILE, "r")) == NULL) {
		if (logging)
			syslog(LOG_ERR, "netromt: cannot open %s\n", PROC_NR_NEIGH_FILE);
		fclose(fpnodes);
		return;
	}

	fgets(nodes_buffer, 90, fpnodes);

	do {
		len = olen = build_header(message);

		while (fgets(nodes_buffer, 90, fpnodes) != NULL) {
			callsign  = strtok(nodes_buffer, " ");
			mnemonic  = strtok(NULL, " ");
			which     = atoi(strtok(NULL, " "));
			number    = atoi(strtok(NULL, " "));
			quality   = atoi(strtok(NULL, " "));
			obs_count = atoi(strtok(NULL, " "));
			neigh_no  = atoi(strtok(NULL, " "));
			neighbour = NULL;
			
			if (obs_count < min_obs || quality == 0) continue;

			/* "Blank" mnemonic */
			if (strcmp(mnemonic, "*") == 0)
				mnemonic = "";

			fseek(fpneigh, 0L, SEEK_SET);

			fgets(neigh_buffer, 90, fpneigh);
			
			while (fgets(neigh_buffer, 90, fpneigh) != NULL) {
				if (atoi(strtok(neigh_buffer, " ")) == neigh_no) {
					neighbour = strtok(NULL, " ");
					break;
				}
			}

			if (neighbour == NULL) {
				if (logging)
					syslog(LOG_ERR, "netromt: corruption in nodes/neighbour matching\n");
				continue;
			}

			if (ax25_aton_entry(callsign, message + len) == -1) {
				if (logging)
					syslog(LOG_ERR, "netromt: invalid callsign '%s' in /proc/net/nr_nodes\n", callsign);
				continue;
			}
			len += CALLSIGN_LEN;

			strcpy(message + len, mnemonic);
			strncat(message + len, "       ", MNEMONIC_LEN - strlen(message + len));
			len += MNEMONIC_LEN;

			if (ax25_aton_entry(neighbour, message + len) == -1) {
				if (logging)
					syslog(LOG_ERR, "netromt: invalid callsign '%s' in /proc/net/nr_neigh\n", neighbour);
				len -= (CALLSIGN_LEN + MNEMONIC_LEN);
				continue;
			}
			len += CALLSIGN_LEN;

			message[len] = quality;		
			len += QUALITY_LEN;

			/* No room for another entry? */
			if (len + ROUTE_LEN > NODES_PACLEN)
				break;
		}

		/* Only send it if there is more that just the header */
		if (len > olen) {
			if (sendto(s, message, len, 0, (struct sockaddr *)dest, dlen) == -1) {
				if (logging)
					syslog(LOG_ERR, "netromt: sendto: %m");
			}
			
			sleep(pause);
		}

	/* If the packet was not full then we are done */
	} while (len + ROUTE_LEN > NODES_PACLEN);

	fclose(fpnodes);
	fclose(fpneigh);
}

void transmit_nodes(int localval, int pause)
{
	struct full_sockaddr_ax25 dest;
	struct full_sockaddr_ax25 src;
	int s, dlen, slen;
	char path[25], *addr;
	int i;

	switch (fork()) {
		case 0:
			break;
		case -1:
			if (logging)
				syslog(LOG_ERR, "netromt: fork: %m\n");
			return;
		default:
			return;
	}

	dlen = ax25_aton("NODES", &dest);

	for (i = 0; i < port_count; i++) {

		addr = ax25_config_get_addr(port_list[i].port);

		if (addr == NULL) continue;

		if (debug && logging)
			syslog(LOG_DEBUG, "transmitting NODES broadcast on port %s\n", port_list[i].port);

		sprintf(path, "%s %s", nr_config_get_addr(NULL), addr);

		ax25_aton(path, &src);
		slen = sizeof(struct full_sockaddr_ax25);

		if ((s = socket(AF_AX25, SOCK_DGRAM, NETROM_PID)) < 0) {
			if (logging)
				syslog(LOG_ERR, "netromt: socket: %m");
			continue;
		}

		if (bind(s, (struct sockaddr *)&src, slen) == -1) {
			if (logging)
				syslog(LOG_ERR, "netromt: bind: %m");
			close(s);
			continue;
		}

		build_mine(s, &dest, dlen, localval, pause);

		if (port_list[i].verbose)
			build_others(s, port_list[i].minimum_obs, &dest, dlen, i, pause);

		close(s);
	}

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

	if (ioctl(s, SIOCNRDECOBS, &i) == -1) {
		if (logging)
			syslog(LOG_ERR, "netromt: SIOCNRDECOBS: %m");
		exit(1);
	}

	close(s);

	exit(0);
}