-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathIpcBridge.cpp
218 lines (192 loc) · 5.22 KB
/
IpcBridge.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "Ctu.h"
#include "IpcStubs.h"
#define BRIDGE_PORT 31337
IpcBridge::IpcBridge(Ctu *_ctu) : ctu(_ctu) {
struct sockaddr_in addr;
serv = socket(AF_INET, SOCK_STREAM, 0);
auto enable = 1;
setsockopt(serv, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
addr.sin_family = AF_INET;
addr.sin_port = htons(BRIDGE_PORT);
addr.sin_addr.s_addr = INADDR_ANY;
memset(addr.sin_zero, '\0', sizeof addr.sin_zero);
bind(serv, (struct sockaddr *) &addr, sizeof addr);
listen(serv, 10);
client = -1;
waitingForAsync = false;
}
void IpcBridge::start() {
LOG_INFO(IpcBridge, "Starting");
for(auto i = 0; i < 24; ++i) {
auto addr = (i + 1) * (1 << 20) + (1 << 28);
ctu->cpu.map(addr, 1024 * 1024);
buffers[i] = addr;
}
auto thread = ctu->tm.createNative([this] { run(); });
thread->resume();
}
bool isReadable(int fd) {
struct timeval tv;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
tv.tv_sec = tv.tv_usec = 0;
return select(fd + 1, &rfds, nullptr, nullptr, &tv) != -1 && FD_ISSET(fd, &rfds);
}
uint64_t IpcBridge::readint() {
uint64_t val = 0;
recv(client, &val, 8, 0);
return val;
}
uint64_t IpcBridge::readint(bool &closed) {
uint64_t val = 0;
if(recv(client, &val, 8, 0) == 0)
closed = true;
return val;
}
string IpcBridge::readstring() {
auto size = readint();
auto buf = new char[size];
recv(client, buf, size, 0);
string ret(buf, size);
delete[] buf;
return ret;
}
IpcData IpcBridge::readdata() {
auto size = readint();
auto buf = new uint8_t[size];
recv(client, buf, size, 0);
auto bufptr = buffers[bufferOff++];
ctu->cpu.writemem(bufptr, buf, size);
delete[] buf;
return IpcData(bufptr, size);
}
void IpcBridge::writeint(uint64_t val) {
send(client, &val, 8, 0);
}
void IpcBridge::writedesc(vector<tuple<IpcData, int>> descs) {
writeint(descs.size());
for(auto [data, flag] : descs) {
writeint(data.size);
auto buf = new uint8_t[data.size];
ctu->cpu.readmem(data.ptr, buf, data.size);
send(client, buf, data.size, 0);
writeint(flag);
}
}
void IpcBridge::writedesc(vector<IpcData> descs) {
writeint(descs.size());
if(descs.size() > 0)
LOG_ERROR(IpcBridge, "C descriptors unsupported");
}
void IpcBridge::sendResponse(ghandle hnd, uint32_t res, bool close, shared_ptr<array<uint8_t, 0x100>> buf, const OutgoingBridgeMessage &orig) {
if(close) {
openHandles.erase(hnd);
ctu->deleteHandle(hnd);
writeint(0xf601);
return;
}
if(res != 0) {
writeint(res);
return;
}
hexdump(buf);
IncomingBridgeMessage msg(buf);
writeint(0);
writeint(msg.data.size()); for(auto v : msg.data) writeint(v);
writeint(msg.copiedHandles.size()); for(auto v : msg.copiedHandles) writeint(v);
writeint(msg.movedHandles.size());
for(auto v : msg.movedHandles) {
openHandles[v] = ctu->getHandle<KObject>(v);
writeint(v);
}
writedesc(orig.a);
writedesc(orig.b);
writedesc(orig.c);
writedesc(orig.x);
writeint(msg.type);
}
void IpcBridge::run() {
if(waitingForAsync)
return;
if(client == -1) {
if(!isReadable(serv))
return;
struct sockaddr_storage client_addr;
socklen_t addr_size = sizeof client_addr;
client = accept(serv, (struct sockaddr *) &client_addr, &addr_size);
LOG_INFO(IpcBridge, "IPC bridge got connection");
} else {
if(!isReadable(client))
return;
auto closed = false;
auto cmd = readint(closed);
if(closed) {
LOG_INFO(IpcBridge, "Client disconnected");
client = -1;
for(auto [hnd, _] : openHandles)
ctu->deleteHandle(hnd);
openHandles.clear();
return;
}
switch(cmd) {
case 0: { // Open service
auto name = readstring();
LOG_DEBUG(IpcBridge, "Attempting to open service %s", name.c_str());
auto sm = ctu->ipc.sm;
if(sm->ports.find(name) == sm->ports.end()) {
LOG_DEBUG(IpcBridge, "Unknown service!");
writeint(0);
return;
}
auto obj = sm->ports[name]->connectSync();
auto hnd = ctu->newHandle(obj);
openHandles[hnd] = obj;
writeint(hnd);
break;
}
case 1: { // Close handle
auto hnd = (ghandle) readint();
openHandles.erase(hnd);
ctu->deleteHandle(hnd);
break;
}
case 2: { // Message
OutgoingBridgeMessage msg;
bufferOff = 0;
msg.type = readint();
msg.data = readarray([this] { return readint(); });
msg.pid = readint();
msg.copiedHandles = readarray([this] { return (ghandle) readint(); });
msg.movedHandles = readarray([this] { return (ghandle) readint(); });
msg.a = readarray([this] { return make_tuple(readdata(), (int) readint()); });
msg.b = readarray([this] { return make_tuple(readdata(), (int) readint()); });
msg.c = readarray([this] { return readdata(); });
msg.x = readarray([this] { return make_tuple(readdata(), (int) readint()); });
auto hnd = (ghandle) readint();
auto packed = msg.pack();
if(openHandles.find(hnd) == openHandles.end()) {
writeint(0xe401); // Bad handle
break;
}
auto obj = dynamic_pointer_cast<IPipe>(openHandles[hnd]);
if(obj->isAsync()) {
waitingForAsync = true;
obj->messageAsync(packed, [=](auto res, auto close) {
sendResponse(hnd, res, close, packed, msg);
waitingForAsync = false;
});
} else {
auto close = false;
auto ret = obj->messageSync(packed, close);
sendResponse(hnd, ret, close, packed, msg);
}
break;
}
default:
close(client);
client = -1;
break;
}
}
}