-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Network.c
20232 lines (16948 loc) · 395 KB
/
Network.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
// SoftEther VPN Source Code - Developer Edition Master Branch
// Mayaqua Kernel
// Network.c
// Network communication module
#include "Network.h"
#include "Cfg.h"
#include "DNS.h"
#include "FileIO.h"
#include "HTTP.h"
#include "Internat.h"
#include "Memory.h"
#include "Microsoft.h"
#include "Object.h"
#include "Pack.h"
#include "Str.h"
#include "TcpIp.h"
#include "Tick64.h"
#include "Unix.h"
#include <string.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#ifdef OS_UNIX
#include <fcntl.h>
#include <netdb.h>
#include <poll.h>
#include <signal.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/time.h>
#endif
#ifdef UNIX_MACOS
#include <sys/event.h>
#endif
#ifdef UNIX
#ifdef UNIX_SOLARIS
#define USE_STATVFS
#include <sys/statvfs.h>
#else
#define MAYAQUA_SUPPORTS_GETIFADDRS
#include <ifaddrs.h>
#endif
#endif
#ifdef OS_WIN32
#include <iphlpapi.h>
#include <WS2tcpip.h>
#include <wincrypt.h>
#include <IcmpAPI.h>
struct ROUTE_CHANGE_DATA
{
HANDLE Handle;
UINT NumCalled;
bool Changed;
};
#endif
// Whether the blocking occurs in SSL
#if defined(UNIX_BSD) || defined(UNIX_MACOS)
#define FIX_SSL_BLOCKING
#endif
// HTTP constant
static char http_detect_server_startwith[] = "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n<HTML><HEAD>\r\n<TITLE>403 Forbidden</TITLE>\r\n</HEAD><BODY>\r\n<H1>Forbidden</H1>\r\nYou don't have permission to access ";
static char http_detect_server_tag_future[] = "9C37197CA7C2428388C2E6E59B829B30";
// Lock related
static LOCK *machine_name_lock = NULL;
static LOCK *disconnect_function_lock = NULL;
extern LOCK *openssl_lock;
static COUNTER *num_tcp_connections = NULL;
static LOCK *unix_dns_server_addr_lock = NULL;
static IP unix_dns_server;
static LIST *WaitThreadList = NULL;
static UCHAR machine_ip_process_hash[SHA1_SIZE];
static LOCK *machine_ip_process_hash_lock = NULL;
static LOCK *current_global_ip_lock = NULL;
static LOCK *current_fqdn_lock = NULL;
static bool current_global_ip_set = false;
static IP current_glocal_ipv4 = {0};
static IP current_glocal_ipv6 = {0};
static char current_fqdn[MAX_SIZE];
static bool g_no_rudp_server = false;
static bool g_no_rudp_register = false;
static bool g_natt_low_priority = false;
static LOCK *host_ip_address_list_cache_lock = NULL;
static UINT64 host_ip_address_list_cache_last = 0;
static LIST *host_ip_address_cache = NULL;
static bool disable_gethostname_by_accept = false;
static LIST *ip_clients = NULL;
static LIST *local_mac_list = NULL;
static LOCK *local_mac_list_lock = NULL;
static UINT rand_port_numbers[256] = {0};
static bool g_use_privateip_file = false;
static bool g_source_ip_validation_force_disable = false;
static DH_CTX *dh_param = NULL;
typedef struct PRIVATE_IP_SUBNET
{
UINT Ip, Mask, Ip2;
} PRIVATE_IP_SUBNET;
static LIST *g_private_ip_list = NULL;
static LIST *g_dyn_value_list = NULL;
//#define RUDP_DETAIL_LOG
// Get a value from a dynamic value list (Returns a default value if the value is not found)
UINT64 GetDynValueOrDefault(char *name, UINT64 default_value, UINT64 min_value, UINT64 max_value)
{
UINT64 ret = GetDynValue(name);
if (ret == 0)
{
return default_value;
}
if (ret < min_value)
{
ret = min_value;
}
if (ret > max_value)
{
ret = max_value;
}
return ret;
}
// Get a value from a dynamic value list (Returns a default value if the value is not found)
// The value is limited to 1/5 to 50 times of the default value for safety
UINT64 GetDynValueOrDefaultSafe(char *name, UINT64 default_value)
{
return GetDynValueOrDefault(name, default_value, default_value / (UINT64)5, default_value * (UINT64)50);
}
// Get a value from a dynamic value list
UINT64 GetDynValue(char *name)
{
UINT64 ret = 0;
// Validate arguments
if (name == NULL)
{
return 0;
}
if (g_dyn_value_list == NULL)
{
return 0;
}
LockList(g_dyn_value_list);
{
UINT i;
for (i = 0; i < LIST_NUM(g_dyn_value_list); i++)
{
DYN_VALUE *vv = LIST_DATA(g_dyn_value_list, i);
if (StrCmpi(vv->Name, name) == 0)
{
ret = vv->Value;
break;
}
}
}
UnlockList(g_dyn_value_list);
return ret;
}
// Set the value to the dynamic value list
void SetDynListValue(char *name, UINT64 value)
{
// Validate arguments
if (name == NULL)
{
return;
}
if (g_dyn_value_list == NULL)
{
return;
}
LockList(g_dyn_value_list);
{
UINT i;
DYN_VALUE *v = NULL;
for (i = 0; i < LIST_NUM(g_dyn_value_list); i++)
{
DYN_VALUE *vv = LIST_DATA(g_dyn_value_list, i);
if (StrCmpi(vv->Name, name) == 0)
{
v = vv;
break;
}
}
if (v == NULL)
{
v = ZeroMalloc(sizeof(DYN_VALUE));
StrCpy(v->Name, sizeof(v->Name), name);
Add(g_dyn_value_list, v);
}
v->Value = value;
}
UnlockList(g_dyn_value_list);
}
// Apply by extracting dynamic value list from the specified PACK
void ExtractAndApplyDynList(PACK *p)
{
BUF *b;
// Validate arguments
if (p == NULL)
{
return;
}
b = PackGetBuf(p, "DynList");
if (b == NULL)
{
return;
}
AddDynList(b);
FreeBuf(b);
}
// Insert the data to the dynamic value list
void AddDynList(BUF *b)
{
PACK *p;
TOKEN_LIST *t;
// Validate arguments
if (b == NULL)
{
return;
}
SeekBufToBegin(b);
p = BufToPack(b);
if (p == NULL)
{
return;
}
t = GetPackElementNames(p);
if (t != NULL)
{
UINT i;
for (i = 0; i < t->NumTokens; i++)
{
char *name = t->Token[i];
UINT64 v = PackGetInt64(p, name);
SetDynListValue(name, v);
}
FreeToken(t);
}
FreePack(p);
}
// Initialization of the dynamic value list
void InitDynList()
{
g_dyn_value_list = NewList(NULL);
}
// Solution of dynamic value list
void FreeDynList()
{
UINT i;
if (g_dyn_value_list == NULL)
{
return;
}
for (i = 0; i < LIST_NUM(g_dyn_value_list); i++)
{
DYN_VALUE *d = LIST_DATA(g_dyn_value_list, i);
Free(d);
}
ReleaseList(g_dyn_value_list);
g_dyn_value_list = NULL;
}
// Disable NAT-T function globally
void DisableRDUPServerGlobally()
{
g_no_rudp_server = true;
}
// Get the current time zone
int GetCurrentTimezone()
{
int ret = 0;
#ifdef OS_WIN32
ret = GetCurrentTimezoneWin32();
#else // OS_WIN32
{
#if defined(UNIX_MACOS) || defined(UNIX_BSD)
struct timeval tv;
struct timezone tz;
Zero(&tv, sizeof(tv));
Zero(&tz, sizeof(tz));
gettimeofday(&tv, &tz);
ret = tz.tz_minuteswest;
#else // defined(UNIX_MACOS) || defined(UNIX_BSD)
tzset();
ret = timezone / 60;
#endif // defined(UNIX_MACOS) || defined(UNIX_BSD)
}
#endif // OS_WIN32
return ret;
}
// Flag of whether to use an alternate host name
bool IsUseAlternativeHostname()
{
return false;
}
#ifdef OS_WIN32
// Get the current time zone (Win32)
int GetCurrentTimezoneWin32()
{
TIME_ZONE_INFORMATION info;
Zero(&info, sizeof(info));
if (GetTimeZoneInformation(&info) == TIME_ZONE_ID_INVALID)
{
return 0;
}
return info.Bias;
}
#endif // OS_WIN32
// Set the current FQDN of the DDNS
void SetCurrentDDnsFqdn(char *name)
{
// Validate arguments
if (name == NULL)
{
return;
}
Lock(current_fqdn_lock);
{
StrCpy(current_fqdn, sizeof(current_fqdn), name);
}
Unlock(current_fqdn_lock);
}
// Get the current DDNS FQDN hash
UINT GetCurrentDDnsFqdnHash()
{
UINT ret;
UCHAR hash[SHA1_SIZE];
char name[MAX_SIZE];
ClearStr(name, sizeof(name));
GetCurrentDDnsFqdn(name, sizeof(name));
Trim(name);
StrUpper(name);
Sha1(hash, name, StrLen(name));
Copy(&ret, hash, sizeof(UINT));
return ret;
}
// Get the current DDNS FQDN
void GetCurrentDDnsFqdn(char *name, UINT size)
{
ClearStr(name, size);
// Validate arguments
if (name == NULL || size == 0)
{
return;
}
Lock(current_fqdn_lock);
{
StrCpy(name, size, current_fqdn);
}
Unlock(current_fqdn_lock);
Trim(name);
}
// Check whether the specified MAC address exists on the local host (high speed)
bool IsMacAddressLocalFast(void *addr)
{
bool ret = false;
// Validate arguments
if (addr == NULL)
{
return false;
}
Lock(local_mac_list_lock);
{
if (local_mac_list == NULL)
{
// First enumeration
RefreshLocalMacAddressList();
}
ret = IsMacAddressLocalInner(local_mac_list, addr);
}
Unlock(local_mac_list_lock);
return ret;
}
// Update the local MAC address list
void RefreshLocalMacAddressList()
{
Lock(local_mac_list_lock);
{
if (local_mac_list != NULL)
{
FreeNicList(local_mac_list);
}
local_mac_list = GetNicList();
}
Unlock(local_mac_list_lock);
}
// Check whether the specified MAC address exists on the local host
bool IsMacAddressLocalInner(LIST *o, void *addr)
{
bool ret = false;
UINT i;
// Validate arguments
if (o == NULL || addr == NULL)
{
return false;
}
for (i = 0; i < LIST_NUM(o); i++)
{
NIC_ENTRY *e = LIST_DATA(o, i);
if (Cmp(e->MacAddress, addr, 6) == 0)
{
ret = true;
break;
}
}
return ret;
}
// Get a list of the NICs on the computer
LIST *GetNicList()
{
LIST *o = NULL;
#ifdef OS_WIN32
o = Win32GetNicList();
if (o != NULL)
{
return o;
}
#endif // OS_WIN32
return NewListFast(NULL);
}
#ifdef OS_WIN32
LIST *Win32GetNicList()
{
UINT i;
LIST *o = NewListFast(NULL);
MS_ADAPTER_LIST *al = MsCreateAdapterList();
if (al == NULL)
{
return NULL;
}
for (i = 0; i < al->Num; i++)
{
MS_ADAPTER *a = al->Adapters[i];
if (a->Type == 6 && a->AddressSize == 6)
{
// If the connection state of the interface is unknown, then exclude it.
// Unknown means that the device is not plugged into the local host.
if (a->MediaConnectState == MediaConnectStateUnknown)
{
continue;
}
NIC_ENTRY *e = ZeroMalloc(sizeof(NIC_ENTRY));
StrCpy(e->IfName, sizeof(e->IfName), a->Title);
Copy(e->MacAddress, a->Address, 6);
Add(o, e);
}
}
MsFreeAdapterList(al);
return o;
}
#endif // OS_WIN32
// Release the NIC list
void FreeNicList(LIST *o)
{
UINT i;
// Validate arguments
if (o == NULL)
{
return;
}
for (i = 0; i < LIST_NUM(o); i++)
{
NIC_ENTRY *e = LIST_DATA(o, i);
Free(e);
}
ReleaseList(o);
}
// If the computer is connected to the FLET'S line currently, detect the type of the line (obsolete)
UINT DetectFletsType()
{
UINT ret = 0;
//LIST *o = GetHostIPAddressList();
// UINT i;
/*
for (i = 0;i < LIST_NUM(o);i++)
{
IP *ip = LIST_DATA(o, i);
if (IsIP6(ip))
{
char ip_str[MAX_SIZE];
IPToStr(ip_str, sizeof(ip_str), ip);
if (IsInSameNetwork6ByStr(ip_str, "2001:c90::", "/32"))
{
// NTT East B-FLETs
ret |= FLETS_DETECT_TYPE_EAST_BFLETS_PRIVATE;
}
if (IsInSameNetwork6ByStr(ip_str, "2408:200::", "/23"))
{
// Wrapping in network of NTT East NGN
ret |= FLETS_DETECT_TYPE_EAST_NGN_PRIVATE;
}
if (IsInSameNetwork6ByStr(ip_str, "2001:a200::", "/23"))
{
// Wrapping in network of NTT West NGN
ret |= FLETS_DETECT_TYPE_WEST_NGN_PRIVATE;
}
}
}
FreeHostIPAddressList(o);
*/
return ret;
}
// Query for the IP address using the DNS proxy for the B FLETs
bool GetIPViaDnsProxyForJapanFlets(IP *ip_ret, char *hostname, bool ipv6, UINT timeout, bool *cancel, char *dns_proxy_hostname)
{
SOCK *s;
char connect_hostname[MAX_SIZE];
char connect_hostname2[MAX_SIZE];
IP dns_proxy_ip;
bool ret = false;
bool dummy_flag = false;
char request_str[512];
// Validate arguments
if (ip_ret == NULL || hostname == NULL)
{
return false;
}
if (timeout == 0)
{
timeout = BFLETS_DNS_PROXY_TIMEOUT_FOR_QUERY;
}
if (cancel == NULL)
{
cancel = &dummy_flag;
}
// Get the IP address of the DNS proxy server
if (IsEmptyStr(dns_proxy_hostname))
{
// B FLETs
if (GetDnsProxyIPAddressForJapanBFlets(&dns_proxy_ip, BFLETS_DNS_PROXY_TIMEOUT_FOR_GET_F, cancel) == false)
{
return false;
}
}
else
{
// FLET'S NEXT
if (GetIP6Ex(&dns_proxy_ip, dns_proxy_hostname, FLETS_NGN_DNS_QUERY_TIMEOUT, cancel) == false)
{
return false;
}
}
if (*cancel)
{
return false;
}
IPToStr(connect_hostname, sizeof(connect_hostname), &dns_proxy_ip);
/*{
StrCpy(connect_hostname, sizeof(connect_hostname), "2409:250:62c0:100:6a05:caff:fe09:5158");
}*/
StrCpy(connect_hostname2, sizeof(connect_hostname2), connect_hostname);
if (IsIP6(&dns_proxy_ip))
{
Format(connect_hostname2, sizeof(connect_hostname2), "[%s]", connect_hostname);
}
s = ConnectEx3(connect_hostname, BFLETS_DNS_PROXY_PORT, timeout, cancel, NULL, NULL, false, false);
if (s == NULL)
{
return false;
}
if (*cancel)
{
Disconnect(s);
ReleaseSock(s);
return false;
}
SetTimeout(s, timeout);
// Start the SSL
if (StartSSLEx(s, NULL, NULL, 0, NULL) && (*cancel == false))
{
UCHAR hash[SHA1_SIZE];
BUF *hash2 = StrToBin(BFLETS_DNS_PROXY_CERT_HASH);
Zero(hash, sizeof(hash));
GetXDigest(s->RemoteX, hash, true);
if (Cmp(hash, hash2->Buf, SHA1_SIZE) == 0)
{
// Send the HTTP Request
Format(request_str, sizeof(request_str),
"GET " BFLETS_DNS_PROXY_PATH "?q=%s&ipv6=%u\r\n"
"\r\n",
hostname, ipv6, connect_hostname2);
if (SendAll(s, request_str, StrLen(request_str), true))
{
if (*cancel == false)
{
BUF *recv_buf = NewBuf();
UINT port_ret;
while (true)
{
UCHAR tmp[MAX_SIZE];
UINT r;
r = Recv(s, tmp, sizeof(tmp), true);
if (r == 0 || (recv_buf->Size > 65536))
{
break;
}
else
{
WriteBuf(recv_buf, tmp, r);
}
}
ret = RUDPParseIPAndPortStr(recv_buf->Buf, recv_buf->Size, ip_ret, &port_ret);
FreeBuf(recv_buf);
}
}
}
FreeBuf(hash2);
}
Disconnect(s);
ReleaseSock(s);
if (ret)
{
DnsCacheUpdate(hostname, ipv6 ? ip_ret : NULL, ipv6 ? NULL : ip_ret);
}
return ret;
}
// Get the IP address of the available DNS proxy in B-FLET'S service that is provided by NTT East of Japan
bool GetDnsProxyIPAddressForJapanBFlets(IP *ip_ret, UINT timeout, bool *cancel)
{
BUF *b;
LIST *o;
bool ret = false;
// Validate arguments
if (ip_ret == NULL)
{
return false;
}
if (timeout == 0)
{
timeout = BFLETS_DNS_PROXY_TIMEOUT_FOR_GET_F;
}
b = QueryFileByUdpForJapanBFlets(timeout, cancel);
if (b == NULL)
{
return false;
}
o = ReadIni(b);
if (o != NULL)
{
INI_ENTRY *e = GetIniEntry(o, "DDnsServerForBFlets");
if (e != NULL)
{
char *s = e->Value;
if (IsEmptyStr(s) == false)
{
IP ip;
if (StrToIP(&ip, s))
{
if (IsZeroIp(&ip) == false)
{
Copy(ip_ret, &ip, sizeof(IP));
ret = true;
}
}
}
}
}
FreeIni(o);
FreeBuf(b);
return ret;
}
// Get a valid F.txt file in B-FLET'S service that is provided by NTT East of Japan
BUF *QueryFileByUdpForJapanBFlets(UINT timeout, bool *cancel)
{
bool dummy_flag = false;
BUF *txt_buf = NULL;
BUF *ret = NULL;
LIST *ip_list = NULL;
UINT i;
// Validate arguments
if (cancel == NULL)
{
cancel = &dummy_flag;
}
if (timeout == 0)
{
timeout = BFLETS_DNS_PROXY_TIMEOUT_FOR_GET_F;
}
txt_buf = ReadDump(UDP_FILE_QUERY_BFLETS_TXT_FILENAME);
if (txt_buf == NULL)
{
return NULL;
}
ip_list = NewListFast(NULL);
while (true)
{
char *line = CfgReadNextLine(txt_buf);
if (line == NULL)
{
break;
}
Trim(line);
if (IsEmptyStr(line) == false && StartWith(line, "#") == false)
{
IP ip;
if (StrToIP6(&ip, line))
{
if (IsZeroIp(&ip) == false)
{
if (IsIPv6LocalNetworkAddress(&ip) == false)
{
Add(ip_list, Clone(&ip, sizeof(IP)));
}
}
}
}
Free(line);
}
FreeBuf(txt_buf);
ret = QueryFileByIPv6Udp(ip_list, timeout, cancel);
for (i = 0; i < LIST_NUM(ip_list); i++)
{
IP *ip = LIST_DATA(ip_list, i);
Free(ip);
}
ReleaseList(ip_list);
return ret;
}
// Request a file by UDP (send the requests to the multiple IP addresses at the same time)
BUF *QueryFileByIPv6Udp(LIST *ip_list, UINT timeout, bool *cancel)
{
bool dummy_flag = false;
UINT64 start_tick, giveup_tick;
UINT64 next_send_tick;
SOCK *s;
INTERRUPT_MANAGER *interrupt;
BUF *buf = NULL;
SOCK_EVENT *se;
UCHAR *tmp_buf;
UINT tmp_buf_size = 65535;
// Validate arguments
if (cancel == NULL)
{
cancel = &dummy_flag;
}
if (ip_list == NULL)
{
return NULL;
}
s = NewUDP6(0, NULL);
if (s == NULL)
{
return NULL;
}
tmp_buf = Malloc(tmp_buf_size);
start_tick = Tick64();
giveup_tick = start_tick + (UINT64)timeout;
next_send_tick = 0;
interrupt = NewInterruptManager();
AddInterrupt(interrupt, giveup_tick);
se = NewSockEvent();
JoinSockToSockEvent(s, se);
while (true)
{
UINT64 now = Tick64();
if (now >= giveup_tick)
{
// Time-out
break;
}
if (*cancel)
{
// User canceled
break;
}
// Receive
while (true)
{
IP src_ip;
UINT src_port;
UINT r;
r = RecvFrom(s, &src_ip, &src_port, tmp_buf, tmp_buf_size);
if (r == SOCK_LATER || r == 0)
{
break;
}
if (src_port == UDP_FILE_QUERY_DST_PORT)
{
if (r >= 40)
{
if (Cmp(tmp_buf, UDP_FILE_QUERY_MAGIC_NUMBER, StrLen(UDP_FILE_QUERY_MAGIC_NUMBER)) == 0)
{
// Successful reception
buf = NewBuf();
WriteBuf(buf, tmp_buf, r);
SeekBuf(buf, 0, 0);
break;
}
}
}
}
if (buf != NULL)
{
// Successful reception
break;
}
if (next_send_tick == 0 || (now >= next_send_tick))
{
// Transmission
UINT i;
for (i = 0; i < LIST_NUM(ip_list); i++)
{
IP *ip = LIST_DATA(ip_list, i);
UCHAR c = 'F';
SendTo(s, ip, UDP_FILE_QUERY_DST_PORT, &c, 1);
}
next_send_tick = now + (UINT64)UDP_FILE_QUERY_RETRY_INTERVAL;
AddInterrupt(interrupt, next_send_tick);
}