evrpc.c 29 KB

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