-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfprs_aprsis.c
310 lines (258 loc) · 6.76 KB
/
fprs_aprsis.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
/*
Copyright Jeroen Vreeken ([email protected]), 2016
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 3 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/>.
*/
#include <eth_ar/fprs.h>
#include <poll.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <errno.h>
#include <time.h>
#include <resolv.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#ifdef __linux__
#include <linux/sockios.h>
#endif
#include <dml/dml.h>
#include "dml_config.h"
#include "fprs_aprsis.h"
static int fd_is = -1;
static GIOChannel *io_is = NULL;
static int to_is = 0;
static int from_is = 0;
static char *call = NULL;
static char *is_host = NULL;
static int is_port;
static gboolean aprs_is_cb(GIOChannel *source, GIOCondition condition, gpointer arg);
static bool filter_type_message = false;
static void (*message_cb)(struct fprs_frame *) = NULL;
static int tcp_connect(char *host, int port)
{
struct addrinfo *result;
struct addrinfo *entry;
struct addrinfo hints = { 0 };
int error;
int sock = -1;
char port_str[10];
int tcp_connect_timeout = 1;
sprintf(port_str, "%d", port);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
error = getaddrinfo(host, port_str, &hints, &result);
if (error) {
fprintf(stderr, "getaddrinfo: %s (%s:%s)\n", gai_strerror(error), host, port_str);
res_init();
return -1;
}
for (entry = result; entry; entry = entry->ai_next) {
if (entry->ai_family == AF_INET6) {
bool ipv6 = atoi(dml_config_value("ipv6", NULL, "1"));
if (!ipv6) {
continue;
}
}
int flags;
sock = socket(entry->ai_family, entry->ai_socktype,
entry->ai_protocol);
flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
if (sock >= 0) {
fd_set fdset_tx, fdset_err;
struct timeval tv;
tv.tv_sec = tcp_connect_timeout;
tv.tv_usec = 0;
if (connect(sock, entry->ai_addr, entry->ai_addrlen)) {
int ret;
do {
errno = 0;
FD_ZERO(&fdset_tx);
FD_ZERO(&fdset_err);
FD_SET(sock, &fdset_tx);
FD_SET(sock, &fdset_err);
tv.tv_sec = tcp_connect_timeout;
tv.tv_usec = 0;
ret = select(sock+1, NULL, &fdset_tx, NULL,
&tv);
} while (
ret < 0
&&
(errno == EAGAIN || errno == EINTR ||
errno == EINPROGRESS));
int error = 0;
socklen_t len = sizeof (error);
int retval = getsockopt (sock, SOL_SOCKET, SO_ERROR,
&error, &len );
if (!retval && error) {
close(sock);
sock = -1;
}
}
if (sock >= 0) {
flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags & ~O_NONBLOCK);
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
&(int){1}, sizeof(int));
#ifdef __linux__
/* number of probes which may fail */
setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
&(int){5}, sizeof(int));
/* Idle time before starting with probes */
setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
&(int){10}, sizeof(int));
/* interval between probes */
setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
&(int){2}, sizeof(int));
#endif
break;
}
}
}
freeaddrinfo(result);
return sock;
}
static int aprsis_open(void)
{
printf("Opening connection to APRS-IS\n");
fd_is = tcp_connect(is_host, is_port);
if (fd_is < 0)
return -1;
char loginline[256];
size_t loginline_len = sizeof(loginline) - 1;
fprs2aprs_login(loginline, &loginline_len, call);
if (write(fd_is, loginline, strlen(loginline)) <= 0)
goto err_write;
if (filter_type_message) {
char *filter = "#filter t/m\r\n";
if (write(fd_is, filter, strlen(filter)) <= 0)
goto err_write;
}
io_is = g_io_channel_unix_new(fd_is);
printf("io_is = %p\n", io_is);
to_is = 0;
from_is = 0;
g_io_channel_set_encoding(io_is, NULL, NULL);
g_io_add_watch(io_is, G_IO_IN, aprs_is_cb, fprs_aprsis_init);
return 0;
err_write:
close(fd_is);
fd_is = -1;
printf("Writing to APRS-IS failed\n");
return -1;
}
static void aprs_is_error(void)
{
close(fd_is);
fd_is = -1;
g_source_remove_by_user_data(fprs_aprsis_init);
g_io_channel_unref(io_is);
io_is = NULL;
printf("Lost connection to APRS-IS (to is: %d, from is: %d)\n", to_is, from_is);
/* Fist attempt to reconnect: */
aprsis_open();
}
static gboolean aprs_is_cb(GIOChannel *source, GIOCondition condition, gpointer arg)
{
static char buffer[1000];
static size_t pos;
ssize_t r;
r = read(fd_is, buffer + pos, 1000 - pos);
// printf("------------------------------------- %zd ----- %zd\n", pos, r);
if (r > 0) {
int i;
char *line = buffer;
for (i = 0; i < r + pos; i++) {
if (buffer[i] == '\r' ||
buffer[i] == '\n') {
buffer[i] = 0;
if (strlen(line)) {
// printf("%s\n", line);
struct fprs_frame *frame = aprs2fprs(buffer);
if (frame) {
if (message_cb) {
from_is++;
message_cb(frame);
}
fprs_frame_destroy(frame);
}
}
line = buffer + i + 1;
}
}
pos = i - (line - buffer);
if (line != buffer + i) {
memmove(buffer, line, i - pos);
}
} else {
if (r == 0) {
printf("Read from aprsis failed\n");
aprs_is_error();
}
return FALSE;
}
return TRUE;
}
int fprs_aprsis_frame(struct fprs_frame *frame, uint8_t *from)
{
if (fd_is < 0) {
aprsis_open();
if (fd_is < 0)
return -1;
}
char aprs[256] = { 0 };
size_t aprs_size = 255;
if (fprs2aprs(aprs, &aprs_size, frame, from, call)) {
printf("Could not convert to APRS-IS frame\n");
return -1;
}
printf("%s", aprs);
if (write(fd_is, aprs, strlen(aprs)) <= 0) {
printf("Write to aprsis failed\n");
aprs_is_error();
return -1;
} else {
to_is++;
}
return 0;
}
int fprs_aprsis_init(char *host, int port, char *mycall, bool req_msg, void (*msg_cb)(struct fprs_frame *))
{
if (fd_is >= 0) {
close(fd_is);
fd_is = -1;
g_io_channel_unref(io_is);
io_is = NULL;
g_source_remove_by_user_data(fprs_aprsis_init);
}
free(is_host);
free(call);
is_host = strdup(host);
is_port = port;
call = strdup(mycall);
filter_type_message = req_msg;
message_cb = msg_cb;
int i;
for (i = 0; i < strlen(call); i++) {
call[i] = toupper(call[i]);
}
return aprsis_open();
}