mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2026-06-17 14:21:30 +00:00
Initialize platform sockets in LiFindExternalAddressIP4()
This commit is contained in:
+33
-14
@@ -52,17 +52,23 @@ int LiFindExternalAddressIP4(const char* stunServer, unsigned short stunPort, un
|
|||||||
char buf[1024];
|
char buf[1024];
|
||||||
} resp;
|
} resp;
|
||||||
|
|
||||||
|
err = initializePlatformSockets();
|
||||||
|
if (err != 0) {
|
||||||
|
Limelog("Failed to initialize sockets: %d\n", err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
err = resolveHostName(stunServer, AF_INET, 0, &stunAddr, &stunAddrLen);
|
err = resolveHostName(stunServer, AF_INET, 0, &stunAddr, &stunAddrLen);
|
||||||
if (err != 0) {
|
if (err != 0) {
|
||||||
Limelog("Failed to resolve STUN server: %d\n", err);
|
Limelog("Failed to resolve STUN server: %d\n", err);
|
||||||
return err;
|
goto Exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
sock = bindUdpSocket(AF_INET, 2048);
|
sock = bindUdpSocket(AF_INET, 2048);
|
||||||
if (sock == INVALID_SOCKET) {
|
if (sock == INVALID_SOCKET) {
|
||||||
err = LastSocketFail();
|
err = LastSocketFail();
|
||||||
Limelog("Failed to connect to STUN server: %d\n", err);
|
Limelog("Failed to connect to STUN server: %d\n", err);
|
||||||
return err;
|
goto Exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
reqMsg.messageType = htons(STUN_MESSAGE_BINDING_REQUEST);
|
reqMsg.messageType = htons(STUN_MESSAGE_BINDING_REQUEST);
|
||||||
@@ -84,7 +90,7 @@ int LiFindExternalAddressIP4(const char* stunServer, unsigned short stunPort, un
|
|||||||
err = LastSocketFail();
|
err = LastSocketFail();
|
||||||
Limelog("Failed to send STUN binding request: %d\n", err);
|
Limelog("Failed to send STUN binding request: %d\n", err);
|
||||||
closeSocket(sock);
|
closeSocket(sock);
|
||||||
return err;
|
goto Exit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,28 +108,33 @@ int LiFindExternalAddressIP4(const char* stunServer, unsigned short stunPort, un
|
|||||||
|
|
||||||
if (bytesRead == 0) {
|
if (bytesRead == 0) {
|
||||||
Limelog("No response from STUN server\n");
|
Limelog("No response from STUN server\n");
|
||||||
return -2;
|
err = -2;
|
||||||
|
goto Exit;
|
||||||
}
|
}
|
||||||
else if (bytesRead == SOCKET_ERROR) {
|
else if (bytesRead == SOCKET_ERROR) {
|
||||||
err = LastSocketFail();
|
err = LastSocketFail();
|
||||||
Limelog("Failed to read STUN binding response: %d\n", err);
|
Limelog("Failed to read STUN binding response: %d\n", err);
|
||||||
return err;
|
goto Exit;
|
||||||
}
|
}
|
||||||
else if (bytesRead < sizeof(resp.hdr)) {
|
else if (bytesRead < sizeof(resp.hdr)) {
|
||||||
Limelog("STUN message truncated: %d\n", bytesRead);
|
Limelog("STUN message truncated: %d\n", bytesRead);
|
||||||
return -3;
|
err = -3;
|
||||||
|
goto Exit;
|
||||||
}
|
}
|
||||||
else if (htonl(resp.hdr.magicCookie) != STUN_MESSAGE_COOKIE) {
|
else if (htonl(resp.hdr.magicCookie) != STUN_MESSAGE_COOKIE) {
|
||||||
Limelog("Bad STUN cookie value: %x\n", htonl(resp.hdr.magicCookie));
|
Limelog("Bad STUN cookie value: %x\n", htonl(resp.hdr.magicCookie));
|
||||||
return -3;
|
err = -3;
|
||||||
|
goto Exit;
|
||||||
}
|
}
|
||||||
else if (memcmp(reqMsg.transactionId, resp.hdr.transactionId, sizeof(reqMsg.transactionId))) {
|
else if (memcmp(reqMsg.transactionId, resp.hdr.transactionId, sizeof(reqMsg.transactionId))) {
|
||||||
Limelog("STUN transaction ID mismatch\n");
|
Limelog("STUN transaction ID mismatch\n");
|
||||||
return -3;
|
err = -3;
|
||||||
|
goto Exit;
|
||||||
}
|
}
|
||||||
else if (htons(resp.hdr.messageType) != STUN_MESSAGE_BINDING_SUCCESS) {
|
else if (htons(resp.hdr.messageType) != STUN_MESSAGE_BINDING_SUCCESS) {
|
||||||
Limelog("STUN message type mismatch: %x\n", htons(resp.hdr.messageType));
|
Limelog("STUN message type mismatch: %x\n", htons(resp.hdr.messageType));
|
||||||
return -4;
|
err = -4;
|
||||||
|
goto Exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
attribute = (PSTUN_ATTRIBUTE_HEADER)(&resp.hdr + 1);
|
attribute = (PSTUN_ATTRIBUTE_HEADER)(&resp.hdr + 1);
|
||||||
@@ -131,7 +142,8 @@ int LiFindExternalAddressIP4(const char* stunServer, unsigned short stunPort, un
|
|||||||
while (bytesRead > sizeof(*attribute)) {
|
while (bytesRead > sizeof(*attribute)) {
|
||||||
if (bytesRead < sizeof(*attribute) + htons(attribute->length)) {
|
if (bytesRead < sizeof(*attribute) + htons(attribute->length)) {
|
||||||
Limelog("STUN attribute out of bounds: %d\n", htons(attribute->length));
|
Limelog("STUN attribute out of bounds: %d\n", htons(attribute->length));
|
||||||
return -5;
|
err = -5;
|
||||||
|
goto Exit;
|
||||||
}
|
}
|
||||||
// Mask off the comprehension bit
|
// Mask off the comprehension bit
|
||||||
else if ((htons(attribute->type) & 0x7FFF) != STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS) {
|
else if ((htons(attribute->type) & 0x7FFF) != STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS) {
|
||||||
@@ -144,19 +156,26 @@ int LiFindExternalAddressIP4(const char* stunServer, unsigned short stunPort, un
|
|||||||
ipv4Attrib = (PSTUN_MAPPED_IPV4_ADDRESS_ATTRIBUTE)attribute;
|
ipv4Attrib = (PSTUN_MAPPED_IPV4_ADDRESS_ATTRIBUTE)attribute;
|
||||||
if (htons(ipv4Attrib->hdr.length) != 8) {
|
if (htons(ipv4Attrib->hdr.length) != 8) {
|
||||||
Limelog("STUN address length mismatch: %d\n", htons(ipv4Attrib->hdr.length));
|
Limelog("STUN address length mismatch: %d\n", htons(ipv4Attrib->hdr.length));
|
||||||
return -5;
|
err = -5;
|
||||||
|
goto Exit;
|
||||||
}
|
}
|
||||||
else if (ipv4Attrib->addressFamily != 1) {
|
else if (ipv4Attrib->addressFamily != 1) {
|
||||||
Limelog("STUN address family mismatch: %x\n", ipv4Attrib->addressFamily);
|
Limelog("STUN address family mismatch: %x\n", ipv4Attrib->addressFamily);
|
||||||
return -5;
|
err = -5;
|
||||||
|
goto Exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The address is XORed with the cookie
|
// The address is XORed with the cookie
|
||||||
*wanAddr = ipv4Attrib->address ^ resp.hdr.magicCookie;
|
*wanAddr = ipv4Attrib->address ^ resp.hdr.magicCookie;
|
||||||
|
|
||||||
return 0;
|
err = 0;
|
||||||
|
goto Exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
Limelog("No XOR mapped address found in STUN response!\n");
|
Limelog("No XOR mapped address found in STUN response!\n");
|
||||||
return -6;
|
err = -6;
|
||||||
|
|
||||||
|
Exit:
|
||||||
|
cleanupPlatformSockets();
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user