mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2026-06-18 14:51:30 +00:00
Use resolveHostName() instead of getaddrinfo() and connectTcpSocket() directly
This commit is contained in:
+9
-46
@@ -78,9 +78,8 @@ unsigned short LiGetPortFromPortFlagIndex(int portFlagIndex)
|
|||||||
unsigned int LiTestClientConnectivity(const char* testServer, unsigned short referencePort, unsigned int testPortFlags)
|
unsigned int LiTestClientConnectivity(const char* testServer, unsigned short referencePort, unsigned int testPortFlags)
|
||||||
{
|
{
|
||||||
unsigned int failingPortFlags;
|
unsigned int failingPortFlags;
|
||||||
struct addrinfo* serverAddrs;
|
struct sockaddr_storage address;
|
||||||
struct addrinfo* current;
|
SOCKADDR_LEN address_length;
|
||||||
struct addrinfo hints;
|
|
||||||
int i;
|
int i;
|
||||||
int err;
|
int err;
|
||||||
SOCKET sockets[PORT_FLAGS_MAX_COUNT];
|
SOCKET sockets[PORT_FLAGS_MAX_COUNT];
|
||||||
@@ -103,36 +102,14 @@ unsigned int LiTestClientConnectivity(const char* testServer, unsigned short ref
|
|||||||
return ML_TEST_RESULT_INCONCLUSIVE;
|
return ML_TEST_RESULT_INCONCLUSIVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(&hints, 0, sizeof(hints));
|
err = resolveHostName(testServer, AF_UNSPEC, referencePort, &address, &address_length);
|
||||||
hints.ai_family = AF_UNSPEC;
|
if (err != 0) {
|
||||||
hints.ai_flags = AI_ADDRCONFIG;
|
return ML_TEST_RESULT_INCONCLUSIVE;
|
||||||
|
|
||||||
err = getaddrinfo(testServer, NULL, &hints, &serverAddrs);
|
|
||||||
if (err != 0 || serverAddrs == NULL) {
|
|
||||||
Limelog("Failed to resolve test server: %d\n", err);
|
|
||||||
serverAddrs = NULL;
|
|
||||||
failingPortFlags = ML_TEST_RESULT_INCONCLUSIVE;
|
|
||||||
goto Exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (current = serverAddrs; failingPortFlags != 0 && current != NULL; current = current->ai_next) {
|
|
||||||
// Test to see if this address is even reachable on a standard port.
|
|
||||||
// This will let us distinguish between port-specific blocks and IP-specific blocks.
|
|
||||||
SOCKET testSocket = connectTcpSocket((struct sockaddr_storage*)current->ai_addr,
|
|
||||||
current->ai_addrlen,
|
|
||||||
referencePort,
|
|
||||||
TEST_PORT_TIMEOUT_SEC);
|
|
||||||
if (testSocket == INVALID_SOCKET) {
|
|
||||||
Limelog("Skipping unavailable test host\n");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
closeSocket(testSocket);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < PORT_FLAGS_MAX_COUNT; i++) {
|
for (i = 0; i < PORT_FLAGS_MAX_COUNT; i++) {
|
||||||
if (testPortFlags & (1 << i)) {
|
if (testPortFlags & (1 << i)) {
|
||||||
sockets[i] = socket(current->ai_family,
|
sockets[i] = socket(address.ss_family,
|
||||||
LiGetProtocolFromPortFlagIndex(i) == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM,
|
LiGetProtocolFromPortFlagIndex(i) == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM,
|
||||||
LiGetProtocolFromPortFlagIndex(i));
|
LiGetProtocolFromPortFlagIndex(i));
|
||||||
if (sockets[i] == INVALID_SOCKET) {
|
if (sockets[i] == INVALID_SOCKET) {
|
||||||
@@ -150,7 +127,7 @@ unsigned int LiTestClientConnectivity(const char* testServer, unsigned short ref
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
((struct sockaddr_in6*)current->ai_addr)->sin6_port = htons(LiGetPortFromPortFlagIndex(i));
|
((struct sockaddr_in6*)&address)->sin6_port = htons(LiGetPortFromPortFlagIndex(i));
|
||||||
if (LiGetProtocolFromPortFlagIndex(i) == IPPROTO_TCP) {
|
if (LiGetProtocolFromPortFlagIndex(i) == IPPROTO_TCP) {
|
||||||
// Enable non-blocking I/O for connect timeout support
|
// Enable non-blocking I/O for connect timeout support
|
||||||
if (setSocketNonBlocking(sockets[i] , 1) != 0) {
|
if (setSocketNonBlocking(sockets[i] , 1) != 0) {
|
||||||
@@ -162,7 +139,7 @@ unsigned int LiTestClientConnectivity(const char* testServer, unsigned short ref
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Initiate an asynchronous connection
|
// Initiate an asynchronous connection
|
||||||
err = connect(sockets[i], current->ai_addr, current->ai_addrlen);
|
err = connect(sockets[i], (struct sockaddr*)&address, address_length);
|
||||||
if (err < 0) {
|
if (err < 0) {
|
||||||
err = (int)LastSocketError();
|
err = (int)LastSocketError();
|
||||||
if (err != EWOULDBLOCK && err != EAGAIN && err != EINPROGRESS) {
|
if (err != EWOULDBLOCK && err != EAGAIN && err != EINPROGRESS) {
|
||||||
@@ -179,7 +156,7 @@ unsigned int LiTestClientConnectivity(const char* testServer, unsigned short ref
|
|||||||
|
|
||||||
// Send a few packets since UDP is unreliable
|
// Send a few packets since UDP is unreliable
|
||||||
for (j = 0; j < 3; j++) {
|
for (j = 0; j < 3; j++) {
|
||||||
err = sendto(sockets[i], buf, sizeof(buf), 0, current->ai_addr, current->ai_addrlen);
|
err = sendto(sockets[i], buf, sizeof(buf), 0, (struct sockaddr*)&address, address_length);
|
||||||
if (err < 0) {
|
if (err < 0) {
|
||||||
err = (int)LastSocketError();
|
err = (int)LastSocketError();
|
||||||
Limelog("Failed to send test packet to UDP %u: %d\n", LiGetPortFromPortFlagIndex(i), err);
|
Limelog("Failed to send test packet to UDP %u: %d\n", LiGetPortFromPortFlagIndex(i), err);
|
||||||
@@ -304,16 +281,6 @@ unsigned int LiTestClientConnectivity(const char* testServer, unsigned short ref
|
|||||||
// call select() again to wait on the remaining sockets.
|
// call select() again to wait on the remaining sockets.
|
||||||
}
|
}
|
||||||
|
|
||||||
// We don't need to try another server if we got this far
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current == NULL) {
|
|
||||||
// None of the addresses we were given worked
|
|
||||||
failingPortFlags = ML_TEST_RESULT_INCONCLUSIVE;
|
|
||||||
goto Exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
Exit:
|
Exit:
|
||||||
for (i = 0; i < PORT_FLAGS_MAX_COUNT; i++) {
|
for (i = 0; i < PORT_FLAGS_MAX_COUNT; i++) {
|
||||||
if (sockets[i] != INVALID_SOCKET) {
|
if (sockets[i] != INVALID_SOCKET) {
|
||||||
@@ -321,10 +288,6 @@ Exit:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (serverAddrs != NULL) {
|
|
||||||
freeaddrinfo(serverAddrs);
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanupPlatformSockets();
|
cleanupPlatformSockets();
|
||||||
return failingPortFlags;
|
return failingPortFlags;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user