This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathBusServer.cpp
398 lines (334 loc) · 9.15 KB
/
BusServer.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
This file is part of duckOS.
duckOS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
duckOS is distributed in the hope that 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 duckOS. If not, see <https://www.gnu.org/licenses/>.
Copyright (c) Byteduck 2016-2021. All rights reserved.
*/
#include "BusServer.h"
#include <unistd.h>
#include <cstring>
#include <memory>
#include <poll.h>
#include "packet.h"
#include "Endpoint.h"
#include "BusConnection.h"
#include <libduck/Log.h>
#include <sys/thread.h>
#include <fcntl.h>
using namespace River;
using Duck::Result, Duck::ResultRet, Duck::Log;
ResultRet<BusServer*> BusServer::create(const std::string& socket_name) {
int fd = open(("/sock/" + socket_name).c_str(), O_RDWR | O_CREAT | O_EXCL | O_NONBLOCK | O_CLOEXEC);
if(fd < 0) {
Log::err("[River] Failed to create socket ", socket_name, " for server: ", strerror(errno));
return Result(errno);
}
return new BusServer(fd, CUSTOM);
}
ResultRet<BusServer*> BusServer::create(BusServer::ServerType type) {
if(type == SESSION || type == SYSTEM) {
int fd = open("/sock/river", O_RDWR | O_CREAT | O_EXCL | O_NONBLOCK | O_CLOEXEC);
if (fd < 0) {
Log::err("[River] Failed to create socket for system server: ", strerror(errno));
return Result(errno);
}
return new BusServer(fd, type);
} else {
Log::err("Cannot open custom BusConnection without specifying a socket name!");
return Result(EINVAL);
}
}
void BusServer::read_and_handle_packets(bool block) {
if(block) {
struct pollfd pfd = {_fd, POLLIN, 0};
poll(&pfd, 1, -1);
}
ResultRet<RiverPacket> pkt_res(0);
while((pkt_res = receive_packet(_fd, false)).code() != NO_PACKET) {
if(pkt_res.is_error())
continue;
auto packet = pkt_res.value();
switch(packet.type) {
case SOCKETFS_CLIENT_CONNECTED:
client_connected(packet);
return;
case SOCKETFS_CLIENT_DISCONNECTED:
client_disconnected(packet);
return;
case REGISTER_ENDPOINT:
register_endpoint(packet);
return;
case GET_ENDPOINT:
get_endpoint(packet);
return;
case REGISTER_FUNCTION:
register_function(packet);
return;
case GET_FUNCTION:
get_function(packet);
return;
case FUNCTION_CALL:
call_function(packet);
return;
case FUNCTION_RETURN:
function_return(packet);
return;
case REGISTER_MESSAGE:
register_message(packet);
return;
case GET_MESSAGE:
get_message(packet);
return;
case SEND_MESSAGE:
send_message(packet);
return;
default:
packet.error = MALFORMED_DATA;
packet.data.clear();
send_packet(packet.__socketfs_from_id, packet);
return;
}
}
}
void* river_bus_server_thread(void* arg) {
auto* self = (BusServer*) arg;
while(true)
self->read_and_handle_packets(true);
}
tid_t BusServer::spawn_thread() {
if(_started)
return -1;
return thread_create(river_bus_server_thread, this);
}
void BusServer::set_allow_new_endpoints(bool allow) {
_allow_new_endpoints = allow;
}
Duck::Result BusServer::send_packet(int pid, const RiverPacket& packet) {
return River::send_packet(_fd, pid, packet);
}
#define VERIFY_ENDPOINT \
if(!_endpoints[packet.endpoint]) { \
send_packet(packet.__socketfs_from_id, { \
packet.type, \
packet.endpoint, \
packet.path, \
ENDPOINT_DOES_NOT_EXIST \
}); \
return; \
} \
auto& endpoint = _endpoints[packet.endpoint];
#define VERIFY_FUNCTION \
if(!endpoint->functions[packet.path]) { \
send_packet(packet.__socketfs_from_id, { \
packet.type, \
packet.endpoint, \
packet.path, \
FUNCTION_DOES_NOT_EXIST \
}); \
return; \
} \
#define VERIFY_MESSAGE \
if(!endpoint->messages[packet.path]) { \
send_packet(packet.__socketfs_from_id, { \
packet.type, \
packet.endpoint, \
packet.path, \
MESSAGE_DOES_NOT_EXIST \
}); \
return; \
} \
void BusServer::client_connected(const RiverPacket& packet) {
_clients[packet.__socketfs_from_id] = std::make_unique<ServerClient>(ServerClient {packet.__socketfs_from_id});
}
void BusServer::client_disconnected(const RiverPacket& packet) {
auto client_it = _clients.find(packet.__socketfs_from_id);
if(client_it == _clients.end()) {
Log::warnf("[River] Unknown client {x} disconnected!",packet.__socketfs_from_id);
return;
}
//Erase the client's registered endpoints
auto& client = client_it->second;
for(auto& endpoint : client->registered_endpoints)
_endpoints.erase(endpoint);
//Send the disconnect message to all of the client's connected endpoints
for(auto& endpoint_name : client->connected_endpoints) {
auto& endpoint = _endpoints[endpoint_name];
if(!endpoint)
continue;
send_packet(endpoint->id, {
.type = CLIENT_DISCONNECTED,
.endpoint = endpoint_name,
.path = "",
.disconnected_pid = packet.__socketfs_from_pid,
.disconnected_id = client->id
});
}
_clients.erase(client_it);
}
void BusServer::register_endpoint(const RiverPacket& packet) {
if(!_allow_new_endpoints) {
send_packet(packet.__socketfs_from_id, {
packet.type,
packet.endpoint,
packet.path,
ILLEGAL_REQUEST
});
return;
}
if(_endpoints[packet.endpoint]) {
send_packet(packet.__socketfs_from_id, {
packet.type,
packet.endpoint,
packet.path,
ENDPOINT_ALREADY_REGISTERED
});
return;
}
_endpoints[packet.endpoint] = std::make_unique<ServerEndpoint>(ServerEndpoint{packet.endpoint, packet.__socketfs_from_id});
Log::dbg("[River] Registering endpoint ", packet.endpoint);
auto& client = _clients[packet.__socketfs_from_id];
if(client) {
client->registered_endpoints.push_back(packet.endpoint);
}
send_packet(packet.__socketfs_from_id, {
packet.type,
packet.endpoint,
packet.path,
SUCCESS
});
}
void BusServer::get_endpoint(const RiverPacket& packet) {
VERIFY_ENDPOINT
//Send client connected message to applicable endpoint
auto& client = _clients[packet.__socketfs_from_id];
if(client) {
client->connected_endpoints.push_back(packet.endpoint);
send_packet(endpoint->id, {
.type = CLIENT_CONNECTED,
.endpoint = packet.endpoint,
.path = "",
.connected_pid = packet.__socketfs_from_pid,
.connected_id = client->id
});
}
send_packet(packet.__socketfs_from_id, {
packet.type,
packet.endpoint,
packet.path,
SUCCESS
});
}
void BusServer::register_function(const RiverPacket& packet) {
VERIFY_ENDPOINT
if(endpoint->id != packet.__socketfs_from_id) {
send_packet(packet.__socketfs_from_id, {
packet.type,
packet.endpoint,
packet.path,
ILLEGAL_REQUEST
});
return;
}
if(endpoint->functions[packet.path]) {
send_packet(packet.__socketfs_from_id, {
packet.type,
packet.endpoint,
packet.path,
FUNCTION_ALREADY_REGISTERED
});
return;
}
endpoint->functions[packet.path] = std::make_unique<ServerFunction>(ServerFunction {packet.path});
send_packet(packet.__socketfs_from_id, {
packet.type,
packet.endpoint,
packet.path,
SUCCESS
});
}
void BusServer::get_function(const RiverPacket& packet) {
VERIFY_ENDPOINT
VERIFY_FUNCTION
send_packet(packet.__socketfs_from_id, {
packet.type,
packet.endpoint,
packet.path,
SUCCESS
});
}
void BusServer::call_function(const RiverPacket& packet) {
VERIFY_ENDPOINT
VERIFY_FUNCTION
RiverPacket func_packet = packet;
func_packet.sender = packet.__socketfs_from_id;
func_packet.__socketfs_from_id = 0;
send_packet(endpoint->id, func_packet);
}
void BusServer::function_return(const RiverPacket& packet) {
VERIFY_ENDPOINT
VERIFY_FUNCTION
if(endpoint->id != packet.__socketfs_from_id || packet.recipient == SOCKETFS_RECIPIENT_HOST || packet.recipient == _self_pid) {
send_packet(packet.__socketfs_from_id, {
packet.type,
packet.endpoint,
packet.path,
ILLEGAL_REQUEST
});
return;
}
send_packet(packet.recipient, packet);
}
void BusServer::register_message(const RiverPacket& packet) {
VERIFY_ENDPOINT
if(endpoint->id != packet.__socketfs_from_id) {
send_packet(packet.__socketfs_from_id, {
packet.type,
packet.endpoint,
packet.path,
ILLEGAL_REQUEST
});
return;
}
if(endpoint->messages[packet.path]) {
send_packet(packet.__socketfs_from_id, {
packet.type,
packet.endpoint,
packet.path,
MESSAGE_ALREADY_REGISTERED
});
return;
}
endpoint->messages[packet.path] = std::make_unique<ServerMessage>(ServerMessage {packet.path});
send_packet(packet.__socketfs_from_id, {
packet.type,
packet.endpoint,
packet.path,
SUCCESS
});
}
void BusServer::get_message(const RiverPacket& packet) {
VERIFY_ENDPOINT
VERIFY_MESSAGE
send_packet(packet.__socketfs_from_id, {
packet.type,
packet.endpoint,
packet.path,
SUCCESS
});
}
void BusServer::send_message(const RiverPacket& packet) {
VERIFY_ENDPOINT
VERIFY_MESSAGE
RiverPacket message_packet = packet;
message_packet.sender = packet.__socketfs_from_id;
message_packet.__socketfs_from_id = 0;
send_packet(packet.recipient, message_packet);
}