fs.mitm: Simplify initialization

This commit is contained in:
Tony Wasserka 2018-06-16 17:09:23 +02:00
parent 1fdb9696b5
commit 98ea14e880
4 changed files with 30 additions and 49 deletions

View file

@ -9,10 +9,10 @@
class IMitMServiceObject : public IServiceObject {
protected:
Service *forward_service;
u64 process_id;
u64 title_id;
u64 process_id = 0;
u64 title_id = 0;
public:
IMitMServiceObject(Service *s) : forward_service(s), process_id(0), title_id(0) {
IMitMServiceObject(Service *s) : forward_service(s) {
}
@ -22,7 +22,7 @@ class IMitMServiceObject : public IServiceObject {
virtual void clone_to(void *o) = 0;
protected:
virtual ~IMitMServiceObject() { }
virtual ~IMitMServiceObject() = default;
virtual Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) = 0;
virtual void postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) = 0;
virtual Result handle_deferred() = 0;

View file

@ -19,33 +19,35 @@ class MitMSession final : public ISession<T> {
/* This will be for the actual session. */
Service forward_service;
IpcParsedCommand cur_out_r;
u32 mitm_domain_id;
u32 mitm_domain_id = 0;
bool got_first_message;
public:
MitMSession<T>(MitMServer<T> *s, Handle s_h, Handle c_h, const char *srv) : ISession<T>(s, s_h, c_h, NULL, 0), mitm_domain_id(0), got_first_message(false) {
MitMSession<T>(MitMServer<T> *s, Handle s_h, Handle c_h, const char *srv) : ISession<T>(s, s_h, c_h, NULL, 0), got_first_message(false) {
this->server = s;
this->server_handle = s_h;
this->client_handle = c_h;
if (R_FAILED(smMitMGetService(&forward_service, srv))) {
/* TODO: Panic. */
}
if (R_FAILED(ipcQueryPointerBufferSize(forward_service.handle, &this->pointer_buffer_size))) {
size_t pointer_buffer_size = 0;
if (R_FAILED(ipcQueryPointerBufferSize(forward_service.handle, &pointer_buffer_size))) {
/* TODO: Panic. */
}
this->service_object = std::make_shared<T>(&forward_service);
this->pointer_buffer = new char[this->pointer_buffer_size];
this->pointer_buffer.resize(pointer_buffer_size);
}
MitMSession<T>(MitMServer<T> *s, Handle s_h, Handle c_h, Handle f_h) : ISession<T>(s, s_h, c_h, NULL, 0), mitm_domain_id(0), got_first_message(true) {
MitMSession<T>(MitMServer<T> *s, Handle s_h, Handle c_h, Handle f_h) : ISession<T>(s, s_h, c_h, NULL, 0), got_first_message(true) {
this->server = s;
this->server_handle = s_h;
this->client_handle = c_h;
serviceCreate(&this->forward_service, f_h);
if (R_FAILED(ipcQueryPointerBufferSize(forward_service.handle, &this->pointer_buffer_size))) {
size_t pointer_buffer_size = 0;
if (R_FAILED(ipcQueryPointerBufferSize(forward_service.handle, &pointer_buffer_size))) {
/* TODO: Panic. */
}
this->service_object = std::make_shared<T>(&forward_service);
this->pointer_buffer = new char[this->pointer_buffer_size];
this->pointer_buffer.resize(pointer_buffer_size);
}
virtual ~MitMSession() {
@ -92,7 +94,7 @@ class MitMSession final : public ISession<T> {
obj = this->service_object;
}
if (obj != nullptr) {
retval = obj->dispatch(r, c, cmd_id, (u8 *)this->pointer_buffer, this->pointer_buffer_size);
retval = obj->dispatch(r, c, cmd_id, (u8 *)this->pointer_buffer.data(), this->pointer_buffer.size());
if (R_SUCCEEDED(retval)) {
if (r.IsDomainMessage) {
ipcParseForDomain(&cur_out_r);
@ -197,7 +199,7 @@ class MitMSession final : public ISession<T> {
if (this->active_object == this->service_object && (r.CommandType == IpcCommandType_Request || r.CommandType == IpcCommandType_RequestWithContext)) {
IpcCommand c;
ipcInitialize(&c);
this->service_object->postprocess(cur_out_r, c, cmd_id, (u8 *)this->pointer_buffer, this->pointer_buffer_size);
this->service_object->postprocess(cur_out_r, c, cmd_id, (u8 *)this->pointer_buffer.data(), this->pointer_buffer.size());
} else if (r.CommandType == IpcCommandType_Control || r.CommandType == IpcCommandType_ControlWithContext) {
if (cmd_id == IpcCtrl_Cmd_ConvertCurrentObjectToDomain) {
this->is_domain = true;
@ -226,4 +228,4 @@ class MitMSession final : public ISession<T> {
}
}
}
};
};

View file

@ -18,9 +18,7 @@ class IPCSession final : public ISession<T> {
fatalSimple(rc);
}
this->service_object = std::make_shared<T>();
this->pointer_buffer_size = pbs;
this->pointer_buffer = new char[this->pointer_buffer_size];
this->is_domain = false;
this->pointer_buffer.resize(pbs);
}
IPCSession<T>(std::shared_ptr<T> so, size_t pbs = 0x400) : ISession<T>(NULL, 0, 0, so, 0) {
@ -28,8 +26,6 @@ class IPCSession final : public ISession<T> {
if (R_FAILED((rc = svcCreateSession(&this->server_handle, &this->client_handle, 0, 0)))) {
fatalSimple(rc);
}
this->pointer_buffer_size = pbs;
this->pointer_buffer = new char[this->pointer_buffer_size];
this->is_domain = false;
this->pointer_buffer.resize(pbs);
}
};

View file

@ -17,7 +17,6 @@ enum IpcControlCommand {
IpcCtrl_Cmd_CloneCurrentObjectEx = 4
};
#define POINTER_BUFFER_SIZE_MAX 0xFFFF
#define RESULT_DEFER_SESSION (0x6580A)
@ -34,39 +33,23 @@ class ISession : public IWaitable {
IServer<T> *server;
Handle server_handle;
Handle client_handle;
char *pointer_buffer;
size_t pointer_buffer_size;
std::vector<char> pointer_buffer;
bool is_domain;
bool is_domain = false;
std::shared_ptr<DomainOwner> domain;
std::shared_ptr<IServiceObject> active_object;
static_assert(sizeof(pointer_buffer) <= POINTER_BUFFER_SIZE_MAX, "Incorrect Size for PointerBuffer!");
public:
ISession<T>(IServer<T> *s, Handle s_h, Handle c_h, size_t pbs = 0x400) : server(s), server_handle(s_h), client_handle(c_h), pointer_buffer_size(pbs) {
ISession<T>(IServer<T> *s, Handle s_h, Handle c_h, size_t pbs = 0x400) : server(s), server_handle(s_h), client_handle(c_h), pointer_buffer(pbs) {
this->service_object = std::make_shared<T>();
if (this->pointer_buffer_size) {
this->pointer_buffer = new char[this->pointer_buffer_size];
}
this->is_domain = false;
this->domain.reset();
this->active_object.reset();
}
ISession<T>(IServer<T> *s, Handle s_h, Handle c_h, std::shared_ptr<T> so, size_t pbs = 0x400) : service_object(so), server(s), server_handle(s_h), client_handle(c_h), pointer_buffer_size(pbs) {
if (this->pointer_buffer_size) {
this->pointer_buffer = new char[this->pointer_buffer_size];
}
this->is_domain = false;
this->domain.reset();
this->active_object.reset();
ISession<T>(IServer<T> *s, Handle s_h, Handle c_h, std::shared_ptr<T> so, size_t pbs = 0x400) : service_object(so), server(s), server_handle(s_h), client_handle(c_h), pointer_buffer(pbs) {
}
~ISession() override {
delete this->pointer_buffer;
if (server_handle) {
svcCloseHandle(server_handle);
}
@ -153,7 +136,7 @@ class ISession : public IWaitable {
break;
case IpcCommandType_Request:
case IpcCommandType_RequestWithContext:
retval = this->active_object->dispatch(r, c, cmd_id, (u8 *)this->pointer_buffer, this->pointer_buffer_size);
retval = this->active_object->dispatch(r, c, cmd_id, (u8 *)pointer_buffer.data(), pointer_buffer.size());
break;
case IpcCommandType_Control:
case IpcCommandType_ControlWithContext:
@ -185,7 +168,7 @@ class ISession : public IWaitable {
/* Prepare pointer buffer... */
IpcCommand c_for_reply;
ipcInitialize(&c_for_reply);
ipcAddRecvStatic(&c_for_reply, this->pointer_buffer, this->pointer_buffer_size, 0);
ipcAddRecvStatic(&c_for_reply, this->pointer_buffer.data(), this->pointer_buffer.size(), 0);
ipcPrepareHeader(&c_for_reply, 0);
if (R_SUCCEEDED(rc = svcReplyAndReceive(&handle_index, &this->server_handle, 1, 0, U64_MAX))) {
@ -247,19 +230,19 @@ class ISession : public IWaitable {
/* TODO: Implement. */
switch ((IpcControlCommand)cmd_id) {
case IpcCtrl_Cmd_ConvertCurrentObjectToDomain:
rc = WrapIpcCommandImpl<&ISession::ConvertCurrentObjectToDomain>(this, r, out_c, (u8 *)this->pointer_buffer, this->pointer_buffer_size);
rc = WrapIpcCommandImpl<&ISession::ConvertCurrentObjectToDomain>(this, r, out_c, (u8 *)this->pointer_buffer.data(), pointer_buffer.size());
break;
case IpcCtrl_Cmd_CopyFromCurrentDomain:
rc = WrapIpcCommandImpl<&ISession::CopyFromCurrentDomain>(this, r, out_c, (u8 *)this->pointer_buffer, this->pointer_buffer_size);
rc = WrapIpcCommandImpl<&ISession::CopyFromCurrentDomain>(this, r, out_c, (u8 *)this->pointer_buffer.data(), pointer_buffer.size());
break;
case IpcCtrl_Cmd_CloneCurrentObject:
rc = WrapIpcCommandImpl<&ISession::CloneCurrentObject>(this, r, out_c, (u8 *)this->pointer_buffer, this->pointer_buffer_size);
rc = WrapIpcCommandImpl<&ISession::CloneCurrentObject>(this, r, out_c, (u8 *)this->pointer_buffer.data(), pointer_buffer.size());
break;
case IpcCtrl_Cmd_QueryPointerBufferSize:
rc = WrapIpcCommandImpl<&ISession::QueryPointerBufferSize>(this, r, out_c, (u8 *)this->pointer_buffer, this->pointer_buffer_size);
rc = WrapIpcCommandImpl<&ISession::QueryPointerBufferSize>(this, r, out_c, (u8 *)this->pointer_buffer.data(), pointer_buffer.size());
break;
case IpcCtrl_Cmd_CloneCurrentObjectEx:
rc = WrapIpcCommandImpl<&ISession::CloneCurrentObjectEx>(this, r, out_c, (u8 *)this->pointer_buffer, sizeof(this->pointer_buffer));
rc = WrapIpcCommandImpl<&ISession::CloneCurrentObjectEx>(this, r, out_c, (u8 *)this->pointer_buffer.data(), pointer_buffer.size());
break;
default:
break;
@ -282,7 +265,7 @@ class ISession : public IWaitable {
return {0xF601};
}
std::tuple<Result, u32> QueryPointerBufferSize() {
return {0x0, (u32)this->pointer_buffer_size};
return {0x0, (u32)this->pointer_buffer.size()};
}
std::tuple<Result> CloneCurrentObjectEx() {
/* TODO */