dns.mitm: support % in hosts file as stand-in for environment identifier

This commit is contained in:
Michael Scire 2021-02-02 00:38:04 -08:00 committed by SciresM
parent ffbdf29c10
commit 6950989552
7 changed files with 148 additions and 8 deletions

View file

@ -61,6 +61,7 @@
#include <stratosphere/ncm.hpp>
#include <stratosphere/nim.hpp>
#include <stratosphere/ns.hpp>
#include <stratosphere/nsd.hpp>
#include <stratosphere/patcher.hpp>
#include <stratosphere/pcv.hpp>
#include <stratosphere/pgl.hpp>

View file

@ -0,0 +1,18 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere/nsd/nsd_types.hpp>
#include <stratosphere/nsd/impl/device/nsd_device.hpp>

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/nsd/nsd_types.hpp>
namespace ams::nsd::impl::device {
const EnvironmentIdentifier &GetEnvironmentIdentifierFromSettings();
}

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
namespace ams::nsd {
struct EnvironmentIdentifier {
static constexpr size_t Size = 8;
char value[Size];
constexpr friend bool operator==(const EnvironmentIdentifier &lhs, const EnvironmentIdentifier &rhs) {
return util::Strncmp(lhs.value, rhs.value, Size) == 0;
}
constexpr friend bool operator!=(const EnvironmentIdentifier &lhs, const EnvironmentIdentifier &rhs) {
return !(lhs == rhs);
}
};
constexpr inline const EnvironmentIdentifier EnvironmentIdentifierOfProductDevice = {"lp1"};
constexpr inline const EnvironmentIdentifier EnvironmentIdentifierOfNotProductDevice = {"dd1"};
}

View file

@ -0,0 +1,49 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
namespace ams::nsd::impl::device {
namespace {
constexpr const char SettingsName[] = "nsd";
constexpr const char SettingsKey[] = "environment_identifier";
constinit os::SdkMutex g_environment_identifier_lock;
constinit EnvironmentIdentifier g_environment_identifier = {};
constinit bool g_determined_environment_identifier = false;
}
const EnvironmentIdentifier &GetEnvironmentIdentifierFromSettings() {
std::scoped_lock lk(g_environment_identifier_lock);
if (!g_determined_environment_identifier) {
/* Check size. */
AMS_ABORT_UNLESS(settings::fwdbg::GetSettingsItemValueSize(SettingsName, SettingsKey) != 0);
/* Get value. */
const size_t size = settings::fwdbg::GetSettingsItemValue(g_environment_identifier.value, EnvironmentIdentifier::Size, SettingsName, SettingsKey);
AMS_ABORT_UNLESS(size < EnvironmentIdentifier::Size);
AMS_ABORT_UNLESS(g_environment_identifier == EnvironmentIdentifierOfProductDevice || g_environment_identifier == EnvironmentIdentifierOfNotProductDevice);
g_determined_environment_identifier = true;
}
return g_environment_identifier;
}
}

View file

@ -31,7 +31,7 @@ namespace ams::util {
}
template<typename T>
int Strncmp(const T *lhs, const T *rhs, int count) {
constexpr int Strncmp(const T *lhs, const T *rhs, int count) {
AMS_ASSERT(lhs != nullptr);
AMS_ASSERT(rhs != nullptr);
AMS_ABORT_UNLESS(count >= 0);
@ -50,7 +50,7 @@ namespace ams::util {
}
template<typename T>
int Strnicmp(const T *lhs, const T *rhs, int count) {
constexpr int Strnicmp(const T *lhs, const T *rhs, int count) {
AMS_ASSERT(lhs != nullptr);
AMS_ASSERT(rhs != nullptr);
AMS_ABORT_UNLESS(count >= 0);

View file

@ -66,10 +66,7 @@ namespace ams::mitm::socket::resolver {
constexpr const char DefaultHostsFile[] =
"# Nintendo telemetry servers\n"
"127.0.0.1 receive-*.*.srv.nintendo.net\n";
static_assert(wildcardcmp("receive-*.*.srv.nintendo.net", "receive-lp1.dg.srv.nintendo.net") == 1);
static_assert(wildcardcmp("receive-*.*.srv.nintendo.net", "receive-lp1.er.srv.nintendo.net") == 1);
"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;
@ -77,6 +74,11 @@ namespace ams::mitm::socket::resolver {
constinit char g_specific_emummc_hosts_path[0x40] = {};
void ParseHostsFile(const char *file_data) {
/* Get the environment identifier from settings. */
const auto env = ams::nsd::impl::device::GetEnvironmentIdentifierFromSettings();
const auto env_len = std::strlen(env.value);
/* Parse the file. */
enum class State {
IgnoredLine,
BeginLine,
@ -189,8 +191,13 @@ namespace ams::mitm::socket::resolver {
if (c == '\n') {
state = State::BeginLine;
} else if (c != ' ' && c != '\r' && c != '\t') {
current_hostname[0] = c;
work = 1;
if (c == '%') {
std::memcpy(current_hostname, env.value, env_len);
work = env_len;
} else {
current_hostname[0] = c;
work = 1;
}
state = State::HostName;
}
break;
@ -207,6 +214,10 @@ namespace ams::mitm::socket::resolver {
} else {
state = State::WhiteSpace;
}
} else if (c == '%') {
AMS_ABORT_UNLESS(work < sizeof(current_hostname) - env_len);
std::memcpy(current_hostname + work, env.value, env_len);
work += env_len;
} else {
AMS_ABORT_UNLESS(work < sizeof(current_hostname) - 1);
current_hostname[work++] = c;