forked from erikarn/LinBPQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpqaxip.c
3076 lines (2324 loc) · 64.6 KB
/
bpqaxip.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
/*
Copyright 2001-2015 John Wiseman G8BPQ
This file is part of LinBPQ/BPQ32.
LinBPQ/BPQ32 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.
LinBPQ/BPQ32 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 LinBPQ/BPQ32. If not, see http://www.gnu.org/licenses
*/
//
// DLL to provide AXIP support for G8BPQ switch in a
// 32bit environment,
//
// Uses BPQ EXTERNAL interface
//
// Version 1.1 August 2001
//
// Send to all matching entries in map table
// (Mainly for NODES braodcasts to multiple stations)
//
// Version 1.2 September 2001
//
// Support UDP as well as raw IP
// Version 1.3 October 2001
//
// Allow host names as well as numeric IP addresses
//
// Version 1.4 November 2002
//
// Implement keepalive for NAT routers
//
// Version 1.5 December 2004
//
// Implement a "MHEARD" facility
//
// Version 1.6 August 2005
//
// Treat NULL string in Registry as use current directory
// Version 1.7 December 2005
//
// Create a separate thread to open sockets to avoid hang on XP SP2
// Version 1.8 January 2006
//
// Get config file location from Node (will check bpq directory)
// Version 1.9 March 2006
//
// Allow multiple listening UDP ports
// Kick off resolver on EXTRESTART
// Remove redundant DYNAMIC processing
// Version 1.10 October 2006
//
// Add "Minimize to Tray" option
// Write diagnostics to BPQ console window instead of STDOUT
// Version 1.11 October 2007
//
// Sort MHeard and discard last entry if full
// Add Commands to re-read config file and manually add an ARP entry
// Version 1.12 February 2008
//
// Check received length
// Changes for unload of bpq32.dll
// Add Close Driver function
// Dynamic Load of bpq32.dll
// Version 1.13 October 2008
//
// Add Linux-style config of broadcast addressess
// Version 1.13.2 January 2009
//
// Add Start Minimized Option
// Version 1.13.3 February 2009
//
// Save Window positions
// Version 1.13.4 March 2009
//
// Fix loop on config file error
// Version 1.14.1 April 2009
//
// Add option to reject messages if sender is not in ARP Table
// Add option to add received calls to ARP Table
// Version 1.15.1 May 2009
//
// Add IP/TCP option
// Version 1.15.2 August 2009
//
// Extra Debug Output in TCP Mode
// Fix problem if TCP entry was first in table
// Include TCP sessions in MHEARD
// Add T flag to Resolver window fot TCP Sessions
// Set SO_KEEPALIVE and SO_CONDITIONAL_ACCEPT socket options
// Version 1.15.4 August 2009
// Recycle Listening Socket if no connect for 60 mins
// Clear data connections if no data for 60 mins
// Repaint MH Window after clear.
// Version 1.15.5 Spetmber 2010
// Add option to get config from bpq32.dll
// Moved to BPQ32.dll - no separate version number onw.
// October 2010
// Allow multiple axip ports.
// June 2011
// Add IPv6 support
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#include "CHeaders.h"
#ifndef WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include "bpq32.h"
#ifndef LINBPQ
#include "kernelresource.h"
#include <process.h>
#endif
#include <time.h>
#define WSA_ACCEPT WM_USER + 1
#define WSA_DATA WM_USER + 2
#define WSA_CONNECT WM_USER + 3
// Cater for only systems without IPV6_V6ONLY
#ifndef IPV6_V6ONLY
#define IPV6_V6ONLY 0x27
#endif
#ifndef MAXGETHOSTSTRUCT
#define MAXGETHOSTSTRUCT 1024
#endif
#define BUFFLEN 360
// BUFFLEN-4 = L2 POINTER (FOR CLEARING TIMEOUT WHEN ACKMODE USED)
// BUFFLEN-8 = TIMESTAMP
// BUFFLEN-12 = BUFFER ALLOCATED FLAG (ADDR OF ALLOCATING ROUTINE)
#define MAXDATA BUFFLEN-16
#define FEND 0xC0 // KISS CONTROL CODES
#define FESC 0xDB
#define TFEND 0xDC
#define TFESC 0xDD
int ResolveDelay = 0;
extern BOOL StartMinimized;
VOID * zalloc(int len);
int ResetExtDriver(int num);
BOOL ProcessConfig();
VOID FreeConfig();
extern char * PortConfig[35];
extern UCHAR BPQDirectory[];
extern int OffsetH, OffsetW;
static void ResolveNames(struct AXIPPORTINFO * PORT);
void OpenSockets(struct AXIPPORTINFO * PORT);
void CloseSockets();
static int CONVFROMAX25(char * incall, char * outcall);
void CreateMHWindow(struct AXIPPORTINFO * PORT);
int Update_MH_List(struct AXIPPORTINFO * PORT, UCHAR * ipad, char * call, char proto, short port, BOOL IPv6);
int Update_MH_KeepAlive(struct AXIPPORTINFO * PORT, struct in_addr ipad, char proto, short port);
unsigned short int compute_crc(unsigned char *buf,int l);
unsigned int find_arp(unsigned char * call);
BOOL add_arp_entry(struct AXIPPORTINFO * PORT, unsigned char * call, UCHAR * ip, int len, int port,unsigned char * name,
int keepalive, BOOL BCFlag, BOOL AutoAdded, int TCPMode, int SourcePort, BOOL IPv6);
BOOL add_bc_entry(struct AXIPPORTINFO * PORT, unsigned char * call, int len);
BOOL convtoax25(unsigned char * callsign, unsigned char * ax25call, int * calllen);
static BOOL ReadConfigFile(int Port);
static int ProcessLine(char * buf, struct AXIPPORTINFO * PORT);
int CheckKeepalives(struct AXIPPORTINFO * PORT);
BOOL CopyScreentoBuffer(char * buff, struct AXIPPORTINFO * PORT);
int DumpFrameInHex(unsigned char * msg, int len);
VOID SendFrame(struct AXIPPORTINFO * PORT, struct arp_table_entry * arp_table, UCHAR * buff, int txlen);
BOOL CheckSourceisResolvable(struct AXIPPORTINFO * PORT, char * call, int Port, VOID * rxaddr);
int DataSocket_Read(struct arp_table_entry * sockptr, SOCKET sock);
int GetMessageFromBuffer(struct AXIPPORTINFO * PORT, char * Buffer);
int KissEncode(UCHAR * inbuff, UCHAR * outbuff, int len);
int KissDecode(UCHAR * inbuff, int len);
int Socket_Accept(int SocketId);
int Socket_Connect(int SocketId, int Error);
int Socket_Data(int sock, int error, int eventcode);
VOID TCPConnectThread(struct arp_table_entry * arp);
VOID __cdecl Debugprintf(const char * format, ...);
VOID __cdecl Consoleprintf(const char * format, ...);
BOOL OpenListeningSocket(struct AXIPPORTINFO * PORT, struct arp_table_entry * arp);
VOID Format_Addr(unsigned char * Addr, char * Output, BOOL IPV6);
static void CreateResolverWindow(struct AXIPPORTINFO * PORT);
VOID SaveMDIWindowPos(HWND hWnd, char * RegKey, char * Value, BOOL Minimized);
union
{
struct sockaddr_in sinx;
struct sockaddr_in6 sinx6;
} sinx;
/*
union
{
struct sockaddr_in destaddr;
struct sockaddr_in6 destaddr6;
} destaddr;
*/
#define IP_AXIP 93 // IP Protocol for AXIP
#pragma pack(1)
struct iphdr {
// unsigned int version:4; // Version of IP
// unsigned int h_len:4; // length of the header
unsigned char h_lenvers; // Version + length of the header
unsigned char tos; // Type of service
unsigned short total_len; // total length of the packet
unsigned short ident; // unique identifier
unsigned short frag_and_flags; // flags
unsigned char ttl;
unsigned char proto; // protocol (TCP, UDP etc)
unsigned short checksum; // IP checksum
unsigned int sourceIP;
unsigned int destIP;
};
#pragma pack()
#define TCPMaster 1
#define TCPSlave 2
#define TCPListening 1
#define TCPConnecting 2
#define TCPConnected 4
#ifndef LINBPQ
LOGFONT LFTTYFONT ;
extern HFONT hFont ;
RECT ResRect;
RECT MHRect;
extern HKEY REGTREE;
extern HWND ClientWnd, FrameWnd;
extern HMENU hMainFrameMenu, hBaseMenu, hWndMenu;
extern HBRUSH bgBrush;
#endif
//struct tagMSG Msg;
//char buf[MAXGETHOSTSTRUCT];
int addrlen6 = sizeof(struct sockaddr_in6);
int addrlen = sizeof(struct sockaddr_in);
extern unsigned short CRCTAB[];
unsigned int AXIPInst = 0;
DWORD n;
struct AXIPPORTINFO * Portlist[33];
int InitAXIP(int Port);
int CurrentResEntries;
static char ConfigClassName[]="CONFIG";
HANDLE hInstance;
VOID SaveAXIPWindowPos(int port)
{
#ifndef LINBPQ
struct AXIPPORTINFO * PORT;
char Key[80];
PORT = Portlist[port];
if (PORT == NULL)
return;
sprintf(Key, "PACTOR\\PORT%d", port);
SaveMDIWindowPos(PORT->hMHWnd, Key, "MHSize", PORT->MHMinimized);
SaveMDIWindowPos(PORT->hResWnd, Key, "ResSize", PORT->ResMinimized);
#endif
return;
}
static int ExtProc(int fn, int port,unsigned char * buff)
{
struct iphdr * iphdrptr;
int len,txlen=0,err,index,digiptr,i;
unsigned short int crc;
char rxbuff[500];
char axcall[7];
char errmsg[100];
union
{
struct sockaddr_in rxaddr;
struct sockaddr_in6 rxaddr6;
} RXaddr;
struct AXIPPORTINFO * PORT = Portlist[port];
switch (fn)
{
case 1: // poll
//
// Check Keepalive timers
//
time(&PORT->ltime);
if (PORT->ltime-PORT->lasttime >9 )
{
PORT->lasttime=PORT->ltime;
CheckKeepalives(PORT);
}
if (PORT->needip)
{
len = recvfrom(PORT->sock,rxbuff,500,0,(struct sockaddr *)&RXaddr.rxaddr,&addrlen);
if (len == -1)
{
err = WSAGetLastError();
}
else
{
iphdrptr=(struct iphdr *)&rxbuff;
if (len == ntohs(iphdrptr->total_len))
{
len-=20; // IP HEADER
if (memcmp(&rxbuff[20], "Keepalive", 9) == 0 )
{
if (PORT->MHEnabled)
Update_MH_KeepAlive(PORT, RXaddr.rxaddr.sin_addr,'I',93);
return 0;
}
crc = compute_crc(&rxbuff[20], len);
if (crc == 0xf0b8) // Good CRC
{
len-=2; // Remove CRC
if (len > MAXDATA)
{
sprintf(errmsg,"BPQAXIP Invalid Msg Len=%d Source=%s",len,inet_ntoa(RXaddr.rxaddr.sin_addr));
OutputDebugString(errmsg);
DumpFrameInHex(&rxbuff[20], len);
return 0;
}
memcpy(&buff[7],&rxbuff[20],len);
len+=7;
PutLengthinBuffer(buff, len); // Neded for arm5 portability
// buff[5]=(len & 0xff);
// buff[6]=(len >> 8);
//
// Do MH Proccessing if enabled
//
if (PORT->MHEnabled)
Update_MH_List(PORT, (UCHAR *)&RXaddr.rxaddr.sin_addr.s_addr, &buff[14], 'I', 93, 0);
if (PORT->Checkifcanreply)
{
char call[7];
memcpy(call, &buff[14], 7);
call[6] &= 0x7e; // Mask End of Address bit
if (CheckSourceisResolvable(PORT, call, 0, &RXaddr))
return 1;
else
// Can't reply. If AutoConfig is set, add to table and accept, else reject
if (PORT->AutoAddARP)
return add_arp_entry(PORT, call, (UCHAR *)&RXaddr.rxaddr.sin_addr.s_addr, 7, 0, inet_ntoa(RXaddr.rxaddr.sin_addr), 0, TRUE, TRUE, 0, 0, FALSE);
else
{
char From[10];
From[ConvFromAX25(call, From)] = 0;
Debugprintf("AXIP Packet from %s dropped - can't reply", From);
return 0;
}
}
else
return(1);
}
//
// CRC Error
//
sprintf(errmsg,"BPQAXIP Invalid CRC=%d Source=%s",crc,inet_ntoa(RXaddr.rxaddr.sin_addr));
OutputDebugString(errmsg);
return (0);
}
//
// Bad Length
//
return (0);
}
}
for (i=0;i<PORT->NumberofUDPPorts;i++)
{
if (PORT->IPv6[i])
len = recvfrom(PORT->udpsock[i],rxbuff,500,0,(struct sockaddr *)&RXaddr.rxaddr, &addrlen6);
else
len = recvfrom(PORT->udpsock[i],rxbuff,500,0,(struct sockaddr *)&RXaddr.rxaddr, &addrlen);
if (len == -1)
{
err = WSAGetLastError();
}
else
{
if (memcmp(rxbuff, "Keepalive", 9) == 0 )
{
if (PORT->MHEnabled)
Update_MH_KeepAlive(PORT, RXaddr.rxaddr.sin_addr, 'U', PORT->udpport[i]);
continue;
}
crc = compute_crc(&rxbuff[0], len);
if (crc == 0xf0b8) // Good CRC
{
len-=2; // Remove CRC
if (len > MAXDATA)
{
sprintf(errmsg,"BPQAXIP Invalid Msg Len=%d Source=%s Port %d",len,inet_ntoa(RXaddr.rxaddr.sin_addr),PORT->udpport[i]);
OutputDebugString(errmsg);
DumpFrameInHex(&rxbuff[0], len);
return 0;
}
memcpy(&buff[7],&rxbuff[0],len);
len+=7;
PutLengthinBuffer(buff, len);
//
// Do MH Proccessing if enabled
//
if (PORT->MHEnabled)
if (PORT->IPv6[i])
Update_MH_List(PORT, (UCHAR *)&RXaddr.rxaddr6.sin6_addr, &buff[14], 'U', PORT->udpport[i], TRUE);
else
Update_MH_List(PORT, (UCHAR *)&RXaddr.rxaddr.sin_addr.s_addr, &buff[14], 'U', PORT->udpport[i], FALSE);
if (PORT->Checkifcanreply)
{
char call[7];
memcpy(call, &buff[14], 7);
call[6] &= 0x7e; // Mask End of Address bit
if (CheckSourceisResolvable(PORT, call, htons(RXaddr.rxaddr.sin_port), &RXaddr))
return 1;
else
{
// Can't reply. If AutoConfig is set, add to table and accept, else reject
if (PORT->AutoAddARP)
if (PORT->IPv6[i])
{
char Addr[80];
Format_Addr((UCHAR *)&RXaddr.rxaddr6.sin6_addr, Addr, TRUE);
return add_arp_entry(PORT, call, (UCHAR *)&RXaddr.rxaddr6.sin6_addr, 7, htons(RXaddr.rxaddr6.sin6_port), Addr, 0, TRUE, TRUE, 0, PORT->udpport[i], TRUE);
}
else
return add_arp_entry(PORT, call, (UCHAR *)&RXaddr.rxaddr.sin_addr.s_addr, 7, htons(RXaddr.rxaddr.sin_port), inet_ntoa(RXaddr.rxaddr.sin_addr), 0, TRUE, TRUE, 0, PORT->udpport[i], FALSE);
else
{
char From[10];
From[ConvFromAX25(call, From)] = 0;
Debugprintf("AXUDP Packet from %s dropped - can't reply", From);
return 0;
}
}
}
else
return(1);
}
//
// CRC Error
//
sprintf(errmsg,"BPQAXIP Invalid CRC=%d Source=%s Port %d",crc,inet_ntoa(RXaddr.rxaddr.sin_addr),PORT->udpport[i]);
Debugprintf(errmsg);
rxbuff[len] = 0;
Debugprintf(rxbuff);
return (0);
}
}
if (PORT->NeedTCP)
{
len = GetMessageFromBuffer(PORT, rxbuff);
if (len)
{
len = KissDecode(rxbuff, len-1); // Len includes FEND
len -= 2; // Ignore Cheksum
memcpy(&buff[7],&rxbuff[0],len);
len+=7;
PutLengthinBuffer(buff, len); // fix big endian issue
// buff[5]=(len & 0xff);
// buff[6]=(len >> 8);
return 1;
}
}
return (0);
case 2: // send
// txlen=(buff[6]<<8) + buff[5] - 5; // Len includes buffer header (7) but we add crc
txlen = GetLengthfromBuffer(buff) - 5;
crc=compute_crc(&buff[7], txlen - 2);
crc ^= 0xffff;
buff[txlen+5]=(crc&0xff);
buff[txlen+6]=(crc>>8);
memcpy(axcall, &buff[7], 7); // Set to send to dest addr
// if digis are present, scan down list for first non-used call
if ((buff[20] & 1) == 0)
{
// end of addr bit not set, so scan digis
digiptr=21; // start of first digi
while (((buff[digiptr+6] & 0x80) == 0x80) && ((buff[digiptr+6] & 0x1) == 0))
{
// This digi has been used, and it is not the last
digiptr+=7;
}
// if this has not been used, use it
if ((buff[digiptr+6] & 0x80) == 0)
memcpy(axcall,&buff[digiptr],7); // get next call
}
axcall[6] &= 0x7e;
// If addresses to a broadcast address, send to all entries
for (i=0; i< PORT->NumberofBroadcastAddreses; i++)
{
if (memcmp(axcall, PORT->BroadcastAddresses[i].callsign, 7) == 0)
{
for (index = 0; index < PORT->arp_table_len; index++)
{
if (PORT->arp_table[index].BCFlag) SendFrame(PORT, &PORT->arp_table[index], &buff[7], txlen);
}
return 0;
}
}
// Send to all matching calls in arp table
index = 0;
while (index < PORT->arp_table_len)
{
if (memcmp(PORT->arp_table[index].callsign,axcall,PORT->arp_table[index].len) == 0)
{
SendFrame(PORT, &PORT->arp_table[index], &buff[7], txlen);
}
index++;
}
return (0);
case 3: // CHECK IF OK TO SEND
return (0); // OK
case 4: // reinit
CloseSockets(PORT);
ProcessConfig();
FreeConfig();
ReadConfigFile(port);
_beginthread(OpenSockets, 0, PORT );
ResolveDelay = 2;
#ifndef LINBPQ
InvalidateRect(PORT->hResWnd,NULL,TRUE);
#endif
break;
case 5: // Terminate
CloseSockets(PORT);
#ifndef LINBPQ
SendMessage(PORT->hMHWnd, WM_CLOSE, 0, 0);
SendMessage(PORT->hResWnd, WM_CLOSE, 0, 0);
#endif
break;
}
return (0);
}
VOID SendFrame(struct AXIPPORTINFO * PORT, struct arp_table_entry * arp_table, UCHAR * buff, int txlen)
{
int txsock, i, SourceSocket;
if (arp_table->TCPMode)
{
if (arp_table->TCPState == TCPConnected)
{
char outbuff[1000];
int newlen;
newlen = KissEncode(buff, outbuff, txlen);
send(arp_table->TCPSock, outbuff, newlen, 0);
}
return;
}
// Seelcte source port by choosing right socket
// First Set Default for Protocol
for (i = 0; i < PORT->NumberofUDPPorts; i++)
{
if (PORT->IPv6[i] == arp_table->IPv6)
{
SourceSocket = PORT->udpsock[i]; // Use as source socket, therefore source port
break;
}
}
for (i = 0; i < PORT->NumberofUDPPorts; i++)
{
if (PORT->udpport[i] == arp_table->SourcePort && PORT->IPv6[i] == arp_table->IPv6)
{
SourceSocket = PORT->udpsock[i]; // Use as source socket, therefore source port
break;
}
}
if (arp_table->error == 0)
{
int sent;
if (arp_table->port == 0) txsock = PORT->sock; else txsock = SourceSocket;
if (arp_table->IPv6)
sent = sendto(txsock, buff, txlen, 0, (struct sockaddr *)&arp_table->destaddr6, sizeof(arp_table->destaddr6));
else
sent = sendto(txsock, buff, txlen, 0, (struct sockaddr *)&arp_table->destaddr, sizeof(arp_table->destaddr));
// if (sent != txlen)
// perror("Sendto");
// reset Keepalive Timer
arp_table->keepalive=arp_table->keepaliveinit;
}
}
unsigned short int compute_crc_ccitt(unsigned char *buf, int len);
unsigned short CCCITTChecksum(unsigned char* data, unsigned int length);
UINT AXIPExtInit(struct PORTCONTROL * PortEntry)
{
// char Msg[10] = {0xD0, 01, 00, 0x11, 00, 0x0B};
// unsigned short crc;
// crc = CCCITTChecksum(Msg, 4);
// crc = CalcCRC(Msg, 4);
WritetoConsole("AXIP ");
InitAXIP(PortEntry->PORTNUMBER);
WritetoConsole("\n");
return ((int) ExtProc);
}
InitAXIP(int Port)
{
struct AXIPPORTINFO * PORT;
//
// Read config first, to get UDP info if needed
//
if (!ReadConfigFile(Port))
return (FALSE);
PORT = Portlist[Port];
if (PORT == NULL)
return FALSE;
PORT->Port = Port;
//
// Start Resolver Thread if needed
//
if (PORT->NeedResolver)
{
CreateResolverWindow(PORT);
_beginthread(ResolveNames, 0, PORT );
}
time(&PORT->lasttime); // Get initial time value
_beginthread(OpenSockets, 0, PORT );
// Start TCP outward connect threads
//
// Open MH window if needed
if (PORT->MHEnabled)
CreateMHWindow(PORT);
return (TRUE);
}
void OpenSockets(struct AXIPPORTINFO * PORT)
{
char Msg[255];
int err;
u_long param=1;
BOOL bcopt=TRUE;
int i;
int index = 0;
struct arp_table_entry * arp;
// Moved from InitAXIP, to avoid hang if started too early on XP SP2
// Create and bind socket
if (PORT->needip)
{
PORT->sock=socket(AF_INET,SOCK_RAW,IP_AXIP);
if (PORT->sock == INVALID_SOCKET)
{
WritetoConsole("AXIP Failed to create RAW socket\n");
err = WSAGetLastError();
return;
}
ioctl (PORT->sock,FIONBIO,¶m);
setsockopt (PORT->sock,SOL_SOCKET,SO_BROADCAST,(const char FAR *)&bcopt,4);
sinx.sinx.sin_family = AF_INET;
sinx.sinx.sin_addr.s_addr = INADDR_ANY;
sinx.sinx.sin_port = 0;
if (bind(PORT->sock, (struct sockaddr *) &sinx, sizeof(sinx)) != 0 )
{
//
// Bind Failed
//
err = WSAGetLastError();
sprintf(Msg, "Bind Failed for RAW socket - error code = %d", err);
WritetoConsole(Msg);
return;
}
}
for (i=0;i<PORT->NumberofUDPPorts;i++)
{
int ret;
if (PORT->IPv6[i])
PORT->udpsock[i]=socket(AF_INET6,SOCK_DGRAM,0);
else
PORT->udpsock[i]=socket(AF_INET,SOCK_DGRAM,0);
if (PORT->udpsock[i] == INVALID_SOCKET)
{
WritetoConsole("Failed to create UDP socket");
err = WSAGetLastError();
return;
}
ioctl (PORT->udpsock[i],FIONBIO,¶m);
setsockopt (PORT->udpsock[i],SOL_SOCKET,SO_BROADCAST,(const char FAR *)&bcopt,4);
#ifndef WIN32
if (PORT->IPv6[i])
if (setsockopt(PORT->udpsock[i], IPPROTO_IPV6, IPV6_V6ONLY, ¶m, sizeof(param)) < 0)
perror("setting option IPV6_V6ONLY");
#endif
if (PORT->IPv6[i])
{
sinx.sinx.sin_family = AF_INET6;
memset (&sinx.sinx6.sin6_addr, 0, 16);
}
else
{
sinx.sinx.sin_family = AF_INET;
sinx.sinx.sin_addr.s_addr = INADDR_ANY;
}
sinx.sinx.sin_port = htons(PORT->udpport[i]);
if (PORT->IPv6[i])
ret = bind(PORT->udpsock[i], (struct sockaddr *) &sinx.sinx, sizeof(sinx.sinx6));
else
ret = bind(PORT->udpsock[i], (struct sockaddr *) &sinx.sinx, sizeof(sinx.sinx));
if (ret != 0)
{
// Bind Failed
err = WSAGetLastError();
sprintf(Msg, "Bind Failed for UDP socket %d - error code = %d", PORT->udpport[i], err);
WritetoConsole(Msg);
continue;
}
}
// Open any TCP sockets
while (index < PORT->arp_table_len)
{
arp = &PORT->arp_table[index++];
if (arp->TCPMode == TCPMaster)
{
arp->TCPBuffer=malloc(4000);
arp->TCPState = 0;
if (arp->TCPThreadID == 0)
{
arp->TCPThreadID = _beginthread(TCPConnectThread, 0, arp);
Debugprintf("TCP Connect thread created for %s Handle %x", arp->hostname, arp->TCPThreadID);
}
continue;
}
if (arp->TCPMode == TCPSlave)
{
OpenListeningSocket(PORT, arp);
}
}
}
OpenListeningSocket(struct AXIPPORTINFO * PORT, struct arp_table_entry * arp)
{
char Msg[255];
struct sockaddr_in * psin;
BOOL bOptVal = TRUE;
struct sockaddr_in local_sin; /* Local socket - internet style */
u_long param=1;
arp->TCPBuffer=malloc(4000);
arp->TCPState = 0;
arp->TCPListenSock = socket(AF_INET, SOCK_STREAM, 0);
ioctl (arp->TCPListenSock, FIONBIO, ¶m);
if (arp->TCPListenSock == INVALID_SOCKET)
{
sprintf(Msg, "socket() failed error %d", WSAGetLastError());
WritetoConsole(Msg);
return FALSE;
}
// Debugprintf("TCP Listening Socket Created - socket %d port %d ", arp->TCPListenSock, arp->port);
setsockopt (arp->TCPListenSock, SOL_SOCKET, SO_REUSEADDR, (char *)¶m,4);
psin=&local_sin;
psin->sin_family = AF_INET;
psin->sin_addr.s_addr = htonl(INADDR_ANY); // Local Host Only
psin->sin_port = htons(arp->port); /* Convert to network ordering */
if (bind(arp->TCPListenSock , (struct sockaddr FAR *) &local_sin, sizeof(local_sin)) == SOCKET_ERROR)
{
sprintf(Msg, "bind(sock) failed Error %d", WSAGetLastError());
Debugprintf(Msg);
closesocket(arp->TCPListenSock);
return FALSE;
}
if (listen(arp->TCPListenSock, 1) < 0)
{
sprintf(Msg, "listen(sock) failed Error %d", WSAGetLastError());
Debugprintf(Msg);
closesocket(arp->TCPListenSock);
return FALSE;
}
arp->TCPState = TCPListening;
return TRUE;
}
void CloseSockets(struct AXIPPORTINFO * PORT)
{
int i;
int index = 0;
struct arp_table_entry * arp;