Skip to content

Commit 7c974e0

Browse files
authored
Merge pull request #20221 from hrydgard/net-fixes-3
More infrastructure networking fixes
2 parents 1d97dfe + 1cef3aa commit 7c974e0

File tree

10 files changed

+124
-73
lines changed

10 files changed

+124
-73
lines changed

Common/Net/HTTPNaettRequest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ bool HTTPSRequest::Done() {
6969
if (completed_)
7070
return true;
7171

72+
_dbg_assert_(res_ != nullptr);
73+
7274
if (!naettComplete(res_)) {
7375
int total = 0;
7476
int size = naettGetTotalBytesRead(res_, &total);

Common/Net/Resolve.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
#include <cstring>
77
#include <string>
88
#include <vector>
9+
#include <map>
910

1011
#include "Common/Log.h"
1112
#include "Common/TimeUtil.h"
13+
#include "Common/StringUtils.h"
1214
#include "Common/Data/Encoding/Utf8.h"
1315
#include "Common/Net/SocketCompat.h"
1416

@@ -411,7 +413,15 @@ static bool parse_dns_response(unsigned char *buffer, size_t response_len, uint3
411413
return false;
412414
}
413415

414-
// This was written by ChatGPT! (And then cleaned up...)
416+
// This was written by ChatGPT, although not much of that remains, after all the cleanup and fixing...
417+
418+
// Specialized cache for the direct DNS lookups
419+
struct DNSCacheEntry {
420+
uint32_t ipv4Address;
421+
};
422+
423+
static std::map<std::string, DNSCacheEntry> g_directDNSCache;
424+
415425
bool DirectDNSLookupIPV4(const char *dns_server_ip, const char *domain, uint32_t *ipv4_addr) {
416426
if (!strlen(dns_server_ip)) {
417427
WARN_LOG(Log::sceNet, "Direct lookup: DNS server not specified");
@@ -423,6 +433,15 @@ bool DirectDNSLookupIPV4(const char *dns_server_ip, const char *domain, uint32_t
423433
return false;
424434
}
425435

436+
std::string key = StringFromFormat("%s:%s", dns_server_ip, domain);
437+
438+
auto iter = g_directDNSCache.find(key);
439+
if (iter != g_directDNSCache.end()) {
440+
INFO_LOG(Log::sceNet, "Returning cached response from direct DNS request for '%s' to DNS server '%s", domain, dns_server_ip);
441+
*ipv4_addr = iter->second.ipv4Address;
442+
return true;
443+
}
444+
426445
SOCKET sockfd = socket(AF_INET, SOCK_DGRAM, 0);
427446
// Create UDP socket
428447
if (sockfd < 0) {
@@ -470,11 +489,17 @@ bool DirectDNSLookupIPV4(const char *dns_server_ip, const char *domain, uint32_t
470489
closesocket(sockfd);
471490
return 1;
472491
}
492+
473493
// Close socket
474494
closesocket(sockfd);
475495

476496
// Done communicating, time to parse.
477-
return parse_dns_response(buffer, response_len, ipv4_addr);
497+
if (!parse_dns_response(buffer, response_len, ipv4_addr)) {
498+
return false;
499+
}
500+
501+
g_directDNSCache[key] = DNSCacheEntry{ *ipv4_addr };
502+
return true;
478503
}
479504

480505
} // namespace net

Core/Dialog/PSPNetconfDialog.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,12 @@ int PSPNetconfDialog::Update(int animSpeed) {
116116
std::string json;
117117
if (!jsonReady_ && PollInfraJsonDownload(&json)) {
118118
if (!json.empty()) {
119-
INFO_LOG(Log::sceNet, "PollInfraJsonDownload returned a string");
120119
if (!LoadAutoDNS(json)) {
121120
// If the JSON parse fails, throw away the cache file at least.
122121
ERROR_LOG(Log::sceNet, "Failed to parse bad json. Deleting cache file.");
123122
DeleteAutoDNSCacheFile();
124123
} else {
125-
INFO_LOG(Log::sceNet, "Got and processed the AutoDNS json.");
124+
DEBUG_LOG(Log::sceNet, "Got and processed the AutoDNS json.");
126125
}
127126
} else {
128127
// TODO: Show a notice?

Core/HLE/sceNet.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,11 @@ bool LoadAutoDNS(std::string_view json) {
348348
if (inet_ntop(ptr->ai_family, &(((struct sockaddr_in*)ptr->ai_addr)->sin_addr), ipstr, sizeof(ipstr)) != 0) {
349349
INFO_LOG(Log::sceNet, "Successfully resolved '%s' to '%s', overriding DNS.", dyn_dns.c_str(), ipstr);
350350
if (g_infraDNSConfig.dns != ipstr) {
351-
WARN_LOG(Log::sceNet, "Replacing specified DNS IP %s with dyndns %s!", g_infraDNSConfig.dns.c_str(), ipstr);
351+
INFO_LOG(Log::sceNet, "Replacing specified DNS IP %s with dyndns %s!", g_infraDNSConfig.dns.c_str(), ipstr);
352352
g_infraDNSConfig.dns = ipstr;
353+
// If dyndns is working, we do not need the fixed lookups. So let's kick them.
354+
INFO_LOG(Log::sceNet, "Clearing fixed DNS lookups.");
355+
g_infraDNSConfig.fixedDNS.clear();
353356
} else {
354357
INFO_LOG(Log::sceNet, "DynDNS: %s already up to date", g_infraDNSConfig.dns.c_str());
355358
}
@@ -382,14 +385,18 @@ void StartInfraJsonDownload() {
382385
WARN_LOG(Log::sceNet, "json is already being downloaded. Still, starting a new download.");
383386
}
384387

385-
const char *acceptMime = "application/json, text/*; q=0.9, */*; q=0.8";
386-
g_infraDL = g_DownloadManager.StartDownload(jsonUrl, Path(), http::RequestFlags::Cached24H, acceptMime);
388+
if (!g_Config.bDontDownloadInfraJson) {
389+
const char * const acceptMime = "application/json, text/*; q=0.9, */*; q=0.8";
390+
g_infraDL = g_DownloadManager.StartDownload(jsonUrl, Path(), http::RequestFlags::Cached24H, acceptMime);
391+
}
387392
}
388393

389394
bool PollInfraJsonDownload(std::string *jsonOutput) {
390395
if (!g_Config.bInfrastructureAutoDNS) {
391396
INFO_LOG(Log::sceNet, "Auto DNS disabled, returning success");
392397
jsonOutput->clear();
398+
// In case there's an old request, get rid of it.
399+
g_infraDL.reset();
393400
return true;
394401
}
395402

@@ -402,6 +409,8 @@ bool PollInfraJsonDownload(std::string *jsonOutput) {
402409
return true; // A clear output but returning true means something vent very wrong.
403410
}
404411
*jsonOutput = std::string((const char *)jsonStr.get(), jsonSize);
412+
// In case there's an old request, get rid of it.
413+
g_infraDL.reset();
405414
return true;
406415
}
407416

@@ -871,7 +880,7 @@ static u32 sceNetTerm() {
871880

872881
// Give time to make sure everything are cleaned up
873882
hleEatMicro(adhocDefaultDelay);
874-
return hleLogWarning(Log::sceNet, retval, "at %08x", currentMIPS->pc);
883+
return hleLogInfo(Log::sceNet, retval);
875884
}
876885

877886
/*
@@ -920,7 +929,7 @@ static int sceNetInit(u32 poolSize, u32 calloutPri, u32 calloutStack, u32 netini
920929
return hleLogError(Log::sceNet, SCE_KERNEL_ERROR_NO_MEMORY, "unable to allocate pool");
921930
}
922931

923-
WARN_LOG(Log::sceNet, "sceNetInit(poolsize=%d, calloutpri=%i, calloutstack=%d, netintrpri=%i, netintrstack=%d) at %08x", poolSize, calloutPri, calloutStack, netinitPri, netinitStack, currentMIPS->pc);
932+
INFO_LOG(Log::sceNet, "sceNetInit(poolsize=%d, calloutpri=%i, calloutstack=%d, netintrpri=%i, netintrstack=%d) at %08x", poolSize, calloutPri, calloutStack, netinitPri, netinitStack, currentMIPS->pc);
924933

925934
netMallocStat.pool = poolSize - 0x20; // On Vantage Master Portable this is slightly (32 bytes) smaller than the poolSize arg when tested with JPCSP + prx files
926935
netMallocStat.maximum = 0x4050; // Dummy maximum foot print
@@ -1356,7 +1365,7 @@ static int NetApctl_AddHandler(u32 handlerPtr, u32 handlerArg) {
13561365
return retval;
13571366
}
13581367
apctlHandlers[retval] = handler;
1359-
WARN_LOG(Log::sceNet, "Added Apctl handler(%x, %x): %d", handlerPtr, handlerArg, retval);
1368+
INFO_LOG(Log::sceNet, "Added Apctl handler(%x, %x): %d", handlerPtr, handlerArg, retval);
13601369
}
13611370
else {
13621371
ERROR_LOG(Log::sceNet, "Existing Apctl handler(%x, %x)", handlerPtr, handlerArg);

Core/HLE/sceNetAdhoc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2425,7 +2425,7 @@ static u32 sceNetAdhocctlAddHandler(u32 handlerPtr, u32 handlerArg) {
24252425
return retval;
24262426
}
24272427
adhocctlHandlers[retval] = handler;
2428-
WARN_LOG(Log::sceNet, "UNTESTED sceNetAdhocctlAddHandler(%x, %x): added handler %d", handlerPtr, handlerArg, retval);
2428+
INFO_LOG(Log::sceNet, "UNTESTED sceNetAdhocctlAddHandler(%x, %x): added handler %d", handlerPtr, handlerArg, retval);
24292429
} else if(foundHandler) {
24302430
ERROR_LOG(Log::sceNet, "UNTESTED sceNetAdhocctlAddHandler(%x, %x): Same handler already exists", handlerPtr, handlerArg);
24312431
retval = 0; //Faking success

0 commit comments

Comments
 (0)