BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file sys_request_server.c |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | * |
||
29 | * @section DESCRIPTION |
||
30 | * |
||
31 | * A simple a IPC interface for NCD to talk to other processes over a Unix socket. |
||
32 | * |
||
33 | * Synopsis: |
||
34 | * sys.request_server(listen_address, string request_handler_template, list args) |
||
35 | * |
||
36 | * Description: |
||
37 | * Initializes a request server on the given socket path. Requests are served by |
||
38 | * starting a template process for every request. Multiple such processes may |
||
39 | * exist simultaneously. Termination of these processess may be initiated at |
||
40 | * any time if the request server no longer needs the request in question served. |
||
41 | * The payload of a request is a value, and can be accessed as _request.data |
||
42 | * from within the handler process. Replies to the request can be sent using |
||
43 | * _request->reply(data); replies are values too. Finally, _request->finish() |
||
44 | * should be called to indicate that no further replies will be sent. Calling |
||
45 | * finish() will immediately initiate termination of the handler process. |
||
46 | * Requests can be sent to NCD using the badvpn-ncd-request program. |
||
47 | * |
||
48 | * The listen address should be in the same format as for the socket module. |
||
49 | * In particular, it must be in one of the following forms: |
||
50 | * - {"tcp", {"ipv4", ipv4_address, port_number}}, |
||
51 | * - {"tcp", {"ipv6", ipv6_address, port_number}}, |
||
52 | * - {"unix", socket_path}. |
||
53 | * |
||
54 | * Predefined objects and variables in request_handler_template: |
||
55 | * _caller - provides access to objects as seen from the sys.request_server() |
||
56 | * command |
||
57 | * _request.data - the request payload as sent by the client |
||
58 | * string _request.client_addr - the address of the client. The form is |
||
59 | * like the second part of the sys.request_server() address format, e.g. |
||
60 | * {"ipv4", "1.2.3.4", "4000"}. |
||
61 | * |
||
62 | * Synopsis: |
||
63 | * sys.request_server.request::reply(reply_data) |
||
64 | * |
||
65 | * Synopsis: |
||
66 | * sys.request_server.request::finish() |
||
67 | */ |
||
68 | |||
69 | #include <stdlib.h> |
||
70 | #include <string.h> |
||
71 | #include <limits.h> |
||
72 | #include <inttypes.h> |
||
73 | #include <errno.h> |
||
74 | |||
75 | #include <misc/offset.h> |
||
76 | #include <misc/debug.h> |
||
77 | #include <misc/byteorder.h> |
||
78 | #include <protocol/packetproto.h> |
||
79 | #include <protocol/requestproto.h> |
||
80 | #include <structure/LinkedList0.h> |
||
81 | #include <system/BConnection.h> |
||
82 | #include <system/BConnectionGeneric.h> |
||
83 | #include <system/BAddr.h> |
||
84 | #include <flow/PacketProtoDecoder.h> |
||
85 | #include <flow/PacketStreamSender.h> |
||
86 | #include <flow/PacketPassFifoQueue.h> |
||
87 | #include <ncd/NCDValParser.h> |
||
88 | #include <ncd/NCDValGenerator.h> |
||
89 | #include <ncd/extra/address_utils.h> |
||
90 | |||
91 | #include <ncd/module_common.h> |
||
92 | |||
93 | #include <generated/blog_channel_ncd_sys_request_server.h> |
||
94 | |||
95 | #define SEND_PAYLOAD_MTU 32768 |
||
96 | #define RECV_PAYLOAD_MTU 32768 |
||
97 | |||
98 | #define SEND_MTU (SEND_PAYLOAD_MTU + sizeof(struct requestproto_header)) |
||
99 | #define RECV_MTU (RECV_PAYLOAD_MTU + sizeof(struct requestproto_header)) |
||
100 | |||
101 | struct instance { |
||
102 | NCDModuleInst *i; |
||
103 | NCDValRef request_handler_template; |
||
104 | NCDValRef args; |
||
105 | BListener listener; |
||
106 | LinkedList0 connections_list; |
||
107 | int dying; |
||
108 | }; |
||
109 | |||
110 | #define CONNECTION_STATE_RUNNING 1 |
||
111 | #define CONNECTION_STATE_TERMINATING 2 |
||
112 | |||
113 | struct reply; |
||
114 | |||
115 | struct connection { |
||
116 | struct instance *inst; |
||
117 | LinkedList0Node connections_list_node; |
||
118 | BConnection con; |
||
119 | BAddr addr; |
||
120 | PacketProtoDecoder recv_decoder; |
||
121 | PacketPassInterface recv_if; |
||
122 | PacketPassFifoQueue send_queue; |
||
123 | PacketStreamSender send_pss; |
||
124 | LinkedList0 requests_list; |
||
125 | LinkedList0 replies_list; |
||
126 | int state; |
||
127 | }; |
||
128 | |||
129 | struct request { |
||
130 | struct connection *con; |
||
131 | uint32_t request_id; |
||
132 | LinkedList0Node requests_list_node; |
||
133 | NCDValMem request_data_mem; |
||
134 | NCDValRef request_data; |
||
135 | struct reply *end_reply; |
||
136 | NCDModuleProcess process; |
||
137 | int terminating; |
||
138 | int got_finished; |
||
139 | }; |
||
140 | |||
141 | struct reply { |
||
142 | struct connection *con; |
||
143 | LinkedList0Node replies_list_node; |
||
144 | PacketPassFifoQueueFlow send_qflow; |
||
145 | PacketPassInterface *send_qflow_if; |
||
146 | uint8_t *send_buf; |
||
147 | }; |
||
148 | |||
149 | static void listener_handler (struct instance *o); |
||
150 | static void connection_free (struct connection *c); |
||
151 | static void connection_free_link (struct connection *c); |
||
152 | static void connection_terminate (struct connection *c); |
||
153 | static void connection_con_handler (struct connection *c, int event); |
||
154 | static void connection_recv_decoder_handler_error (struct connection *c); |
||
155 | static void connection_recv_if_handler_send (struct connection *c, uint8_t *data, int data_len); |
||
156 | static int request_init (struct connection *c, uint32_t request_id, const uint8_t *data, int data_len); |
||
157 | static void request_free (struct request *r); |
||
158 | static struct request * find_request (struct connection *c, uint32_t request_id); |
||
159 | static void request_process_handler_event (NCDModuleProcess *process, int event); |
||
160 | static int request_process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object); |
||
161 | static int request_process_caller_obj_func_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object); |
||
162 | static int request_process_request_obj_func_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out_value); |
||
163 | static void request_terminate (struct request *r); |
||
164 | static struct reply * reply_init (struct connection *c, uint32_t request_id, NCDValRef reply_data); |
||
165 | static void reply_start (struct reply *r, uint32_t type); |
||
166 | static void reply_free (struct reply *r); |
||
167 | static void reply_send_qflow_if_handler_done (struct reply *r); |
||
168 | static void instance_free (struct instance *o); |
||
169 | |||
170 | enum {STRING_REQUEST, STRING_DATA, STRING_CLIENT_ADDR, |
||
171 | STRING_SYS_REQUEST_SERVER_REQUEST}; |
||
172 | |||
173 | static const char *strings[] = { |
||
174 | "_request", "data", "client_addr", |
||
175 | "sys.request_server.request", NULL |
||
176 | }; |
||
177 | |||
178 | static void listener_handler (struct instance *o) |
||
179 | { |
||
180 | ASSERT(!o->dying) |
||
181 | |||
182 | BReactor *reactor = o->i->params->iparams->reactor; |
||
183 | BPendingGroup *pg = BReactor_PendingGroup(reactor); |
||
184 | |||
185 | struct connection *c = malloc(sizeof(*c)); |
||
186 | if (!c) { |
||
187 | ModuleLog(o->i, BLOG_ERROR, "malloc failed"); |
||
188 | goto fail0; |
||
189 | } |
||
190 | |||
191 | c->inst = o; |
||
192 | |||
193 | LinkedList0_Prepend(&o->connections_list, &c->connections_list_node); |
||
194 | |||
195 | if (!BConnection_Init(&c->con, BConnection_source_listener(&o->listener, &c->addr), reactor, c, (BConnection_handler)connection_con_handler)) { |
||
196 | ModuleLog(o->i, BLOG_ERROR, "BConnection_Init failed"); |
||
197 | goto fail1; |
||
198 | } |
||
199 | |||
200 | BConnection_SendAsync_Init(&c->con); |
||
201 | BConnection_RecvAsync_Init(&c->con); |
||
202 | StreamPassInterface *con_send_if = BConnection_SendAsync_GetIf(&c->con); |
||
203 | StreamRecvInterface *con_recv_if = BConnection_RecvAsync_GetIf(&c->con); |
||
204 | |||
205 | PacketPassInterface_Init(&c->recv_if, RECV_MTU, (PacketPassInterface_handler_send)connection_recv_if_handler_send, c, pg); |
||
206 | |||
207 | if (!PacketProtoDecoder_Init(&c->recv_decoder, con_recv_if, &c->recv_if, pg, c, (PacketProtoDecoder_handler_error)connection_recv_decoder_handler_error)) { |
||
208 | ModuleLog(o->i, BLOG_ERROR, "PacketProtoDecoder_Init failed"); |
||
209 | goto fail2; |
||
210 | } |
||
211 | |||
212 | PacketStreamSender_Init(&c->send_pss, con_send_if, PACKETPROTO_ENCLEN(SEND_MTU), pg); |
||
213 | |||
214 | PacketPassFifoQueue_Init(&c->send_queue, PacketStreamSender_GetInput(&c->send_pss), pg); |
||
215 | |||
216 | LinkedList0_Init(&c->requests_list); |
||
217 | |||
218 | LinkedList0_Init(&c->replies_list); |
||
219 | |||
220 | c->state = CONNECTION_STATE_RUNNING; |
||
221 | |||
222 | ModuleLog(o->i, BLOG_INFO, "connection initialized"); |
||
223 | return; |
||
224 | |||
225 | fail2: |
||
226 | PacketPassInterface_Free(&c->recv_if); |
||
227 | BConnection_RecvAsync_Free(&c->con); |
||
228 | BConnection_SendAsync_Free(&c->con); |
||
229 | BConnection_Free(&c->con); |
||
230 | fail1: |
||
231 | LinkedList0_Remove(&o->connections_list, &c->connections_list_node); |
||
232 | free(c); |
||
233 | fail0: |
||
234 | return; |
||
235 | } |
||
236 | |||
237 | static void connection_free (struct connection *c) |
||
238 | { |
||
239 | struct instance *o = c->inst; |
||
240 | ASSERT(c->state == CONNECTION_STATE_TERMINATING) |
||
241 | ASSERT(LinkedList0_IsEmpty(&c->requests_list)) |
||
242 | ASSERT(LinkedList0_IsEmpty(&c->replies_list)) |
||
243 | |||
244 | LinkedList0_Remove(&o->connections_list, &c->connections_list_node); |
||
245 | free(c); |
||
246 | } |
||
247 | |||
248 | static void connection_free_link (struct connection *c) |
||
249 | { |
||
250 | PacketPassFifoQueue_PrepareFree(&c->send_queue); |
||
251 | |||
252 | LinkedList0Node *ln; |
||
253 | while (ln = LinkedList0_GetFirst(&c->replies_list)) { |
||
254 | struct reply *r = UPPER_OBJECT(ln, struct reply, replies_list_node); |
||
255 | ASSERT(r->con == c) |
||
256 | reply_free(r); |
||
257 | } |
||
258 | |||
259 | PacketPassFifoQueue_Free(&c->send_queue); |
||
260 | PacketStreamSender_Free(&c->send_pss); |
||
261 | PacketProtoDecoder_Free(&c->recv_decoder); |
||
262 | PacketPassInterface_Free(&c->recv_if); |
||
263 | BConnection_RecvAsync_Free(&c->con); |
||
264 | BConnection_SendAsync_Free(&c->con); |
||
265 | BConnection_Free(&c->con); |
||
266 | } |
||
267 | |||
268 | static void connection_terminate (struct connection *c) |
||
269 | { |
||
270 | ASSERT(c->state == CONNECTION_STATE_RUNNING) |
||
271 | |||
272 | for (LinkedList0Node *ln = LinkedList0_GetFirst(&c->requests_list); ln; ln = LinkedList0Node_Next(ln)) { |
||
273 | struct request *r = UPPER_OBJECT(ln, struct request, requests_list_node); |
||
274 | |||
275 | if (!r->terminating) { |
||
276 | request_terminate(r); |
||
277 | } |
||
278 | } |
||
279 | |||
280 | connection_free_link(c); |
||
281 | |||
282 | c->state = CONNECTION_STATE_TERMINATING; |
||
283 | |||
284 | if (LinkedList0_IsEmpty(&c->requests_list)) { |
||
285 | connection_free(c); |
||
286 | return; |
||
287 | } |
||
288 | } |
||
289 | |||
290 | static void connection_con_handler (struct connection *c, int event) |
||
291 | { |
||
292 | struct instance *o = c->inst; |
||
293 | ASSERT(c->state == CONNECTION_STATE_RUNNING) |
||
294 | |||
295 | ModuleLog(o->i, BLOG_INFO, "connection closed"); |
||
296 | |||
297 | connection_terminate(c); |
||
298 | } |
||
299 | |||
300 | static void connection_recv_decoder_handler_error (struct connection *c) |
||
301 | { |
||
302 | struct instance *o = c->inst; |
||
303 | ASSERT(c->state == CONNECTION_STATE_RUNNING) |
||
304 | |||
305 | ModuleLog(o->i, BLOG_ERROR, "decoder error"); |
||
306 | |||
307 | connection_terminate(c); |
||
308 | } |
||
309 | |||
310 | static void connection_recv_if_handler_send (struct connection *c, uint8_t *data, int data_len) |
||
311 | { |
||
312 | struct instance *o = c->inst; |
||
313 | ASSERT(c->state == CONNECTION_STATE_RUNNING) |
||
314 | ASSERT(data_len >= 0) |
||
315 | ASSERT(data_len <= RECV_MTU) |
||
316 | |||
317 | PacketPassInterface_Done(&c->recv_if); |
||
318 | |||
319 | if (data_len < sizeof(struct requestproto_header)) { |
||
320 | ModuleLog(o->i, BLOG_ERROR, "missing requestproto header"); |
||
321 | goto fail; |
||
322 | } |
||
323 | |||
324 | struct requestproto_header header; |
||
325 | memcpy(&header, data, sizeof(header)); |
||
326 | uint32_t request_id = ltoh32(header.request_id); |
||
327 | uint32_t type = ltoh32(header.type); |
||
328 | |||
329 | switch (type) { |
||
330 | case REQUESTPROTO_TYPE_CLIENT_REQUEST: { |
||
331 | if (find_request(c, request_id)) { |
||
332 | ModuleLog(o->i, BLOG_ERROR, "request with the same ID already exists"); |
||
333 | goto fail; |
||
334 | } |
||
335 | |||
336 | if (!request_init(c, request_id, data + sizeof(header), data_len - sizeof(header))) { |
||
337 | goto fail; |
||
338 | } |
||
339 | } break; |
||
340 | |||
341 | case REQUESTPROTO_TYPE_CLIENT_ABORT: { |
||
342 | struct request *r = find_request(c, request_id); |
||
343 | if (!r) { |
||
344 | // this is expected if we finish before we get the abort |
||
345 | return; |
||
346 | } |
||
347 | |||
348 | if (!r->terminating) { |
||
349 | request_terminate(r); |
||
350 | } |
||
351 | } break; |
||
352 | |||
353 | default: |
||
354 | ModuleLog(o->i, BLOG_ERROR, "invalid requestproto type"); |
||
355 | goto fail; |
||
356 | } |
||
357 | |||
358 | return; |
||
359 | |||
360 | fail: |
||
361 | connection_terminate(c); |
||
362 | } |
||
363 | |||
364 | static int request_init (struct connection *c, uint32_t request_id, const uint8_t *data, int data_len) |
||
365 | { |
||
366 | struct instance *o = c->inst; |
||
367 | ASSERT(c->state == CONNECTION_STATE_RUNNING) |
||
368 | ASSERT(!find_request(c, request_id)) |
||
369 | ASSERT(data_len >= 0) |
||
370 | ASSERT(data_len <= RECV_PAYLOAD_MTU) |
||
371 | |||
372 | struct request *r = malloc(sizeof(*r)); |
||
373 | if (!r) { |
||
374 | ModuleLog(o->i, BLOG_ERROR, "malloc failed"); |
||
375 | goto fail0; |
||
376 | } |
||
377 | |||
378 | r->con = c; |
||
379 | r->request_id = request_id; |
||
380 | |||
381 | LinkedList0_Prepend(&c->requests_list, &r->requests_list_node); |
||
382 | |||
383 | NCDValMem_Init(&r->request_data_mem, o->i->params->iparams->string_index); |
||
384 | |||
385 | if (!NCDValParser_Parse(MemRef_Make((const char *)data, data_len), &r->request_data_mem, &r->request_data)) { |
||
386 | ModuleLog(o->i, BLOG_ERROR, "NCDValParser_Parse failed"); |
||
387 | goto fail1; |
||
388 | } |
||
389 | |||
390 | if (!(r->end_reply = reply_init(c, request_id, NCDVal_NewInvalid()))) { |
||
391 | goto fail1; |
||
392 | } |
||
393 | |||
394 | if (!NCDModuleProcess_InitValue(&r->process, o->i, o->request_handler_template, o->args, request_process_handler_event)) { |
||
395 | ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed"); |
||
396 | goto fail3; |
||
397 | } |
||
398 | |||
399 | NCDModuleProcess_SetSpecialFuncs(&r->process, request_process_func_getspecialobj); |
||
400 | |||
401 | r->terminating = 0; |
||
402 | r->got_finished = 0; |
||
403 | |||
404 | ModuleLog(o->i, BLOG_INFO, "request initialized"); |
||
405 | return 1; |
||
406 | |||
407 | fail3: |
||
408 | reply_free(r->end_reply); |
||
409 | fail1: |
||
410 | NCDValMem_Free(&r->request_data_mem); |
||
411 | LinkedList0_Remove(&c->requests_list, &r->requests_list_node); |
||
412 | free(r); |
||
413 | fail0: |
||
414 | return 0; |
||
415 | } |
||
416 | |||
417 | static void request_free (struct request *r) |
||
418 | { |
||
419 | struct connection *c = r->con; |
||
420 | NCDModuleProcess_AssertFree(&r->process); |
||
421 | |||
422 | if (c->state != CONNECTION_STATE_TERMINATING) { |
||
423 | uint32_t type = r->got_finished ? REQUESTPROTO_TYPE_SERVER_FINISHED : REQUESTPROTO_TYPE_SERVER_ERROR; |
||
424 | reply_start(r->end_reply, type); |
||
425 | } |
||
426 | |||
427 | NCDModuleProcess_Free(&r->process); |
||
428 | NCDValMem_Free(&r->request_data_mem); |
||
429 | LinkedList0_Remove(&c->requests_list, &r->requests_list_node); |
||
430 | free(r); |
||
431 | } |
||
432 | |||
433 | static struct request * find_request (struct connection *c, uint32_t request_id) |
||
434 | { |
||
435 | for (LinkedList0Node *ln = LinkedList0_GetFirst(&c->requests_list); ln; ln = LinkedList0Node_Next(ln)) { |
||
436 | struct request *r = UPPER_OBJECT(ln, struct request, requests_list_node); |
||
437 | if (!r->terminating && r->request_id == request_id) { |
||
438 | return r; |
||
439 | } |
||
440 | } |
||
441 | |||
442 | return NULL; |
||
443 | } |
||
444 | |||
445 | static void request_process_handler_event (NCDModuleProcess *process, int event) |
||
446 | { |
||
447 | struct request *r = UPPER_OBJECT(process, struct request, process); |
||
448 | struct connection *c = r->con; |
||
449 | struct instance *o = c->inst; |
||
450 | |||
451 | switch (event) { |
||
452 | case NCDMODULEPROCESS_EVENT_UP: { |
||
453 | ASSERT(!r->terminating) |
||
454 | } break; |
||
455 | |||
456 | case NCDMODULEPROCESS_EVENT_DOWN: { |
||
457 | ASSERT(!r->terminating) |
||
458 | |||
459 | NCDModuleProcess_Continue(&r->process); |
||
460 | } break; |
||
461 | |||
462 | case NCDMODULEPROCESS_EVENT_TERMINATED: { |
||
463 | ASSERT(r->terminating) |
||
464 | |||
465 | request_free(r); |
||
466 | |||
467 | if (c->state == CONNECTION_STATE_TERMINATING && LinkedList0_IsEmpty(&c->requests_list)) { |
||
468 | connection_free(c); |
||
469 | |||
470 | if (o->dying && LinkedList0_IsEmpty(&o->connections_list)) { |
||
471 | instance_free(o); |
||
472 | return; |
||
473 | } |
||
474 | } |
||
475 | } break; |
||
476 | |||
477 | default: ASSERT(0); |
||
478 | } |
||
479 | } |
||
480 | |||
481 | static int request_process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object) |
||
482 | { |
||
483 | struct request *r = UPPER_OBJECT(process, struct request, process); |
||
484 | |||
485 | if (name == NCD_STRING_CALLER) { |
||
486 | *out_object = NCDObject_Build(-1, r, NCDObject_no_getvar, request_process_caller_obj_func_getobj); |
||
487 | return 1; |
||
488 | } |
||
489 | |||
490 | if (name == ModuleString(r->con->inst->i, STRING_REQUEST)) { |
||
491 | *out_object = NCDObject_Build(ModuleString(r->con->inst->i, STRING_SYS_REQUEST_SERVER_REQUEST), r, request_process_request_obj_func_getvar, NCDObject_no_getobj); |
||
492 | return 1; |
||
493 | } |
||
494 | |||
495 | return 0; |
||
496 | } |
||
497 | |||
498 | static int request_process_caller_obj_func_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object) |
||
499 | { |
||
500 | struct request *r = NCDObject_DataPtr(obj); |
||
501 | |||
502 | return NCDModuleInst_Backend_GetObj(r->con->inst->i, name, out_object); |
||
503 | } |
||
504 | |||
505 | static int request_process_request_obj_func_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out) |
||
506 | { |
||
507 | struct request *r = NCDObject_DataPtr(obj); |
||
508 | |||
509 | if (name == ModuleString(r->con->inst->i, STRING_DATA)) { |
||
510 | *out = NCDVal_NewCopy(mem, r->request_data); |
||
511 | return 1; |
||
512 | } |
||
513 | |||
514 | if (name == ModuleString(r->con->inst->i, STRING_CLIENT_ADDR)) { |
||
515 | *out = ncd_make_baddr(r->con->addr, mem); |
||
516 | return 1; |
||
517 | } |
||
518 | |||
519 | return 0; |
||
520 | } |
||
521 | |||
522 | static void request_terminate (struct request *r) |
||
523 | { |
||
524 | ASSERT(!r->terminating) |
||
525 | |||
526 | NCDModuleProcess_Terminate(&r->process); |
||
527 | |||
528 | r->terminating = 1; |
||
529 | } |
||
530 | |||
531 | static struct reply * reply_init (struct connection *c, uint32_t request_id, NCDValRef reply_data) |
||
532 | { |
||
533 | struct instance *o = c->inst; |
||
534 | ASSERT(c->state == CONNECTION_STATE_RUNNING) |
||
535 | NCDVal_Assert(reply_data); |
||
536 | |||
537 | struct reply *r = malloc(sizeof(*r)); |
||
538 | if (!r) { |
||
539 | ModuleLog(o->i, BLOG_ERROR, "malloc failed"); |
||
540 | goto fail0; |
||
541 | } |
||
542 | |||
543 | r->con = c; |
||
544 | |||
545 | LinkedList0_Prepend(&c->replies_list, &r->replies_list_node); |
||
546 | |||
547 | PacketPassFifoQueueFlow_Init(&r->send_qflow, &c->send_queue); |
||
548 | |||
549 | r->send_qflow_if = PacketPassFifoQueueFlow_GetInput(&r->send_qflow); |
||
550 | PacketPassInterface_Sender_Init(r->send_qflow_if, (PacketPassInterface_handler_done)reply_send_qflow_if_handler_done, r); |
||
551 | |||
552 | ExpString str; |
||
553 | if (!ExpString_Init(&str)) { |
||
554 | ModuleLog(o->i, BLOG_ERROR, "ExpString_Init failed"); |
||
555 | goto fail1; |
||
556 | } |
||
557 | |||
558 | if (!ExpString_AppendZeros(&str, sizeof(struct packetproto_header) + sizeof(struct requestproto_header))) { |
||
559 | ModuleLog(o->i, BLOG_ERROR, "ExpString_AppendZeros failed"); |
||
560 | goto fail2; |
||
561 | } |
||
562 | |||
563 | if (!NCDVal_IsInvalid(reply_data) && !NCDValGenerator_AppendGenerate(reply_data, &str)) { |
||
564 | ModuleLog(o->i, BLOG_ERROR, "NCDValGenerator_AppendGenerate failed"); |
||
565 | goto fail2; |
||
566 | } |
||
567 | |||
568 | size_t len = ExpString_Length(&str); |
||
569 | if (len > INT_MAX || len > PACKETPROTO_ENCLEN(SEND_MTU) || len - sizeof(struct packetproto_header) > UINT16_MAX) { |
||
570 | ModuleLog(o->i, BLOG_ERROR, "reply is too long"); |
||
571 | goto fail2; |
||
572 | } |
||
573 | |||
574 | r->send_buf = (uint8_t *)ExpString_Get(&str); |
||
575 | |||
576 | struct packetproto_header pp; |
||
577 | pp.len = htol16(len - sizeof(pp)); |
||
578 | |||
579 | struct requestproto_header rp; |
||
580 | rp.request_id = htol32(request_id); |
||
581 | |||
582 | memcpy(r->send_buf, &pp, sizeof(pp)); |
||
583 | memcpy(r->send_buf + sizeof(pp), &rp, sizeof(rp)); |
||
584 | |||
585 | return r; |
||
586 | |||
587 | fail2: |
||
588 | ExpString_Free(&str); |
||
589 | fail1: |
||
590 | PacketPassFifoQueueFlow_Free(&r->send_qflow); |
||
591 | LinkedList0_Remove(&c->replies_list, &r->replies_list_node); |
||
592 | free(r); |
||
593 | fail0: |
||
594 | return NULL; |
||
595 | } |
||
596 | |||
597 | static void reply_start (struct reply *r, uint32_t type) |
||
598 | { |
||
599 | struct requestproto_header rp; |
||
600 | memcpy(&rp, r->send_buf + sizeof(struct packetproto_header), sizeof(rp)); |
||
601 | rp.type = htol32(type); |
||
602 | memcpy(r->send_buf + sizeof(struct packetproto_header), &rp, sizeof(rp)); |
||
603 | |||
604 | struct packetproto_header pp; |
||
605 | memcpy(&pp, r->send_buf, sizeof(pp)); |
||
606 | |||
607 | int len = ltoh16(pp.len) + sizeof(struct packetproto_header); |
||
608 | |||
609 | PacketPassInterface_Sender_Send(r->send_qflow_if, r->send_buf, len); |
||
610 | } |
||
611 | |||
612 | static void reply_free (struct reply *r) |
||
613 | { |
||
614 | struct connection *c = r->con; |
||
615 | PacketPassFifoQueueFlow_AssertFree(&r->send_qflow); |
||
616 | |||
617 | free(r->send_buf); |
||
618 | PacketPassFifoQueueFlow_Free(&r->send_qflow); |
||
619 | LinkedList0_Remove(&c->replies_list, &r->replies_list_node); |
||
620 | free(r); |
||
621 | } |
||
622 | |||
623 | static void reply_send_qflow_if_handler_done (struct reply *r) |
||
624 | { |
||
625 | reply_free(r); |
||
626 | } |
||
627 | |||
628 | static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
629 | { |
||
630 | struct instance *o = vo; |
||
631 | o->i = i; |
||
632 | |||
633 | // check arguments |
||
634 | NCDValRef listen_addr_arg; |
||
635 | NCDValRef request_handler_template_arg; |
||
636 | NCDValRef args_arg; |
||
637 | if (!NCDVal_ListRead(params->args, 3, &listen_addr_arg, &request_handler_template_arg, &args_arg)) { |
||
638 | ModuleLog(o->i, BLOG_ERROR, "wrong arity"); |
||
639 | goto fail0; |
||
640 | } |
||
641 | if (!NCDVal_IsString(request_handler_template_arg) || !NCDVal_IsList(args_arg)) { |
||
642 | ModuleLog(o->i, BLOG_ERROR, "wrong type"); |
||
643 | goto fail0; |
||
644 | } |
||
645 | o->request_handler_template = request_handler_template_arg; |
||
646 | o->args = args_arg; |
||
647 | |||
648 | // read listen address |
||
649 | struct BConnection_addr addr; |
||
650 | if (!ncd_read_bconnection_addr(listen_addr_arg, &addr)) { |
||
651 | ModuleLog(o->i, BLOG_ERROR, "wrong listen address"); |
||
652 | goto fail0; |
||
653 | } |
||
654 | |||
655 | // init listener |
||
656 | if (!BListener_InitGeneric(&o->listener, addr, i->params->iparams->reactor, o, (BListener_handler)listener_handler)) { |
||
657 | ModuleLog(o->i, BLOG_ERROR, "BListener_InitGeneric failed"); |
||
658 | goto fail0; |
||
659 | } |
||
660 | |||
661 | // init connections list |
||
662 | LinkedList0_Init(&o->connections_list); |
||
663 | |||
664 | // set not dying |
||
665 | o->dying = 0; |
||
666 | |||
667 | // signal up |
||
668 | NCDModuleInst_Backend_Up(i); |
||
669 | return; |
||
670 | |||
671 | fail0: |
||
672 | NCDModuleInst_Backend_DeadError(i); |
||
673 | } |
||
674 | |||
675 | static void instance_free (struct instance *o) |
||
676 | { |
||
677 | ASSERT(o->dying) |
||
678 | ASSERT(LinkedList0_IsEmpty(&o->connections_list)) |
||
679 | |||
680 | NCDModuleInst_Backend_Dead(o->i); |
||
681 | } |
||
682 | |||
683 | static void func_die (void *vo) |
||
684 | { |
||
685 | struct instance *o = vo; |
||
686 | ASSERT(!o->dying) |
||
687 | |||
688 | // free listener |
||
689 | BListener_Free(&o->listener); |
||
690 | |||
691 | // terminate connections |
||
692 | LinkedList0Node *next_ln; |
||
693 | for (LinkedList0Node *ln = LinkedList0_GetFirst(&o->connections_list); ln && (next_ln = LinkedList0Node_Next(ln)), ln; ln = next_ln) { |
||
694 | struct connection *c = UPPER_OBJECT(ln, struct connection, connections_list_node); |
||
695 | ASSERT(c->inst == o) |
||
696 | |||
697 | if (c->state != CONNECTION_STATE_TERMINATING) { |
||
698 | connection_terminate(c); |
||
699 | } |
||
700 | } |
||
701 | |||
702 | // set dying |
||
703 | o->dying = 1; |
||
704 | |||
705 | // if no connections, die right away |
||
706 | if (LinkedList0_IsEmpty(&o->connections_list)) { |
||
707 | instance_free(o); |
||
708 | return; |
||
709 | } |
||
710 | } |
||
711 | |||
712 | static void reply_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
713 | { |
||
714 | NCDValRef reply_data; |
||
715 | if (!NCDVal_ListRead(params->args, 1, &reply_data)) { |
||
716 | ModuleLog(i, BLOG_ERROR, "wrong arity"); |
||
717 | goto fail; |
||
718 | } |
||
719 | |||
720 | NCDModuleInst_Backend_Up(i); |
||
721 | |||
722 | struct request *r = params->method_user; |
||
723 | struct connection *c = r->con; |
||
724 | |||
725 | if (r->terminating) { |
||
726 | ModuleLog(i, BLOG_ERROR, "request is dying, cannot submit reply"); |
||
727 | goto fail; |
||
728 | } |
||
729 | |||
730 | struct reply *rpl = reply_init(c, r->request_id, reply_data); |
||
731 | if (!rpl) { |
||
732 | ModuleLog(i, BLOG_ERROR, "failed to submit reply"); |
||
733 | goto fail; |
||
734 | } |
||
735 | |||
736 | reply_start(rpl, REQUESTPROTO_TYPE_SERVER_REPLY); |
||
737 | return; |
||
738 | |||
739 | fail: |
||
740 | NCDModuleInst_Backend_DeadError(i); |
||
741 | } |
||
742 | |||
743 | static void finish_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
744 | { |
||
745 | if (!NCDVal_ListRead(params->args, 0)) { |
||
746 | ModuleLog(i, BLOG_ERROR, "wrong arity"); |
||
747 | goto fail; |
||
748 | } |
||
749 | |||
750 | NCDModuleInst_Backend_Up(i); |
||
751 | |||
752 | struct request *r = params->method_user; |
||
753 | |||
754 | if (r->terminating) { |
||
755 | ModuleLog(i, BLOG_ERROR, "request is dying, cannot submit finished"); |
||
756 | goto fail; |
||
757 | } |
||
758 | |||
759 | r->got_finished = 1; |
||
760 | |||
761 | request_terminate(r); |
||
762 | return; |
||
763 | |||
764 | fail: |
||
765 | NCDModuleInst_Backend_DeadError(i); |
||
766 | } |
||
767 | |||
768 | static struct NCDModule modules[] = { |
||
769 | { |
||
770 | .type = "sys.request_server", |
||
771 | .func_new2 = func_new, |
||
772 | .func_die = func_die, |
||
773 | .alloc_size = sizeof(struct instance) |
||
774 | }, { |
||
775 | .type = "sys.request_server.request::reply", |
||
776 | .func_new2 = reply_func_new |
||
777 | }, { |
||
778 | .type = "sys.request_server.request::finish", |
||
779 | .func_new2 = finish_func_new |
||
780 | }, { |
||
781 | .type = NULL |
||
782 | } |
||
783 | }; |
||
784 | |||
785 | const struct NCDModuleGroup ncdmodule_sys_request_server = { |
||
786 | .modules = modules, |
||
787 | .strings = strings |
||
788 | }; |