forked from wb2osz/direwolf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
igate.c
2459 lines (2029 loc) · 73 KB
/
igate.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// This file is part of Dire Wolf, an amateur radio packet TNC.
//
// Copyright (C) 2013, 2014, 2015, 2016 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/>.
//
/*------------------------------------------------------------------
*
* Module: igate.c
*
* Purpose: IGate client.
*
* Description: Establish connection with a tier 2 IGate server
* and relay packets between RF and Internet.
*
* References: APRS-IS (Automatic Packet Reporting System-Internet Service)
* http://www.aprs-is.net/Default.aspx
*
* APRS iGate properties
* http://wiki.ham.fi/APRS_iGate_properties
*
* Notes to iGate developers
* https://github.com/hessu/aprsc/blob/master/doc/IGATE-HINTS.md#igates-dropping-duplicate-packets-unnecessarily
*
* SATgate mode.
* http://www.tapr.org/pipermail/aprssig/2016-January/045283.html
*
*---------------------------------------------------------------*/
/*------------------------------------------------------------------
*
* From http://windows.microsoft.com/en-us/windows7/ipv6-frequently-asked-questions
*
* How can I enable IPv6?
* Follow these steps:
*
* Open Network Connections by clicking the Start button, and then clicking
* Control Panel. In the search box, type adapter, and then, under Network
* and Sharing Center, click View network connections.
*
* Right-click your network connection, and then click Properties.
* If you're prompted for an administrator password or confirmation, type
* the password or provide confirmation.
*
* Select the check box next to Internet Protocol Version 6 (TCP/IPv6).
*
*---------------------------------------------------------------*/
/*
* Native Windows: Use the Winsock interface.
* Linux: Use the BSD socket interface.
* Cygwin: Can use either one.
*/
#include "direwolf.h" // Sets _WIN32_WINNT for XP API level needed by ws2tcpip.h
#if __WIN32__
/* The goal is to support Windows XP and later. */
#include <winsock2.h>
#include <ws2tcpip.h> // _WIN32_WINNT must be set to 0x0501 before including this
#else
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#endif
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include "direwolf.h"
#include "ax25_pad.h"
#include "textcolor.h"
#include "version.h"
#include "digipeater.h"
#include "tq.h"
#include "igate.h"
#include "latlong.h"
#include "pfilter.h"
#include "dtime_now.h"
#include "mheard.h"
#if __WIN32__
static unsigned __stdcall connnect_thread (void *arg);
static unsigned __stdcall igate_recv_thread (void *arg);
static unsigned __stdcall satgate_delay_thread (void *arg);
#else
static void * connnect_thread (void *arg);
static void * igate_recv_thread (void *arg);
static void * satgate_delay_thread (void *arg);
#endif
static dw_mutex_t dp_mutex; /* Critical section for delayed packet queue. */
static packet_t dp_queue_head;
static void satgate_delay_packet (packet_t pp, int chan);
static void send_packet_to_server (packet_t pp, int chan);
static void send_msg_to_server (const char *msg, int msg_len);
static void maybe_xmit_packet_from_igate (char *message, int chan);
static void rx_to_ig_init (void);
static void rx_to_ig_remember (packet_t pp);
static int rx_to_ig_allow (packet_t pp);
static void ig_to_tx_init (void);
static int ig_to_tx_allow (packet_t pp, int chan);
/*
* File descriptor for socket to IGate server.
* Set to -1 if not connected.
* (Don't use SOCKET type because it is unsigned.)
*/
static volatile int igate_sock = -1;
/*
* After connecting to server, we want to make sure
* that the login sequence is sent first.
* This is set to true after the login is complete.
*/
static volatile int ok_to_send = 0;
/*
* Convert Internet address to text.
* Can't use InetNtop because it is supported only on Windows Vista and later.
*/
static char * ia_to_text (int Family, void * pAddr, char * pStringBuf, size_t StringBufSize)
{
struct sockaddr_in *sa4;
struct sockaddr_in6 *sa6;
switch (Family) {
case AF_INET:
sa4 = (struct sockaddr_in *)pAddr;
#if __WIN32__
snprintf (pStringBuf, StringBufSize, "%d.%d.%d.%d", sa4->sin_addr.S_un.S_un_b.s_b1,
sa4->sin_addr.S_un.S_un_b.s_b2,
sa4->sin_addr.S_un.S_un_b.s_b3,
sa4->sin_addr.S_un.S_un_b.s_b4);
#else
inet_ntop (AF_INET, &(sa4->sin_addr), pStringBuf, StringBufSize);
#endif
break;
case AF_INET6:
sa6 = (struct sockaddr_in6 *)pAddr;
#if __WIN32__
snprintf (pStringBuf, StringBufSize, "%x:%x:%x:%x:%x:%x:%x:%x",
ntohs(((unsigned short *)(&(sa6->sin6_addr)))[0]),
ntohs(((unsigned short *)(&(sa6->sin6_addr)))[1]),
ntohs(((unsigned short *)(&(sa6->sin6_addr)))[2]),
ntohs(((unsigned short *)(&(sa6->sin6_addr)))[3]),
ntohs(((unsigned short *)(&(sa6->sin6_addr)))[4]),
ntohs(((unsigned short *)(&(sa6->sin6_addr)))[5]),
ntohs(((unsigned short *)(&(sa6->sin6_addr)))[6]),
ntohs(((unsigned short *)(&(sa6->sin6_addr)))[7]));
#else
inet_ntop (AF_INET6, &(sa6->sin6_addr), pStringBuf, StringBufSize);
#endif
break;
default:
snprintf (pStringBuf, StringBufSize, "Invalid address family!");
}
//assert (strlen(pStringBuf) < StringBufSize);
return pStringBuf;
}
#if ITEST
/* For unit testing. */
int main (int argc, char *argv[])
{
struct audio_s audio_config;
struct igate_config_s igate_config;
struct digi_config_s digi_config;
packet_t pp;
memset (&audio_config, 0, sizeof(audio_config));
audio_config.adev[0].num_chans = 2;
strlcpy (audio_config.achan[0].mycall, "WB2OSZ-1", sizeof(audio_config.achan[0].mycall));
strlcpy (audio_config.achan[1].mycall, "WB2OSZ-2", sizeof(audio_config.achan[0].mycall));
memset (&igate_config, 0, sizeof(igate_config));
strlcpy (igate_config.t2_server_name, "localhost", sizeof(igate_config.t2_server_name));
igate_config.t2_server_port = 14580;
strlcpy (igate_config.t2_login, "WB2OSZ-JL", sizeof(igate_config.t2_login));
strlcpy (igate_config.t2_passcode, "-1", sizeof(igate_config.t2_passcode));
igate_config.t2_filter = strdup ("r/1/2/3");
igate_config.tx_chan = 0;
strlcpy (igate_config.tx_via, ",WIDE2-1", sizeof(igate_config.tx_via));
igate_config.tx_limit_1 = 3;
igate_config.tx_limit_5 = 5;
memset (&digi_config, 0, sizeof(digi_config));
igate_init(&igate_config, &digi_config);
while (igate_sock == -1) {
SLEEP_SEC(1);
}
SLEEP_SEC (2);
pp = ax25_from_text ("A>B,C,D:Ztest message 1", 0);
igate_send_rec_packet (0, pp);
ax25_delete (pp);
SLEEP_SEC (2);
pp = ax25_from_text ("A>B,C,D:Ztest message 2", 0);
igate_send_rec_packet (0, pp);
ax25_delete (pp);
SLEEP_SEC (2);
pp = ax25_from_text ("A>B,C,D:Ztest message 2", 0); /* Should suppress duplicate. */
igate_send_rec_packet (0, pp);
ax25_delete (pp);
SLEEP_SEC (2);
pp = ax25_from_text ("A>B,TCPIP,D:ZShould drop this due to path", 0);
igate_send_rec_packet (0, pp);
ax25_delete (pp);
SLEEP_SEC (2);
pp = ax25_from_text ("A>B,C,D:?Should drop query", 0);
igate_send_rec_packet (0, pp);
ax25_delete (pp);
SLEEP_SEC (5);
pp = ax25_from_text ("A>B,C,D:}E>F,G*,H:Zthird party stuff", 0);
igate_send_rec_packet (0, pp);
ax25_delete (pp);
#if 1
while (1) {
SLEEP_SEC (20);
text_color_set(DW_COLOR_INFO);
dw_printf ("Send received packet\n");
send_msg_to_server ("W1ABC>APRS:?", strlen("W1ABC>APRS:?");
}
#endif
return 0;
}
#endif
/*
* Global stuff (to this file)
*
* These are set by init function and need to
* be kept around in case connection is lost and
* we need to reestablish the connection later.
*/
static struct audio_s *save_audio_config_p;
static struct igate_config_s *save_igate_config_p;
static struct digi_config_s *save_digi_config_p;
static int s_debug;
/*
* Statistics for IGate function.
* Note that the RF related counters are just a subset of what is happening on radio channels.
*
* TODO: should have debug option to print these occasionally.
*/
static int stats_failed_connect; /* Number of times we tried to connect to */
/* a server and failed. A small number is not */
/* a bad thing. Each name should have a bunch */
/* of addresses for load balancing and */
/* redundancy. */
static int stats_connects; /* Number of successful connects to a server. */
/* Normally you'd expect this to be 1. */
/* Could be larger if one disappears and we */
/* try again to find a different one. */
static time_t stats_connect_at; /* Most recent time connection was established. */
/* can be used to determine elapsed connect time. */
static int stats_rf_recv_packets; /* Number of candidate packets from the radio. */
/* This is not the total number of AX.25 frames received */
/* over the radio; only APRS packets get this far. */
static int stats_uplink_packets; /* Number of packets passed along to the IGate */
/* server after filtering. */
static int stats_uplink_bytes; /* Total number of bytes sent to IGate server */
/* including login, packets, and hearbeats. */
static int stats_downlink_bytes; /* Total number of bytes from IGate server including */
/* packets, heartbeats, other messages. */
static int stats_downlink_packets; /* Number of packets from IGate server for possible transmission. */
/* Fewer might be transmitted due to filtering or rate limiting. */
static int stats_rf_xmit_packets; /* Number of packets passed along to radio, for the IGate function, */
/* after filtering, rate limiting, or other restrictions. */
/* Number of packets transmitted for beacons, digipeating, */
/* or client applications are not included here. */
static int stats_msg_cnt; /* Number of "messages" transmitted. Subset of above. */
/* A "message" has the data type indicator of ":" and it is */
/* not the special case of telemetry metadata. */
/*
* Make some of these available for IGate statistics beacon like
*
* WB2OSZ>APDW14,WIDE1-1:<IGATE,MSG_CNT=2,PKT_CNT=0,DIR_CNT=10,LOC_CNT=35,RF_CNT=45
*
* MSG_CNT is only "messages." From original spec.
* PKT_CNT is other (non-message) packets. Followed precedent of APRSISCE32.
*/
int igate_get_msg_cnt (void) {
return (stats_msg_cnt);
}
int igate_get_pkt_cnt (void) {
return (stats_rf_xmit_packets - stats_msg_cnt);
}
int igate_get_upl_cnt (void) {
return (stats_uplink_packets);
}
int igate_get_dnl_cnt (void) {
return (stats_downlink_packets);
}
/*-------------------------------------------------------------------
*
* Name: igate_init
*
* Purpose: One time initialization when main application starts up.
*
* Inputs: p_audio_config - Audio channel configuration. All we care about is:
* - Number of radio channels.
* - Radio call and SSID for each channel.
*
* p_igate_config - IGate configuration.
*
* p_digi_config - Digipeater configuration.
* All we care about here is the packet filtering options.
*
* debug_level - 0 print packets FROM APRS-IS,
* establishing connection with sergver, and
* and anything rejected by client side filtering.
* 1 plus packets sent TO server or why not.
* 2 plus duplicate detection overview.
* 3 plus duplicate detection details.
*
* Description: This starts two threads:
*
* * to establish and maintain a connection to the server.
* * to listen for packets from the server.
*
*--------------------------------------------------------------------*/
void igate_init (struct audio_s *p_audio_config, struct igate_config_s *p_igate_config, struct digi_config_s *p_digi_config, int debug_level)
{
#if __WIN32__
HANDLE connnect_th;
HANDLE cmd_recv_th;
HANDLE satgate_delay_th;
#else
pthread_t connect_listen_tid;
pthread_t cmd_listen_tid;
pthread_t satgate_delay_tid;
int e;
#endif
s_debug = debug_level;
dp_queue_head = NULL;
#if DEBUGx
text_color_set(DW_COLOR_DEBUG);
dw_printf ("igate_init ( %s, %d, %s, %s, %s )\n",
p_igate_config->t2_server_name,
p_igate_config->t2_server_port,
p_igate_config->t2_login,
p_igate_config->t2_passcode,
p_igate_config->t2_filter);
#endif
/*
* Save the arguments for later use.
*/
save_audio_config_p = p_audio_config;
save_igate_config_p = p_igate_config;
save_digi_config_p = p_digi_config;
stats_failed_connect = 0;
stats_connects = 0;
stats_connect_at = 0;
stats_rf_recv_packets = 0;
stats_uplink_packets = 0;
stats_uplink_bytes = 0;
stats_downlink_bytes = 0;
stats_downlink_packets = 0;
stats_rf_xmit_packets = 0;
stats_msg_cnt = 0;
rx_to_ig_init ();
ig_to_tx_init ();
/*
* Continue only if we have server name, login, and passcode.
*/
if (strlen(p_igate_config->t2_server_name) == 0 ||
strlen(p_igate_config->t2_login) == 0 ||
strlen(p_igate_config->t2_passcode) == 0) {
return;
}
/*
* This connects to the server and sets igate_sock.
* It also sends periodic messages to say I'm still alive.
*/
#if __WIN32__
connnect_th = (HANDLE)_beginthreadex (NULL, 0, connnect_thread, (void *)NULL, 0, NULL);
if (connnect_th == NULL) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Internal error: Could not create IGate connection thread\n");
return;
}
#else
e = pthread_create (&connect_listen_tid, NULL, connnect_thread, (void *)NULL);
if (e != 0) {
text_color_set(DW_COLOR_ERROR);
perror("Internal error: Could not create IGate connection thread");
return;
}
#endif
/*
* This reads messages from client when igate_sock is valid.
*/
#if __WIN32__
cmd_recv_th = (HANDLE)_beginthreadex (NULL, 0, igate_recv_thread, NULL, 0, NULL);
if (cmd_recv_th == NULL) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Internal error: Could not create IGate reading thread\n");
return;
}
#else
e = pthread_create (&cmd_listen_tid, NULL, igate_recv_thread, NULL);
if (e != 0) {
text_color_set(DW_COLOR_ERROR);
perror("Internal error: Could not create IGate reading thread");
return;
}
#endif
/*
* This lets delayed packets continue after specified amount of time.
*/
if (p_igate_config->satgate_delay > 0) {
#if __WIN32__
satgate_delay_th = (HANDLE)_beginthreadex (NULL, 0, satgate_delay_thread, NULL, 0, NULL);
if (satgate_delay_th == NULL) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Internal error: Could not create SATgate delay thread\n");
return;
}
#else
e = pthread_create (&satgate_delay_tid, NULL, satgate_delay_thread, NULL);
if (e != 0) {
text_color_set(DW_COLOR_ERROR);
perror("Internal error: Could not create SATgate delay thread");
return;
}
#endif
dw_mutex_init(&dp_mutex);
}
} /* end igate_init */
/*-------------------------------------------------------------------
*
* Name: connnect_thread
*
* Purpose: Establish connection with IGate server.
* Send periodic heartbeat to keep keep connection active.
* Reconnect if something goes wrong and we got disconnected.
*
* Inputs: arg - Not used.
*
* Outputs: igate_sock - File descriptor for communicating with client app.
* Will be -1 if not connected.
*
* References: TCP client example.
* http://msdn.microsoft.com/en-us/library/windows/desktop/ms737591(v=vs.85).aspx
*
* Linux IPv6 HOWTO
* http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/
*
*--------------------------------------------------------------------*/
/*
* Addresses don't get mixed up very well.
* IPv6 always shows up last so we'd probably never
* end up using any of them. Use our own shuffle.
*/
static void shuffle (struct addrinfo *host[], int nhosts)
{
int j, k;
assert (RAND_MAX >= nhosts); /* for % to work right */
if (nhosts < 2) return;
srand (time(NULL));
for (j=0; j<nhosts; j++) {
k = rand() % nhosts;
assert (k >=0 && k<nhosts);
if (j != k) {
struct addrinfo *temp;
temp = host[j]; host[j] = host[k]; host[k] = temp;
}
}
}
#define MAX_HOSTS 50
#if __WIN32__
static unsigned __stdcall connnect_thread (void *arg)
#else
static void * connnect_thread (void *arg)
#endif
{
struct addrinfo hints;
struct addrinfo *ai_head = NULL;
struct addrinfo *ai;
struct addrinfo *hosts[MAX_HOSTS];
int num_hosts, n;
int err;
char server_port_str[12]; /* text form of port number */
char ipaddr_str[46]; /* text form of IP address */
#if __WIN32__
WSADATA wsadata;
#endif
snprintf (server_port_str, sizeof(server_port_str), "%d", save_igate_config_p->t2_server_port);
#if DEBUGx
text_color_set(DW_COLOR_DEBUG);
dw_printf ("DEBUG: igate connect_thread start, port = %d = '%s'\n", save_igate_config_p->t2_server_port, server_port_str);
#endif
#if __WIN32__
err = WSAStartup (MAKEWORD(2,2), &wsadata);
if (err != 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf("WSAStartup failed: %d\n", err);
return (0);
}
if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 2) {
text_color_set(DW_COLOR_ERROR);
dw_printf("Could not find a usable version of Winsock.dll\n");
WSACleanup();
//sleep (1);
return (0);
}
#endif
memset (&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; /* Allow either IPv4 or IPv6. */
// IPv6 is half baked on Windows XP.
// We might need to leave out IPv6 support for Windows version.
// hints.ai_family = AF_INET; /* IPv4 only. */
#if IPV6_ONLY
/* IPv6 addresses always show up at end of list. */
/* Force use of them for testing. */
hints.ai_family = AF_INET6; /* IPv6 only */
#endif
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
/*
* Repeat forever.
*/
while (1) {
/*
* Connect to IGate server if not currently connected.
*/
if (igate_sock == -1) {
SLEEP_SEC (5);
ai_head = NULL;
err = getaddrinfo(save_igate_config_p->t2_server_name, server_port_str, &hints, &ai_head);
if (err != 0) {
text_color_set(DW_COLOR_ERROR);
#if __WIN32__
dw_printf ("Can't get address for IGate server %s, err=%d\n",
save_igate_config_p->t2_server_name, WSAGetLastError());
#else
dw_printf ("Can't get address for IGate server %s, %s\n",
save_igate_config_p->t2_server_name, gai_strerror(err));
#endif
freeaddrinfo(ai_head);
continue;
}
#if DEBUG_DNS
text_color_set(DW_COLOR_DEBUG);
dw_printf ("getaddrinfo returns:\n");
#endif
num_hosts = 0;
for (ai = ai_head; ai != NULL; ai = ai->ai_next) {
#if DEBUG_DNS
text_color_set(DW_COLOR_DEBUG);
ia_to_text (ai->ai_family, ai->ai_addr, ipaddr_str, sizeof(ipaddr_str));
dw_printf (" %s\n", ipaddr_str);
#endif
hosts[num_hosts] = ai;
if (num_hosts < MAX_HOSTS) num_hosts++;
}
// We can get multiple addresses back for the host name.
// These should be somewhat randomized for load balancing.
// It turns out the IPv6 addresses are always at the
// end for both Windows and Linux. We do our own shuffling
// to mix them up better and give IPv6 a chance.
shuffle (hosts, num_hosts);
#if DEBUG_DNS
text_color_set(DW_COLOR_DEBUG);
dw_printf ("after shuffling:\n");
for (n=0; n<num_hosts; n++) {
ia_to_text (hosts[n]->ai_family, hosts[n]->ai_addr, ipaddr_str, sizeof(ipaddr_str));
dw_printf (" %s\n", ipaddr_str);
}
#endif
// Try each address until we find one that is successful.
for (n=0; n<num_hosts; n++) {
#if __WIN32__
SOCKET is;
#else
int is;
#endif
ai = hosts[n];
ia_to_text (ai->ai_family, ai->ai_addr, ipaddr_str, sizeof(ipaddr_str));
is = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
#if __WIN32__
if (is == INVALID_SOCKET) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("IGate: Socket creation failed, err=%d", WSAGetLastError());
WSACleanup();
is = -1;
stats_failed_connect++;
continue;
}
#else
if (err != 0) {
text_color_set(DW_COLOR_INFO);
dw_printf("Connect to IGate server %s (%s) failed.\n\n",
save_igate_config_p->t2_server_name, ipaddr_str);
(void) close (is);
is = -1;
stats_failed_connect++;
continue;
}
#endif
#ifndef DEBUG_DNS
err = connect(is, ai->ai_addr, (int)ai->ai_addrlen);
#if __WIN32__
if (err == SOCKET_ERROR) {
text_color_set(DW_COLOR_INFO);
dw_printf("Connect to IGate server %s (%s) failed.\n\n",
save_igate_config_p->t2_server_name, ipaddr_str);
closesocket (is);
is = -1;
stats_failed_connect++;
continue;
}
// TODO: set TCP_NODELAY?
#else
if (err != 0) {
text_color_set(DW_COLOR_INFO);
dw_printf("Connect to IGate server %s (%s) failed.\n\n",
save_igate_config_p->t2_server_name, ipaddr_str);
(void) close (is);
is = -1;
stats_failed_connect++;
continue;
}
/* IGate documentation says to use it. */
/* Does it really make a difference for this application? */
int flag = 1;
err = setsockopt (is, IPPROTO_TCP, TCP_NODELAY, (void*)(long)(&flag), sizeof(flag));
if (err < 0) {
text_color_set(DW_COLOR_INFO);
dw_printf("setsockopt TCP_NODELAY failed.\n");
}
#endif
stats_connects++;
stats_connect_at = time(NULL);
/* Success. */
text_color_set(DW_COLOR_INFO);
dw_printf("\nNow connected to IGate server %s (%s)\n", save_igate_config_p->t2_server_name, ipaddr_str );
if (strchr(ipaddr_str, ':') != NULL) {
dw_printf("Check server status here http://[%s]:14501\n\n", ipaddr_str);
}
else {
dw_printf("Check server status here http://%s:14501\n\n", ipaddr_str);
}
/*
* Set igate_sock so everyone else can start using it.
* But make the Rx -> Internet messages wait until after login.
*/
ok_to_send = 0;
igate_sock = is;
#endif
break;
}
freeaddrinfo(ai_head);
if (igate_sock != -1) {
char stemp[256];
/*
* Send login message.
* Software name and version must not contain spaces.
*/
SLEEP_SEC(3);
snprintf (stemp, sizeof(stemp), "user %s pass %s vers Dire-Wolf %d.%d",
save_igate_config_p->t2_login, save_igate_config_p->t2_passcode,
MAJOR_VERSION, MINOR_VERSION);
if (save_igate_config_p->t2_filter != NULL) {
strlcat (stemp, " filter ", sizeof(stemp));
strlcat (stemp, save_igate_config_p->t2_filter, sizeof(stemp));
}
send_msg_to_server (stemp, strlen(stemp));
/* Delay until it is ok to start sending packets. */
SLEEP_SEC(7);
ok_to_send = 1;
}
}
/*
* If connected to IGate server, send heartbeat periodically to keep connection active.
*/
if (igate_sock != -1) {
SLEEP_SEC(10);
}
if (igate_sock != -1) {
SLEEP_SEC(10);
}
if (igate_sock != -1) {
SLEEP_SEC(10);
}
if (igate_sock != -1) {
char heartbeat[10];
strlcpy (heartbeat, "#", sizeof(heartbeat));
/* This will close the socket if any error. */
send_msg_to_server (heartbeat, strlen(heartbeat));
}
}
exit(0); // Unreachable but stops compiler from complaining
// about function not returning a value.
} /* end connnect_thread */
/*-------------------------------------------------------------------
*
* Name: igate_send_rec_packet
*
* Purpose: Send a packet to the IGate server
*
* Inputs: chan - Radio channel it was received on.
*
* recv_pp - Pointer to packet object.
* *** CALLER IS RESPONSIBLE FOR DELETING IT! **
*
*
* Description: Send message to IGate Server if connected.
*
* Assumptions: (1) Caller has already verified it is an APRS packet.
* i.e. control = 3 for UI frame, protocol id = 0xf0 for no layer 3
*
* (2) This is being called only for packets received with
* a correct CRC. We don't want to propagate corrupted data.
*
*--------------------------------------------------------------------*/
#define IGATE_MAX_MSG 512 /* "All 'packets' sent to APRS-IS must be in the TNC2 format terminated */
/* by a carriage return, line feed sequence. No line may exceed 512 bytes */
/* including the CR/LF sequence." */
void igate_send_rec_packet (int chan, packet_t recv_pp)
{
packet_t pp;
int n;
unsigned char *pinfo;
int info_len;
if (igate_sock == -1) {
return; /* Silently discard if not connected. */
}
if ( ! ok_to_send) {
return; /* Login not complete. */
}
/* Gather statistics. */
stats_rf_recv_packets++;
/*
* Check for filtering from specified channel to the IGate server.
*
* Should we do this after unwrapping the payload from a third party packet?
* In my experience, third party packets have only been seen coming from IGates.
* In that case, the payload will have TCPIP in the path and it will be dropped.
*/
if (save_digi_config_p->filter_str[chan][MAX_CHANS] != NULL) {
if (pfilter(chan, MAX_CHANS, save_digi_config_p->filter_str[chan][MAX_CHANS], recv_pp, 1) != 1) {
// Is this useful troubleshooting information or just distracting noise?
// Originally this was always printed but there was a request to add a "quiet" option to suppress this.
// version 1.4: Instead, make the default off and activate it only with the debug igate option.
if (s_debug >= 1) {
text_color_set(DW_COLOR_INFO);
dw_printf ("Packet from channel %d to IGate was rejected by filter: %s\n", chan, save_digi_config_p->filter_str[chan][MAX_CHANS]);
}
return;
}
}
/*
* First make a copy of it because it might be modified in place.
*/
pp = ax25_dup (recv_pp);
assert (pp != NULL);
/*
* Third party frames require special handling to unwrap payload.
*/
while (ax25_get_dti(pp) == '}') {
packet_t inner_pp;
for (n = 0; n < ax25_get_num_repeaters(pp); n++) {
char via[AX25_MAX_ADDR_LEN]; /* includes ssid. Do we want to ignore it? */
ax25_get_addr_with_ssid (pp, n + AX25_REPEATER_1, via);
if (strcmp(via, "TCPIP") == 0 ||
strcmp(via, "TCPXX") == 0 ||
strcmp(via, "RFONLY") == 0 ||
strcmp(via, "NOGATE") == 0) {
if (s_debug >= 1) {
text_color_set(DW_COLOR_DEBUG);
dw_printf ("Rx IGate: Do not relay with %s in path.\n", via);
}
ax25_delete (pp);
return;
}
}
if (s_debug >= 1) {
text_color_set(DW_COLOR_DEBUG);
dw_printf ("Rx IGate: Unwrap third party message.\n");
}
inner_pp = ax25_unwrap_third_party(pp);
if (inner_pp == NULL) {
ax25_delete (pp);
return;
}
ax25_delete (pp);
pp = inner_pp;
}
/*
* Do not relay packets with TCPIP, TCPXX, RFONLY, or NOGATE in the via path.
*/
for (n = 0; n < ax25_get_num_repeaters(pp); n++) {
char via[AX25_MAX_ADDR_LEN]; /* includes ssid. Do we want to ignore it? */
ax25_get_addr_with_ssid (pp, n + AX25_REPEATER_1, via);
if (strcmp(via, "TCPIP") == 0 ||
strcmp(via, "TCPXX") == 0 ||
strcmp(via, "RFONLY") == 0 ||
strcmp(via, "NOGATE") == 0) {
if (s_debug >= 1) {
text_color_set(DW_COLOR_DEBUG);
dw_printf ("Rx IGate: Do not relay with %s in path.\n", via);
}
ax25_delete (pp);
return;
}
}
/*
* Do not relay generic query.
*/
if (ax25_get_dti(pp) == '?') {
if (s_debug >= 1) {
text_color_set(DW_COLOR_DEBUG);
dw_printf ("Rx IGate: Do not relay generic query.\n");
}
ax25_delete (pp);
return;
}
/*
* Cut the information part at the first CR or LF.
* This is required because CR/LF is used as record separator when sending to server.