2018-06-10 03:33:22 +02:00
|
|
|
#pragma once
|
|
|
|
#include <switch.h>
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
2018-06-15 01:50:01 +02:00
|
|
|
#include "mitm_query_service.hpp"
|
2018-06-10 03:33:22 +02:00
|
|
|
#include "sm_mitm.h"
|
|
|
|
#include "mitm_session.hpp"
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class MitMSession;
|
|
|
|
|
|
|
|
template <typename T>
|
2018-06-13 00:00:09 +02:00
|
|
|
class MitMServer final : public IServer<T> {
|
2018-06-10 03:33:22 +02:00
|
|
|
static_assert(std::is_base_of<IServiceObject, T>::value, "Service Objects must derive from IServiceObject");
|
2018-06-13 00:00:09 +02:00
|
|
|
private:
|
2018-06-10 03:33:22 +02:00
|
|
|
char mitm_name[9];
|
|
|
|
|
2018-06-15 01:50:01 +02:00
|
|
|
public:
|
|
|
|
MitMServer(ISession<MitMQueryService<T>> **out_query_session, const char *service_name, unsigned int max_s, bool s_d = false) : IServer<T>(service_name, max_s, s_d) {
|
2018-06-13 00:00:09 +02:00
|
|
|
Handle tmp_hnd;
|
2018-06-15 01:50:01 +02:00
|
|
|
Handle out_query_h;
|
2018-06-13 00:00:09 +02:00
|
|
|
Result rc;
|
|
|
|
|
|
|
|
if (R_SUCCEEDED((rc = smGetServiceOriginal(&tmp_hnd, smEncodeName(service_name))))) {
|
|
|
|
svcCloseHandle(tmp_hnd);
|
|
|
|
} else {
|
|
|
|
fatalSimple(rc);
|
2018-06-10 03:33:22 +02:00
|
|
|
}
|
|
|
|
strncpy(mitm_name, service_name, 8);
|
|
|
|
mitm_name[8] = '\x00';
|
2018-06-15 01:50:01 +02:00
|
|
|
if (R_FAILED((rc = smMitMInstall(&this->port_handle, &out_query_h, mitm_name)))) {
|
2018-06-13 00:00:09 +02:00
|
|
|
fatalSimple(rc);
|
2018-06-10 03:33:22 +02:00
|
|
|
}
|
2018-06-15 01:50:01 +02:00
|
|
|
*out_query_session = new ServiceSession<MitMQueryService<T>>(NULL, out_query_h, 0);
|
2018-06-10 03:33:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~MitMServer() {
|
2018-06-13 00:00:09 +02:00
|
|
|
if (this->port_handle) {
|
|
|
|
if (R_FAILED(smMitMUninstall(this->mitm_name))) {
|
2018-06-10 03:33:22 +02:00
|
|
|
/* TODO: Panic. */
|
|
|
|
}
|
2018-06-13 00:00:09 +02:00
|
|
|
/* svcCloseHandle(port_handle); was called by ~IServer. */
|
2018-06-10 03:33:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-13 00:00:09 +02:00
|
|
|
ISession<T> *get_new_session(Handle session_h) override {
|
|
|
|
return new MitMSession<T>(this, session_h, 0, mitm_name);
|
2018-06-10 03:33:22 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|