Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

fix(net): support multi-IPv4 fallback and stop misclassifying TCP errno 61 as DNS error (closes #357)

Rafael Abud Ferreira96d23e2a390b
diff --git a/.github/triage/rules.json b/.github/triage/rules.json
index 4877570..6f30b31 100644
--- a/.github/triage/rules.json
+++ b/.github/triage/rules.json
@@ -9,15 +9,14 @@
"0x804101e1",
"dns lookup failed",
"sinkhole ip",
- "connect failed to (?:login\\.live\\.com|[^\\s]+\\.xboxlive\\.com|[^\\s]+\\.microsoft\\.com)[^\\n]*errno 61",
"dns.*bloqueado",
"dns.*servidores da microsoft",
"dns.*microsoft servers"
],
"fixed_in": "1.3.2",
- "reply": "**DNS blocked or sinkholed** (`0x8041013d` / `0x804101e1` / `errno 61`): the DNS configured on your PS5 or router is blocking Microsoft/Xbox domains (`login.live.com`, `*.xboxlive.com`). To keep Sony updates blocked while allowing Xbox Cloud Gaming, run the **NanoDNS** payload on your PS5 and set the PS5 network **Primary DNS** to your PS5's local IP address and **Secondary DNS** to `0.0.0.0` (or use `1.1.1.1` / `8.8.8.8` if you don't use a DNS blocker).",
- "reply_pt": "**DNS bloqueado ou redirecionado (sinkhole)** (`0x8041013d` / `0x804101e1` / `errno 61`): o DNS configurado no seu PS5 ou roteador está bloqueando os domínios da Microsoft/Xbox (`login.live.com`, `*.xboxlive.com`). Para manter os servidores da Sony bloqueados sem bloquear o Xbox Cloud Gaming, execute o payload **NanoDNS** no próprio PS5 e configure na rede do console o **DNS Primário** com o IP local do seu PS5 e o **DNS Secundário** como `0.0.0.0` (ou use `1.1.1.1` / `8.8.8.8` caso não utilize bloqueio de DNS).",
- "reply_es": "**DNS bloqueado o redirigido (sinkhole)** (`0x8041013d` / `0x804101e1` / `errno 61`): el DNS configurado en tu PS5 o router está bloqueando los dominios de Microsoft/Xbox (`login.live.com`, `*.xboxlive.com`). Para mantener bloqueados los servidores de Sony sin bloquear Xbox Cloud Gaming, ejecuta el payload **NanoDNS** en tu PS5 y configura en la red de la consola el **DNS Primario** con la IP local de tu PS5 y el **DNS Secundario** como `0.0.0.0` (o usa `1.1.1.1` / `8.8.8.8` si no usas bloqueador de DNS)."
+ "reply": "**DNS blocked or sinkholed** (`0x8041013d` / `0x804101e1`): the DNS configured on your PS5 or router is blocking Microsoft/Xbox domains (`login.live.com`, `*.xboxlive.com`). To keep Sony updates blocked while allowing Xbox Cloud Gaming, run the **NanoDNS** payload on your PS5 and set the PS5 network **Primary DNS** to your PS5's local IP address and **Secondary DNS** to `0.0.0.0` (or use `1.1.1.1` / `8.8.8.8` if you don't use a DNS blocker).",
+ "reply_pt": "**DNS bloqueado ou redirecionado (sinkhole)** (`0x8041013d` / `0x804101e1`): o DNS configurado no seu PS5 ou roteador está bloqueando os domínios da Microsoft/Xbox (`login.live.com`, `*.xboxlive.com`). Para manter os servidores da Sony bloqueados sem bloquear o Xbox Cloud Gaming, execute o payload **NanoDNS** no próprio PS5 e configure na rede do console o **DNS Primário** com o IP local do seu PS5 e o **DNS Secundário** como `0.0.0.0` (ou use `1.1.1.1` / `8.8.8.8` caso não utilize bloqueio de DNS).",
+ "reply_es": "**DNS bloqueado o redirigido (sinkhole)** (`0x8041013d` / `0x804101e1`): el DNS configurado en tu PS5 o router está bloqueando los dominios de Microsoft/Xbox (`login.live.com`, `*.xboxlive.com`). Para mantener bloqueados los servidores de Sony sin bloquear Xbox Cloud Gaming, ejecuta el payload **NanoDNS** en tu PS5 y configura en la red de la consola el **DNS Primario** com o IP local de tu PS5 y el **DNS Secundario** como `0.0.0.0` (o usa `1.1.1.1` / `8.8.8.8` si no usas bloqueador de DNS)."
},
{
"id": "xbox-profile",
diff --git a/.github/triage/test_triage.py b/.github/triage/test_triage.py
index a6e4cbb..23ba0f0 100644
--- a/.github/triage/test_triage.py
+++ b/.github/triage/test_triage.py
@@ -119,6 +119,18 @@ class Triage(unittest.TestCase):
self.assertIn("area: auth", labels)
self.assertIn("Gamertag", comment)
+ def test_tcp_refusal_errno_61_not_flagged_as_dns_blocked(self):
+ # Issue #357: TCP connect rejection (errno 61) is not a DNS blockage / NanoDNS problem.
+ labels, comment = self.run_triage(
+ form("can't sign in", "1.6.0", "11.60",
+ LOG + "21:43:16 [W] token refresh: connect failed to login.live.com:443 [20.190.173.67] (errno 61)\n"
+ "21:43:19 [E] sign-in failed: token refresh failed: connect failed to login.live.com:443 [20.190.173.67] (errno 61)")
+ )
+ self.assertNotIn("area: network", labels)
+ if comment:
+ self.assertNotIn("NanoDNS", comment)
+ self.assertNotIn("DNS blocked or sinkholed", comment)
+
def test_multilingual_spanish_and_portuguese(self):
labels, comment = self.run_triage(
form("Error al adquirir el juego gratuito: 9P22THR57FV7", "1.3.0", "13.60", LOG),
diff --git a/src/app/worker.cpp b/src/app/worker.cpp
index c55da9a..c36cefe 100644
--- a/src/app/worker.cpp
+++ b/src/app/worker.cpp
@@ -153,8 +153,7 @@ namespace {
bool isDnsOrSinkholeError(const std::string& err) {
return err.find("DNS lookup failed") != std::string::npos ||
err.find("0x8041013d") != std::string::npos ||
- err.find("0x804101e1") != std::string::npos ||
- err.find("(errno 61)") != std::string::npos;
+ err.find("0x804101e1") != std::string::npos;
}
void signInAndLoad(auth::AuthManager& am, xcloud::GssvClient& gssv) {
diff --git a/src/net/http.cpp b/src/net/http.cpp
index cfeb87a..fbe7c2a 100644
--- a/src/net/http.cpp
+++ b/src/net/http.cpp
@@ -110,75 +110,117 @@ void setTimeouts(int fd, int timeoutMs) {
::setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);
}
+} // namespace
+
int connectTcp(const std::string& host, int port, int timeoutMs, std::string& err) {
- uint32_t ip = 0;
- if (!platform::resolveIPv4(host, ip, err)) return -1;
- uint32_t hostOrderIp = ntohl(ip);
- char ipStr[32];
- std::snprintf(ipStr, sizeof ipStr, "%u.%u.%u.%u",
- (hostOrderIp >> 24) & 0xFF, (hostOrderIp >> 16) & 0xFF,
- (hostOrderIp >> 8) & 0xFF, hostOrderIp & 0xFF);
- if (host != "localhost" && host != "127.0.0.1" &&
- (hostOrderIp == 0 || (hostOrderIp >> 24) == 127)) {
- err = "DNS lookup failed for " + host + " (sinkhole IP " + ipStr + ")";
+ std::vector<uint32_t> addrs;
+ if (!platform::resolveIPv4(host, addrs, err)) return -1;
+ if (addrs.empty()) {
+ err = "DNS lookup failed for " + host + ": no addresses found";
return -1;
}
- sockaddr_in sa{};
- sa.sin_family = AF_INET;
- sa.sin_port = htons(static_cast<uint16_t>(port));
- sa.sin_addr.s_addr = ip;
- int fd = ::socket(AF_INET, SOCK_STREAM, 0);
- if (fd < 0) {
- err = "socket() failed (errno " + std::to_string(errno) + ")";
- return -1;
- }
- setTimeouts(fd, timeoutMs);
- int one = 1;
- ::setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof one);
+ uint64_t startMs = platform::nowMs();
+ std::string lastErr;
+
+ for (size_t i = 0; i < addrs.size(); ++i) {
+ uint32_t ip = addrs[i];
+ uint32_t hostOrderIp = ntohl(ip);
+ char ipStr[32];
+ std::snprintf(ipStr, sizeof ipStr, "%u.%u.%u.%u",
+ (hostOrderIp >> 24) & 0xFF, (hostOrderIp >> 16) & 0xFF,
+ (hostOrderIp >> 8) & 0xFF, hostOrderIp & 0xFF);
+ if (host != "localhost" && host != "127.0.0.1" &&
+ (hostOrderIp == 0 || (hostOrderIp >> 24) == 127)) {
+ lastErr = "DNS lookup failed for " + host + " (sinkhole IP " + ipStr + ")";
+ continue;
+ }
+
+ int remainingMs = timeoutMs;
+ if (timeoutMs > 0) {
+ uint64_t elapsed = platform::nowMs() - startMs;
+ if (elapsed >= static_cast<uint64_t>(timeoutMs)) {
+ if (lastErr.empty()) {
+ lastErr = "connect timed out to " + host + ":" + std::to_string(port);
+ }
+ break;
+ }
+ remainingMs = static_cast<int>(static_cast<uint64_t>(timeoutMs) - elapsed);
+ }
+
+ int fd = ::socket(AF_INET, SOCK_STREAM, 0);
+ if (fd < 0) {
+ lastErr = "socket() failed (errno " + std::to_string(errno) + ")";
+ break;
+ }
+ setTimeouts(fd, remainingMs > 0 ? remainingMs : timeoutMs);
+ int one = 1;
+ ::setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof one);
#ifdef SO_NOSIGPIPE
- ::setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof one);
+ ::setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof one);
#endif
- // A larger window: catalog responses are hundreds of KB from far away.
- int buf = 1 << 20;
- ::setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &buf, sizeof buf);
-
- int on = 1;
- ::ioctl(fd, FIONBIO, &on);
- int rc = ::connect(fd, reinterpret_cast<sockaddr*>(&sa), sizeof sa);
- if (rc != 0) {
- if (errno == EINPROGRESS || errno == EWOULDBLOCK) {
- struct pollfd pfd{};
- pfd.fd = fd;
- pfd.events = POLLOUT;
- int connectTimeout = (timeoutMs > 0) ? std::min(timeoutMs, 10000) : 10000;
- int pr = ::poll(&pfd, 1, connectTimeout);
- if (pr <= 0) {
- err = (pr == 0) ? "connect timed out to " + host + ":" + std::to_string(port) + " [" + ipStr + "]"
- : "poll failed connecting to " + host + ":" + std::to_string(port) + " [" + ipStr + "] (errno " + std::to_string(errno) + ")";
- ::close(fd);
- return -1;
- }
- int so_error = 0;
- socklen_t len = sizeof so_error;
- if (::getsockopt(fd, SOL_SOCKET, SO_ERROR, &so_error, &len) < 0 || so_error != 0) {
- err = "connect failed to " + host + ":" + std::to_string(port) + " [" + ipStr + "] (errno " + std::to_string(so_error ? so_error : errno) + ")";
+ // A larger window: catalog responses are hundreds of KB from far away.
+ int buf = 1 << 20;
+ ::setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &buf, sizeof buf);
+
+ int on = 1;
+ ::ioctl(fd, FIONBIO, &on);
+
+ sockaddr_in sa{};
+ sa.sin_family = AF_INET;
+ sa.sin_port = htons(static_cast<uint16_t>(port));
+ sa.sin_addr.s_addr = ip;
+
+ int rc = ::connect(fd, reinterpret_cast<sockaddr*>(&sa), sizeof sa);
+ if (rc != 0) {
+ if (errno == EINPROGRESS || errno == EWOULDBLOCK) {
+ struct pollfd pfd{};
+ pfd.fd = fd;
+ pfd.events = POLLOUT;
+ int connectTimeout = (remainingMs > 0) ? std::min(remainingMs, 10000) : 10000;
+ if (addrs.size() > 1 && (remainingMs <= 0 || remainingMs > 3000)) {
+ connectTimeout = (remainingMs > 0) ? std::min(remainingMs, 3000) : 3000;
+ }
+ int pr = ::poll(&pfd, 1, connectTimeout);
+ if (pr <= 0) {
+ lastErr = (pr == 0) ? "connect timed out to " + host + ":" + std::to_string(port) + " [" + ipStr + "]"
+ : "poll failed connecting to " + host + ":" + std::to_string(port) + " [" + ipStr + "] (errno " + std::to_string(errno) + ")";
+ ::close(fd);
+ if (i + 1 < addrs.size()) {
+ XC_LOGW("%s, trying next IP (%zu/%zu)", lastErr.c_str(), i + 2, addrs.size());
+ }
+ continue;
+ }
+ int so_error = 0;
+ socklen_t len = sizeof so_error;
+ if (::getsockopt(fd, SOL_SOCKET, SO_ERROR, &so_error, &len) < 0 || so_error != 0) {
+ lastErr = "connect failed to " + host + ":" + std::to_string(port) + " [" + ipStr + "] (errno " + std::to_string(so_error ? so_error : errno) + ")";
+ ::close(fd);
+ if (i + 1 < addrs.size()) {
+ XC_LOGW("%s, trying next IP (%zu/%zu)", lastErr.c_str(), i + 2, addrs.size());
+ }
+ continue;
+ }
+ } else {
+ lastErr = "connect failed to " + host + ":" + std::to_string(port) + " [" + ipStr + "] (errno " + std::to_string(errno) + ")";
::close(fd);
- return -1;
+ if (i + 1 < addrs.size()) {
+ XC_LOGW("%s, trying next IP (%zu/%zu)", lastErr.c_str(), i + 2, addrs.size());
+ }
+ continue;
}
- } else {
- err = "connect failed to " + host + ":" + std::to_string(port) + " [" + ipStr + "] (errno " + std::to_string(errno) + ")";
- ::close(fd);
- return -1;
}
+ on = 0;
+ ::ioctl(fd, FIONBIO, &on);
+ return fd;
}
- on = 0;
- ::ioctl(fd, FIONBIO, &on);
- return fd;
+
+ err = lastErr.empty() ? ("connect failed to " + host + ":" + std::to_string(port)) : lastErr;
+ return -1;
}
+namespace {
std::function<Response(const Request&)> g_testTransport;
-
} // namespace
bool dechunk(const std::string& in, std::string& out) {
diff --git a/src/net/http.h b/src/net/http.h
index 97abd88..7e7c1a3 100644
--- a/src/net/http.h
+++ b/src/net/http.h
@@ -123,6 +123,7 @@ long long chunkedLength(std::string_view in);
bool keepAliveAfter(int status, bool http10, const std::string& connection, long long bodyEnd, size_t received);
// Helpers.
+int connectTcp(const std::string& host, int port, int timeoutMs, std::string& err);
std::string urlEncode(const std::string& s);
std::string formEncode(const std::vector<std::pair<std::string, std::string>>& fields);
diff --git a/src/platform/android/platform_android.cpp b/src/platform/android/platform_android.cpp
index 1b151ad..61d335a 100644
--- a/src/platform/android/platform_android.cpp
+++ b/src/platform/android/platform_android.cpp
@@ -5,6 +5,8 @@
#include "platform/platform.h"
#include "util/log.h"
+#include <algorithm>
+#include <arpa/inet.h>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
@@ -80,7 +82,12 @@ bool randomBytes(void* out, size_t len) {
return true;
}
-bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {
+bool resolveIPv4(const std::string& host, std::vector<uint32_t>& addrs, std::string& err) {
+ in_addr literal{};
+ if (::inet_pton(AF_INET, host.c_str(), &literal) == 1) {
+ addrs.push_back(literal.s_addr);
+ return true;
+ }
addrinfo hints{};
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
@@ -90,8 +97,26 @@ bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {
err = "DNS lookup failed for " + host + ": " + gai_strerror(rc);
return false;
}
- addr = reinterpret_cast<sockaddr_in*>(res->ai_addr)->sin_addr.s_addr;
+ for (addrinfo* p = res; p != nullptr; p = p->ai_next) {
+ if (p->ai_family == AF_INET && p->ai_addr) {
+ uint32_t a = reinterpret_cast<sockaddr_in*>(p->ai_addr)->sin_addr.s_addr;
+ if (std::find(addrs.begin(), addrs.end(), a) == addrs.end()) {
+ addrs.push_back(a);
+ }
+ }
+ }
::freeaddrinfo(res);
+ if (addrs.empty()) {
+ err = "DNS lookup failed for " + host + ": no IPv4 address found";
+ return false;
+ }
+ return true;
+}
+
+bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {
+ std::vector<uint32_t> addrs;
+ if (!resolveIPv4(host, addrs, err)) return false;
+ addr = addrs[0];
return true;
}
diff --git a/src/platform/linux/platform_linux.cpp b/src/platform/linux/platform_linux.cpp
index a6e6b87..ce35366 100644
--- a/src/platform/linux/platform_linux.cpp
+++ b/src/platform/linux/platform_linux.cpp
@@ -10,6 +10,8 @@
#include "platform/toasts.h"
#include "util/log.h"
+#include <algorithm>
+#include <arpa/inet.h>
#include <atomic>
#include <cerrno>
#include <csignal>
@@ -131,7 +133,12 @@ bool randomBytes(void* out, size_t len) {
return true;
}
-bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {
+bool resolveIPv4(const std::string& host, std::vector<uint32_t>& addrs, std::string& err) {
+ in_addr literal{};
+ if (::inet_pton(AF_INET, host.c_str(), &literal) == 1) {
+ addrs.push_back(literal.s_addr);
+ return true;
+ }
addrinfo hints{};
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
@@ -141,8 +148,26 @@ bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {
err = "DNS lookup failed for " + host + ": " + gai_strerror(rc);
return false;
}
- addr = reinterpret_cast<sockaddr_in*>(res->ai_addr)->sin_addr.s_addr;
+ for (addrinfo* p = res; p != nullptr; p = p->ai_next) {
+ if (p->ai_family == AF_INET && p->ai_addr) {
+ uint32_t a = reinterpret_cast<sockaddr_in*>(p->ai_addr)->sin_addr.s_addr;
+ if (std::find(addrs.begin(), addrs.end(), a) == addrs.end()) {
+ addrs.push_back(a);
+ }
+ }
+ }
::freeaddrinfo(res);
+ if (addrs.empty()) {
+ err = "DNS lookup failed for " + host + ": no IPv4 address found";
+ return false;
+ }
+ return true;
+}
+
+bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {
+ std::vector<uint32_t> addrs;
+ if (!resolveIPv4(host, addrs, err)) return false;
+ addr = addrs[0];
return true;
}
diff --git a/src/platform/platform.h b/src/platform/platform.h
index 5e00797..8597da7 100644
--- a/src/platform/platform.h
+++ b/src/platform/platform.h
@@ -9,6 +9,7 @@
#include <cstdint>
#include <functional>
#include <string>
+#include <vector>
namespace xc::platform {
@@ -37,9 +38,10 @@ std::string caBundlePath();
bool randomBytes(void* out, size_t len);
-// Host name -> IPv4 address (network byte order). The PS5 app sandbox has no
+// Host name -> IPv4 address(es) (network byte order). The PS5 app sandbox has no
// resolv.conf, so the console goes through sceNetResolver instead of
// getaddrinfo. Returns false (with a message in `err`) on failure.
+bool resolveIPv4(const std::string& host, std::vector<uint32_t>& addrs, std::string& err);
bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err);
// Returns the active local IPv4 address (e.g. "192.168.1.50") or "127.0.0.1".
std::string localIPv4();
diff --git a/src/platform/ps4/platform_ps4.cpp b/src/platform/ps4/platform_ps4.cpp
index 9092731..47e9fdf 100644
--- a/src/platform/ps4/platform_ps4.cpp
+++ b/src/platform/ps4/platform_ps4.cpp
@@ -16,6 +16,7 @@
#include <dlfcn.h>
#include <orbis/NetCtl.h>
+#include <algorithm>
#include <arpa/inet.h>
#include <atomic>
#include <ifaddrs.h>
@@ -916,6 +917,30 @@ int sceNetInit(void);
int sceNetPoolCreate(const char* name, int size, int flags);
int sceNetResolverCreate(const char* name, int memid, int flags);
int sceNetResolverStartNtoa(int rid, const char* hostname, uint32_t* addr, int timeout, int retry, int flags);
+
+struct SceNetInAddr {
+ uint32_t s_addr;
+};
+
+union SceNetAddrUnion {
+ SceNetInAddr addr;
+ uint8_t addr6[16];
+};
+
+struct SceNetResolverAddr {
+ SceNetAddrUnion u;
+ uint32_t af;
+ uint32_t reserved[3];
+};
+
+struct SceNetResolverInfo {
+ SceNetResolverAddr addrs[10];
+ uint32_t records;
+ uint32_t recordsv4;
+ uint32_t reserved[14];
+};
+
+int sceNetResolverStartNtoaMultipleRecords(int rid, const char* hostname, SceNetResolverInfo* info, int timeout, int retry, int flags);
int sceNetResolverDestroy(int rid);
int sceKernelAvailableDirectMemorySize(off_t start, off_t end, size_t align, off_t* startOut, size_t* sizeOut);
size_t sceKernelGetDirectMemorySize(void);
@@ -1041,10 +1066,10 @@ bool randomBytes(void* out, size_t len) {
return true;
}
-bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {
+bool resolveIPv4(const std::string& host, std::vector<uint32_t>& addrs, std::string& err) {
in_addr literal{};
if (::inet_pton(AF_INET, host.c_str(), &literal) == 1) {
- addr = literal.s_addr;
+ addrs.push_back(literal.s_addr);
return true;
}
int pool = g_netPool.load();
@@ -1059,16 +1084,40 @@ bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {
err = "DNS lookup failed for " + host + ": " + buf;
return false;
}
- uint32_t a = 0;
- int rc = sceNetResolverStartNtoa(rid, host.c_str(), &a, 5000000, 2, 0);
+ SceNetResolverInfo info{};
+ int rc = sceNetResolverStartNtoaMultipleRecords(rid, host.c_str(), &info, 5000000, 2, 0);
+ if (rc >= 0) {
+ uint32_t count = std::min<uint32_t>(info.records, 10);
+ for (uint32_t i = 0; i < count; ++i) {
+ if (info.addrs[i].af == AF_INET && info.addrs[i].u.addr.s_addr != 0) {
+ uint32_t a = info.addrs[i].u.addr.s_addr;
+ if (std::find(addrs.begin(), addrs.end(), a) == addrs.end()) {
+ addrs.push_back(a);
+ }
+ }
+ }
+ }
+ if (addrs.empty()) {
+ uint32_t a = 0;
+ rc = sceNetResolverStartNtoa(rid, host.c_str(), &a, 5000000, 2, 0);
+ if (rc >= 0 && a != 0) {
+ addrs.push_back(a);
+ }
+ }
sceNetResolverDestroy(rid);
- if (rc < 0) {
+ if (addrs.empty()) {
char buf[64];
std::snprintf(buf, sizeof buf, "0x%08x", static_cast<unsigned>(rc));
err = "DNS lookup failed for " + host + " (" + buf + ")";
return false;
}
- addr = a;
+ return true;
+}
+
+bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {
+ std::vector<uint32_t> addrs;
+ if (!resolveIPv4(host, addrs, err)) return false;
+ addr = addrs[0];
return true;
}
diff --git a/src/platform/ps5/platform_ps5.cpp b/src/platform/ps5/platform_ps5.cpp
index c7a200b..cb4a262 100644
--- a/src/platform/ps5/platform_ps5.cpp
+++ b/src/platform/ps5/platform_ps5.cpp
@@ -4,6 +4,7 @@
#include "platform/toasts.h"
#include "util/log.h"
+#include <algorithm>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
@@ -38,6 +39,30 @@ int sceNetInit(void);
int sceNetPoolCreate(const char* name, int size, int flags);
int sceNetResolverCreate(const char* name, int memid, int flags);
int sceNetResolverStartNtoa(int rid, const char* hostname, uint32_t* addr, int timeout, int retry, int flags);
+
+struct SceNetInAddr {
+ uint32_t s_addr;
+};
+
+union SceNetAddrUnion {
+ SceNetInAddr addr;
+ uint8_t addr6[16];
+};
+
+struct SceNetResolverAddr {
+ SceNetAddrUnion u;
+ uint32_t af;
+ uint32_t reserved[3];
+};
+
+struct SceNetResolverInfo {
+ SceNetResolverAddr addrs[10];
+ uint32_t records;
+ uint32_t recordsv4;
+ uint32_t reserved[14];
+};
+
+int sceNetResolverStartNtoaMultipleRecords(int rid, const char* hostname, SceNetResolverInfo* info, int timeout, int retry, int flags);
int sceNetResolverDestroy(int rid);
}
@@ -217,10 +242,10 @@ bool randomBytes(void* out, size_t len) {
return true;
}
-bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {
+bool resolveIPv4(const std::string& host, std::vector<uint32_t>& addrs, std::string& err) {
in_addr literal{};
if (::inet_pton(AF_INET, host.c_str(), &literal) == 1) {
- addr = literal.s_addr;
+ addrs.push_back(literal.s_addr);
return true;
}
int pool = g_netPool.load();
@@ -235,17 +260,40 @@ bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {
err = "DNS lookup failed for " + host + ": " + buf;
return false;
}
- uint32_t a = 0;
- // timeout 5s (5000000 us), 2 retries (prevents 20-30s DNS freezes).
- int rc = sceNetResolverStartNtoa(rid, host.c_str(), &a, 5000000, 2, 0);
+ SceNetResolverInfo info{};
+ int rc = sceNetResolverStartNtoaMultipleRecords(rid, host.c_str(), &info, 5000000, 2, 0);
+ if (rc >= 0) {
+ uint32_t count = std::min<uint32_t>(info.records, 10);
+ for (uint32_t i = 0; i < count; ++i) {
+ if (info.addrs[i].af == AF_INET && info.addrs[i].u.addr.s_addr != 0) {
+ uint32_t a = info.addrs[i].u.addr.s_addr;
+ if (std::find(addrs.begin(), addrs.end(), a) == addrs.end()) {
+ addrs.push_back(a);
+ }
+ }
+ }
+ }
+ if (addrs.empty()) {
+ uint32_t a = 0;
+ rc = sceNetResolverStartNtoa(rid, host.c_str(), &a, 5000000, 2, 0);
+ if (rc >= 0 && a != 0) {
+ addrs.push_back(a);
+ }
+ }
sceNetResolverDestroy(rid);
- if (rc < 0) {
+ if (addrs.empty()) {
char buf[64];
std::snprintf(buf, sizeof buf, "0x%08x", static_cast<unsigned>(rc));
err = "DNS lookup failed for " + host + " (" + buf + ")";
return false;
}
- addr = a; // network byte order
+ return true;
+}
+
+bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {
+ std::vector<uint32_t> addrs;
+ if (!resolveIPv4(host, addrs, err)) return false;
+ addr = addrs[0];
return true;
}
diff --git a/src/platform/windows/platform_windows.cpp b/src/platform/windows/platform_windows.cpp
index 8eed197..e6b151b 100644
--- a/src/platform/windows/platform_windows.cpp
+++ b/src/platform/windows/platform_windows.cpp
@@ -17,6 +17,7 @@
#include <io.h>
#endif
+#include <algorithm>
#include <chrono>
#include <cstdio>
#include <cstdlib>
@@ -133,7 +134,7 @@ bool randomBytes(void* out, size_t len) {
return true;
}
-bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {
+bool resolveIPv4(const std::string& host, std::vector<uint32_t>& addrs, std::string& err) {
#if defined(_WIN32) || defined(__CYGWIN__)
addrinfo hints{};
hints.ai_family = AF_INET;
@@ -144,17 +145,35 @@ bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {
err = "DNS lookup failed for " + host + " (error " + std::to_string(WSAGetLastError()) + ")";
return false;
}
- addr = reinterpret_cast<sockaddr_in*>(res->ai_addr)->sin_addr.s_addr;
+ for (addrinfo* p = res; p != nullptr; p = p->ai_next) {
+ if (p->ai_family == AF_INET && p->ai_addr) {
+ uint32_t a = reinterpret_cast<sockaddr_in*>(p->ai_addr)->sin_addr.s_addr;
+ if (std::find(addrs.begin(), addrs.end(), a) == addrs.end()) {
+ addrs.push_back(a);
+ }
+ }
+ }
freeaddrinfo(res);
+ if (addrs.empty()) {
+ err = "DNS lookup failed for " + host + ": no IPv4 address found";
+ return false;
+ }
return true;
#else
(void)host;
- (void)addr;
+ (void)addrs;
err = "resolveIPv4 not implemented on this host";
return false;
#endif
}
+bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {
+ std::vector<uint32_t> addrs;
+ if (!resolveIPv4(host, addrs, err)) return false;
+ addr = addrs[0];
+ return true;
+}
+
std::string localIPv4() {
#if defined(_WIN32) || defined(__CYGWIN__)
SOCKET s = socket(AF_INET, SOCK_DGRAM, 0);
diff --git a/tests/test_main.cpp b/tests/test_main.cpp
index cedb3a2..aa13a26 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -70,6 +70,14 @@
#include <csignal>
#include <sys/wait.h>
#include <unistd.h>
+#if !defined(_WIN32)
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#else
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#endif
#include <cstdlib>
#include <ctime>
#include <map>
@@ -1422,6 +1430,55 @@ static void testHttpKeepAlive() {
CHECK(keepAliveAfter(200, true, "Keep-Alive", 10, 10));
}
+static void testResolveIPv4AndConnectTcp() {
+ std::string err;
+ std::vector<uint32_t> addrs;
+ CHECK(platform::resolveIPv4("127.0.0.1", addrs, err));
+ CHECK(!addrs.empty());
+ CHECK(addrs[0] == htonl(0x7F000001));
+
+ uint32_t singleAddr = 0;
+ CHECK(platform::resolveIPv4("127.0.0.1", singleAddr, err));
+ CHECK(singleAddr == htonl(0x7F000001));
+
+ addrs.clear();
+ CHECK(!platform::resolveIPv4("this-domain-should-never-exist-xyz123.local", addrs, err));
+ CHECK(addrs.empty());
+ CHECK(!err.empty());
+
+ // Connecting to an unopened port on loopback fails with connection refusal, NOT a DNS error
+ int fd = net::connectTcp("127.0.0.1", 1, 500, err);
+ CHECK(fd < 0);
+ CHECK(err.find("DNS lookup failed") == std::string::npos);
+ CHECK(err.find("connect failed to 127.0.0.1:1") != std::string::npos ||
+ err.find("connect timed out") != std::string::npos);
+
+ // Sinkhole addresses are detected
+ fd = net::connectTcp("0.0.0.0", 443, 500, err);
+ CHECK(fd < 0);
+ CHECK(err.find("sinkhole IP") != std::string::npos);
+
+ // Successful connectTcp to a listening server socket
+ int serverFd = ::socket(AF_INET, SOCK_STREAM, 0);
+ if (serverFd >= 0) {
+ sockaddr_in sa{};
+ sa.sin_family = AF_INET;
+ sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ sa.sin_port = 0;
+ if (::bind(serverFd, reinterpret_cast<sockaddr*>(&sa), sizeof sa) == 0 &&
+ ::listen(serverFd, 1) == 0) {
+ socklen_t len = sizeof sa;
+ if (::getsockname(serverFd, reinterpret_cast<sockaddr*>(&sa), &len) == 0) {
+ int port = ntohs(sa.sin_port);
+ int clientFd = net::connectTcp("127.0.0.1", port, 2000, err);
+ CHECK(clientFd >= 0);
+ if (clientFd >= 0) ::close(clientFd);
+ }
+ }
+ ::close(serverFd);
+ }
+}
+
// The home rows come from the network on every load(): the cache's rows
// (kept for the instant start) are never added to them, and a cache that
// holds every row twice heals at the next save.
@@ -7906,6 +7963,7 @@ int main() {
testLoginCountry();
testPrivacyAndRobustness();
testHttpKeepAlive();
+ testResolveIPv4AndConnectTcp();
testStoreLeftovers();
testLibraryRowsOnce();
testHostPacer();