forked from moonlight-stream/moonlight-common-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlatformSockets.c
749 lines (648 loc) · 24.2 KB
/
PlatformSockets.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
#include "Limelight-internal.h"
#define TEST_PORT_TIMEOUT_SEC 3
#define RCV_BUFFER_SIZE_MIN 32767
#define RCV_BUFFER_SIZE_STEP 16384
#if defined(__vita__)
#define TCPv4_MSS 512
#else
#define TCPv4_MSS 536
#endif
#define TCPv6_MSS 1220
#if defined(LC_WINDOWS)
#ifndef SIO_UDP_CONNRESET
#define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR, 12)
#endif
static HMODULE WlanApiLibraryHandle;
static HANDLE WlanHandle;
DWORD (WINAPI *pfnWlanOpenHandle)(DWORD dwClientVersion, PVOID pReserved, PDWORD pdwNegotiatedVersion, PHANDLE phClientHandle);
DWORD (WINAPI *pfnWlanCloseHandle)(HANDLE hClientHandle, PVOID pReserved);
DWORD (WINAPI *pfnWlanEnumInterfaces)(HANDLE hClientHandle, PVOID pReserved, PWLAN_INTERFACE_INFO_LIST *ppInterfaceList);
VOID (WINAPI *pfnWlanFreeMemory)(PVOID pMemory);
DWORD (WINAPI *pfnWlanSetInterface)(HANDLE hClientHandle, CONST GUID *pInterfaceGuid, WLAN_INTF_OPCODE OpCode, DWORD dwDataSize, CONST PVOID pData, PVOID pReserved);
#ifndef WLAN_API_MAKE_VERSION
#define WLAN_API_MAKE_VERSION(_major, _minor) (((DWORD)(_minor)) << 16 | (_major))
#endif
#endif
void addrToUrlSafeString(struct sockaddr_storage* addr, char* string, size_t stringLength)
{
char addrstr[URLSAFESTRING_LEN];
#ifdef AF_INET6
if (addr->ss_family == AF_INET6) {
struct sockaddr_in6* sin6 = (struct sockaddr_in6*)addr;
inet_ntop(addr->ss_family, &sin6->sin6_addr, addrstr, sizeof(addrstr));
// IPv6 addresses need to be enclosed in brackets for URLs
snprintf(string, stringLength, "[%s]", addrstr);
}
else
#endif
{
struct sockaddr_in* sin = (struct sockaddr_in*)addr;
inet_ntop(addr->ss_family, &sin->sin_addr, addrstr, sizeof(addrstr));
// IPv4 addresses are returned without changes
snprintf(string, stringLength, "%s", addrstr);
}
}
void shutdownTcpSocket(SOCKET s) {
// Calling shutdown() prior to close wakes up callers
// blocked in connect(), recv(), and friends.
shutdown(s, SHUT_RDWR);
}
int setNonFatalRecvTimeoutMs(SOCKET s, int timeoutMs) {
#if defined(LC_WINDOWS)
// Windows says that SO_RCVTIMEO puts the socket into an indeterminate state
// when a timeout occurs. MSDN doesn't go into it any more than that, but it
// seems likely that they are referring to the inability to know whether a
// cancelled request consumed some data or not (very relevant for stream-based
// protocols like TCP). Since our sockets are UDP which is already unreliable,
// losing some data in a very rare case is fine, especially because we get to
// halve the number of syscalls per packet by avoiding select().
return setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeoutMs, sizeof(timeoutMs));
#elif defined(__WIIU__) || defined(__3DS__)
// timeouts aren't supported on Wii U
return -1;
#else
struct timeval val;
val.tv_sec = 0;
val.tv_usec = timeoutMs * 1000;
return setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char*)&val, sizeof(val));
#endif
}
int pollSockets(struct pollfd* pollFds, int pollFdsCount, int timeoutMs) {
#if defined(LC_WINDOWS)
// We could have used WSAPoll() but it has some nasty bugs
// https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
//
// We'll emulate WSAPoll() with select(). Fortunately, Microsoft's definition
// of fd_set does not have the same stack corruption hazards that UNIX does.
fd_set readFds, writeFds, exceptFds;
int i, err, nfds;
struct timeval tv;
FD_ZERO(&readFds);
FD_ZERO(&writeFds);
FD_ZERO(&exceptFds);
nfds = 0;
for (i = 0; i < pollFdsCount; i++) {
// Clear revents on input like poll() does
pollFds[i].revents = 0;
if (pollFds[i].events & POLLIN) {
FD_SET(pollFds[i].fd, &readFds);
}
if (pollFds[i].events & POLLOUT) {
FD_SET(pollFds[i].fd, &writeFds);
// Windows signals failed connections as an exception,
// while Linux signals them as writeable.
FD_SET(pollFds[i].fd, &exceptFds);
}
}
tv.tv_sec = timeoutMs / 1000;
tv.tv_usec = (timeoutMs % 1000) * 1000;
err = select(nfds, &readFds, &writeFds, &exceptFds, timeoutMs >= 0 ? &tv : NULL);
if (err <= 0) {
// Error or timeout
return err;
}
for (i = 0; i < pollFdsCount; i++) {
if (FD_ISSET(pollFds[i].fd, &readFds)) {
pollFds[i].revents |= POLLRDNORM;
}
if (FD_ISSET(pollFds[i].fd, &writeFds)) {
pollFds[i].revents |= POLLWRNORM;
}
if (FD_ISSET(pollFds[i].fd, &exceptFds)) {
pollFds[i].revents |= POLLERR;
}
}
return err;
#else
return poll(pollFds, pollFdsCount, timeoutMs);
#endif
}
bool isSocketReadable(SOCKET s) {
struct pollfd pfd;
int err;
pfd.fd = s;
pfd.events = POLLIN;
err = pollSockets(&pfd, 1, 0);
if (err <= 0) {
return false;
}
return true;
}
int recvUdpSocket(SOCKET s, char* buffer, int size, bool useSelect) {
int err;
do {
if (useSelect) {
struct pollfd pfd;
// Wait up to 100 ms for the socket to be readable
pfd.fd = s;
pfd.events = POLLIN;
err = pollSockets(&pfd, 1, UDP_RECV_POLL_TIMEOUT_MS);
if (err <= 0) {
// Return if an error or timeout occurs
return err;
}
// This won't block since the socket is readable
err = (int)recvfrom(s, buffer, size, 0, NULL, NULL);
}
else {
// The caller has already configured a timeout on this
// socket via SO_RCVTIMEO, so we can avoid a syscall
// for each packet.
err = (int)recvfrom(s, buffer, size, 0, NULL, NULL);
if (err < 0 &&
(LastSocketError() == EWOULDBLOCK ||
LastSocketError() == EINTR ||
LastSocketError() == EAGAIN ||
#if defined(LC_WINDOWS)
// This error is specific to overlapped I/O which isn't even
// possible to perform with recvfrom(). It seems to randomly
// be returned instead of WSAETIMEDOUT on certain systems.
LastSocketError() == WSA_IO_PENDING ||
#endif
LastSocketError() == ETIMEDOUT)) {
// Return 0 for timeout
return 0;
}
}
// We may receive an error due to a previous ICMP Port Unreachable error received
// by this socket. We want to ignore those and continue reading. If the remote party
// is really dead, ENet or TCP connection failures will trigger connection teardown.
#if defined(LC_WINDOWS)
} while (err < 0 && LastSocketError() == WSAECONNRESET);
#else
} while (err < 0 && LastSocketError() == ECONNREFUSED);
#endif
return err;
}
void closeSocket(SOCKET s) {
#if defined(LC_WINDOWS)
closesocket(s);
#else
close(s);
#endif
}
SOCKET bindUdpSocket(int addressFamily, struct sockaddr_storage* localAddr, SOCKADDR_LEN addrLen, int bufferSize) {
SOCKET s;
LC_SOCKADDR bindAddr;
int err;
s = createSocket(addressFamily, SOCK_DGRAM, IPPROTO_UDP, false);
if (s == INVALID_SOCKET) {
return INVALID_SOCKET;
}
// Use localAddr to bind if it was provided
if (localAddr && localAddr->ss_family != 0) {
memcpy(&bindAddr, localAddr, addrLen);
SET_PORT(&bindAddr, 0);
}
else {
// Otherwise wildcard bind to the specified address family
memset(&bindAddr, 0, sizeof(bindAddr));
SET_FAMILY(&bindAddr, addressFamily);
#ifdef AF_INET6
LC_ASSERT(addressFamily == AF_INET || addressFamily == AF_INET6);
addrLen = (addressFamily == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6));
#else
LC_ASSERT(addressFamily == AF_INET);
addrLen = sizeof(struct sockaddr_in);
#endif
}
if (bind(s, (struct sockaddr*) &bindAddr, addrLen) == SOCKET_ERROR) {
err = LastSocketError();
Limelog("bind() failed: %d\n", err);
closeSocket(s);
SetLastSocketError(err);
return INVALID_SOCKET;
}
#if defined(LC_DARWIN)
{
// Disable SIGPIPE on iOS
int val = 1;
setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (char*)&val, sizeof(val));
}
#elif defined(LC_WINDOWS)
{
// Disable WSAECONNRESET for UDP sockets on Windows
BOOL val = FALSE;
DWORD bytesReturned = 0;
if (WSAIoctl(s, SIO_UDP_CONNRESET, &val, sizeof(val), NULL, 0, &bytesReturned, NULL, NULL) != 0) {
Limelog("WSAIoctl(SIO_UDP_CONNRESET) failed: %d\n", LastSocketError());
}
}
#endif
if (bufferSize != 0) {
// We start at the requested recv buffer value and step down until we find
// a value that the OS will accept.
for (;;) {
err = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char*)&bufferSize, sizeof(bufferSize));
if (err == 0) {
// Successfully set a buffer size
break;
}
else if (bufferSize <= RCV_BUFFER_SIZE_MIN) {
// Failed to set a buffer size within the allowable range
break;
}
else if (bufferSize - RCV_BUFFER_SIZE_STEP <= RCV_BUFFER_SIZE_MIN) {
// Last shot - we're trying the minimum
bufferSize = RCV_BUFFER_SIZE_MIN;
}
else {
// Lower the requested size by another step
bufferSize -= RCV_BUFFER_SIZE_STEP;
}
}
#if defined(LC_DEBUG)
if (err == 0) {
Limelog("Selected receive buffer size: %d\n", bufferSize);
}
else {
Limelog("Unable to set receive buffer size: %d\n", LastSocketError());
}
{
SOCKADDR_LEN len = sizeof(bufferSize);
if (getsockopt(s, SOL_SOCKET, SO_RCVBUF, (char*)&bufferSize, &len) == 0) {
Limelog("Actual receive buffer size: %d\n", bufferSize);
}
}
#endif
}
return s;
}
int setSocketNonBlocking(SOCKET s, bool enabled) {
#if defined(__vita__)
int val = enabled ? 1 : 0;
return setsockopt(s, SOL_SOCKET, SO_NONBLOCK, (char*)&val, sizeof(val));
#elif defined(O_NONBLOCK)
return fcntl(s, F_SETFL, (enabled ? O_NONBLOCK : 0) | (fcntl(s, F_GETFL) & ~O_NONBLOCK));
#elif defined(FIONBIO)
#ifdef LC_WINDOWS
u_long val = enabled ? 1 : 0;
#else
int val = enabled ? 1 : 0;
#endif
return ioctlsocket(s, FIONBIO, &val);
#else
#error Please define your platform non-blocking sockets API!
#endif
}
SOCKET createSocket(int addressFamily, int socketType, int protocol, bool nonBlocking) {
SOCKET s;
s = socket(addressFamily, socketType, protocol);
if (s == INVALID_SOCKET) {
Limelog("socket() failed: %d\n", (int)LastSocketError());
return INVALID_SOCKET;
}
#ifdef LC_DARWIN
{
// Disable SIGPIPE on iOS
int val = 1;
setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (char*)&val, sizeof(val));
}
#endif
if (nonBlocking) {
setSocketNonBlocking(s, true);
}
return s;
}
SOCKET connectTcpSocket(struct sockaddr_storage* dstaddr, SOCKADDR_LEN addrlen, unsigned short port, int timeoutSec) {
SOCKET s;
LC_SOCKADDR addr;
struct pollfd pfd;
int err;
#if defined(LC_WINDOWS) || defined(TCP_NOOPT) || defined(TCP_MAXSEG)
int val;
#endif
// Create a non-blocking TCP socket
s = createSocket(dstaddr->ss_family, SOCK_STREAM, IPPROTO_TCP, true);
if (s == INVALID_SOCKET) {
return INVALID_SOCKET;
}
// Some broken routers/firewalls (or routes with multiple broken routers) may result in TCP packets
// being dropped without without us receiving an ICMP Fragmentation Needed packet. For example,
// a router can elect to drop rather than fragment even without DF set. A misconfigured firewall
// or router on the path back to us may block the ICMP Fragmentation Needed packet required for
// PMTUD to work and thus we end up with a black hole route. Some OSes recover from this better
// than others, but we can avoid the issue altogether by capping our MSS to the value mandated
// by RFC 879 and RFC 2460.
//
// Note: This only changes the max packet size we can *receive* from the host PC.
// We still must split our own sends into smaller chunks with TCP_NODELAY enabled to
// avoid MTU issues on the way out to to the target.
#if defined(LC_WINDOWS)
// Windows doesn't support setting TCP_MAXSEG but IP_PMTUDISC_DONT forces the MSS to the protocol
// minimum which is what we want here. Linux doesn't do this (disabling PMTUD just avoids setting DF).
if (dstaddr->ss_family == AF_INET) {
val = IP_PMTUDISC_DONT;
if (setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER, (char*)&val, sizeof(val)) < 0) {
Limelog("setsockopt(IP_MTU_DISCOVER, IP_PMTUDISC_DONT) failed: %d\n", val, (int)LastSocketError());
}
}
else {
val = IP_PMTUDISC_DONT;
if (setsockopt(s, IPPROTO_IPV6, IPV6_MTU_DISCOVER, (char*)&val, sizeof(val)) < 0) {
Limelog("setsockopt(IPV6_MTU_DISCOVER, IP_PMTUDISC_DONT) failed: %d\n", val, (int)LastSocketError());
}
}
#elif defined(TCP_NOOPT)
// On BSD-based OSes (including macOS/iOS), TCP_NOOPT seems to be the only way to
// restrict MSS to the minimum. It strips all options out of the SYN packet which
// forces the remote party to fall back to the minimum MSS. TCP_MAXSEG doesn't seem
// to work correctly for outbound connections on macOS/iOS.
val = 1;
if (setsockopt(s, IPPROTO_TCP, TCP_NOOPT, (char*)&val, sizeof(val)) < 0) {
Limelog("setsockopt(TCP_NOOPT, %d) failed: %d\n", val, (int)LastSocketError());
}
#elif defined(TCP_MAXSEG)
val = dstaddr->ss_family == AF_INET ? TCPv4_MSS : TCPv6_MSS;
if (setsockopt(s, IPPROTO_TCP, TCP_MAXSEG, (char*)&val, sizeof(val)) < 0) {
Limelog("setsockopt(TCP_MAXSEG, %d) failed: %d\n", val, (int)LastSocketError());
}
#endif
// Start connection
memcpy(&addr, dstaddr, addrlen);
SET_PORT(&addr, port);
err = connect(s, (struct sockaddr*) &addr, addrlen);
if (err < 0) {
err = (int)LastSocketError();
if (err != EWOULDBLOCK && err != EAGAIN && err != EINPROGRESS) {
goto Exit;
}
}
// Wait for the connection to complete or the timeout to elapse
pfd.fd = s;
pfd.events = POLLOUT;
err = pollSockets(&pfd, 1, timeoutSec * 1000);
if (err < 0) {
// pollSockets() failed
err = LastSocketError();
Limelog("pollSockets() failed: %d\n", err);
closeSocket(s);
SetLastSocketError(err);
return INVALID_SOCKET;
}
else if (err == 0) {
// pollSockets() timed out
Limelog("Connection timed out after %d seconds (TCP port %u)\n", timeoutSec, port);
closeSocket(s);
SetLastSocketError(ETIMEDOUT);
return INVALID_SOCKET;
}
else {
// The socket was signalled
SOCKADDR_LEN len = sizeof(err);
getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&err, &len);
if (err != 0 || (pfd.revents & POLLERR)) {
// Get the error code
err = (err != 0) ? err : LastSocketFail();
}
}
// Disable non-blocking I/O now that the connection is established
setSocketNonBlocking(s, false);
Exit:
if (err != 0) {
Limelog("connect() failed: %d\n", err);
closeSocket(s);
SetLastSocketError(err);
return INVALID_SOCKET;
}
return s;
}
// See TCP_MAXSEG note in connectTcpSocket() above for more information.
// TCP_NODELAY must be enabled on the socket for this function to work!
int sendMtuSafe(SOCKET s, char* buffer, int size) {
int bytesSent = 0;
while (bytesSent < size) {
int bytesToSend = size - bytesSent > TCPv4_MSS ?
TCPv4_MSS : size - bytesSent;
if (send(s, &buffer[bytesSent], bytesToSend, 0) < 0) {
return -1;
}
bytesSent += bytesToSend;
}
return bytesSent;
}
int enableNoDelay(SOCKET s) {
int err;
int val;
val = 1;
err = setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char*)&val, sizeof(val));
if (err == SOCKET_ERROR) {
return LastSocketError();
}
return 0;
}
int resolveHostName(const char* host, int family, int tcpTestPort, struct sockaddr_storage* addr, SOCKADDR_LEN* addrLen)
{
struct addrinfo hints, *res, *currentAddr;
int err;
memset(&hints, 0, sizeof(hints));
hints.ai_family = family;
hints.ai_flags = AI_ADDRCONFIG;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
err = getaddrinfo(host, NULL, &hints, &res);
if (err != 0) {
Limelog("getaddrinfo(%s) failed: %d\n", host, err);
return err;
}
else if (res == NULL) {
Limelog("getaddrinfo(%s) returned success without addresses\n", host);
return -1;
}
for (currentAddr = res; currentAddr != NULL; currentAddr = currentAddr->ai_next) {
// Use the test port to ensure this address is working if:
// a) We have multiple addresses
// b) The caller asked us to test even with a single address
if (tcpTestPort != 0 && (res->ai_next != NULL || (tcpTestPort & TCP_PORT_FLAG_ALWAYS_TEST))) {
SOCKET testSocket = connectTcpSocket((struct sockaddr_storage*)currentAddr->ai_addr,
(SOCKADDR_LEN)currentAddr->ai_addrlen,
tcpTestPort & TCP_PORT_MASK,
TEST_PORT_TIMEOUT_SEC);
if (testSocket == INVALID_SOCKET) {
// Try the next address
continue;
}
else {
closeSocket(testSocket);
}
}
memcpy(addr, currentAddr->ai_addr, currentAddr->ai_addrlen);
*addrLen = (SOCKADDR_LEN)currentAddr->ai_addrlen;
freeaddrinfo(res);
return 0;
}
Limelog("No working addresses found for host: %s\n", host);
freeaddrinfo(res);
return -1;
}
#ifdef AF_INET6
bool isInSubnetV6(struct sockaddr_in6* sin6, unsigned char* subnet, int prefixLength) {
int i;
for (i = 0; i < prefixLength; i++) {
unsigned char mask = 1 << (i % 8);
if ((sin6->sin6_addr.s6_addr[i / 8] & mask) != (subnet[i / 8] & mask)) {
return false;
}
}
return true;
}
#endif
bool isPrivateNetworkAddress(struct sockaddr_storage* address) {
// We only count IPv4 addresses as possibly private for now
if (address->ss_family == AF_INET) {
unsigned int addr;
memcpy(&addr, &((struct sockaddr_in*)address)->sin_addr, sizeof(addr));
addr = htonl(addr);
// 10.0.0.0/8
if ((addr & 0xFF000000) == 0x0A000000) {
return true;
}
// 172.16.0.0/12
else if ((addr & 0xFFF00000) == 0xAC100000) {
return true;
}
// 192.168.0.0/16
else if ((addr & 0xFFFF0000) == 0xC0A80000) {
return true;
}
// 169.254.0.0/16
else if ((addr & 0xFFFF0000) == 0xA9FE0000) {
return true;
}
}
#ifdef AF_INET6
else if (address->ss_family == AF_INET6) {
struct sockaddr_in6* sin6 = (struct sockaddr_in6*)address;
static unsigned char linkLocalPrefix[] = {0xfe, 0x80};
static unsigned char siteLocalPrefix[] = {0xfe, 0xc0};
static unsigned char uniqueLocalPrefix[] = {0xfc, 0x00};
// fe80::/10
if (isInSubnetV6(sin6, linkLocalPrefix, 10)) {
return true;
}
// fec0::/10
else if (isInSubnetV6(sin6, siteLocalPrefix, 10)) {
return true;
}
// fc00::/7
else if (isInSubnetV6(sin6, uniqueLocalPrefix, 7)) {
return true;
}
}
#endif
return false;
}
// Enable platform-specific low latency options (best-effort)
void enterLowLatencyMode(void) {
#if defined(LC_WINDOWS)
DWORD negotiatedVersion;
PWLAN_INTERFACE_INFO_LIST wlanInterfaceList;
DWORD i;
// Reduce timer period to increase wait precision
timeBeginPeriod(1);
// Load wlanapi.dll dynamically because it will not always be present on Windows Server SKUs.
WlanApiLibraryHandle = LoadLibraryA("wlanapi.dll");
if (WlanApiLibraryHandle == NULL) {
Limelog("WLANAPI is not supported on this OS\n");
return;
}
pfnWlanOpenHandle = (void*)GetProcAddress(WlanApiLibraryHandle, "WlanOpenHandle");
pfnWlanCloseHandle = (void*)GetProcAddress(WlanApiLibraryHandle, "WlanCloseHandle");
pfnWlanFreeMemory = (void*)GetProcAddress(WlanApiLibraryHandle, "WlanFreeMemory");
pfnWlanEnumInterfaces = (void*)GetProcAddress(WlanApiLibraryHandle, "WlanEnumInterfaces");
pfnWlanSetInterface = (void*)GetProcAddress(WlanApiLibraryHandle, "WlanSetInterface");
if (pfnWlanOpenHandle == NULL || pfnWlanCloseHandle == NULL ||
pfnWlanFreeMemory == NULL || pfnWlanEnumInterfaces == NULL || pfnWlanSetInterface == NULL) {
LC_ASSERT(pfnWlanOpenHandle != NULL);
LC_ASSERT(pfnWlanCloseHandle != NULL);
LC_ASSERT(pfnWlanFreeMemory != NULL);
LC_ASSERT(pfnWlanEnumInterfaces != NULL);
LC_ASSERT(pfnWlanSetInterface != NULL);
// This should never happen since that would mean Microsoft removed a public API, but
// we'll check and fail gracefully just in case.
FreeLibrary(WlanApiLibraryHandle);
WlanApiLibraryHandle = NULL;
return;
}
// Use the Vista+ WLAN API version
LC_ASSERT(WlanHandle == NULL);
if (pfnWlanOpenHandle(WLAN_API_MAKE_VERSION(2, 0), NULL, &negotiatedVersion, &WlanHandle) != ERROR_SUCCESS) {
WlanHandle = NULL;
return;
}
if (pfnWlanEnumInterfaces(WlanHandle, NULL, &wlanInterfaceList) != ERROR_SUCCESS) {
pfnWlanCloseHandle(WlanHandle, NULL);
WlanHandle = NULL;
return;
}
for (i = 0; i < wlanInterfaceList->dwNumberOfItems; i++) {
if (wlanInterfaceList->InterfaceInfo[i].isState == wlan_interface_state_connected) {
DWORD error;
BOOL value;
// Enable media streaming mode for 802.11 wireless interfaces to reduce latency and
// unneccessary background scanning operations that cause packet loss and jitter.
//
// https://docs.microsoft.com/en-us/windows-hardware/drivers/network/oid-wdi-set-connection-quality
// https://docs.microsoft.com/en-us/previous-versions/windows/hardware/wireless/native-802-11-media-streaming
value = TRUE;
error = pfnWlanSetInterface(WlanHandle, &wlanInterfaceList->InterfaceInfo[i].InterfaceGuid,
wlan_intf_opcode_media_streaming_mode, sizeof(value), &value, NULL);
if (error == ERROR_SUCCESS) {
Limelog("WLAN interface %d is now in low latency mode\n", i);
}
}
}
pfnWlanFreeMemory(wlanInterfaceList);
#else
#endif
}
void exitLowLatencyMode(void) {
#if defined(LC_WINDOWS)
// Closing our WLAN client handle will undo our optimizations
if (WlanHandle != NULL) {
pfnWlanCloseHandle(WlanHandle, NULL);
WlanHandle = NULL;
}
// Release the library reference to wlanapi.dll
if (WlanApiLibraryHandle != NULL) {
pfnWlanOpenHandle = NULL;
pfnWlanCloseHandle = NULL;
pfnWlanFreeMemory = NULL;
pfnWlanEnumInterfaces = NULL;
pfnWlanSetInterface = NULL;
FreeLibrary(WlanApiLibraryHandle);
WlanApiLibraryHandle = NULL;
}
// Restore original timer period
timeEndPeriod(1);
#else
#endif
}
int initializePlatformSockets(void) {
#if defined(LC_WINDOWS)
WSADATA data;
return WSAStartup(MAKEWORD(2, 0), &data);
#elif defined(__vita__) || defined(__WIIU__)
return 0; // already initialized
#elif defined(LC_POSIX) && !defined(LC_CHROME)
// Disable SIGPIPE signals to avoid us getting
// killed when a socket gets an EPIPE error
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
if (sigaction(SIGPIPE, &sa, 0) == -1) {
perror("sigaction");
return -1;
}
return 0;
#else
return 0;
#endif
}
void cleanupPlatformSockets(void) {
#if defined(LC_WINDOWS)
WSACleanup();
#else
#endif
}