-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdxrfd.cpp
4281 lines (3771 loc) · 150 KB
/
dxrfd.cpp
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
/* by KI4LKF */
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdarg.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <regex.h>
static regex_t preg;
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
/* Required for Binary search trees using C++ STL */
#include <string>
#include <map>
#include <set>
#include <utility>
using namespace std;
#include <pthread.h>
#define VERSION "3.08"
#define OWNER_SIZE 8
#define IP_SIZE 15
#define MAXHOSTNAMELEN 64
#define CALL_SIZE 8
/*
Timeout is 30 seconds.
If after 30 seconds, we have not received the KEEPALIVE,
we drop that station
*/
static short TIMEOUT = 60;
static char STATUS_FILE[FILENAME_MAX + 1];
/* configuration data */
/* Put that in a structure, later */
static char OWNER[OWNER_SIZE + 1];
static char ADMIN[CALL_SIZE + 1];
static char LISTEN_IP[IP_SIZE + 1];
static int LISTEN_PORT = 30001;
static int COMMAND_PORT = 30002;
/* max number of XRF repeaters */
static unsigned int MAX_USERS = 10;
/* max number of dvap/dvtool dongles */
static unsigned int MAX_OTHER_USERS=10;
static bool QSO_DETAILS = false;
static char USERS[FILENAME_MAX + 1];
static char BLOCKS[FILENAME_MAX + 1];
// Input from XRF repeaters
static int srv_sock = -1;
// Input from admin commands
static int cmd_sock = -1;
// Input from dvap, dvtool dongles
static int ref_sock = -1;
/* ACK back to dvap, dvtool users */
static unsigned char REF_ACK[3] = { 3, 96, 0 };
/*** for replying with DASHBOARD information ***/
#define LH_MAX_SIZE 39
typedef map<string, string> dt_lh_type;
static dt_lh_type dt_lh_list;
/* inbound dongles(dvap, dvtool, ...) on port 20001 */
struct inbound
{
/* the callsign of the remote */
char call[CALL_SIZE + 1];
/* if true, packets from this call are dropped */
bool isMute;
time_t connect_time;
/* IP and port of remote */
struct sockaddr_in sin;
/* if countdown expires, the connection is terminated */
short countdown;
/* This user talked on this module */
char mod; /* A B C D E*/
bool is_ref;
char links[5];
/*
The index is the local module: 0=A, 1=B, 2=C, 3=D, 4=E
The value is the remote module
Example: links[1] = 'C'
That means that our local module B is linked to remote module C
The remote system is identified by inbound->call
*/
/* the serial number of the dongle */
char serial[9];
};
/* the Key in this inbound_list map is the unique IP-port address of the remote, example: x.x.x.x-20001 */
typedef map<string, inbound *> inbound_type;
static inbound_type inbound_list;
/*
Just before we send data to dvtool/dvap users,
we save the header here, for re-transmit later
*/
static struct
{
uint32_t s_addr;
unsigned char hdr[58];
} temp_r[5];
/* inbound repeaters on port 30001 */
struct a_user
{
/* callsign of the connected repeater */
char call[CALL_SIZE + 1];
/* if true, packets from this call are dropped */
bool isMute;
/* is this another XRF reflector */
bool is_xrf;
time_t connect_time;
/* The first index identifies the local reflector module
index 0 identifies reflector module A
index 1 identifies reflector module B
index 2 identifies reflector module C
index 3 identifies reflector module D
index 4 identifies reflector module E
The second index identifies the remote repeater band.
from 0 to 3
which is from A...D
Example:
rpt_mods[1][2] = 'C'
rpt_mods[1][3] = 'D'
Explanation:
Reflector module B is linked to repeater module C
Reflector module B is linked to repeater module D
This means that the remote repeater
has linked both repeater bands C and D to our reflector module B
*/
char rpt_mods[5][4];
/* time link was established */
time_t link_time[5][4];
/* IP address and UDP port of connected station */
/* For easy access to the connected station */
struct sockaddr_in sin;
/* if countdown expires, the connection is terminated */
short countdown;
/* This user talked on this module */
char mod; /* A B C D E */
};
/* The BST/map of connected users */
/* The KEY is the unique IP address string of ip[IP_SIZE + 1] */
/* The data is a pointer to struct a_user */
typedef map<string, struct a_user *> a_user_list_type;
static a_user_list_type a_user_list;
/*
Just before we send the data to XRF repeaters
we save the header here for re-transmit later
*/
static struct
{
uint32_t s_addr;
unsigned char hdr[56];
unsigned char old_sid[2];
} temp_x[5];
/* the map of which reflectors we can link to */
typedef map<string,string> call_ip_type;
static call_ip_type call_ip_map;
/*
The BST/set of blocked callsigns.
The KEY is the unique blocked callsign.
There is no data in the set.
Blocked callsigns added by the administrator of the reflector.
*/
typedef set<string> blocks_type;
static blocks_type blocks;
/* socket descriptor set */
static fd_set fdset;
/* 20 ms delay */
static struct timeval tv;
static time_t tNow = 0;
/* timing for XRF users */
static time_t HBinterStart = 0;
/* timing for dvtool/dvap users */
static time_t inboundStart = 0;
/* Variables used by more than one function */
static const short READBUFFER_SIZE = 1024;
static unsigned char readBuffer[READBUFFER_SIZE];
static struct sockaddr_in fromUser;
static struct sockaddr_in fromCmd;
/* dvap, dvtool input buffer */
static unsigned char refbuf[READBUFFER_SIZE];
static struct sockaddr_in fromInbound;
/* log file for tracing data */
static FILE *logfp = NULL;
/* status file */
static FILE *statusfp = NULL;
/*** rcd data ***/
static pthread_t playback_thread;
pthread_attr_t attr;
/* 30 users recording at the same time */
static const unsigned short int MAX_RCD_USERS = 30;
/* each user can record 750 packets = 15 seconds of recorded audio */
static const unsigned short int MAX_RCD_DATA = 750;
struct rcd
{
bool locked;
time_t ts;
struct sockaddr_in sin;
short int recvlen;
unsigned short idx; /*** index into data ***/
unsigned char data[MAX_RCD_DATA][58]; /*** 10 seconds ***/
};
/* key is streamid */
typedef map<string, struct rcd *> rcd_list_type;
static rcd_list_type rcd_list;
static struct rcd *an_rcd;
static rcd_list_type::iterator rcd_pos;
static pair<rcd_list_type::iterator,bool> rcd_insert_pair;
static char an_rcd_streamid[32];
static time_t check_rcd_time = 0;
static bool keep_running = true;
static u_int16_t streamid_raw = 0;
/* The reflector uses these functions only */
static void check_heartbeat();
static void print_users();
static void print_version();
static void mute_users(bool mute);
static bool mute_call(char *call, bool mute);
static void print_blocks();
static void print_links_file();
static void print_links_screen();
static bool get_ip(char *call, char *ip);
static void handle_cmd(char *buf);
static void runit();
static void traceit(const char *fmt,...);
static int read_config(char *);
static int srv_open();
static int cmd_open();
static void sigCatch(int signum);
static int open_users(char *filename);
static int open_blocks(char *filename);
static bool resolve_rmt(char *name, int type, struct sockaddr_in *addr);
static void *playback(void *arg);
/* dvap, dongles, ... */
static void send_heartbeat();
static int ref_open();
static int link_to_ref(char *call);
static int link_to_xrf(char local_mod, char *ref, char remote_mod, char *IP);
static bool resolve_rmt(char *name, int type, struct sockaddr_in *addr)
{
struct addrinfo hints;
struct addrinfo *res;
struct addrinfo *rp;
int rc = 0;
bool found = false;
memset(&hints, 0x00, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = type;
rc = getaddrinfo(name, NULL, &hints, &res);
if (rc != 0)
{
traceit("getaddrinfo return error code %d for [%s]\n", rc, name);
return false;
}
for (rp = res; rp != NULL; rp = rp->ai_next)
{
if ((rp->ai_family == AF_INET) &&
(rp->ai_socktype == type))
{
memcpy(addr, rp->ai_addr, sizeof(struct sockaddr_in));
found = true;
break;
}
}
freeaddrinfo(res);
return found;
}
static int link_to_xrf(char local_mod, char *ref, char remote_mod, char *IP)
{
short counter;
char request[CALL_SIZE + 3];
struct sockaddr_in sin;
a_user_list_type::iterator user_pos;
if (remote_mod == 'X')
remote_mod = ' '; /* unlink request */
else
{
user_pos = a_user_list.find(IP);
if (user_pos != a_user_list.end())
{
/* It already exists */
traceit("Remote reflector [%s] ip=%s already linked, unlink first\n", ref, IP);
return 0;
}
}
/* send the request to the remote XRF reflector */
strcpy(request, OWNER);
request[8] = local_mod;
request[9] = remote_mod;
request[10] = '\0';
memset(&sin,0,sizeof(struct sockaddr_in));
sin.sin_family = AF_INET;
sin.sin_port = htons(LISTEN_PORT);
sin.sin_addr.s_addr = inet_addr(IP);
traceit("Sending request [%s] to reflector %s\n", request, ref);
for (counter = 0; counter < 5; counter++)
sendto(srv_sock, request, CALL_SIZE + 3,
0,(struct sockaddr *)&sin,
sizeof(struct sockaddr_in));
return 0;
}
static int link_to_ref(char *call)
{
char payload[MAXHOSTNAMELEN + 1];
/* IP-port */
char search_value[MAXHOSTNAMELEN + 7];
inbound_type::iterator inbound_pos;
inbound *inbound_ptr;
pair<inbound_type::iterator,bool> inbound_insert_pair;
struct sockaddr_in sin;
unsigned char queryCommand[56];
unsigned short j;
char local_mod = ' ';
char ref[CALL_SIZE + 1];
char remote_mod = ' ';
local_mod = call[0];
if ((local_mod != 'A') && (local_mod != 'B') &&
(local_mod != 'C') && (local_mod != 'D'))
{
traceit("Invalid local module %c for linking\n", local_mod);
return -1;
}
memset(ref, ' ', sizeof(ref));
memcpy(ref, call + 1, 6);
ref[8] = '\0';
/* Is it blocked ? */
if (blocks.find(ref) != blocks.end())
return -1;
if ((memcmp(ref, "REF", 3) != 0) && (memcmp(ref, "XRF", 3) != 0))
{
traceit("XRF or REF only\n");
return -1;
}
if (strcmp(ref, OWNER) == 0)
{
traceit("Can not link to itself\n");
return -1;
}
remote_mod = call[7];
if ((remote_mod != 'A') && (remote_mod != 'B') && (remote_mod != 'C') &&
(remote_mod != 'D') && (remote_mod != 'X'))
{
traceit("Invalid remote module %c\n", remote_mod);
return -1;
}
payload[0] = '\0';
/* get the IP address from database */
if (!get_ip(ref, payload))
return -1;
/* No IP in db? */
if (payload[0] == '\0')
{
traceit("No host or IP address for %s\n", ref);
return -1;
}
if (strcmp(payload, "0.0.0.0") == 0)
{
traceit("Invalid IP in db\n");
return -1;
}
if (memcmp(ref, "XRF", 3) == 0)
{
return link_to_xrf(local_mod, ref, remote_mod, payload);
}
/* Is it already linked? */
sprintf(search_value, "%s-20001", payload);
inbound_pos = inbound_list.find(search_value);
if (inbound_pos != inbound_list.end())
{
inbound_ptr = (inbound *)inbound_pos->second;
if (!inbound_ptr->is_ref)
{
traceit("This connection is not a reflector\n");
return -1;
}
/* update the links */
if (remote_mod == 'X')
remote_mod = ' ';
else
if ((inbound_ptr->links[0] == remote_mod) ||
(inbound_ptr->links[1] == remote_mod) ||
(inbound_ptr->links[2] == remote_mod) ||
(inbound_ptr->links[3] == remote_mod) ||
(inbound_ptr->links[4] == remote_mod))
{
traceit ("Already set or duplicate assignment\n");
return 0;
}
if (local_mod == 'A')
inbound_ptr->links[0] = remote_mod;
else
if (local_mod == 'B')
inbound_ptr->links[1] = remote_mod;
else
if (local_mod == 'C')
inbound_ptr->links[2] = remote_mod;
else
if (local_mod == 'D')
inbound_ptr->links[3] = remote_mod;
else
inbound_ptr->links[4] = remote_mod;
if ((inbound_ptr->links[0] == ' ') &&
(inbound_ptr->links[1] == ' ') &&
(inbound_ptr->links[2] == ' ') &&
(inbound_ptr->links[3] == ' ') &&
(inbound_ptr->links[4] == ' '))
{
/* all links removed, disconnect from remote system */
traceit("All links removed, disconnecting from %s\n", inbound_ptr->call);
refbuf[0] = 5;
refbuf[1] = 0;
refbuf[2] = 24;
refbuf[3] = 0;
refbuf[4] = 0;
sendto(ref_sock,(char *)refbuf,5,0,
(struct sockaddr *)&(inbound_ptr->sin),
sizeof(struct sockaddr_in));
free(inbound_pos->second);
inbound_pos->second = NULL;
inbound_list.erase(inbound_pos);
}
print_links_file();
return 0;
}
if (remote_mod == 'X')
{
traceit("X can not be used, there is no connection to unlink\n");
return -1;
}
if ((inbound_list.size() + 1) > MAX_OTHER_USERS)
{
traceit("Over the MAX_OTHER_USERS limit of %d\n", MAX_OTHER_USERS);
return -1;
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(payload);
sin.sin_port = htons(20001);
/* assume that we will be ok */
inbound_ptr = (inbound *)malloc(sizeof(inbound));
if (inbound_ptr)
{
inbound_ptr->countdown = TIMEOUT;
memcpy((char *)&(inbound_ptr->sin),(char *)&sin, sizeof(struct sockaddr_in));
strcpy(inbound_ptr->call, ref);
time(&(inbound_ptr->connect_time));
inbound_ptr->isMute = false;
inbound_ptr->mod = ' ';
inbound_ptr->is_ref = true;
inbound_ptr->links[0] = inbound_ptr->links[1] = inbound_ptr->links[2] = inbound_ptr->links[3] = inbound_ptr->links[4] = ' ';
if (local_mod == 'A')
inbound_ptr->links[0] = remote_mod;
else
if (local_mod == 'B')
inbound_ptr->links[1] = remote_mod;
else
if (local_mod == 'C')
inbound_ptr->links[2] = remote_mod;
else
if (local_mod == 'D')
inbound_ptr->links[3] = remote_mod;
else
inbound_ptr->links[4] = remote_mod;
strcpy(inbound_ptr->serial, "REF ");
inbound_insert_pair = inbound_list.insert(pair<string, inbound *>(search_value, inbound_ptr));
if (inbound_insert_pair.second)
{
traceit("new CALL=%s,REPEATER,ip=%s, users=%d\n",
inbound_ptr->call,search_value,inbound_list.size() + a_user_list.size());
/* request to connect */
queryCommand[0] = 5;
queryCommand[1] = 0;
queryCommand[2] = 24;
queryCommand[3] = 0;
queryCommand[4] = 1;
for (j = 0; j < 2; j++)
sendto(ref_sock,(char *)queryCommand,5,0,
(struct sockaddr *)&(inbound_ptr->sin),
sizeof(struct sockaddr_in));
print_links_file();
}
else
{
traceit("failed to add CALL=%s,ip=%s\n",inbound_ptr->call,search_value);
free(inbound_ptr);
inbound_ptr = NULL;
return -1;
}
}
else
{
traceit("malloc() failed for call=%s,ip=%s\n",ref,search_value);
return -1;
}
return 0;
}
/* send keepalive to dvap, dongles, ... */
static void send_heartbeat()
{
inbound_type::iterator pos;
inbound_type::iterator temp_pos = inbound_list.end();
inbound *inbound_ptr;
blocks_type::iterator block_pos;
char dropped = ' ';
for (pos = inbound_list.begin(); pos != inbound_list.end(); pos++)
{
inbound_ptr = (inbound *)pos->second;
block_pos = blocks.find(inbound_ptr->call);
if (block_pos == blocks.end()) /* not blocked */
{
sendto(ref_sock,(char *)REF_ACK,3,0,
(struct sockaddr *)&(inbound_ptr->sin),
sizeof(struct sockaddr_in));
if (inbound_ptr->countdown >= 0)
inbound_ptr->countdown --;
if (inbound_ptr->countdown < 0)
{
dropped = 't'; // timeout
temp_pos = pos;
}
}
else
{
dropped = 'b'; // blocked
temp_pos = pos;
}
}
if (temp_pos != inbound_list.end())
{
inbound_ptr = (inbound *)temp_pos->second;
traceit("call=%s %s, removing %s\n",
inbound_ptr->call,
(dropped == 't')?"timeout":"blocked",
temp_pos->first.c_str());
if (dropped == 'b')
{
/* disconnect */
refbuf[0] = 5;
refbuf[1] = 0;
refbuf[2] = 24;
refbuf[3] = 0;
refbuf[4] = 0;
sendto(ref_sock,(char *)refbuf,5,0,
(struct sockaddr *)&(inbound_ptr->sin),
sizeof(struct sockaddr_in));
}
free(temp_pos->second);
temp_pos->second = NULL;
inbound_list.erase(temp_pos);
print_links_file();
}
}
static int open_blocks(char *filename)
{
FILE *fp = NULL;
char inbuf[512];
char *p = NULL;
blocks_type::iterator pos;
short int i;
char *call_ptr;
char call[9];
const char *delim = " \t";
fp = fopen(filename, "r");
if (!fp)
{
traceit("Failed to open file %s\n", filename);
return 1;
}
blocks.clear();
traceit("Reading from file: [%s]\n", filename);
while (fgets(inbuf, 511, fp) != NULL)
{
inbuf[511] = '\0';
p = strchr(inbuf, '\r');
if (p)
*p = '\0';
p = strchr(inbuf, '\n');
if (p)
*p = '\0';
call_ptr = strtok(inbuf, delim);
if (call_ptr)
{
if (strlen(call_ptr) <= 8)
{
memset(call, ' ', 9); call[8] = '\0';
memcpy(call, call_ptr, strlen(call_ptr));
for (i = 0; i < CALL_SIZE; i++)
{
if (call[i] == '_')
call[i] = ' ';
}
blocks.insert(call);
}
}
}
fclose(fp);
traceit("BEGIN listing blocked callsigns...%d calls blocked\n", blocks.size());
for (pos = blocks.begin(); pos != blocks.end(); pos++)
traceit("--->[%s]\n", pos->c_str());
traceit("END listing blocked callsigns\n");
return 0;
}
static int open_users(char *filename)
{
FILE *fp = NULL;
char inbuf[512];
char *p = NULL;
char *gwy_ptr;
char gwy[9];
char *host_ptr;
const char *delim = " \t";
call_ip_type::iterator pos;
fp = fopen(filename, "r");
if (!fp)
{
traceit("Failed to open file %s\n", filename);
return 1;
}
call_ip_map.clear();
traceit("Reading from file: [%s]\n", filename);
while (fgets(inbuf, 511, fp) != NULL)
{
inbuf[511] = '\0';
p = strchr(inbuf, '\r');
if (p)
*p = '\0';
p = strchr(inbuf, '\n');
if (p)
*p = '\0';
gwy_ptr = strtok(inbuf, delim);
host_ptr = strtok(NULL, delim);
if (gwy_ptr && host_ptr)
{
if (strlen(gwy_ptr) <= 8)
{
memset(gwy, ' ', 9); gwy[8] = '\0';
memcpy(gwy, gwy_ptr, strlen(gwy_ptr));
call_ip_map[gwy] = host_ptr;
}
}
}
fclose(fp);
for (pos = call_ip_map.begin(); pos != call_ip_map.end(); pos++)
traceit("[%s],[%s]\n", pos->first.c_str(), pos->second.c_str());
traceit("Loaded %d entries\n", call_ip_map.size());
return 0;
}
/* trace data to the log */
static void traceit(const char *fmt,...)
{
time_t ltime;
struct tm tm;
const short BFSZ = 512;
char buf[BFSZ];
time(<ime);
localtime_r(<ime,&tm);
snprintf(buf,BFSZ - 1,"%02d%02d%02d at %02d:%02d:%02d:",
tm.tm_mon+1,tm.tm_mday,tm.tm_year % 100,
tm.tm_hour,tm.tm_min,tm.tm_sec);
va_list args;
va_start(args,fmt);
vsnprintf(buf + strlen(buf), BFSZ - strlen(buf) -1, fmt, args);
va_end(args);
fprintf(logfp, "%s",buf);
return;
}
/* Search for that host */
static bool get_ip(char *call, char *ip)
{
bool ok = false;
call_ip_type::iterator pos;
struct sockaddr_in a_net_addr;
pos = call_ip_map.find(call);
if (pos == call_ip_map.end())
{
traceit("Callsign [%s] not found in database\n", call);
return false; // not found
}
ok = resolve_rmt((char *)pos->second.c_str(), SOCK_DGRAM, &(a_net_addr));
if (!ok)
{
traceit("Failed to resolve [%s] for callsign [%s]\n",
pos->second.c_str(), call);
return false;
}
if (ip)
strcpy(ip, inet_ntoa(a_net_addr.sin_addr));
return true;
}
/* Process the configuration file */
static int read_config(char *cfgFile)
{
short int valid_params = 11;
short int params = 0;
FILE *cnf = NULL;
char inbuf[1024];
char *p;
char *ptr = NULL;
unsigned short int i;
unsigned short int j;
cnf = fopen(cfgFile, "r");
if (!cnf)
{
traceit("Failed to open file %s\n", cfgFile);
return 1;
}
traceit("Reading file %s\n", cfgFile);
while (fgets(inbuf, 1020, cnf) != NULL)
{
if (strchr(inbuf, '#'))
continue;
p = strchr(inbuf, '\r');
if (p)
*p = '\0';
p = strchr(inbuf, '\n');
if (p)
*p = '\0';
p = strchr(inbuf, '=');
if (!p)
continue;
*p = '\0';
if (strcmp(inbuf,"OWNER") == 0)
{
memset(OWNER,' ', sizeof(OWNER));
OWNER[OWNER_SIZE] = '\0';
ptr = strchr(p + 1, ' ');
if (ptr)
*ptr = '\0';
if (strlen(p + 1) != OWNER_SIZE - 2)
traceit("OWNER value %s invalid, length must be exactly %d\n",
p + 1, OWNER_SIZE - 2);
else
{
memcpy(OWNER, p + 1, OWNER_SIZE - 2);
for (i = 0; i < strlen(OWNER); i++)
OWNER[i] = toupper(OWNER[i]);
if ((memcmp(OWNER, "XRF", 3) != 0) ||
!isdigit(OWNER[3]) ||
!isdigit(OWNER[4]) ||
!isdigit(OWNER[5]))
traceit("Reflector names must be XRFzzz where zzz are digits from 1 thru 9\n");
else
{
traceit("OWNER=%s\n",OWNER);
params ++;
}
}
}
else
if (strcmp(inbuf,"ADMIN") == 0)
{
memset(ADMIN,' ', sizeof(ADMIN));
ADMIN[CALL_SIZE] = '\0';
/* no spaces after the equal sign */
if (p[1] == ' ')
traceit("ADMIN: no spaces after the equal sign\n");
else
{
/* take up to 8 characters, throw away the rest */
p[CALL_SIZE + 1] = '\0';
/* valid length? */
if ((strlen(p + 1) < 3) || (strlen(p + 1) > CALL_SIZE))
traceit("ADMIN value [%s] invalid\n", p + 1);
else
{
memcpy(ADMIN, p + 1, strlen(p + 1));
/* uppercase it */
for (j = 0; j < CALL_SIZE; j++)
ADMIN[j] = toupper(ADMIN[j]);
traceit("ADMIN=[%s]\n",ADMIN);
params ++;
}
}
}
else
if (strcmp(inbuf,"LISTEN_IP") == 0)
{
ptr = strchr(p + 1, ' ');
if (ptr)
*ptr = '\0';
if (strlen(p + 1) < 1)
traceit("LISTEN_IP value %s invalid\n", p + 1);
else
{
memset(LISTEN_IP, '\0', sizeof(LISTEN_IP));
strncpy(LISTEN_IP, p + 1, IP_SIZE);
traceit("LISTEN_IP=%s\n",LISTEN_IP);
params ++;
}
}
else
if (strcmp(inbuf,"LISTEN_PORT") == 0)
{
LISTEN_PORT = atoi(p + 1);
traceit("LISTEN_PORT=%d\n",LISTEN_PORT);
params ++;
}
else
if (strcmp(inbuf,"COMMAND_PORT") == 0)
{
COMMAND_PORT = atoi(p + 1);
traceit("COMMAND_PORT=%d\n",COMMAND_PORT);
params ++;
}
else
if (strcmp(inbuf,"MAX_USERS") == 0)
{
MAX_USERS = atoi(p + 1);
traceit("MAX_USERS=%d\n",MAX_USERS);
params ++;
}
else
if (strcmp(inbuf,"MAX_OTHER_USERS") == 0)
{
MAX_OTHER_USERS = atoi(p + 1);
traceit("MAX_OTHER_USERS=%d\n",MAX_OTHER_USERS);
params ++;
}
else
if (strcmp(inbuf,"STATUS_FILE") == 0)
{
memset(STATUS_FILE, '\0', sizeof(STATUS_FILE));
strncpy(STATUS_FILE, p + 1,FILENAME_MAX);
traceit("STATUS_FILE=%s\n",STATUS_FILE);
params ++;
}
else
if (strcmp(inbuf,"USERS") == 0)
{
memset(USERS, '\0', sizeof(USERS));
strncpy(USERS, p + 1, FILENAME_MAX);
traceit("USERS=%s\n",USERS);
params ++;
}
else
if (strcmp(inbuf,"BLOCKS") == 0)
{
memset(BLOCKS, '\0', sizeof(BLOCKS));
strncpy(BLOCKS, p + 1, FILENAME_MAX);
traceit("BLOCKS=%s\n",BLOCKS);
params ++;
}
else