Skip to content

Commit

Permalink
Merge remote-tracking branch 'SciresM/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemeowx2 authored Oct 28, 2021
2 parents cab3953 + 1d86435 commit 5c2a353
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 78 deletions.
12 changes: 5 additions & 7 deletions ldn_mitm/source/lan_discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ namespace ams::mitm::ldn {
return -1;
}
return this->socket->recvPacket([&](LANPacketType type, const void *data, size_t size, ReplyFunc reply) -> int {
(void)reply;

AMS_UNUSED(reply);
if (type == LANPacketType::Connect) {
LogFormat("on connect");
NodeInfo *info = (decltype(info))data;
Expand Down Expand Up @@ -90,8 +89,7 @@ namespace ams::mitm::ldn {
const auto state = this->discovery->getState();
if (state == CommState::Station || state == CommState::StationConnected) {
return this->recvPacket([&](LANPacketType type, const void *data, size_t size, ReplyFunc reply) -> int {
(void)reply;

AMS_UNUSED(reply);
if (type == LANPacketType::SyncNetwork) {
LogFormat("SyncNetwork");
NetworkInfo *info = (decltype(info))data;
Expand Down Expand Up @@ -283,7 +281,7 @@ namespace ams::mitm::ldn {
return MAKERESULT(ModuleID, 8);
}
}
rc = setSocketOpts(fd);
rc = setSocketOpts(fd);
if (R_FAILED(rc)) {
return rc;
}
Expand Down Expand Up @@ -573,7 +571,7 @@ namespace ams::mitm::ldn {
if (R_FAILED(rc)) {
return rc;
}

this->setState(CommState::AccessPointCreated);

this->initNodeStateChange();
Expand Down Expand Up @@ -755,7 +753,7 @@ namespace ams::mitm::ldn {
return rc;
}

rc = os::CreateThread(&this->workerThread, &Worker, this, stack.get(), StackSize, 0x15, 2);
rc = os::CreateThread(&this->workerThread, &Worker, this, reinterpret_cast<void *>(util::AlignUp(reinterpret_cast<uintptr_t>(stack.get()), os::ThreadStackAlignment)), StackSize, 0x15, 2);
if (R_FAILED(rc)) {
LogFormat("LANDiscovery Failed to threadCreate: %x", rc);
return 0xF601;
Expand Down
3 changes: 2 additions & 1 deletion ldn_mitm/source/lan_discovery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ namespace ams::mitm::ldn {
Result getFakeMac(MacAddress *mac);
Result getNodeInfo(NodeInfo *node, const UserConfig *userConfig, u16 localCommunicationVersion);
LanEventFunc lanEvent;
std::unique_ptr<u8[StackSize]> stack;
std::unique_ptr<u8[]> stack;
public:
Result initialize(LanEventFunc lanEvent = EmptyFunc, bool listening = true);
Result finalize();
Expand All @@ -178,6 +178,7 @@ namespace ams::mitm::ldn {
networkInfo({}), listenPort(port),
state(CommState::None)
{
this->stack = std::make_unique<u8[]>(os::ThreadStackAlignment + StackSize);
LogFormat("LANDiscovery");
};
~LANDiscovery();
Expand Down
10 changes: 5 additions & 5 deletions ldn_mitm/source/lan_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <arpa/inet.h>
#include <cstring>
#include <unistd.h>
#include <stratosphere.hpp>

#define POLL_UNKNOWN (~(POLLIN | POLLPRI | POLLOUT))
// Only used when debuging
Expand Down Expand Up @@ -65,6 +66,7 @@ void LanSocket::resetRecvSize() {
}

int LanSocket::recvPartPacket(u8 *buffer, size_t bufLen, struct sockaddr_in *addr) {
AMS_UNUSED(bufLen);
constexpr int HeaderSize = sizeof(LANPacketHeader);
ssize_t len = this->recvfrom((void *)(this->buffer + this->recvSize), sizeof(this->buffer) - this->recvSize, addr);
if (len <= 0) {
Expand Down Expand Up @@ -180,17 +182,15 @@ void LanSocket::prepareHeader(LANPacketHeader &header, LANPacketType type) {
}

ssize_t TcpLanSocketBase::recvfrom(void *buf, size_t len, struct sockaddr_in *addr) {
(void)addr;

AMS_UNUSED(addr);
auto rc = ::recvfrom(this->fd, buf, len, 0, nullptr, 0);
if (rc == 0) {
return -0xFD23;
}
return rc;
}
int TcpLanSocketBase::sendto(const void *buf, size_t len, struct sockaddr_in *addr) {
(void)addr;

AMS_UNUSED(addr);
return ::sendto(this->fd, buf, len, 0, nullptr, 0);
}

Expand Down Expand Up @@ -278,4 +278,4 @@ int LanSocket::decompress(const void *input, size_t input_size, uint8_t *output,
*output_size = out - output;

return in == in_end ? 0 : -1;
}
}
6 changes: 2 additions & 4 deletions ldn_mitm/source/ldn_icommunication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ namespace ams::mitm::ldn {
}

Result ICommunicationService::Scan(sf::Out<u32> outCount, sf::OutAutoSelectArray<NetworkInfo> buffer, u16 channel, ScanFilter filter) {
(void)channel;

AMS_UNUSED(channel);
Result rc = 0;
u16 count = buffer.GetSize();

Expand Down Expand Up @@ -174,8 +173,7 @@ namespace ams::mitm::ldn {

/*nyi*/
Result ICommunicationService::SetStationAcceptPolicy(u8 policy) {
(void)policy;

AMS_UNUSED(policy);
return 0;
}

Expand Down
171 changes: 111 additions & 60 deletions ldn_mitm/source/ldnmitm_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,104 +13,155 @@
* 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>

#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <malloc.h>

#include <switch.h>
#include <stratosphere.hpp>

#include "ldnmitm_service.hpp"

extern "C" {
extern u32 __start__;

#define INNER_HEAP_SIZE 0x100000
size_t nx_inner_heap_size = INNER_HEAP_SIZE;
char nx_inner_heap[INNER_HEAP_SIZE];
#include <switch/services/bsd.h>

}

#include "ldnmitm_service.hpp"

namespace ams {
ncm::ProgramId CurrentProgramId = { 0x4200000000000010ul };

namespace result {
bool CallFatalOnResultAssertion = false;
}
namespace {

namespace init {
void InitializeSystemModule() {
svcSleepThread(10000000000L);
constexpr size_t MallocBufferSize = 1_MB;
alignas(os::MemoryPageSize) constinit u8 g_malloc_buffer[MallocBufferSize];

consteval size_t GetLibnxBsdTransferMemorySize(const ::SocketInitConfig *config) {
const u32 tcp_tx_buf_max_size = config->tcp_tx_buf_max_size != 0 ? config->tcp_tx_buf_max_size : config->tcp_tx_buf_size;
const u32 tcp_rx_buf_max_size = config->tcp_rx_buf_max_size != 0 ? config->tcp_rx_buf_max_size : config->tcp_rx_buf_size;
const u32 sum = tcp_tx_buf_max_size + tcp_rx_buf_max_size + config->udp_tx_buf_size + config->udp_rx_buf_size;

return config->sb_efficiency * util::AlignUp(sum, os::MemoryPageSize);
}

constexpr const ::SocketInitConfig LibnxSocketInitConfig = {
.bsdsockets_version = 1,

.tcp_tx_buf_size = 0x800,
.tcp_rx_buf_size = 0x1000,
.tcp_tx_buf_max_size = 0x2000,
.tcp_rx_buf_max_size = 0x2000,

.udp_tx_buf_size = 0x2000,
.udp_rx_buf_size = 0x2000,

.sb_efficiency = 4,

.num_bsd_sessions = 3,
.bsd_service_type = BsdServiceType_User,
};

alignas(os::MemoryPageSize) constinit u8 g_socket_tmem_buffer[GetLibnxBsdTransferMemorySize(std::addressof(LibnxSocketInitConfig))];

constexpr const ::BsdInitConfig LibnxBsdInitConfig = {
.version = LibnxSocketInitConfig.bsdsockets_version,

#define SOCK_BUFFERSIZE 0x1000
const SocketInitConfig socketInitConfig = {
.bsdsockets_version = 1,
.tmem_buffer = g_socket_tmem_buffer,
.tmem_buffer_size = sizeof(g_socket_tmem_buffer),

.tcp_tx_buf_size = 0x800,
.tcp_rx_buf_size = 0x1000,
.tcp_tx_buf_max_size = 0x2000,
.tcp_rx_buf_max_size = 0x2000,
.tcp_tx_buf_size = LibnxSocketInitConfig.tcp_tx_buf_size,
.tcp_rx_buf_size = LibnxSocketInitConfig.tcp_rx_buf_size,
.tcp_tx_buf_max_size = LibnxSocketInitConfig.tcp_tx_buf_max_size,
.tcp_rx_buf_max_size = LibnxSocketInitConfig.tcp_rx_buf_max_size,

.udp_tx_buf_size = 0x2000,
.udp_rx_buf_size = 0x2000,
.udp_tx_buf_size = LibnxSocketInitConfig.udp_tx_buf_size,
.udp_rx_buf_size = LibnxSocketInitConfig.udp_rx_buf_size,

.sb_efficiency = 4,
.sb_efficiency = LibnxSocketInitConfig.sb_efficiency,
};

.num_bsd_sessions = 3,
.bsd_service_type = BsdServiceType_User,
}

namespace mitm {

namespace {

struct LdnMitmManagerOptions {
static constexpr size_t PointerBufferSize = 0x1000;
static constexpr size_t MaxDomains = 0x10;
static constexpr size_t MaxDomainObjects = 0x100;
static constexpr bool CanDeferInvokeRequest = false;
static constexpr bool CanManageMitmServers = true;
};

class ServerManager final : public sf::hipc::ServerManager<1, LdnMitmManagerOptions, 3> {
private:
virtual ams::Result OnNeedsToAccept(int port_index, Server *server) override;
};

ServerManager g_server_manager;

Result ServerManager::OnNeedsToAccept(int port_index, Server *server) {
AMS_UNUSED(port_index);
/* Acknowledge the mitm session. */
std::shared_ptr<::Service> fsrv;
sm::MitmProcessInfo client_info;
server->AcknowledgeMitmSession(std::addressof(fsrv), std::addressof(client_info));
return this->AcceptMitmImpl(server, sf::CreateSharedObjectEmplaced<mitm::ldn::ILdnMitMService, mitm::ldn::LdnMitMService>(decltype(fsrv)(fsrv), client_info), fsrv);
}

}

}

namespace init {

void InitializeSystemModule() {
/* Sleep 10 seconds (seems unnecessary). */
os::SleepThread(TimeSpan::FromSeconds(10));

/* Initialize our connection to sm. */
R_ABORT_UNLESS(sm::Initialize());
R_ABORT_UNLESS(fsInitialize());

/* Initialize fs. */
fs::InitializeForSystem();
fs::SetEnabledAutoAbort(false);

/* Initialize other services. */

R_ABORT_UNLESS(ipinfoInit());
R_ABORT_UNLESS(socketInitialize(&socketInitConfig));
R_ABORT_UNLESS(bsdInitialize(&LibnxBsdInitConfig, LibnxSocketInitConfig.num_bsd_sessions, LibnxSocketInitConfig.bsd_service_type));
R_ABORT_UNLESS(socketInitialize(&LibnxSocketInitConfig));
R_ABORT_UNLESS(fsdevMountSdmc());

LogFormat("__appInit done");
LogFormat("InitializeSystemModule done");
}

void FinalizeSystemModule() {
fsdevUnmountAll();
socketExit();
ipinfoExit();
fsExit();
void FinalizeSystemModule() { /* ... */ }

void Startup() {
/* Initialize the global malloc allocator. */
init::InitializeAllocator(g_malloc_buffer, sizeof(g_malloc_buffer));
}

}

struct LdnMitmManagerOptions {
static constexpr size_t PointerBufferSize = 0x1000;
static constexpr size_t MaxDomains = 0x10;
static constexpr size_t MaxDomainObjects = 0x100;
static constexpr bool CanDeferInvokeRequest = false;
static constexpr bool CanManageMitmServers = true;
};

class ServerManager final : public sf::hipc::ServerManager<1, LdnMitmManagerOptions, 3> {
private:
virtual ams::Result OnNeedsToAccept(int port_index, Server *server) override;
};

ServerManager g_server_manager;

ams::Result ServerManager::OnNeedsToAccept(int port_index, Server *server) {
(void)port_index;

/* Acknowledge the mitm session. */
std::shared_ptr<::Service> fsrv;
sm::MitmProcessInfo client_info;
server->AcknowledgeMitmSession(std::addressof(fsrv), std::addressof(client_info));
return this->AcceptMitmImpl(server, sf::CreateSharedObjectEmplaced<ams::mitm::ldn::ILdnMitMService, ams::mitm::ldn::LdnMitMService>(decltype(fsrv)(fsrv), client_info), fsrv);
void NORETURN Exit(int rc) {
AMS_UNUSED(rc);
AMS_ABORT("Exit called by immortal process");
}

void Main() {
LogFormat("main");

constexpr sm::ServiceName MitmServiceName = sm::ServiceName::Encode("ldn:u");
//sf::hipc::ServerManager<2, LdnMitmManagerOptions, 3> server_manager;
R_ABORT_UNLESS((g_server_manager.RegisterMitmServer<ams::mitm::ldn::LdnMitMService>(0, MitmServiceName)));
R_ABORT_UNLESS((mitm::g_server_manager.RegisterMitmServer<mitm::ldn::LdnMitMService>(0, MitmServiceName)));
LogFormat("registered");

g_server_manager.LoopProcess();
mitm::g_server_manager.LoopProcess();
}

}

0 comments on commit 5c2a353

Please sign in to comment.