forked from wb2osz/direwolf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdigipeater.c
350 lines (285 loc) · 9.65 KB
/
cdigipeater.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
//
// This file is part of Dire Wolf, an amateur radio packet TNC.
//
// Copyright (C) 2016, 2017 John Langner, WB2OSZ
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
/*------------------------------------------------------------------
*
* Name: cdigipeater.c
*
* Purpose: Act as an digital repeater for connected AX.25 mode.
* Similar digipeater.c is for APRS.
*
*
* Description: Decide whether the specified packet should
* be digipeated. Put my callsign in the digipeater field used.
*
* APRS and connected mode were two split into two
* separate files. Yes, there is duplicate code but they
* are significantly different and I thought it would be
* too confusing to munge them together.
*
* References: The Ax.25 protcol barely mentions digipeaters and
* and doesn't describe how they should work.
*
*------------------------------------------------------------------*/
#define CDIGIPEATER_C
#include "direwolf.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <ctype.h> /* for isdigit, isupper */
#include "regex.h"
#include <sys/unistd.h>
#include "ax25_pad.h"
#include "cdigipeater.h"
#include "textcolor.h"
#include "tq.h"
#include "pfilter.h"
static packet_t cdigipeat_match (int from_chan, packet_t pp, char *mycall_rec, char *mycall_xmit,
int has_alias, regex_t *alias, int to_chan, char *cfilter_str);
/*
* Keep pointer to configuration options.
* Set by cdigipeater_init and used later.
*/
static struct audio_s *save_audio_config_p;
static struct cdigi_config_s *save_cdigi_config_p;
/*
* Maintain count of packets digipeated for each combination of from/to channel.
*/
static int cdigi_count[MAX_CHANS][MAX_CHANS];
int cdigipeater_get_count (int from_chan, int to_chan) {
return (cdigi_count[from_chan][to_chan]);
}
/*------------------------------------------------------------------------------
*
* Name: cdigipeater_init
*
* Purpose: Initialize with stuff from configuration file.
*
* Inputs: p_audio_config - Configuration for audio channels.
*
* p_cdigi_config - Connected Digipeater configuration details.
*
* Outputs: Save pointers to configuration for later use.
*
* Description: Called once at application startup time.
*
*------------------------------------------------------------------------------*/
void cdigipeater_init (struct audio_s *p_audio_config, struct cdigi_config_s *p_cdigi_config)
{
save_audio_config_p = p_audio_config;
save_cdigi_config_p = p_cdigi_config;
}
/*------------------------------------------------------------------------------
*
* Name: cdigipeater
*
* Purpose: Re-transmit packet if it matches the rules.
*
* Inputs: chan - Radio channel where it was received.
*
* pp - Packet object.
*
* Returns: None.
*
*------------------------------------------------------------------------------*/
void cdigipeater (int from_chan, packet_t pp)
{
int to_chan;
if ( from_chan < 0 || from_chan >= MAX_CHANS || ( ! save_audio_config_p->achan[from_chan].valid) ) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("cdigipeater: Did not expect to receive on invalid channel %d.\n", from_chan);
return;
}
/*
* First pass: Look at packets being digipeated to same channel.
*
* There was a reason for two passes for APRS.
* Might not have a benefit here.
*/
for (to_chan=0; to_chan<MAX_CHANS; to_chan++) {
if (save_cdigi_config_p->enabled[from_chan][to_chan]) {
if (to_chan == from_chan) {
packet_t result;
result = cdigipeat_match (from_chan, pp, save_audio_config_p->achan[from_chan].mycall,
save_audio_config_p->achan[to_chan].mycall,
save_cdigi_config_p->has_alias[from_chan][to_chan],
&(save_cdigi_config_p->alias[from_chan][to_chan]), to_chan,
save_cdigi_config_p->cfilter_str[from_chan][to_chan]);
if (result != NULL) {
tq_append (to_chan, TQ_PRIO_0_HI, result);
cdigi_count[from_chan][to_chan]++;
}
}
}
}
/*
* Second pass: Look at packets being digipeated to different channel.
*/
for (to_chan=0; to_chan<MAX_CHANS; to_chan++) {
if (save_cdigi_config_p->enabled[from_chan][to_chan]) {
if (to_chan != from_chan) {
packet_t result;
result = cdigipeat_match (from_chan, pp, save_audio_config_p->achan[from_chan].mycall,
save_audio_config_p->achan[to_chan].mycall,
save_cdigi_config_p->has_alias[from_chan][to_chan],
&(save_cdigi_config_p->alias[from_chan][to_chan]), to_chan,
save_cdigi_config_p->cfilter_str[from_chan][to_chan]);
if (result != NULL) {
tq_append (to_chan, TQ_PRIO_0_HI, result);
cdigi_count[from_chan][to_chan]++;
}
}
}
}
} /* end cdigipeater */
/*------------------------------------------------------------------------------
*
* Name: cdigipeat_match
*
* Purpose: A simple digipeater for connected mode AX.25.
*
* Input: pp - Pointer to a packet object.
*
* mycall_rec - Call of my station, with optional SSID,
* associated with the radio channel where the
* packet was received.
*
* mycall_xmit - Call of my station, with optional SSID,
* associated with the radio channel where the
* packet is to be transmitted. Could be the same as
* mycall_rec or different.
*
* has_alias - True if we have an alias.
*
* alias - Optional compiled pattern for my station aliases.
* Do NOT attempt to use this if 'has_alias' is false.
*
* to_chan - Channel number that we are transmitting to.
*
* cfilter_str - Filter expression string for the from/to channel pair or NULL.
* Note that only a subset of the APRS filters are applicable here.
*
* Returns: Packet object for transmission or NULL.
* The original packet is not modified. The caller is responsible for freeing it.
* We make a copy and return that modified copy!
* This is very important because we could digipeat from one channel to many.
*
* Description: The packet will be digipeated if the next unused digipeater
* field matches one of the following:
*
* - mycall_rec
* - alias list
*
* APRS digipeating drops duplicates within 30 seconds but we don't do that here.
*
*------------------------------------------------------------------------------*/
static packet_t cdigipeat_match (int from_chan, packet_t pp, char *mycall_rec, char *mycall_xmit,
int has_alias, regex_t *alias, int to_chan, char *cfilter_str)
{
int r;
char repeater[AX25_MAX_ADDR_LEN];
int err;
char err_msg[100];
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("cdigipeat_match (from_chan=%d, pp=%p, mycall_rec=%s, mycall_xmit=%s, has_alias=%d, alias=%p, to_chan=%d, cfilter_str=%s\n",
from_chan, pp, mycall_rec, mycall_xmit, has_alias, alias, to_chan, cfilter_str);
#endif
/*
* First check if filtering has been configured.
* Note that we have three different config file filter commands:
*
* FILTER - APRS digipeating and IGate client side.
* Originally this was the only one.
* Should we change it to AFILTER to make it clearer?
* CFILTER - Similar for connected moded digipeater.
* IGFILTER - APRS-IS (IGate) server side - completely diffeent.
* Confusing with similar name but much different idea.
* Maybe this should be renamed to SUBSCRIBE or something like that.
*
* Logically this should come later, after an address/alias match.
* But here we only have to do it once.
*/
if (cfilter_str != NULL) {
if (pfilter(from_chan, to_chan, cfilter_str, pp, 0) != 1) {
return(NULL);
}
}
/*
* Find the first repeater station which doesn't have "has been repeated" set.
*
* r = index of the address position in the frame.
*/
r = ax25_get_first_not_repeated(pp);
if (r < AX25_REPEATER_1) {
return (NULL); // Nothing to do.
}
ax25_get_addr_with_ssid(pp, r, repeater);
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("First unused digipeater is %s\n", repeater);
#endif
/*
* First check for explicit use of my call.
* Note that receive and transmit channels could have different callsigns.
*/
if (strcmp(repeater, mycall_rec) == 0) {
packet_t result;
result = ax25_dup (pp);
assert (result != NULL);
/* If using multiple radio channels, they could have different calls. */
ax25_set_addr (result, r, mycall_xmit);
ax25_set_h (result, r);
return (result);
}
/*
* If we have an alias match, substitute MYCALL.
*/
if (has_alias) {
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("Checking %s for alias match.\n", repeater);
#endif
err = regexec(alias,repeater,0,NULL,0);
if (err == 0) {
packet_t result;
result = ax25_dup (pp);
assert (result != NULL);
ax25_set_addr (result, r, mycall_xmit);
ax25_set_h (result, r);
return (result);
}
else if (err != REG_NOMATCH) {
regerror(err, alias, err_msg, sizeof(err_msg));
text_color_set (DW_COLOR_ERROR);
dw_printf ("%s\n", err_msg);
}
}
else {
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("No alias was specified.\n");
#endif
}
/*
* Don't repeat it if we get here.
*/
return (NULL);
} /* end cdigipeat_match */
/* end cdigipeater.c */