Minor fixes in Common/Net from Nemo's writeup

This commit is contained in:
Henrik Rydgård committed 2026-08-09 19:03:28 +02:00
1 parent 78b25599f3
commit d31b475bb2
4 files changed
+20 -30

No files matched your search

+1 -1
View File
@@ -352,7 +352,7 @@ void Server::HandleRequestDefault(const ServerRequest &request) {
}
void Server::Handle404(const ServerRequest &request) {
INFO_LOG(Log::HTTP, "No handler for '%.*s', falling back to 404.", (int)request.resource().size(), request.resource().data());
INFO_LOG(Log::HTTP, "No handler for '%.*s', falling back to 404.", STR_VIEW(request.resource()));
const char *payload = "<html><body>404 not found</body></html>\r\n";
request.WriteHttpResponseHeader("1.0", 404, strlen(payload));
request.Out()->Push(payload);
+12 -12
View File
@@ -501,7 +501,7 @@ static bool encode_domain_name(const char *domain, unsigned char *encoded, size_
// Function to parse and print the DNS response
static bool parse_dns_response(unsigned char *buffer, size_t response_len, uint32_t *output) {
if (response_len < sizeof(DNSHeader)) {
ERROR_LOG(Log::sceNet, "DNS response too short");
ERROR_LOG(Log::Net, "DNS response too short");
return false;
}
@@ -524,13 +524,13 @@ static bool parse_dns_response(unsigned char *buffer, size_t response_len, uint3
int jump = *ptr;
ptr += jump + 1;
if (ptr >= end) {
ERROR_LOG(Log::sceNet, "DNS response malformed (question section)");
ERROR_LOG(Log::Net, "DNS response malformed (question section)");
return false;
}
}
ptr += 5; // Null byte + QTYPE (2 bytes) + QCLASS (2 bytes)
if (ptr > end) {
ERROR_LOG(Log::sceNet, "DNS response malformed (question section end)");
ERROR_LOG(Log::Net, "DNS response malformed (question section end)");
return false;
}
}
@@ -544,13 +544,13 @@ static bool parse_dns_response(unsigned char *buffer, size_t response_len, uint3
// Skip the name (can be a pointer or a sequence)
if (ptr >= end) {
ERROR_LOG(Log::sceNet, "DNS response malformed (answer %d name)", i);
ERROR_LOG(Log::Net, "DNS response malformed (answer %d name)", i);
return false;
}
if ((*ptr & 0xC0) == 0xC0) {
if (ptr + 2 > end) {
ERROR_LOG(Log::sceNet, "DNS response malformed (answer %d name pointer)", i);
ERROR_LOG(Log::Net, "DNS response malformed (answer %d name pointer)", i);
return false;
}
ptr += 2; // Pointer (2 bytes)
@@ -559,7 +559,7 @@ static bool parse_dns_response(unsigned char *buffer, size_t response_len, uint3
int jump = *ptr;
ptr += jump + 1;
if (ptr >= end) {
ERROR_LOG(Log::sceNet, "DNS response malformed (answer %d name loop)", i);
ERROR_LOG(Log::Net, "DNS response malformed (answer %d name loop)", i);
return false;
}
}
@@ -567,7 +567,7 @@ static bool parse_dns_response(unsigned char *buffer, size_t response_len, uint3
}
if (ptr + 10 > end) {
ERROR_LOG(Log::sceNet, "DNS response too short for answer %d header", i);
ERROR_LOG(Log::Net, "DNS response too short for answer %d header", i);
return false;
}
@@ -587,7 +587,7 @@ static bool parse_dns_response(unsigned char *buffer, size_t response_len, uint3
DEBUG_LOG(Log::Net, " Data length: %d", (int)data_len);
if (ptr + data_len > end) {
ERROR_LOG(Log::sceNet, "DNS response data exceeds buffer");
ERROR_LOG(Log::Net, "DNS response data exceeds buffer");
return false;
}
@@ -649,13 +649,13 @@ bool DirectDNSLookupIPV4(const char *dns_server_ip, const char *domain, uint32_t
timeout.tv_sec = 5;
timeout.tv_usec = 0;
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
WARN_LOG(Log::sceNet, "Failed to set socket timeout for DNS query");
WARN_LOG(Log::Net, "Failed to set socket timeout for DNS query");
}
#else
// Windows version
DWORD timeout = 5000; // 5 seconds
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout)) < 0) {
WARN_LOG(Log::sceNet, "Failed to set socket timeout for DNS query");
WARN_LOG(Log::Net, "Failed to set socket timeout for DNS query");
}
#endif
@@ -679,7 +679,7 @@ bool DirectDNSLookupIPV4(const char *dns_server_ip, const char *domain, uint32_t
unsigned char *qname = buffer + sizeof(DNSHeader);
size_t qname_space = sizeof(buffer) - sizeof(DNSHeader) - 4; // Reserve 4 bytes for qtype and qclass
if (!encode_domain_name(domain, qname, qname_space)) {
ERROR_LOG(Log::sceNet, "Domain name too long or invalid: %s", domain);
ERROR_LOG(Log::Net, "Domain name too long or invalid: %s", domain);
closesocket(sockfd);
return false;
}
@@ -700,7 +700,7 @@ bool DirectDNSLookupIPV4(const char *dns_server_ip, const char *domain, uint32_t
socklen_t server_len = sizeof(server_addr);
int response_len = recvfrom(sockfd, (char *)buffer, sizeof(buffer), 0, (struct sockaddr *)&server_addr, &server_len);
if (response_len < 0) {
ERROR_LOG(Log::sceNet, "Failed to receive DNS response (timeout or error)");
ERROR_LOG(Log::Net, "Failed to receive DNS response (timeout or error)");
closesocket(sockfd);
return false;
}
+3 -15
View File
@@ -263,13 +263,6 @@ bool OutputSink::Push(const char *buf, size_t bytes) {
return true;
}
bool OutputSink::PushCRLF(const std::string &s) {
if (Push(s)) {
return Push("r\n", 2);
}
return false;
}
size_t OutputSink::PushAtMost(const char *buf, size_t bytes) {
Drain();
@@ -310,14 +303,9 @@ bool OutputSink::Printf(const char *fmt, ...) {
// There wasn't enough space. Let's use a buffer instead.
// This could be caused by wraparound.
char temp[4096];
result = vsnprintf(temp, sizeof(temp), fmt, args);
result = vsnprintf(temp, sizeof(temp), fmt, backup);
if ((size_t)result < sizeof(temp) && result > 0) {
// In case it did return the null terminator.
if (temp[result - 1] == '\0') {
result--;
}
success = Push(temp, result);
// We've written so there's nothing more.
result = 0;
@@ -329,10 +317,10 @@ bool OutputSink::Printf(const char *fmt, ...) {
// Okay, did we actually write?
if (result >= (int)avail) {
// This means the result string was too big for the buffer.
ERROR_LOG(Log::IO, "Not enough space to format output.");
ERROR_LOG(Log::Net, "Not enough space to format output.");
return false;
} else if (result < 0) {
ERROR_LOG(Log::IO, "vsnprintf failed.");
ERROR_LOG(Log::Net, "vsnprintf failed.");
return false;
}
+4 -2
View File
@@ -2,6 +2,8 @@
#include <string>
#include "Common/Common.h"
namespace net {
class InputSink {
@@ -64,8 +66,8 @@ public:
bool Push(const std::string &s);
bool Push(const char *buf, size_t bytes);
size_t PushAtMost(const char *buf, size_t bytes);
bool PushCRLF(const std::string &s);
bool Printf(const char *fmt, ...);
ATTR_FORMAT_PRINTF(2, 3)
bool Printf(MSVC_FORMAT_PRINTF const char *fmt, ...);
bool Flush(bool allowBlock = true);
void Discard();