Repository navigation
feat(libs): add ANYPS5_DNS_OVERRIDE host DNS override for guest lookups #1015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Thimo188
wants to merge
7
commits into
main
Choose a base branch
from
upstream-pr/23975
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+383
−6
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0696ea3
feat(libs): parse ANYPS5_DNS_OVERRIDE entries
MarcZX10 7147032
feat(libs): look up ANYPS5_DNS_OVERRIDE names
MarcZX10 ad4935b
feat(libSceNet): answer resolver lookups from ANYPS5_DNS_OVERRIDE
MarcZX10 65e83ad
feat(libScePosixForWebKit): answer getaddrinfo and gethostbyname from…
MarcZX10 0fab625
docs: describe ANYPS5_DNS_OVERRIDE for users
MarcZX10 c56bb68
fix(tests): use _putenv_s for DNS override tests on Windows
MarcZX10 4764619
fix(libSceNet): link DnsOverride into the net test targets
MarcZX10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| #include "common/DnsOverride.hpp" | ||
| #include <cctype> | ||
| #include <cstddef> | ||
| #include <cstdio> | ||
| #include <cstdlib> | ||
|
|
||
| namespace DnsOverride { | ||
| namespace { | ||
| bool IsDigit(char value) { return std::isdigit(static_cast<unsigned char>(value)) != 0; } | ||
|
|
||
| bool ParseAddress(const std::string& text, Address& address) { | ||
| std::size_t index = 0; | ||
| for (std::size_t part = 0; part < address.size(); ++part) { | ||
| if (part != 0) { | ||
| if (index >= text.size() || text[index] != '.') return false; | ||
| ++index; | ||
| } | ||
| const std::size_t start = index; | ||
| unsigned value = 0; | ||
| while (index < text.size() && IsDigit(text[index])) { | ||
| value = value * 10 + static_cast<unsigned>(text[index] - '0'); | ||
| if (value > 255 || index - start == 3) return false; | ||
| ++index; | ||
| } | ||
| const std::size_t digits = index - start; | ||
| if (digits == 0 || (digits > 1 && text[start] == '0')) return false; | ||
| address[part] = static_cast<std::uint8_t>(value); | ||
| } | ||
| return index == text.size(); | ||
| } | ||
|
|
||
| std::string ToLower(const std::string& text) { | ||
| std::string lower; | ||
| for (const char value : text) lower.push_back(static_cast<char>(std::tolower(static_cast<unsigned char>(value)))); | ||
| return lower; | ||
| } | ||
|
|
||
| bool ParseEntry(const std::string& text, Entry& entry) { | ||
| const std::size_t equals = text.find('='); | ||
| if (equals == std::string::npos || equals == 0) return false; | ||
| const std::string hostname = text.substr(0, equals); | ||
| for (const char value : hostname) | ||
| if (std::isspace(static_cast<unsigned char>(value))) return false; | ||
| if (!ParseAddress(text.substr(equals + 1), entry.address)) return false; | ||
| entry.hostname = ToLower(hostname); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| Parsed Parse(const char* text) { | ||
| Parsed parsed; | ||
| if (!text || *text == '\0') return parsed; | ||
| const std::string all(text); | ||
| std::size_t start = 0; | ||
| for (;;) { | ||
| const std::size_t comma = all.find(',', start); | ||
| const std::string item = all.substr(start, comma == std::string::npos ? std::string::npos : comma - start); | ||
| Entry entry; | ||
| if (ParseEntry(item, entry)) parsed.entries.push_back(entry); | ||
| else parsed.malformed.push_back(item); | ||
| if (comma == std::string::npos) break; | ||
| start = comma + 1; | ||
| } | ||
| return parsed; | ||
| } | ||
|
|
||
| Table::Table(const Parsed& parsed) : entries(parsed.entries) {} | ||
|
|
||
| bool Table::Find(const char* hostname, Address& address) const { | ||
| if (!hostname) return false; | ||
| const std::string key = ToLower(hostname); | ||
| for (const auto& entry : entries) { | ||
| if (entry.hostname != key) continue; | ||
| address = entry.address; | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| namespace { | ||
| const Table& ProcessTable() { | ||
| static const Table table = [] { | ||
| const Parsed parsed = Parse(std::getenv("ANYPS5_DNS_OVERRIDE")); | ||
| for (const auto& item : parsed.malformed) | ||
| std::fprintf(stderr, "[DNS-OVERRIDE] ignored malformed ANYPS5_DNS_OVERRIDE entry: %s\n", item.c_str()); | ||
| return Table(parsed); | ||
| }(); | ||
| return table; | ||
| } | ||
| } | ||
|
|
||
| bool LookupOverride(const char* hostname, Address& address) { | ||
| return ProcessTable().Find(hostname, address); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #pragma once | ||
|
|
||
| #include <array> | ||
| #include <cstdint> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace DnsOverride { | ||
| using Address = std::array<std::uint8_t, 4>; | ||
|
|
||
| struct Entry { | ||
| std::string hostname; | ||
| Address address{}; | ||
| }; | ||
|
|
||
| struct Parsed { | ||
| std::vector<Entry> entries; | ||
| std::vector<std::string> malformed; | ||
| }; | ||
|
|
||
| Parsed Parse(const char* text); | ||
|
|
||
| class Table { | ||
| public: | ||
| explicit Table(const Parsed& parsed); | ||
| bool Find(const char* hostname, Address& address) const; | ||
|
|
||
| private: | ||
| std::vector<Entry> entries; | ||
| }; | ||
|
|
||
| bool LookupOverride(const char* hostname, Address& address); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| #include "common/DnsOverride.hpp" | ||
| #include <cstdio> | ||
| #include <cstdlib> | ||
| #include <cstring> | ||
|
|
||
| namespace { | ||
| void Require(bool condition, const char* message) { | ||
| if (condition) return; | ||
| std::fprintf(stderr, "dns override lookup: %s\n", message); | ||
| std::abort(); | ||
| } | ||
|
|
||
| void RequireHit(const DnsOverride::Table& table, const char* hostname, DnsOverride::Address expected) { | ||
| DnsOverride::Address address{}; | ||
| Require(table.Find(hostname, address), "expected override missing"); | ||
| Require(address == expected, "override address changed"); | ||
| } | ||
|
|
||
| void RequireMiss(const DnsOverride::Table& table, const char* hostname) { | ||
| DnsOverride::Address address{9, 9, 9, 9}; | ||
| Require(!table.Find(hostname, address), "unexpected override"); | ||
| Require(address == (DnsOverride::Address{9, 9, 9, 9}), "miss changed the address"); | ||
| } | ||
| } | ||
|
|
||
| int main() { | ||
| const DnsOverride::Table table(DnsOverride::Parse("Example.COM=192.0.2.7,api.test=10.0.0.2,api.test=10.0.0.3,bad")); | ||
| RequireHit(table, "example.com", {192, 0, 2, 7}); | ||
| RequireHit(table, "EXAMPLE.Com", {192, 0, 2, 7}); | ||
| RequireHit(table, "api.TEST", {10, 0, 0, 2}); | ||
| RequireMiss(table, "sub.example.com"); | ||
| RequireMiss(table, "example.com."); | ||
| RequireMiss(table, "example"); | ||
| RequireMiss(table, ""); | ||
| RequireMiss(table, nullptr); | ||
| RequireMiss(DnsOverride::Table(DnsOverride::Parse(nullptr)), "example.com"); | ||
|
|
||
| #ifdef _WIN32 | ||
| Require(_putenv_s("ANYPS5_DNS_OVERRIDE", "env.test=198.51.100.9,bad entry") == 0, "putenv failed"); | ||
| #else | ||
| Require(setenv("ANYPS5_DNS_OVERRIDE", "env.test=198.51.100.9,bad entry", 1) == 0, "setenv failed"); | ||
| #endif | ||
| DnsOverride::Address address{}; | ||
| Require(DnsOverride::LookupOverride("ENV.test", address), "environment override missing"); | ||
| Require(address == (DnsOverride::Address{198, 51, 100, 9}), "environment override address changed"); | ||
| Require(!DnsOverride::LookupOverride("other.test", address), "environment override matched another name"); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| #include "common/DnsOverride.hpp" | ||
| #include <cstdio> | ||
| #include <cstdlib> | ||
|
|
||
| namespace { | ||
| void Require(bool condition, const char* message) { | ||
| if (condition) return; | ||
| std::fprintf(stderr, "dns override parser: %s\n", message); | ||
| std::abort(); | ||
| } | ||
|
|
||
| void RequireEntry(const DnsOverride::Parsed& parsed, std::size_t index, const char* hostname, | ||
| DnsOverride::Address address) { | ||
| Require(index < parsed.entries.size(), "entry missing"); | ||
| Require(parsed.entries[index].hostname == hostname, "hostname changed"); | ||
| Require(parsed.entries[index].address == address, "address changed"); | ||
| } | ||
|
|
||
| void RequireMalformed(const char* text, std::size_t count) { | ||
| const auto parsed = DnsOverride::Parse(text); | ||
| Require(parsed.entries.empty(), "malformed input produced an entry"); | ||
| Require(parsed.malformed.size() == count, "malformed count differs"); | ||
| } | ||
| } | ||
|
|
||
| int main() { | ||
| Require(DnsOverride::Parse(nullptr).entries.empty(), "null text produced entries"); | ||
| Require(DnsOverride::Parse("").entries.empty() && DnsOverride::Parse("").malformed.empty(), "empty text is not empty"); | ||
|
|
||
| const auto single = DnsOverride::Parse("Example.COM=192.0.2.7"); | ||
| Require(single.entries.size() == 1 && single.malformed.empty(), "single entry not parsed"); | ||
| RequireEntry(single, 0, "example.com", {192, 0, 2, 7}); | ||
|
|
||
| const auto many = DnsOverride::Parse("a.test=10.0.0.1,b.test=0.0.0.0,c.test=255.255.255.255"); | ||
| Require(many.entries.size() == 3 && many.malformed.empty(), "comma list not parsed"); | ||
| RequireEntry(many, 0, "a.test", {10, 0, 0, 1}); | ||
| RequireEntry(many, 1, "b.test", {0, 0, 0, 0}); | ||
| RequireEntry(many, 2, "c.test", {255, 255, 255, 255}); | ||
|
|
||
| const auto mixed = DnsOverride::Parse("good.test=1.2.3.4,bad,also.bad=1.2.3,ok.test=5.6.7.8"); | ||
| Require(mixed.entries.size() == 2 && mixed.malformed.size() == 2, "valid entries lost beside malformed ones"); | ||
| RequireEntry(mixed, 0, "good.test", {1, 2, 3, 4}); | ||
| RequireEntry(mixed, 1, "ok.test", {5, 6, 7, 8}); | ||
| Require(mixed.malformed[0] == "bad" && mixed.malformed[1] == "also.bad=1.2.3", "malformed text changed"); | ||
|
|
||
| RequireMalformed("=1.2.3.4", 1); | ||
| RequireMalformed("host.test=", 1); | ||
| RequireMalformed("host.test", 1); | ||
| RequireMalformed("host.test=1.2.3", 1); | ||
| RequireMalformed("host.test=1.2.3.4.5", 1); | ||
| RequireMalformed("host.test=256.0.0.1", 1); | ||
| RequireMalformed("host.test=1.2.3.04", 1); | ||
| RequireMalformed("host.test=1..2.3", 1); | ||
| RequireMalformed("host.test=1.2.3.4 ", 1); | ||
| RequireMalformed("host.test=::1", 1); | ||
| RequireMalformed("host.test=1234.0.0.1", 1); | ||
| RequireMalformed("bad host=1.2.3.4", 1); | ||
| RequireMalformed(",", 2); | ||
| const auto trailing = DnsOverride::Parse("a.test=1.2.3.4,"); | ||
| Require(trailing.entries.size() == 1 && trailing.malformed.size() == 1, "trailing comma not reported"); | ||
| RequireEntry(trailing, 0, "a.test", {1, 2, 3, 4}); | ||
| return 0; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reject matched IPv6 queries before native resolution.
With
hints.family=28andhints.flags=0x800, this branch substitutes the IPv4 literal but retainsAI_V4MAPPED. Nativegetaddrinfocan then return::ffff:192.0.2.44, which the conversion loop accepts as IPv6. The native numeric-address implementation explicitly supports this mapping. (raw.githubusercontent.com)This violates the PR requirement that an IPv6 lookup for an overridden name finds no address. On an override match, reject family
28before calling the native resolver. Preserve IPv4 override results and unmatched queries. Extend the IPv6 test to include0x800.