Skip to content

Commit

Permalink
dns.mitm: make line ordering explicit, rather than implicit.
Browse files Browse the repository at this point in the history
This doesn't actually change functionality, because this is how std::unordered_map worked anyway...

...but it's better for us to be explicit, I think.
  • Loading branch information
SciresM committed Feb 2, 2021
1 parent 408b81b commit bcda834
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/features/dns_mitm.md
Expand Up @@ -11,6 +11,8 @@ In particular, hosts files parsed by DNS.mitm have the following extensions to t
+ `*` is treated as a wildcard character, matching any collection of 0 or more characters wherever it occurs in a hostname.
+ `%` is treated as a stand-in for the value of `nsd!environment_identifier`. This is always `lp1`, on production devices.

If multiple entries in a host file match a domain, the last-defined match is used.

Please note that homebrew may trigger a hosts file re-parse by sending the extension IPC command 65000 ("AtmosphereReloadHostsFile") to a connected `sfdnsres` session.

### Hosts file selection
Expand Down
26 changes: 20 additions & 6 deletions stratosphere/ams_mitm/source/dns_mitm/dnsmitm_host_redirection.cpp
Expand Up @@ -69,7 +69,21 @@ namespace ams::mitm::socket::resolver {
"127.0.0.1 receive-%.dg.srv.nintendo.net receive-%.er.srv.nintendo.net\n";

constinit os::SdkMutex g_redirection_lock;
std::unordered_map<std::string, ams::socket::InAddrT> g_redirection_map;
std::vector<std::pair<std::string, ams::socket::InAddrT>> g_redirection_list;

void RemoveRedirection(const char *hostname) {
for (auto it = g_redirection_list.begin(); it != g_redirection_list.end(); ++it) {
if (std::strcmp(it->first.c_str(), hostname) == 0) {
g_redirection_list.erase(it);
break;
}
}
}

void AddRedirection(const char *hostname, ams::socket::InAddrT addr) {
RemoveRedirection(hostname);
g_redirection_list.emplace(g_redirection_list.begin(), std::string(hostname), addr);
}

constinit char g_specific_emummc_hosts_path[0x40] = {};

Expand Down Expand Up @@ -206,7 +220,7 @@ namespace ams::mitm::socket::resolver {
AMS_ABORT_UNLESS(work < sizeof(current_hostname));
current_hostname[work] = '\x00';

g_redirection_map[static_cast<const char *>(current_hostname)] = current_address;
AddRedirection(current_hostname, current_address);
work = 0;

if (c == '\n') {
Expand All @@ -229,7 +243,7 @@ namespace ams::mitm::socket::resolver {
AMS_ABORT_UNLESS(work < sizeof(current_hostname));
current_hostname[work] = '\x00';

g_redirection_map[static_cast<const char *>(current_hostname)] = current_address;
AddRedirection(current_hostname, current_address);
}
}

Expand Down Expand Up @@ -293,7 +307,7 @@ namespace ams::mitm::socket::resolver {
std::scoped_lock lk(g_redirection_lock);

/* Clear the redirections map. */
g_redirection_map.clear();
g_redirection_list.clear();

/* Open log file. */
::FsFile log_file;
Expand Down Expand Up @@ -362,15 +376,15 @@ namespace ams::mitm::socket::resolver {

/* Print the redirections. */
Log(log_file, "Redirections:\n");
for (const auto &[host, address] : g_redirection_map) {
for (const auto &[host, address] : g_redirection_list) {
Log(log_file, " `%s` -> %u.%u.%u.%u\n", host.c_str(), (address >> 0) & 0xFF, (address >> 8) & 0xFF, (address >> 16) & 0xFF, (address >> 24) & 0xFF);
}
}

bool GetRedirectedHostByName(ams::socket::InAddrT *out, const char *hostname) {
std::scoped_lock lk(g_redirection_lock);

for (const auto &[host, address] : g_redirection_map) {
for (const auto &[host, address] : g_redirection_list) {
if (wildcardcmp(host.c_str(), hostname)) {
*out = address;
return true;
Expand Down

0 comments on commit bcda834

Please sign in to comment.