Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Common/Net/HTTPNaettRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ bool HTTPSRequest::Done() {
if (completed_)
return true;

_dbg_assert_(res_ != nullptr);

if (!naettComplete(res_)) {
int total = 0;
int size = naettGetTotalBytesRead(res_, &total);
Expand Down
29 changes: 27 additions & 2 deletions Common/Net/Resolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
#include <cstring>
#include <string>
#include <vector>
#include <map>

#include "Common/Log.h"
#include "Common/TimeUtil.h"
#include "Common/StringUtils.h"
#include "Common/Data/Encoding/Utf8.h"
#include "Common/Net/SocketCompat.h"

Expand Down Expand Up @@ -411,7 +413,15 @@ static bool parse_dns_response(unsigned char *buffer, size_t response_len, uint3
return false;
}

// This was written by ChatGPT! (And then cleaned up...)
// This was written by ChatGPT, although not much of that remains, after all the cleanup and fixing...

// Specialized cache for the direct DNS lookups
struct DNSCacheEntry {
uint32_t ipv4Address;
};

static std::map<std::string, DNSCacheEntry> g_directDNSCache;

bool DirectDNSLookupIPV4(const char *dns_server_ip, const char *domain, uint32_t *ipv4_addr) {
if (!strlen(dns_server_ip)) {
WARN_LOG(Log::sceNet, "Direct lookup: DNS server not specified");
Expand All @@ -423,6 +433,15 @@ bool DirectDNSLookupIPV4(const char *dns_server_ip, const char *domain, uint32_t
return false;
}

std::string key = StringFromFormat("%s:%s", dns_server_ip, domain);

auto iter = g_directDNSCache.find(key);
if (iter != g_directDNSCache.end()) {
INFO_LOG(Log::sceNet, "Returning cached response from direct DNS request for '%s' to DNS server '%s", domain, dns_server_ip);
*ipv4_addr = iter->second.ipv4Address;
return true;
}

SOCKET sockfd = socket(AF_INET, SOCK_DGRAM, 0);
// Create UDP socket
if (sockfd < 0) {
Expand Down Expand Up @@ -470,11 +489,17 @@ bool DirectDNSLookupIPV4(const char *dns_server_ip, const char *domain, uint32_t
closesocket(sockfd);
return 1;
}

// Close socket
closesocket(sockfd);

// Done communicating, time to parse.
return parse_dns_response(buffer, response_len, ipv4_addr);
if (!parse_dns_response(buffer, response_len, ipv4_addr)) {
return false;
}

g_directDNSCache[key] = DNSCacheEntry{ *ipv4_addr };
return true;
}

} // namespace net
3 changes: 1 addition & 2 deletions Core/Dialog/PSPNetconfDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,12 @@ int PSPNetconfDialog::Update(int animSpeed) {
std::string json;
if (!jsonReady_ && PollInfraJsonDownload(&json)) {
if (!json.empty()) {
INFO_LOG(Log::sceNet, "PollInfraJsonDownload returned a string");
if (!LoadAutoDNS(json)) {
// If the JSON parse fails, throw away the cache file at least.
ERROR_LOG(Log::sceNet, "Failed to parse bad json. Deleting cache file.");
DeleteAutoDNSCacheFile();
} else {
INFO_LOG(Log::sceNet, "Got and processed the AutoDNS json.");
DEBUG_LOG(Log::sceNet, "Got and processed the AutoDNS json.");
}
} else {
// TODO: Show a notice?
Expand Down
21 changes: 15 additions & 6 deletions Core/HLE/sceNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,11 @@ bool LoadAutoDNS(std::string_view json) {
if (inet_ntop(ptr->ai_family, &(((struct sockaddr_in*)ptr->ai_addr)->sin_addr), ipstr, sizeof(ipstr)) != 0) {
INFO_LOG(Log::sceNet, "Successfully resolved '%s' to '%s', overriding DNS.", dyn_dns.c_str(), ipstr);
if (g_infraDNSConfig.dns != ipstr) {
WARN_LOG(Log::sceNet, "Replacing specified DNS IP %s with dyndns %s!", g_infraDNSConfig.dns.c_str(), ipstr);
INFO_LOG(Log::sceNet, "Replacing specified DNS IP %s with dyndns %s!", g_infraDNSConfig.dns.c_str(), ipstr);
g_infraDNSConfig.dns = ipstr;
// If dyndns is working, we do not need the fixed lookups. So let's kick them.
INFO_LOG(Log::sceNet, "Clearing fixed DNS lookups.");
g_infraDNSConfig.fixedDNS.clear();
} else {
INFO_LOG(Log::sceNet, "DynDNS: %s already up to date", g_infraDNSConfig.dns.c_str());
}
Expand Down Expand Up @@ -382,14 +385,18 @@ void StartInfraJsonDownload() {
WARN_LOG(Log::sceNet, "json is already being downloaded. Still, starting a new download.");
}

const char *acceptMime = "application/json, text/*; q=0.9, */*; q=0.8";
g_infraDL = g_DownloadManager.StartDownload(jsonUrl, Path(), http::RequestFlags::Cached24H, acceptMime);
if (!g_Config.bDontDownloadInfraJson) {
const char * const acceptMime = "application/json, text/*; q=0.9, */*; q=0.8";
g_infraDL = g_DownloadManager.StartDownload(jsonUrl, Path(), http::RequestFlags::Cached24H, acceptMime);
}
}

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

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

Expand Down Expand Up @@ -871,7 +880,7 @@ static u32 sceNetTerm() {

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

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

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

netMallocStat.pool = poolSize - 0x20; // On Vantage Master Portable this is slightly (32 bytes) smaller than the poolSize arg when tested with JPCSP + prx files
netMallocStat.maximum = 0x4050; // Dummy maximum foot print
Expand Down Expand Up @@ -1356,7 +1365,7 @@ static int NetApctl_AddHandler(u32 handlerPtr, u32 handlerArg) {
return retval;
}
apctlHandlers[retval] = handler;
WARN_LOG(Log::sceNet, "Added Apctl handler(%x, %x): %d", handlerPtr, handlerArg, retval);
INFO_LOG(Log::sceNet, "Added Apctl handler(%x, %x): %d", handlerPtr, handlerArg, retval);
}
else {
ERROR_LOG(Log::sceNet, "Existing Apctl handler(%x, %x)", handlerPtr, handlerArg);
Expand Down
2 changes: 1 addition & 1 deletion Core/HLE/sceNetAdhoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2425,7 +2425,7 @@ static u32 sceNetAdhocctlAddHandler(u32 handlerPtr, u32 handlerArg) {
return retval;
}
adhocctlHandlers[retval] = handler;
WARN_LOG(Log::sceNet, "UNTESTED sceNetAdhocctlAddHandler(%x, %x): added handler %d", handlerPtr, handlerArg, retval);
INFO_LOG(Log::sceNet, "UNTESTED sceNetAdhocctlAddHandler(%x, %x): added handler %d", handlerPtr, handlerArg, retval);
} else if(foundHandler) {
ERROR_LOG(Log::sceNet, "UNTESTED sceNetAdhocctlAddHandler(%x, %x): Same handler already exists", handlerPtr, handlerArg);
retval = 0; //Faking success
Expand Down
Loading