Compare commits

..
17 Commits
Author SHA1 Message Date
Cameron Gutman 021474cdd2 Version 5.4 2020-09-07 11:15:52 -07:00
Cameron Gutman 308cd0dd00 Use dynamically allocated buffer for GetAdaptersAddresses() 2020-09-07 10:14:54 -07:00
Cameron Gutman 83360d775b Ignore ICMP Port Unreachable messages during STUN 2020-09-06 15:12:16 -07:00
Cameron Gutman cdbaa0aff3 Version 5.3.1 2020-08-30 13:47:35 -07:00
Cameron Gutman 36ea86faaa Configure SCM to restart our service if it crashes 2020-08-29 22:43:41 -07:00
Cameron Gutman 8590a0813b Stop testing after the first IPv6 relay server 2020-08-28 18:46:56 -07:00
Cameron Gutman 51ae1c8770 Fix socket errors being clobbered by closesocket() 2020-08-28 18:46:36 -07:00
Cameron Gutman f1ca6a71f0 Version 5.3 2020-08-14 18:35:19 -07:00
Cameron Gutman ac92212464 Fix flip-flopped HTTP and HTTPS rule IDs 2020-08-14 17:57:45 -07:00
Cameron Gutman dbf43ac7a1 Don't attempt to relocate WoL port 9 2020-08-14 17:54:48 -07:00
Cameron Gutman 893aa76c9c Replace hardcoded constant with #define 2020-08-14 17:52:59 -07:00
Cameron Gutman 21d8c71a2c Log the internal port for the UPnP mappings 2020-08-14 17:51:06 -07:00
Cameron Gutman 41ef072c9b Validate the port number of loopback traffic 2020-08-14 17:48:55 -07:00
Cameron Gutman 53246bd4c5 Don't redirect stdout for standalone exe invocation 2020-08-14 17:48:14 -07:00
Cameron Gutman ac850e79d8 Add firewall rules for GameStream just in case GFE didn't 2020-08-12 20:54:34 -07:00
Cameron Gutman 944c8993e8 Elevate priorities for the UDP relay threads 2020-08-11 01:12:48 -07:00
Cameron Gutman b6508d9024 Remove superfluous select() call 2020-08-11 00:59:13 -07:00
7 changed files with 233 additions and 140 deletions
+38
View File
@@ -70,6 +70,7 @@
ErrorControl="ignore"
Interactive="no">
<ServiceConfig DelayedAutoStart="no" OnInstall="yes" OnReinstall="yes" ServiceSid="unrestricted"/>
<util:ServiceConfig FirstFailureActionType="restart" SecondFailureActionType="restart" ThirdFailureActionType="restart" ResetPeriodInDays="1" RestartServiceDelayInSeconds="10"/>
</ServiceInstall>
<ServiceControl Id="StartService" Start="install" Stop="both" Remove="uninstall" Name="MISS" Wait="yes" />
</Component>
@@ -80,6 +81,43 @@
Name="Moonlight Internet Streaming Tester"/>
</File>
</Component>
<Component Id="GameStreamFirewallRules" Guid="{16CA2511-B533-46F8-8882-F8AEEFDFD7DF}" KeyPath="yes">
<fire:FirewallException Id="HttpsFwException"
Scope="any"
Port="47984"
Protocol="tcp"
Name="Moonlight - HTTPS"/>
<fire:FirewallException Id="HttpFwException"
Scope="any"
Port="47989"
Protocol="tcp"
Name="Moonlight - HTTP"/>
<fire:FirewallException Id="RtspFwException"
Scope="any"
Port="48010"
Protocol="tcp"
Name="Moonlight - RTSP"/>
<fire:FirewallException Id="VideoFwException"
Scope="any"
Port="47998"
Protocol="udp"
Name="Moonlight - Video"/>
<fire:FirewallException Id="ControlFwException"
Scope="any"
Port="47999"
Protocol="udp"
Name="Moonlight - Control"/>
<fire:FirewallException Id="AudioFwException"
Scope="any"
Port="48000"
Protocol="udp"
Name="Moonlight - Audio"/>
<fire:FirewallException Id="RtspuFwException"
Scope="any"
Port="48010"
Protocol="udp"
Name="Moonlight - RTSPU"/>
</Component>
<Component Id="Shortcuts" Guid="*">
<Shortcut Id="StartMenuShortcut"
Name="Moonlight Internet Streaming Tester"
+57 -38
View File
@@ -39,6 +39,7 @@ bool PCPMapPort(PSOCKADDR_STORAGE localAddr, int localAddrLen, PSOCKADDR_STORAGE
#define POLLING_DELAY_SEC 120
#define PORT_MAPPING_DURATION_SEC 3600
#define UPNP_DISCOVERY_DELAY_MS 5000
#define GAA_INITIAL_SIZE 8192
static struct port_entry {
int proto;
@@ -116,7 +117,7 @@ bool UPnPMapPort(struct UPNPUrls* urls, struct IGDdatas* data, int proto, const
// Some routers change the description, so we can't check that here
if (!strcmp(intClient, myAddr)) {
if (atoi(leaseDuration) == 0) {
printf("OK (Static)" NL);
printf("OK (Static, Internal port: %s)" NL, intPort);
// If we have an existing permanent mapping, we can just leave it alone.
if (enable) {
@@ -124,7 +125,7 @@ bool UPnPMapPort(struct UPNPUrls* urls, struct IGDdatas* data, int proto, const
}
}
else {
printf("OK (%s seconds remaining)" NL, leaseDuration);
printf("OK (%s seconds remaining, Internal port: %s)" NL, leaseDuration, intPort);
}
// If we're just validating, we found an entry, so we're done.
@@ -206,7 +207,7 @@ bool UPnPMapPort(struct UPNPUrls* urls, struct IGDdatas* data, int proto, const
else if (indefinite) {
printf("STATIC ");
}
if (err == 718 && proto == IPPROTO_UDP) { // ConflictInMappingEntry
if (err == 718 && proto == IPPROTO_UDP && port >= 47000) { // ConflictInMappingEntry
// Some UPnP implementations incorrectly deduplicate on the internal port instead
// of the external port, in violation of the UPnP IGD specification. Since GFE creates
// mappings on the same internal port as us, those routers break our mappings. To
@@ -233,10 +234,7 @@ bool UPnPMapPort(struct UPNPUrls* urls, struct IGDdatas* data, int proto, const
bool GetIP4OnLinkPrefixLength(char* lanAddressString, int* prefixLength)
{
union {
IP_ADAPTER_ADDRESSES addresses;
char buffer[8192];
};
PIP_ADAPTER_ADDRESSES addresses;
ULONG error;
ULONG length;
PIP_ADAPTER_ADDRESSES currentAdapter;
@@ -245,22 +243,34 @@ bool GetIP4OnLinkPrefixLength(char* lanAddressString, int* prefixLength)
inet_pton(AF_INET, lanAddressString, &targetAddress);
// Get a list of all interfaces with IPv4 addresses on the system
length = sizeof(buffer);
error = GetAdaptersAddresses(AF_INET,
GAA_FLAG_SKIP_ANYCAST |
GAA_FLAG_SKIP_MULTICAST |
GAA_FLAG_SKIP_DNS_SERVER |
GAA_FLAG_SKIP_FRIENDLY_NAME,
NULL,
&addresses,
&length);
addresses = NULL;
length = GAA_INITIAL_SIZE;
do {
free(addresses);
addresses = (PIP_ADAPTER_ADDRESSES)malloc(length);
if (addresses == NULL) {
printf("malloc(%u) failed" NL, length);
return false;
}
// Get a list of all interfaces with IPv4 addresses on the system
error = GetAdaptersAddresses(AF_INET,
GAA_FLAG_SKIP_ANYCAST |
GAA_FLAG_SKIP_MULTICAST |
GAA_FLAG_SKIP_DNS_SERVER |
GAA_FLAG_SKIP_FRIENDLY_NAME,
NULL,
addresses,
&length);
} while (error == ERROR_BUFFER_OVERFLOW);
if (error != ERROR_SUCCESS) {
printf("GetAdaptersAddresses() failed: %d" NL, error);
free(addresses);
return false;
}
currentAdapter = &addresses;
currentAdapter = addresses;
currentAddress = nullptr;
while (currentAdapter != nullptr) {
currentAddress = currentAdapter->FirstUnicastAddress;
@@ -271,6 +281,7 @@ bool GetIP4OnLinkPrefixLength(char* lanAddressString, int* prefixLength)
if (RtlEqualMemory(&currentAddrV4->sin_addr, &targetAddress, sizeof(targetAddress))) {
*prefixLength = currentAddress->OnLinkPrefixLength;
free(addresses);
return true;
}
@@ -281,6 +292,7 @@ bool GetIP4OnLinkPrefixLength(char* lanAddressString, int* prefixLength)
}
printf("No adapter found with IPv4 address: %s" NL, lanAddressString);
free(addresses);
return false;
}
@@ -415,7 +427,7 @@ bool NATPMPMapPort(natpmp_t* natpmp, int proto, int port, bool enable, bool inde
lifetime = 604800; // 1 week
}
else {
lifetime = 3600;
lifetime = PORT_MAPPING_DURATION_SEC;
}
printf("Updating NAT-PMP port mapping for %s %d...", proto == IPPROTO_TCP ? "TCP" : "UDP", port);
@@ -805,25 +817,28 @@ void NETIOAPI_API_ IpInterfaceChangeNotificationCallback(PVOID context, PMIB_IPI
SetEvent((HANDLE)context);
}
void ResetLogFile()
void ResetLogFile(bool standaloneExe)
{
char oldLogFilePath[MAX_PATH + 1];
char currentLogFilePath[MAX_PATH + 1];
char timeString[MAX_PATH + 1] = {};
SYSTEMTIME time;
ExpandEnvironmentStringsA("%ProgramData%\\MISS\\miss-old.log", oldLogFilePath, sizeof(oldLogFilePath));
ExpandEnvironmentStringsA("%ProgramData%\\MISS\\miss-current.log", currentLogFilePath, sizeof(currentLogFilePath));
if (!standaloneExe) {
char oldLogFilePath[MAX_PATH + 1];
char currentLogFilePath[MAX_PATH + 1];
// Close the existing stdout handle. This is important because otherwise
// it may still be open as stdout when we try to MoveFileEx below.
fclose(stdout);
ExpandEnvironmentStringsA("%ProgramData%\\MISS\\miss-old.log", oldLogFilePath, sizeof(oldLogFilePath));
ExpandEnvironmentStringsA("%ProgramData%\\MISS\\miss-current.log", currentLogFilePath, sizeof(currentLogFilePath));
// Rotate the current to the old log file
MoveFileExA(currentLogFilePath, oldLogFilePath, MOVEFILE_REPLACE_EXISTING);
// Close the existing stdout handle. This is important because otherwise
// it may still be open as stdout when we try to MoveFileEx below.
fclose(stdout);
// Redirect stdout to this new file
freopen(currentLogFilePath, "w", stdout);
// Rotate the current to the old log file
MoveFileExA(currentLogFilePath, oldLogFilePath, MOVEFILE_REPLACE_EXISTING);
// Redirect stdout to this new file
freopen(currentLogFilePath, "w", stdout);
}
// Print a log header
printf("Moonlight Internet Streaming Service v" VER_VERSION_STR NL);
@@ -860,13 +875,17 @@ DWORD WINAPI GameStreamStateChangeThread(PVOID Context)
return err;
}
int Run()
int Run(bool standaloneExe)
{
HANDLE ifaceChangeEvent = CreateEvent(nullptr, true, false, nullptr);
HANDLE gsChangeEvent = CreateEvent(nullptr, true, false, nullptr);
HANDLE events[2] = { ifaceChangeEvent, gsChangeEvent };
ResetLogFile();
ResetLogFile(standaloneExe);
// Bump the process priority class to above normal. The UDP relay threads will
// further raise their own thread priorities to avoid preemption by other activity.
SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
// Create the UDP alternate port relays
for (int i = 0; i < ARRAYSIZE(k_Ports); i++) {
@@ -895,7 +914,7 @@ int Run()
ULONGLONG beforeSleepTime = GetTickCount64();
DWORD ret = WaitForMultipleObjects(ARRAYSIZE(events), events, false, POLLING_DELAY_SEC * 1000);
if (ret == WAIT_OBJECT_0) {
ResetLogFile();
ResetLogFile(standaloneExe);
printf("Woke up for interface change notification after %lld seconds" NL,
(GetTickCount64() - beforeSleepTime) / 1000);
@@ -904,13 +923,13 @@ int Run()
Sleep(10000);
}
else if (ret == WAIT_OBJECT_0 + 1) {
ResetLogFile();
ResetLogFile(standaloneExe);
printf("Woke up for GameStream state change notification after %lld seconds" NL,
(GetTickCount64() - beforeSleepTime) / 1000);
}
else {
ResetLogFile();
ResetLogFile(standaloneExe);
printf("Woke up for periodic refresh" NL);
}
@@ -971,7 +990,7 @@ ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
// Start the service
err = Run();
err = Run(false);
if (err != 0) {
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = err;
@@ -994,7 +1013,7 @@ int main(int argc, char* argv[])
}
if (argc == 2 && !strcmp(argv[1], "exe")) {
Run();
Run(true);
return 0;
}
+20 -22
View File
@@ -19,34 +19,28 @@ WINAPI
UdpRelayThreadProc(LPVOID Context)
{
PUDP_TUPLE tuple = (PUDP_TUPLE)Context;
fd_set fds;
int err;
USHORT nboPort = htons(tuple->port);
SOCKADDR_IN lastRemoteAddr;
// Ensure the relay threads aren't preempted by games or other CPU intensive activity
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
RtlZeroMemory(&lastRemoteAddr, sizeof(lastRemoteAddr));
for (;;) {
char buffer[4096];
SOCKADDR_IN sourceAddr;
int sourceAddrLen;
FD_ZERO(&fds);
FD_SET(tuple->socket, &fds);
err = select(0, &fds, NULL, NULL, NULL);
if (err <= 0) {
break;
}
int recvLen;
sourceAddrLen = sizeof(sourceAddr);
err = recvfrom(tuple->socket, buffer, sizeof(buffer), 0, (PSOCKADDR)&sourceAddr, &sourceAddrLen);
if (err == SOCKET_ERROR) {
recvLen = recvfrom(tuple->socket, buffer, sizeof(buffer), 0, (PSOCKADDR)&sourceAddr, &sourceAddrLen);
if (recvLen == SOCKET_ERROR) {
continue;
}
SOCKADDR_IN destinationAddr;
if (RtlEqualMemory(&sourceAddr.sin_addr, &in4addr_loopback, sizeof(sourceAddr.sin_addr))) {
if (RtlEqualMemory(&sourceAddr.sin_addr, &in4addr_loopback, sizeof(sourceAddr.sin_addr)) && sourceAddr.sin_port == nboPort) {
// Traffic incoming from loopback interface - send it to the last remote address
destinationAddr = lastRemoteAddr;
}
@@ -57,10 +51,10 @@ UdpRelayThreadProc(LPVOID Context)
// Send it to the normal port via the loopback adapter
destinationAddr = sourceAddr;
destinationAddr.sin_addr = in4addr_loopback;
destinationAddr.sin_port = htons(tuple->port);
destinationAddr.sin_port = nboPort;
}
sendto(tuple->socket, buffer, err, 0, (PSOCKADDR)&destinationAddr, sizeof(destinationAddr));
sendto(tuple->socket, buffer, recvLen, 0, (PSOCKADDR)&destinationAddr, sizeof(destinationAddr));
}
closesocket(tuple->socket);
@@ -74,11 +68,13 @@ int StartUdpRelay(unsigned short Port)
SOCKADDR_IN addr;
HANDLE thread;
PUDP_TUPLE tuple;
int error;
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == INVALID_SOCKET) {
printf("socket() failed: %d\n", WSAGetLastError());
return WSAGetLastError();
error = WSAGetLastError();
printf("socket() failed: %d\n", error);
return error;
}
// Bind to the alternate port
@@ -86,9 +82,10 @@ int StartUdpRelay(unsigned short Port)
addr.sin_family = AF_INET;
addr.sin_port = htons(Port + RELAY_PORT_OFFSET);
if (bind(sock, (PSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR) {
printf("bind() failed: %d\n", WSAGetLastError());
error = WSAGetLastError();
printf("bind() failed: %d\n", error);
closesocket(sock);
return WSAGetLastError();
return error;
}
tuple = (PUDP_TUPLE)malloc(sizeof(*tuple));
@@ -101,9 +98,10 @@ int StartUdpRelay(unsigned short Port)
thread = CreateThread(NULL, 0, UdpRelayThreadProc, tuple, 0, NULL);
if (thread == NULL) {
printf("CreateThread() failed: %d\n", GetLastError());
error = GetLastError();
printf("CreateThread() failed: %d\n", error);
closesocket(sock);
return GetLastError();
return error;
}
CloseHandle(thread);
+59 -37
View File
@@ -29,6 +29,8 @@
#define LOOPBACK_SERVER_PORT_OFFSET -10000
#define GAA_INITIAL_SIZE 8192
static struct port_entry {
int proto;
int port;
@@ -864,31 +866,40 @@ bool FindLocalInterfaceIPAddress(int family, PSOCKADDR_STORAGE addr)
bool FindZeroTierInterfaceAddress(PSOCKADDR_STORAGE addr)
{
union {
IP_ADAPTER_ADDRESSES addresses;
char buffer[8192];
};
PIP_ADAPTER_ADDRESSES addresses;
ULONG error;
ULONG length;
PIP_ADAPTER_ADDRESSES currentAdapter;
PIP_ADAPTER_UNICAST_ADDRESS currentAddress;
// Get all IPv4 interfaces
length = sizeof(buffer);
error = GetAdaptersAddresses(AF_INET,
GAA_FLAG_SKIP_ANYCAST |
GAA_FLAG_SKIP_MULTICAST |
GAA_FLAG_SKIP_DNS_SERVER |
GAA_FLAG_SKIP_FRIENDLY_NAME,
NULL,
&addresses,
&length);
addresses = NULL;
length = GAA_INITIAL_SIZE;
do {
free(addresses);
addresses = (PIP_ADAPTER_ADDRESSES)malloc(length);
if (addresses == NULL) {
fprintf(LOG_OUT, "malloc(%u) failed\n", length);
return false;
}
// Get all IPv4 interfaces
error = GetAdaptersAddresses(AF_INET,
GAA_FLAG_SKIP_ANYCAST |
GAA_FLAG_SKIP_MULTICAST |
GAA_FLAG_SKIP_DNS_SERVER |
GAA_FLAG_SKIP_FRIENDLY_NAME,
NULL,
addresses,
&length);
} while (error == ERROR_BUFFER_OVERFLOW);
if (error != ERROR_SUCCESS) {
fprintf(LOG_OUT, "GetAdaptersAddresses() failed: %d\n", error);
free(addresses);
return false;
}
currentAdapter = &addresses;
currentAdapter = addresses;
while (currentAdapter != NULL) {
// Look for ones that correspond to a ZeroTier device
if (wcsstr(currentAdapter->Description, L"ZeroTier")) {
@@ -897,6 +908,7 @@ bool FindZeroTierInterfaceAddress(PSOCKADDR_STORAGE addr)
while (currentAddress != NULL) {
if (currentAddress->Address.lpSockaddr->sa_family == AF_INET) {
RtlCopyMemory(addr, currentAddress->Address.lpSockaddr, currentAddress->Address.iSockaddrLength);
free(addresses);
return true;
}
@@ -907,15 +919,13 @@ bool FindZeroTierInterfaceAddress(PSOCKADDR_STORAGE addr)
currentAdapter = currentAdapter->Next;
}
free(addresses);
return false;
}
bool FindDuplicateDefaultInterfaces(void)
{
union {
IP_ADAPTER_ADDRESSES addresses;
char buffer[8192];
};
PIP_ADAPTER_ADDRESSES addresses;
ULONG error;
ULONG length;
MIB_IPFORWARDROW defaultRoute;
@@ -928,24 +938,36 @@ bool FindDuplicateDefaultInterfaces(void)
return false;
}
// Get all IPv4 interfaces
length = sizeof(buffer);
error = GetAdaptersAddresses(AF_INET,
GAA_FLAG_SKIP_UNICAST |
GAA_FLAG_SKIP_ANYCAST |
GAA_FLAG_SKIP_MULTICAST |
GAA_FLAG_SKIP_DNS_SERVER |
GAA_FLAG_INCLUDE_GATEWAYS |
GAA_FLAG_SKIP_FRIENDLY_NAME,
NULL,
&addresses,
&length);
addresses = NULL;
length = GAA_INITIAL_SIZE;
do {
free(addresses);
addresses = (PIP_ADAPTER_ADDRESSES)malloc(length);
if (addresses == NULL) {
fprintf(LOG_OUT, "malloc(%u) failed\n", length);
return false;
}
// Get all IPv4 interfaces
error = GetAdaptersAddresses(AF_INET,
GAA_FLAG_SKIP_UNICAST |
GAA_FLAG_SKIP_ANYCAST |
GAA_FLAG_SKIP_MULTICAST |
GAA_FLAG_SKIP_DNS_SERVER |
GAA_FLAG_INCLUDE_GATEWAYS |
GAA_FLAG_SKIP_FRIENDLY_NAME,
NULL,
addresses,
&length);
} while (error == ERROR_BUFFER_OVERFLOW);
if (error != ERROR_SUCCESS) {
fprintf(LOG_OUT, "GetAdaptersAddresses() failed: %d\n", error);
free(addresses);
return false;
}
currentAdapter = &addresses;
currentAdapter = addresses;
while (currentAdapter != NULL) {
if (currentAdapter->OperStatus == IfOperStatusUp &&
currentAdapter->FirstGatewayAddress != NULL &&
@@ -958,6 +980,7 @@ bool FindDuplicateDefaultInterfaces(void)
currentAdapter = currentAdapter->Next;
}
free(addresses);
return matchingInterfaces > 1;
}
@@ -1002,7 +1025,7 @@ UPnPPortStatus UPnPCheckPort(struct UPNPUrls* urls, struct IGDdatas* data, int p
}
else if (err == UPNPCOMMAND_SUCCESS) {
if (!strcmp(myAddr, intClient)) {
fprintf(LOG_OUT, "OK\n");
fprintf(LOG_OUT, "OK (Internal port: %s)\n", intPort);
return OK;
}
else {
@@ -1490,10 +1513,9 @@ int main(int argc, char* argv[])
return 0;
}
}
else {
// Tested against a working server and it failed
break;
}
// Tested against a IPv6 working server
break;
}
}
+56 -40
View File
@@ -45,15 +45,20 @@ bool getExternalAddressPortIP4(unsigned short localPort, PSOCKADDR_IN wanAddr)
int i;
int bytesRead;
int err;
bool ret;
PSTUN_ATTRIBUTE_HEADER attribute;
PSTUN_MAPPED_IPV4_ADDRESS_ATTRIBUTE ipv4Attrib;
struct addrinfo* result;
struct addrinfo hints;
struct sockaddr_in bindAddr;
union {
STUN_MESSAGE hdr;
char buf[1024];
} resp;
sock = INVALID_SOCKET;
ret = false;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_flags = AI_ADDRCONFIG;
@@ -68,18 +73,15 @@ bool getExternalAddressPortIP4(unsigned short localPort, PSOCKADDR_IN wanAddr)
sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol);
if (sock == INVALID_SOCKET) {
fprintf(LOG_OUT, "socket() failed: %d\n", WSAGetLastError());
freeaddrinfo(result);
return false;
goto Exit;
}
struct sockaddr_in bindAddr = {};
bindAddr = {};
bindAddr.sin_family = hints.ai_family;
bindAddr.sin_port = htons(localPort);
if (bind(sock, (struct sockaddr*)&bindAddr, sizeof(bindAddr)) == SOCKET_ERROR) {
fprintf(LOG_OUT, "bind() failed: %d\n", WSAGetLastError());
closesocket(sock);
freeaddrinfo(result);
return false;
goto Exit;
}
reqMsg.messageType = htons(STUN_MESSAGE_BINDING_REQUEST);
@@ -98,57 +100,60 @@ bool getExternalAddressPortIP4(unsigned short localPort, PSOCKADDR_IN wanAddr)
}
}
fd_set fds;
FD_ZERO(&fds);
FD_SET(sock, &fds);
for (;;) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(sock, &fds);
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
int selectRes = select(0, &fds, nullptr, nullptr, &tv);
if (selectRes == 0) {
// Timeout - continue looping
continue;
int selectRes = select(0, &fds, nullptr, nullptr, &tv);
if (selectRes == 0) {
// Timeout - break to the enclosing loop
break;
}
else if (selectRes == SOCKET_ERROR) {
fprintf(LOG_OUT, "select() failed: %d\n", WSAGetLastError());
goto Exit;
}
// recvfrom() will return WSAECONNRESET if the socket has received an ICMP Port Unreachable
// message from one of the IP addresses we've attempted communication with. The socket is still
// operable (and may even contain queued messages to receive). We should ignore that error and
// continue reading, otherwise exit the loop.
bytesRead = recvfrom(sock, resp.buf, sizeof(resp.buf), 0, NULL, NULL);
if (bytesRead != SOCKET_ERROR || WSAGetLastError() != WSAECONNRESET) {
goto RecvDone;
}
}
else if (selectRes == SOCKET_ERROR) {
fprintf(LOG_OUT, "select() failed: %d\n", WSAGetLastError());
closesocket(sock);
freeaddrinfo(result);
return false;
}
// Error handling is below
bytesRead = recvfrom(sock, resp.buf, sizeof(resp.buf), 0, NULL, NULL);
break;
}
freeaddrinfo(result);
closesocket(sock);
RecvDone:
if (bytesRead == 0) {
fprintf(LOG_OUT, "No response from STUN server\n");
return false;
goto Exit;
}
else if (bytesRead == SOCKET_ERROR) {
fprintf(LOG_OUT, "Failed to read STUN binding response: %d\n", WSAGetLastError());
return false;
goto Exit;
}
else if (bytesRead < sizeof(resp.hdr)) {
fprintf(LOG_OUT, "STUN message truncated: %d\n", bytesRead);
return false;
goto Exit;
}
else if (htonl(resp.hdr.magicCookie) != STUN_MESSAGE_COOKIE) {
fprintf(LOG_OUT, "Bad STUN cookie value: %x\n", htonl(resp.hdr.magicCookie));
return false;
goto Exit;
}
else if (memcmp(reqMsg.transactionId, resp.hdr.transactionId, sizeof(reqMsg.transactionId))) {
fprintf(LOG_OUT, "STUN transaction ID mismatch\n");
return false;
goto Exit;
}
else if (htons(resp.hdr.messageType) != STUN_MESSAGE_BINDING_SUCCESS) {
fprintf(LOG_OUT, "STUN message type mismatch: %x\n", htons(resp.hdr.messageType));
return false;
goto Exit;
}
attribute = (PSTUN_ATTRIBUTE_HEADER)(&resp.hdr + 1);
@@ -156,7 +161,7 @@ bool getExternalAddressPortIP4(unsigned short localPort, PSOCKADDR_IN wanAddr)
while (bytesRead > sizeof(*attribute)) {
if (bytesRead < sizeof(*attribute) + htons(attribute->length)) {
fprintf(LOG_OUT, "STUN attribute out of bounds: %d\n", htons(attribute->length));
return false;
goto Exit;
}
// Mask off the comprehension bit
else if ((htons(attribute->type) & 0x7FFF) != STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS) {
@@ -169,11 +174,11 @@ bool getExternalAddressPortIP4(unsigned short localPort, PSOCKADDR_IN wanAddr)
ipv4Attrib = (PSTUN_MAPPED_IPV4_ADDRESS_ATTRIBUTE)attribute;
if (htons(ipv4Attrib->hdr.length) != 8) {
fprintf(LOG_OUT, "STUN address length mismatch: %d\n", htons(ipv4Attrib->hdr.length));
return false;
goto Exit;
}
else if (ipv4Attrib->addressFamily != 1) {
fprintf(LOG_OUT, "STUN address family mismatch: %x\n", ipv4Attrib->addressFamily);
return false;
goto Exit;
}
*wanAddr = {};
@@ -183,9 +188,20 @@ bool getExternalAddressPortIP4(unsigned short localPort, PSOCKADDR_IN wanAddr)
wanAddr->sin_port = ipv4Attrib->port ^ (short)resp.hdr.magicCookie;
wanAddr->sin_addr.S_un.S_addr = ipv4Attrib->address ^ resp.hdr.magicCookie;
return true;
ret = true;
goto Exit;
}
fprintf(LOG_OUT, "No XOR mapped address found in STUN response!\n");
return false;
Exit:
if (sock != INVALID_SOCKET) {
closesocket(sock);
}
if (result != NULL) {
freeaddrinfo(result);
}
return ret;
}
+2 -2
View File
@@ -1,7 +1,7 @@
#pragma once
#define VER_VERSION 5,2,0,0
#define VER_VERSION_STR "5.2.0.0"
#define VER_VERSION 5,4,0,0
#define VER_VERSION_STR "5.4.0.0"
#define VER_COMPANYNAME_STR "Moonlight Game Streaming Project"
#define VER_PRODUCTNAME_STR "Moonlight Internet Hosting Tool"