evrpc.c revision 1.1.1.4 1 /* $NetBSD: evrpc.c,v 1.1.1.4 2021/04/07 02:43:14 christos Exp $ */
2 /*
3 * Copyright (c) 2000-2007 Niels Provos <provos (at) citi.umich.edu>
4 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * 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. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "event2/event-config.h"
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: evrpc.c,v 1.1.1.4 2021/04/07 02:43:14 christos Exp $");
31 #include "evconfig-private.h"
32
33 #ifdef _WIN32
34 #define WIN32_LEAN_AND_MEAN
35 #include <winsock2.h>
36 #include <windows.h>
37 #undef WIN32_LEAN_AND_MEAN
38 #endif
39
40 #include <sys/types.h>
41 #ifndef _WIN32
42 #include <sys/socket.h>
43 #endif
44 #ifdef EVENT__HAVE_SYS_TIME_H
45 #include <sys/time.h>
46 #endif
47 #include <sys/queue.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #ifndef _WIN32
51 #include <unistd.h>
52 #endif
53 #include <errno.h>
54 #include <signal.h>
55 #include <string.h>
56
57 #include <sys/queue.h>
58
59 #include "event2/event.h"
60 #include "event2/event_struct.h"
61 #include "event2/rpc.h"
62 #include "event2/rpc_struct.h"
63 #include "evrpc-internal.h"
64 #include "event2/http.h"
65 #include "event2/buffer.h"
66 #include "event2/tag.h"
67 #include "event2/http_struct.h"
68 #include "event2/http_compat.h"
69 #include "event2/util.h"
70 #include "util-internal.h"
71 #include "log-internal.h"
72 #include "mm-internal.h"
73
74 struct evrpc_base *
75 evrpc_init(struct evhttp *http_server)
76 {
77 struct evrpc_base* base = mm_calloc(1, sizeof(struct evrpc_base));
78 if (base == NULL)
79 return (NULL);
80
81 /* we rely on the tagging sub system */
82 evtag_init();
83
84 TAILQ_INIT(&base->registered_rpcs);
85 TAILQ_INIT(&base->input_hooks);
86 TAILQ_INIT(&base->output_hooks);
87
88 TAILQ_INIT(&base->paused_requests);
89
90 base->http_server = http_server;
91
92 return (base);
93 }
94
95 void
96 evrpc_free(struct evrpc_base *base)
97 {
98 struct evrpc *rpc;
99 struct evrpc_hook *hook;
100 struct evrpc_hook_ctx *pause;
101 int r;
102
103 while ((rpc = TAILQ_FIRST(&base->registered_rpcs)) != NULL) {
104 r = evrpc_unregister_rpc(base, rpc->uri);
105 EVUTIL_ASSERT(r == 0);
106 }
107 while ((pause = TAILQ_FIRST(&base->paused_requests)) != NULL) {
108 TAILQ_REMOVE(&base->paused_requests, pause, next);
109 mm_free(pause);
110 }
111 while ((hook = TAILQ_FIRST(&base->input_hooks)) != NULL) {
112 r = evrpc_remove_hook(base, EVRPC_INPUT, hook);
113 EVUTIL_ASSERT(r);
114 }
115 while ((hook = TAILQ_FIRST(&base->output_hooks)) != NULL) {
116 r = evrpc_remove_hook(base, EVRPC_OUTPUT, hook);
117 EVUTIL_ASSERT(r);
118 }
119 mm_free(base);
120 }
121
122 void *
123 evrpc_add_hook(void *vbase,
124 enum EVRPC_HOOK_TYPE hook_type,
125 int (*cb)(void *, struct evhttp_request *, struct evbuffer *, void *),
126 void *cb_arg)
127 {
128 struct evrpc_hooks_ *base = vbase;
129 struct evrpc_hook_list *head = NULL;
130 struct evrpc_hook *hook = NULL;
131 switch (hook_type) {
132 case EVRPC_INPUT:
133 head = &base->in_hooks;
134 break;
135 case EVRPC_OUTPUT:
136 head = &base->out_hooks;
137 break;
138 default:
139 EVUTIL_ASSERT(hook_type == EVRPC_INPUT || hook_type == EVRPC_OUTPUT);
140 }
141
142 hook = mm_calloc(1, sizeof(struct evrpc_hook));
143 EVUTIL_ASSERT(hook != NULL);
144
145 hook->process = cb;
146 hook->process_arg = cb_arg;
147 TAILQ_INSERT_TAIL(head, hook, next);
148
149 return (hook);
150 }
151
152 static int
153 evrpc_remove_hook_internal(struct evrpc_hook_list *head, void *handle)
154 {
155 struct evrpc_hook *hook = NULL;
156 TAILQ_FOREACH(hook, head, next) {
157 if (hook == handle) {
158 TAILQ_REMOVE(head, hook, next);
159 mm_free(hook);
160 return (1);
161 }
162 }
163
164 return (0);
165 }
166
167 /*
168 * remove the hook specified by the handle
169 */
170
171 int
172 evrpc_remove_hook(void *vbase, enum EVRPC_HOOK_TYPE hook_type, void *handle)
173 {
174 struct evrpc_hooks_ *base = vbase;
175 struct evrpc_hook_list *head = NULL;
176 switch (hook_type) {
177 case EVRPC_INPUT:
178 head = &base->in_hooks;
179 break;
180 case EVRPC_OUTPUT:
181 head = &base->out_hooks;
182 break;
183 default:
184 EVUTIL_ASSERT(hook_type == EVRPC_INPUT || hook_type == EVRPC_OUTPUT);
185 }
186
187 return (evrpc_remove_hook_internal(head, handle));
188 }
189
190 static int
191 evrpc_process_hooks(struct evrpc_hook_list *head, void *ctx,
192 struct evhttp_request *req, struct evbuffer *evbuf)
193 {
194 struct evrpc_hook *hook;
195 TAILQ_FOREACH(hook, head, next) {
196 int res = hook->process(ctx, req, evbuf, hook->process_arg);
197 if (res != EVRPC_CONTINUE)
198 return (res);
199 }
200
201 return (EVRPC_CONTINUE);
202 }
203
204 static void evrpc_pool_schedule(struct evrpc_pool *pool);
205 static void evrpc_request_cb(struct evhttp_request *, void *);
206
207 /*
208 * Registers a new RPC with the HTTP server. The evrpc object is expected
209 * to have been filled in via the EVRPC_REGISTER_OBJECT macro which in turn
210 * calls this function.
211 */
212
213 static char *
214 evrpc_construct_uri(const char *uri)
215 {
216 char *constructed_uri;
217 size_t constructed_uri_len;
218
219 constructed_uri_len = strlen(EVRPC_URI_PREFIX) + strlen(uri) + 1;
220 if ((constructed_uri = mm_malloc(constructed_uri_len)) == NULL)
221 event_err(1, "%s: failed to register rpc at %s",
222 __func__, uri);
223 memcpy(constructed_uri, EVRPC_URI_PREFIX, strlen(EVRPC_URI_PREFIX));
224 memcpy(constructed_uri + strlen(EVRPC_URI_PREFIX), uri, strlen(uri));
225 constructed_uri[constructed_uri_len - 1] = '\0';
226
227 return (constructed_uri);
228 }
229
230 int
231 evrpc_register_rpc(struct evrpc_base *base, struct evrpc *rpc,
232 void (*cb)(struct evrpc_req_generic *, void *), void *cb_arg)
233 {
234 char *constructed_uri = evrpc_construct_uri(rpc->uri);
235
236 rpc->base = base;
237 rpc->cb = cb;
238 rpc->cb_arg = cb_arg;
239
240 TAILQ_INSERT_TAIL(&base->registered_rpcs, rpc, next);
241
242 evhttp_set_cb(base->http_server,
243 constructed_uri,
244 evrpc_request_cb,
245 rpc);
246
247 mm_free(constructed_uri);
248
249 return (0);
250 }
251
252 int
253 evrpc_unregister_rpc(struct evrpc_base *base, const char *name)
254 {
255 char *registered_uri = NULL;
256 struct evrpc *rpc;
257 int r;
258
259 /* find the right rpc; linear search might be slow */
260 TAILQ_FOREACH(rpc, &base->registered_rpcs, next) {
261 if (strcmp(rpc->uri, name) == 0)
262 break;
263 }
264 if (rpc == NULL) {
265 /* We did not find an RPC with this name */
266 return (-1);
267 }
268 TAILQ_REMOVE(&base->registered_rpcs, rpc, next);
269
270 registered_uri = evrpc_construct_uri(name);
271
272 /* remove the http server callback */
273 r = evhttp_del_cb(base->http_server, registered_uri);
274 EVUTIL_ASSERT(r == 0);
275
276 mm_free(registered_uri);
277
278 mm_free((char *)rpc->uri);
279 mm_free(rpc);
280 return (0);
281 }
282
283 static int evrpc_pause_request(void *vbase, void *ctx,
284 void (*cb)(void *, enum EVRPC_HOOK_RESULT));
285 static void evrpc_request_cb_closure(void *, enum EVRPC_HOOK_RESULT);
286
287 static void
288 evrpc_request_cb(struct evhttp_request *req, void *arg)
289 {
290 struct evrpc *rpc = arg;
291 struct evrpc_req_generic *rpc_state = NULL;
292
293 /* let's verify the outside parameters */
294 if (req->type != EVHTTP_REQ_POST ||
295 evbuffer_get_length(req->input_buffer) <= 0)
296 goto error;
297
298 rpc_state = mm_calloc(1, sizeof(struct evrpc_req_generic));
299 if (rpc_state == NULL)
300 goto error;
301 rpc_state->rpc = rpc;
302 rpc_state->http_req = req;
303 rpc_state->rpc_data = NULL;
304
305 if (TAILQ_FIRST(&rpc->base->input_hooks) != NULL) {
306 int hook_res;
307
308 evrpc_hook_associate_meta_(&rpc_state->hook_meta, req->evcon);
309
310 /*
311 * allow hooks to modify the outgoing request
312 */
313 hook_res = evrpc_process_hooks(&rpc->base->input_hooks,
314 rpc_state, req, req->input_buffer);
315 switch (hook_res) {
316 case EVRPC_TERMINATE:
317 goto error;
318 case EVRPC_PAUSE:
319 evrpc_pause_request(rpc->base, rpc_state,
320 evrpc_request_cb_closure);
321 return;
322 case EVRPC_CONTINUE:
323 break;
324 default:
325 EVUTIL_ASSERT(hook_res == EVRPC_TERMINATE ||
326 hook_res == EVRPC_CONTINUE ||
327 hook_res == EVRPC_PAUSE);
328 }
329 }
330
331 evrpc_request_cb_closure(rpc_state, EVRPC_CONTINUE);
332 return;
333
334 error:
335 if (rpc_state)
336 evrpc_reqstate_free_(rpc_state);
337 evhttp_send_error(req, HTTP_SERVUNAVAIL, NULL);
338 return;
339 }
340
341 static void
342 evrpc_request_cb_closure(void *arg, enum EVRPC_HOOK_RESULT hook_res)
343 {
344 struct evrpc_req_generic *rpc_state = arg;
345 struct evrpc *rpc;
346 struct evhttp_request *req;
347
348 EVUTIL_ASSERT(rpc_state);
349 rpc = rpc_state->rpc;
350 req = rpc_state->http_req;
351
352 if (hook_res == EVRPC_TERMINATE)
353 goto error;
354
355 /* let's check that we can parse the request */
356 rpc_state->request = rpc->request_new(rpc->request_new_arg);
357 if (rpc_state->request == NULL)
358 goto error;
359
360 if (rpc->request_unmarshal(
361 rpc_state->request, req->input_buffer) == -1) {
362 /* we failed to parse the request; that's a bummer */
363 goto error;
364 }
365
366 /* at this point, we have a well formed request, prepare the reply */
367
368 rpc_state->reply = rpc->reply_new(rpc->reply_new_arg);
369 if (rpc_state->reply == NULL)
370 goto error;
371
372 /* give the rpc to the user; they can deal with it */
373 rpc->cb(rpc_state, rpc->cb_arg);
374
375 return;
376
377 error:
378 evrpc_reqstate_free_(rpc_state);
379 evhttp_send_error(req, HTTP_SERVUNAVAIL, NULL);
380 return;
381 }
382
383
384 void
385 evrpc_reqstate_free_(struct evrpc_req_generic* rpc_state)
386 {
387 struct evrpc *rpc;
388 EVUTIL_ASSERT(rpc_state != NULL);
389 rpc = rpc_state->rpc;
390
391 /* clean up all memory */
392 if (rpc_state->hook_meta != NULL)
393 evrpc_hook_context_free_(rpc_state->hook_meta);
394 if (rpc_state->request != NULL)
395 rpc->request_free(rpc_state->request);
396 if (rpc_state->reply != NULL)
397 rpc->reply_free(rpc_state->reply);
398 if (rpc_state->rpc_data != NULL)
399 evbuffer_free(rpc_state->rpc_data);
400 mm_free(rpc_state);
401 }
402
403 static void
404 evrpc_request_done_closure(void *, enum EVRPC_HOOK_RESULT);
405
406 void
407 evrpc_request_done(struct evrpc_req_generic *rpc_state)
408 {
409 struct evhttp_request *req;
410 struct evrpc *rpc;
411
412 EVUTIL_ASSERT(rpc_state);
413
414 req = rpc_state->http_req;
415 rpc = rpc_state->rpc;
416
417 if (rpc->reply_complete(rpc_state->reply) == -1) {
418 /* the reply was not completely filled in. error out */
419 goto error;
420 }
421
422 if ((rpc_state->rpc_data = evbuffer_new()) == NULL) {
423 /* out of memory */
424 goto error;
425 }
426
427 /* serialize the reply */
428 rpc->reply_marshal(rpc_state->rpc_data, rpc_state->reply);
429
430 if (TAILQ_FIRST(&rpc->base->output_hooks) != NULL) {
431 int hook_res;
432
433 evrpc_hook_associate_meta_(&rpc_state->hook_meta, req->evcon);
434
435 /* do hook based tweaks to the request */
436 hook_res = evrpc_process_hooks(&rpc->base->output_hooks,
437 rpc_state, req, rpc_state->rpc_data);
438 switch (hook_res) {
439 case EVRPC_TERMINATE:
440 goto error;
441 case EVRPC_PAUSE:
442 if (evrpc_pause_request(rpc->base, rpc_state,
443 evrpc_request_done_closure) == -1)
444 goto error;
445 return;
446 case EVRPC_CONTINUE:
447 break;
448 default:
449 EVUTIL_ASSERT(hook_res == EVRPC_TERMINATE ||
450 hook_res == EVRPC_CONTINUE ||
451 hook_res == EVRPC_PAUSE);
452 }
453 }
454
455 evrpc_request_done_closure(rpc_state, EVRPC_CONTINUE);
456 return;
457
458 error:
459 evrpc_reqstate_free_(rpc_state);
460 evhttp_send_error(req, HTTP_SERVUNAVAIL, NULL);
461 return;
462 }
463
464 void *
465 evrpc_get_request(struct evrpc_req_generic *req)
466 {
467 return req->request;
468 }
469
470 void *
471 evrpc_get_reply(struct evrpc_req_generic *req)
472 {
473 return req->reply;
474 }
475
476 static void
477 evrpc_request_done_closure(void *arg, enum EVRPC_HOOK_RESULT hook_res)
478 {
479 struct evrpc_req_generic *rpc_state = arg;
480 struct evhttp_request *req;
481 EVUTIL_ASSERT(rpc_state);
482 req = rpc_state->http_req;
483
484 if (hook_res == EVRPC_TERMINATE)
485 goto error;
486
487 /* on success, we are going to transmit marshaled binary data */
488 if (evhttp_find_header(req->output_headers, "Content-Type") == NULL) {
489 evhttp_add_header(req->output_headers,
490 "Content-Type", "application/octet-stream");
491 }
492 evhttp_send_reply(req, HTTP_OK, "OK", rpc_state->rpc_data);
493
494 evrpc_reqstate_free_(rpc_state);
495
496 return;
497
498 error:
499 evrpc_reqstate_free_(rpc_state);
500 evhttp_send_error(req, HTTP_SERVUNAVAIL, NULL);
501 return;
502 }
503
504
505 /* Client implementation of RPC site */
506
507 static int evrpc_schedule_request(struct evhttp_connection *connection,
508 struct evrpc_request_wrapper *ctx);
509
510 struct evrpc_pool *
511 evrpc_pool_new(struct event_base *base)
512 {
513 struct evrpc_pool *pool = mm_calloc(1, sizeof(struct evrpc_pool));
514 if (pool == NULL)
515 return (NULL);
516
517 TAILQ_INIT(&pool->connections);
518 TAILQ_INIT(&pool->requests);
519
520 TAILQ_INIT(&pool->paused_requests);
521
522 TAILQ_INIT(&pool->input_hooks);
523 TAILQ_INIT(&pool->output_hooks);
524
525 pool->base = base;
526 pool->timeout = -1;
527
528 return (pool);
529 }
530
531 static void
532 evrpc_request_wrapper_free(struct evrpc_request_wrapper *request)
533 {
534 if (request->hook_meta != NULL)
535 evrpc_hook_context_free_(request->hook_meta);
536 mm_free(request->name);
537 mm_free(request);
538 }
539
540 void
541 evrpc_pool_free(struct evrpc_pool *pool)
542 {
543 struct evhttp_connection *connection;
544 struct evrpc_request_wrapper *request;
545 struct evrpc_hook_ctx *pause;
546 struct evrpc_hook *hook;
547 int r;
548
549 while ((request = TAILQ_FIRST(&pool->requests)) != NULL) {
550 TAILQ_REMOVE(&pool->requests, request, next);
551 evrpc_request_wrapper_free(request);
552 }
553
554 while ((pause = TAILQ_FIRST(&pool->paused_requests)) != NULL) {
555 TAILQ_REMOVE(&pool->paused_requests, pause, next);
556 mm_free(pause);
557 }
558
559 while ((connection = TAILQ_FIRST(&pool->connections)) != NULL) {
560 TAILQ_REMOVE(&pool->connections, connection, next);
561 evhttp_connection_free(connection);
562 }
563
564 while ((hook = TAILQ_FIRST(&pool->input_hooks)) != NULL) {
565 r = evrpc_remove_hook(pool, EVRPC_INPUT, hook);
566 EVUTIL_ASSERT(r);
567 }
568
569 while ((hook = TAILQ_FIRST(&pool->output_hooks)) != NULL) {
570 r = evrpc_remove_hook(pool, EVRPC_OUTPUT, hook);
571 EVUTIL_ASSERT(r);
572 }
573
574 mm_free(pool);
575 }
576
577 /*
578 * Add a connection to the RPC pool. A request scheduled on the pool
579 * may use any available connection.
580 */
581
582 void
583 evrpc_pool_add_connection(struct evrpc_pool *pool,
584 struct evhttp_connection *connection)
585 {
586 EVUTIL_ASSERT(connection->http_server == NULL);
587 TAILQ_INSERT_TAIL(&pool->connections, connection, next);
588
589 /*
590 * associate an event base with this connection
591 */
592 if (pool->base != NULL)
593 evhttp_connection_set_base(connection, pool->base);
594
595 /*
596 * unless a timeout was specifically set for a connection,
597 * the connection inherits the timeout from the pool.
598 */
599 if (!evutil_timerisset(&connection->timeout))
600 evhttp_connection_set_timeout(connection, pool->timeout);
601
602 /*
603 * if we have any requests pending, schedule them with the new
604 * connections.
605 */
606
607 if (TAILQ_FIRST(&pool->requests) != NULL) {
608 struct evrpc_request_wrapper *request =
609 TAILQ_FIRST(&pool->requests);
610 TAILQ_REMOVE(&pool->requests, request, next);
611 evrpc_schedule_request(connection, request);
612 }
613 }
614
615 void
616 evrpc_pool_remove_connection(struct evrpc_pool *pool,
617 struct evhttp_connection *connection)
618 {
619 TAILQ_REMOVE(&pool->connections, connection, next);
620 }
621
622 void
623 evrpc_pool_set_timeout(struct evrpc_pool *pool, int timeout_in_secs)
624 {
625 struct evhttp_connection *evcon;
626 TAILQ_FOREACH(evcon, &pool->connections, next) {
627 evhttp_connection_set_timeout(evcon, timeout_in_secs);
628 }
629 pool->timeout = timeout_in_secs;
630 }
631
632
633 static void evrpc_reply_done(struct evhttp_request *, void *);
634 static void evrpc_request_timeout(evutil_socket_t, short, void *);
635
636 /*
637 * Finds a connection object associated with the pool that is currently
638 * idle and can be used to make a request.
639 */
640 static struct evhttp_connection *
641 evrpc_pool_find_connection(struct evrpc_pool *pool)
642 {
643 struct evhttp_connection *connection;
644 TAILQ_FOREACH(connection, &pool->connections, next) {
645 if (TAILQ_FIRST(&connection->requests) == NULL)
646 return (connection);
647 }
648
649 return (NULL);
650 }
651
652 /*
653 * Prototypes responsible for evrpc scheduling and hooking
654 */
655
656 static void evrpc_schedule_request_closure(void *ctx, enum EVRPC_HOOK_RESULT);
657
658 /*
659 * We assume that the ctx is no longer queued on the pool.
660 */
661 static int
662 evrpc_schedule_request(struct evhttp_connection *connection,
663 struct evrpc_request_wrapper *ctx)
664 {
665 struct evhttp_request *req = NULL;
666 struct evrpc_pool *pool = ctx->pool;
667 struct evrpc_status status;
668
669 if ((req = evhttp_request_new(evrpc_reply_done, ctx)) == NULL)
670 goto error;
671
672 /* serialize the request data into the output buffer */
673 ctx->request_marshal(req->output_buffer, ctx->request);
674
675 /* we need to know the connection that we might have to abort */
676 ctx->evcon = connection;
677
678 /* if we get paused we also need to know the request */
679 ctx->req = req;
680
681 if (TAILQ_FIRST(&pool->output_hooks) != NULL) {
682 int hook_res;
683
684 evrpc_hook_associate_meta_(&ctx->hook_meta, connection);
685
686 /* apply hooks to the outgoing request */
687 hook_res = evrpc_process_hooks(&pool->output_hooks,
688 ctx, req, req->output_buffer);
689
690 switch (hook_res) {
691 case EVRPC_TERMINATE:
692 goto error;
693 case EVRPC_PAUSE:
694 /* we need to be explicitly resumed */
695 if (evrpc_pause_request(pool, ctx,
696 evrpc_schedule_request_closure) == -1)
697 goto error;
698 return (0);
699 case EVRPC_CONTINUE:
700 /* we can just continue */
701 break;
702 default:
703 EVUTIL_ASSERT(hook_res == EVRPC_TERMINATE ||
704 hook_res == EVRPC_CONTINUE ||
705 hook_res == EVRPC_PAUSE);
706 }
707 }
708
709 evrpc_schedule_request_closure(ctx, EVRPC_CONTINUE);
710 return (0);
711
712 error:
713 memset(&status, 0, sizeof(status));
714 status.error = EVRPC_STATUS_ERR_UNSTARTED;
715 (*ctx->cb)(&status, ctx->request, ctx->reply, ctx->cb_arg);
716 evrpc_request_wrapper_free(ctx);
717 return (-1);
718 }
719
720 static void
721 evrpc_schedule_request_closure(void *arg, enum EVRPC_HOOK_RESULT hook_res)
722 {
723 struct evrpc_request_wrapper *ctx = arg;
724 struct evhttp_connection *connection = ctx->evcon;
725 struct evhttp_request *req = ctx->req;
726 struct evrpc_pool *pool = ctx->pool;
727 struct evrpc_status status;
728 char *uri = NULL;
729 int res = 0;
730
731 if (hook_res == EVRPC_TERMINATE)
732 goto error;
733
734 uri = evrpc_construct_uri(ctx->name);
735 if (uri == NULL)
736 goto error;
737
738 if (pool->timeout > 0) {
739 /*
740 * a timeout after which the whole rpc is going to be aborted.
741 */
742 struct timeval tv;
743 evutil_timerclear(&tv);
744 tv.tv_sec = pool->timeout;
745 evtimer_add(&ctx->ev_timeout, &tv);
746 }
747
748 /* start the request over the connection */
749 res = evhttp_make_request(connection, req, EVHTTP_REQ_POST, uri);
750 mm_free(uri);
751
752 if (res == -1)
753 goto error;
754
755 return;
756
757 error:
758 memset(&status, 0, sizeof(status));
759 status.error = EVRPC_STATUS_ERR_UNSTARTED;
760 (*ctx->cb)(&status, ctx->request, ctx->reply, ctx->cb_arg);
761 evrpc_request_wrapper_free(ctx);
762 }
763
764 /* we just queue the paused request on the pool under the req object */
765 static int
766 evrpc_pause_request(void *vbase, void *ctx,
767 void (*cb)(void *, enum EVRPC_HOOK_RESULT))
768 {
769 struct evrpc_hooks_ *base = vbase;
770 struct evrpc_hook_ctx *pause = mm_malloc(sizeof(*pause));
771 if (pause == NULL)
772 return (-1);
773
774 pause->ctx = ctx;
775 pause->cb = cb;
776
777 TAILQ_INSERT_TAIL(&base->pause_requests, pause, next);
778 return (0);
779 }
780
781 int
782 evrpc_resume_request(void *vbase, void *ctx, enum EVRPC_HOOK_RESULT res)
783 {
784 struct evrpc_hooks_ *base = vbase;
785 struct evrpc_pause_list *head = &base->pause_requests;
786 struct evrpc_hook_ctx *pause;
787
788 TAILQ_FOREACH(pause, head, next) {
789 if (pause->ctx == ctx)
790 break;
791 }
792
793 if (pause == NULL)
794 return (-1);
795
796 (*pause->cb)(pause->ctx, res);
797 TAILQ_REMOVE(head, pause, next);
798 mm_free(pause);
799 return (0);
800 }
801
802 int
803 evrpc_make_request(struct evrpc_request_wrapper *ctx)
804 {
805 struct evrpc_pool *pool = ctx->pool;
806
807 /* initialize the event structure for this rpc */
808 evtimer_assign(&ctx->ev_timeout, pool->base, evrpc_request_timeout, ctx);
809
810 /* we better have some available connections on the pool */
811 EVUTIL_ASSERT(TAILQ_FIRST(&pool->connections) != NULL);
812
813 /*
814 * if no connection is available, we queue the request on the pool,
815 * the next time a connection is empty, the rpc will be send on that.
816 */
817 TAILQ_INSERT_TAIL(&pool->requests, ctx, next);
818
819 evrpc_pool_schedule(pool);
820
821 return (0);
822 }
823
824
825 struct evrpc_request_wrapper *
826 evrpc_make_request_ctx(
827 struct evrpc_pool *pool, void *request, void *reply,
828 const char *rpcname,
829 void (*req_marshal)(struct evbuffer*, void *),
830 void (*rpl_clear)(void *),
831 int (*rpl_unmarshal)(void *, struct evbuffer *),
832 void (*cb)(struct evrpc_status *, void *, void *, void *),
833 void *cbarg)
834 {
835 struct evrpc_request_wrapper *ctx = (struct evrpc_request_wrapper *)
836 mm_malloc(sizeof(struct evrpc_request_wrapper));
837 if (ctx == NULL)
838 return (NULL);
839
840 ctx->pool = pool;
841 ctx->hook_meta = NULL;
842 ctx->evcon = NULL;
843 ctx->name = mm_strdup(rpcname);
844 if (ctx->name == NULL) {
845 mm_free(ctx);
846 return (NULL);
847 }
848 ctx->cb = cb;
849 ctx->cb_arg = cbarg;
850 ctx->request = request;
851 ctx->reply = reply;
852 ctx->request_marshal = req_marshal;
853 ctx->reply_clear = rpl_clear;
854 ctx->reply_unmarshal = rpl_unmarshal;
855
856 return (ctx);
857 }
858
859 static void
860 evrpc_reply_done_closure(void *, enum EVRPC_HOOK_RESULT);
861
862 static void
863 evrpc_reply_done(struct evhttp_request *req, void *arg)
864 {
865 struct evrpc_request_wrapper *ctx = arg;
866 struct evrpc_pool *pool = ctx->pool;
867 int hook_res = EVRPC_CONTINUE;
868
869 /* cancel any timeout we might have scheduled */
870 event_del(&ctx->ev_timeout);
871
872 ctx->req = req;
873
874 /* we need to get the reply now */
875 if (req == NULL) {
876 evrpc_reply_done_closure(ctx, EVRPC_CONTINUE);
877 return;
878 }
879
880 if (TAILQ_FIRST(&pool->input_hooks) != NULL) {
881 evrpc_hook_associate_meta_(&ctx->hook_meta, ctx->evcon);
882
883 /* apply hooks to the incoming request */
884 hook_res = evrpc_process_hooks(&pool->input_hooks,
885 ctx, req, req->input_buffer);
886
887 switch (hook_res) {
888 case EVRPC_TERMINATE:
889 case EVRPC_CONTINUE:
890 break;
891 case EVRPC_PAUSE:
892 /*
893 * if we get paused we also need to know the
894 * request. unfortunately, the underlying
895 * layer is going to free it. we need to
896 * request ownership explicitly
897 */
898 evhttp_request_own(req);
899
900 evrpc_pause_request(pool, ctx,
901 evrpc_reply_done_closure);
902 return;
903 default:
904 EVUTIL_ASSERT(hook_res == EVRPC_TERMINATE ||
905 hook_res == EVRPC_CONTINUE ||
906 hook_res == EVRPC_PAUSE);
907 }
908 }
909
910 evrpc_reply_done_closure(ctx, hook_res);
911
912 /* http request is being freed by underlying layer */
913 }
914
915 static void
916 evrpc_reply_done_closure(void *arg, enum EVRPC_HOOK_RESULT hook_res)
917 {
918 struct evrpc_request_wrapper *ctx = arg;
919 struct evhttp_request *req = ctx->req;
920 struct evrpc_pool *pool = ctx->pool;
921 struct evrpc_status status;
922 int res = -1;
923
924 memset(&status, 0, sizeof(status));
925 status.http_req = req;
926
927 /* we need to get the reply now */
928 if (req == NULL) {
929 status.error = EVRPC_STATUS_ERR_TIMEOUT;
930 } else if (hook_res == EVRPC_TERMINATE) {
931 status.error = EVRPC_STATUS_ERR_HOOKABORTED;
932 } else {
933 res = ctx->reply_unmarshal(ctx->reply, req->input_buffer);
934 if (res == -1)
935 status.error = EVRPC_STATUS_ERR_BADPAYLOAD;
936 }
937
938 if (res == -1) {
939 /* clear everything that we might have written previously */
940 ctx->reply_clear(ctx->reply);
941 }
942
943 (*ctx->cb)(&status, ctx->request, ctx->reply, ctx->cb_arg);
944
945 evrpc_request_wrapper_free(ctx);
946
947 /* the http layer owned the original request structure, but if we
948 * got paused, we asked for ownership and need to free it here. */
949 if (req != NULL && evhttp_request_is_owned(req))
950 evhttp_request_free(req);
951
952 /* see if we can schedule another request */
953 evrpc_pool_schedule(pool);
954 }
955
956 static void
957 evrpc_pool_schedule(struct evrpc_pool *pool)
958 {
959 struct evrpc_request_wrapper *ctx = TAILQ_FIRST(&pool->requests);
960 struct evhttp_connection *evcon;
961
962 /* if no requests are pending, we have no work */
963 if (ctx == NULL)
964 return;
965
966 if ((evcon = evrpc_pool_find_connection(pool)) != NULL) {
967 TAILQ_REMOVE(&pool->requests, ctx, next);
968 evrpc_schedule_request(evcon, ctx);
969 }
970 }
971
972 static void
973 evrpc_request_timeout(evutil_socket_t fd, short what, void *arg)
974 {
975 struct evrpc_request_wrapper *ctx = arg;
976 struct evhttp_connection *evcon = ctx->evcon;
977 EVUTIL_ASSERT(evcon != NULL);
978
979 evhttp_connection_fail_(evcon, EVREQ_HTTP_TIMEOUT);
980 }
981
982 /*
983 * frees potential meta data associated with a request.
984 */
985
986 static void
987 evrpc_meta_data_free(struct evrpc_meta_list *meta_data)
988 {
989 struct evrpc_meta *entry;
990 EVUTIL_ASSERT(meta_data != NULL);
991
992 while ((entry = TAILQ_FIRST(meta_data)) != NULL) {
993 TAILQ_REMOVE(meta_data, entry, next);
994 mm_free(entry->key);
995 mm_free(entry->data);
996 mm_free(entry);
997 }
998 }
999
1000 static struct evrpc_hook_meta *
1001 evrpc_hook_meta_new_(void)
1002 {
1003 struct evrpc_hook_meta *ctx;
1004 ctx = mm_malloc(sizeof(struct evrpc_hook_meta));
1005 EVUTIL_ASSERT(ctx != NULL);
1006
1007 TAILQ_INIT(&ctx->meta_data);
1008 ctx->evcon = NULL;
1009
1010 return (ctx);
1011 }
1012
1013 static void
1014 evrpc_hook_associate_meta_(struct evrpc_hook_meta **pctx,
1015 struct evhttp_connection *evcon)
1016 {
1017 struct evrpc_hook_meta *ctx = *pctx;
1018 if (ctx == NULL)
1019 *pctx = ctx = evrpc_hook_meta_new_();
1020 ctx->evcon = evcon;
1021 }
1022
1023 static void
1024 evrpc_hook_context_free_(struct evrpc_hook_meta *ctx)
1025 {
1026 evrpc_meta_data_free(&ctx->meta_data);
1027 mm_free(ctx);
1028 }
1029
1030 /* Adds meta data */
1031 void
1032 evrpc_hook_add_meta(void *ctx, const char *key,
1033 const void *data, size_t data_size)
1034 {
1035 struct evrpc_request_wrapper *req = ctx;
1036 struct evrpc_hook_meta *store = NULL;
1037 struct evrpc_meta *meta = NULL;
1038
1039 if ((store = req->hook_meta) == NULL)
1040 store = req->hook_meta = evrpc_hook_meta_new_();
1041
1042 meta = mm_malloc(sizeof(struct evrpc_meta));
1043 EVUTIL_ASSERT(meta != NULL);
1044 meta->key = mm_strdup(key);
1045 EVUTIL_ASSERT(meta->key != NULL);
1046 meta->data_size = data_size;
1047 meta->data = mm_malloc(data_size);
1048 EVUTIL_ASSERT(meta->data != NULL);
1049 memcpy(meta->data, data, data_size);
1050
1051 TAILQ_INSERT_TAIL(&store->meta_data, meta, next);
1052 }
1053
1054 int
1055 evrpc_hook_find_meta(void *ctx, const char *key, void **data, size_t *data_size)
1056 {
1057 struct evrpc_request_wrapper *req = ctx;
1058 struct evrpc_meta *meta = NULL;
1059
1060 if (req->hook_meta == NULL)
1061 return (-1);
1062
1063 TAILQ_FOREACH(meta, &req->hook_meta->meta_data, next) {
1064 if (strcmp(meta->key, key) == 0) {
1065 *data = meta->data;
1066 *data_size = meta->data_size;
1067 return (0);
1068 }
1069 }
1070
1071 return (-1);
1072 }
1073
1074 struct evhttp_connection *
1075 evrpc_hook_get_connection(void *ctx)
1076 {
1077 struct evrpc_request_wrapper *req = ctx;
1078 return (req->hook_meta != NULL ? req->hook_meta->evcon : NULL);
1079 }
1080
1081 int
1082 evrpc_send_request_generic(struct evrpc_pool *pool,
1083 void *request, void *reply,
1084 void (*cb)(struct evrpc_status *, void *, void *, void *),
1085 void *cb_arg,
1086 const char *rpcname,
1087 void (*req_marshal)(struct evbuffer *, void *),
1088 void (*rpl_clear)(void *),
1089 int (*rpl_unmarshal)(void *, struct evbuffer *))
1090 {
1091 struct evrpc_status status;
1092 struct evrpc_request_wrapper *ctx;
1093 ctx = evrpc_make_request_ctx(pool, request, reply,
1094 rpcname, req_marshal, rpl_clear, rpl_unmarshal, cb, cb_arg);
1095 if (ctx == NULL)
1096 goto error;
1097 return (evrpc_make_request(ctx));
1098 error:
1099 memset(&status, 0, sizeof(status));
1100 status.error = EVRPC_STATUS_ERR_UNSTARTED;
1101 (*(cb))(&status, request, reply, cb_arg);
1102 return (-1);
1103 }
1104
1105 /** Takes a request object and fills it in with the right magic */
1106 static struct evrpc *
1107 evrpc_register_object(const char *name,
1108 void *(*req_new)(void*), void *req_new_arg, void (*req_free)(void *),
1109 int (*req_unmarshal)(void *, struct evbuffer *),
1110 void *(*rpl_new)(void*), void *rpl_new_arg, void (*rpl_free)(void *),
1111 int (*rpl_complete)(void *),
1112 void (*rpl_marshal)(struct evbuffer *, void *))
1113 {
1114 struct evrpc* rpc = (struct evrpc *)mm_calloc(1, sizeof(struct evrpc));
1115 if (rpc == NULL)
1116 return (NULL);
1117 rpc->uri = mm_strdup(name);
1118 if (rpc->uri == NULL) {
1119 mm_free(rpc);
1120 return (NULL);
1121 }
1122 rpc->request_new = req_new;
1123 rpc->request_new_arg = req_new_arg;
1124 rpc->request_free = req_free;
1125 rpc->request_unmarshal = req_unmarshal;
1126 rpc->reply_new = rpl_new;
1127 rpc->reply_new_arg = rpl_new_arg;
1128 rpc->reply_free = rpl_free;
1129 rpc->reply_complete = rpl_complete;
1130 rpc->reply_marshal = rpl_marshal;
1131 return (rpc);
1132 }
1133
1134 int
1135 evrpc_register_generic(struct evrpc_base *base, const char *name,
1136 void (*callback)(struct evrpc_req_generic *, void *), void *cbarg,
1137 void *(*req_new)(void *), void *req_new_arg, void (*req_free)(void *),
1138 int (*req_unmarshal)(void *, struct evbuffer *),
1139 void *(*rpl_new)(void *), void *rpl_new_arg, void (*rpl_free)(void *),
1140 int (*rpl_complete)(void *),
1141 void (*rpl_marshal)(struct evbuffer *, void *))
1142 {
1143 struct evrpc* rpc =
1144 evrpc_register_object(name, req_new, req_new_arg, req_free, req_unmarshal,
1145 rpl_new, rpl_new_arg, rpl_free, rpl_complete, rpl_marshal);
1146 if (rpc == NULL)
1147 return (-1);
1148 evrpc_register_rpc(base, rpc,
1149 (void (*)(struct evrpc_req_generic*, void *))callback, cbarg);
1150 return (0);
1151 }
1152
1153 /** accessors for obscure and undocumented functionality */
1154 struct evrpc_pool *
1155 evrpc_request_get_pool(struct evrpc_request_wrapper *ctx)
1156 {
1157 return (ctx->pool);
1158 }
1159
1160 void
1161 evrpc_request_set_pool(struct evrpc_request_wrapper *ctx,
1162 struct evrpc_pool *pool)
1163 {
1164 ctx->pool = pool;
1165 }
1166
1167 void
1168 evrpc_request_set_cb(struct evrpc_request_wrapper *ctx,
1169 void (*cb)(struct evrpc_status*, void *request, void *reply, void *arg),
1170 void *cb_arg)
1171 {
1172 ctx->cb = cb;
1173 ctx->cb_arg = cb_arg;
1174 }
1175