evrpc.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * Copyright (c) 2006 Niels Provos <provos@citi.umich.edu>
  3. * All rights reserved.
  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. #ifndef _EVRPC_H_
  28. #define _EVRPC_H_
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /** @file evrpc.h
  33. *
  34. * This header files provides basic support for an RPC server and client.
  35. *
  36. * To support RPCs in a server, every supported RPC command needs to be
  37. * defined and registered.
  38. *
  39. * EVRPC_HEADER(SendCommand, Request, Reply);
  40. *
  41. * SendCommand is the name of the RPC command.
  42. * Request is the name of a structure generated by event_rpcgen.py.
  43. * It contains all parameters relating to the SendCommand RPC. The
  44. * server needs to fill in the Reply structure.
  45. * Reply is the name of a structure generated by event_rpcgen.py. It
  46. * contains the answer to the RPC.
  47. *
  48. * To register an RPC with an HTTP server, you need to first create an RPC
  49. * base with:
  50. *
  51. * struct evrpc_base *base = evrpc_init(http);
  52. *
  53. * A specific RPC can then be registered with
  54. *
  55. * EVRPC_REGISTER(base, SendCommand, Request, Reply, FunctionCB, arg);
  56. *
  57. * when the server receives an appropriately formatted RPC, the user callback
  58. * is invokved. The callback needs to fill in the reply structure.
  59. *
  60. * void FunctionCB(EVRPC_STRUCT(SendCommand)* rpc, void *arg);
  61. *
  62. * To send the reply, call EVRPC_REQUEST_DONE(rpc);
  63. *
  64. * See the regression test for an example.
  65. */
  66. struct evbuffer;
  67. struct event_base;
  68. struct evrpc_req_generic;
  69. /* Encapsulates a request */
  70. struct evrpc {
  71. TAILQ_ENTRY(evrpc) next;
  72. /* the URI at which the request handler lives */
  73. const char* uri;
  74. /* creates a new request structure */
  75. void *(*request_new)(void);
  76. /* frees the request structure */
  77. void (*request_free)(void *);
  78. /* unmarshals the buffer into the proper request structure */
  79. int (*request_unmarshal)(void *, struct evbuffer *);
  80. /* creates a new reply structure */
  81. void *(*reply_new)(void);
  82. /* creates a new reply structure */
  83. void (*reply_free)(void *);
  84. /* verifies that the reply is valid */
  85. int (*reply_complete)(void *);
  86. /* marshals the reply into a buffer */
  87. void (*reply_marshal)(struct evbuffer*, void *);
  88. /* the callback invoked for each received rpc */
  89. void (*cb)(struct evrpc_req_generic *, void *);
  90. void *cb_arg;
  91. /* reference for further configuration */
  92. struct evrpc_base *base;
  93. };
  94. /** The type of a specific RPC Message
  95. *
  96. * @param rpcname the name of the RPC message
  97. */
  98. #define EVRPC_STRUCT(rpcname) struct evrpc_req__##rpcname
  99. struct evhttp_request;
  100. struct evrpc_status;
  101. /* We alias the RPC specific structs to this voided one */
  102. struct evrpc_req_generic {
  103. /* the unmarshaled request object */
  104. void *request;
  105. /* the empty reply object that needs to be filled in */
  106. void *reply;
  107. /*
  108. * the static structure for this rpc; that can be used to
  109. * automatically unmarshal and marshal the http buffers.
  110. */
  111. struct evrpc *rpc;
  112. /*
  113. * the http request structure on which we need to answer.
  114. */
  115. struct evhttp_request* http_req;
  116. /*
  117. * callback to reply and finish answering this rpc
  118. */
  119. void (*done)(struct evrpc_req_generic* rpc);
  120. };
  121. /** Creates the definitions and prototypes for an RPC
  122. *
  123. * You need to use EVRPC_HEADER to create structures and function prototypes
  124. * needed by the server and client implementation. The structures have to be
  125. * defined in an .rpc file and converted to source code via event_rpcgen.py
  126. *
  127. * @param rpcname the name of the RPC
  128. * @param reqstruct the name of the RPC request structure
  129. * @param replystruct the name of the RPC reply structure
  130. * @see EVRPC_GENERATE()
  131. */
  132. #define EVRPC_HEADER(rpcname, reqstruct, rplystruct) \
  133. EVRPC_STRUCT(rpcname) { \
  134. struct reqstruct* request; \
  135. struct rplystruct* reply; \
  136. struct evrpc* rpc; \
  137. struct evhttp_request* http_req; \
  138. void (*done)(struct evrpc_status *, \
  139. struct evrpc* rpc, void *request, void *reply); \
  140. }; \
  141. int evrpc_send_request_##rpcname(struct evrpc_pool *, \
  142. struct reqstruct *, struct rplystruct *, \
  143. void (*)(struct evrpc_status *, \
  144. struct reqstruct *, struct rplystruct *, void *cbarg), \
  145. void *);
  146. /** Generates the code for receiving and sending an RPC message
  147. *
  148. * EVRPC_GENERATE is used to create the code corresponding to sending
  149. * and receiving a particular RPC message
  150. *
  151. * @param rpcname the name of the RPC
  152. * @param reqstruct the name of the RPC request structure
  153. * @param replystruct the name of the RPC reply structure
  154. * @see EVRPC_HEADER()
  155. */
  156. #define EVRPC_GENERATE(rpcname, reqstruct, rplystruct) \
  157. int evrpc_send_request_##rpcname(struct evrpc_pool *pool, \
  158. struct reqstruct *request, struct rplystruct *reply, \
  159. void (*cb)(struct evrpc_status *, \
  160. struct reqstruct *, struct rplystruct *, void *cbarg), \
  161. void *cbarg) { \
  162. struct evrpc_status status; \
  163. struct evrpc_request_wrapper *ctx; \
  164. ctx = (struct evrpc_request_wrapper *) \
  165. malloc(sizeof(struct evrpc_request_wrapper)); \
  166. if (ctx == NULL) \
  167. goto error; \
  168. ctx->pool = pool; \
  169. ctx->evcon = NULL; \
  170. ctx->name = strdup(#rpcname); \
  171. if (ctx->name == NULL) { \
  172. free(ctx); \
  173. goto error; \
  174. } \
  175. ctx->cb = (void (*)(struct evrpc_status *, \
  176. void *, void *, void *))cb; \
  177. ctx->cb_arg = cbarg; \
  178. ctx->request = (void *)request; \
  179. ctx->reply = (void *)reply; \
  180. ctx->request_marshal = (void (*)(struct evbuffer *, void *))reqstruct##_marshal; \
  181. ctx->reply_clear = (void (*)(void *))rplystruct##_clear; \
  182. ctx->reply_unmarshal = (int (*)(void *, struct evbuffer *))rplystruct##_unmarshal; \
  183. return (evrpc_make_request(ctx)); \
  184. error: \
  185. memset(&status, 0, sizeof(status)); \
  186. status.error = EVRPC_STATUS_ERR_UNSTARTED; \
  187. (*(cb))(&status, request, reply, cbarg); \
  188. return (-1); \
  189. }
  190. /** Provides access to the HTTP request object underlying an RPC
  191. *
  192. * Access to the underlying http object; can be used to look at headers or
  193. * for getting the remote ip address
  194. *
  195. * @param rpc_req the rpc request structure provided to the server callback
  196. * @return an struct evhttp_request object that can be inspected for
  197. * HTTP headers or sender information.
  198. */
  199. #define EVRPC_REQUEST_HTTP(rpc_req) (rpc_req)->http_req
  200. /** Creates the reply to an RPC request
  201. *
  202. * EVRPC_REQUEST_DONE is used to answer a request; the reply is expected
  203. * to have been filled in. The request and reply pointers become invalid
  204. * after this call has finished.
  205. *
  206. * @param rpc_req the rpc request structure provided to the server callback
  207. */
  208. #define EVRPC_REQUEST_DONE(rpc_req) do { \
  209. struct evrpc_req_generic *_req = (struct evrpc_req_generic *)(rpc_req); \
  210. _req->done(_req); \
  211. } while (0)
  212. /* Takes a request object and fills it in with the right magic */
  213. #define EVRPC_REGISTER_OBJECT(rpc, name, request, reply) \
  214. do { \
  215. (rpc)->uri = strdup(#name); \
  216. if ((rpc)->uri == NULL) { \
  217. fprintf(stderr, "failed to register object\n"); \
  218. exit(1); \
  219. } \
  220. (rpc)->request_new = (void *(*)(void))request##_new; \
  221. (rpc)->request_free = (void (*)(void *))request##_free; \
  222. (rpc)->request_unmarshal = (int (*)(void *, struct evbuffer *))request##_unmarshal; \
  223. (rpc)->reply_new = (void *(*)(void))reply##_new; \
  224. (rpc)->reply_free = (void (*)(void *))reply##_free; \
  225. (rpc)->reply_complete = (int (*)(void *))reply##_complete; \
  226. (rpc)->reply_marshal = (void (*)(struct evbuffer*, void *))reply##_marshal; \
  227. } while (0)
  228. struct evrpc_base;
  229. struct evhttp;
  230. /* functions to start up the rpc system */
  231. /** Creates a new rpc base from which RPC requests can be received
  232. *
  233. * @param server a pointer to an existing HTTP server
  234. * @return a newly allocated evrpc_base struct
  235. * @see evrpc_free()
  236. */
  237. struct evrpc_base *evrpc_init(struct evhttp *server);
  238. /**
  239. * Frees the evrpc base
  240. *
  241. * For now, you are responsible for making sure that no rpcs are ongoing.
  242. *
  243. * @param base the evrpc_base object to be freed
  244. * @see evrpc_init
  245. */
  246. void evrpc_free(struct evrpc_base *base);
  247. /** register RPCs with the HTTP Server
  248. *
  249. * registers a new RPC with the HTTP server, each RPC needs to have
  250. * a unique name under which it can be identified.
  251. *
  252. * @param base the evrpc_base structure in which the RPC should be
  253. * registered.
  254. * @param name the name of the RPC
  255. * @param request the name of the RPC request structure
  256. * @param reply the name of the RPC reply structure
  257. * @param callback the callback that should be invoked when the RPC
  258. * is received. The callback has the following prototype
  259. * void (*callback)(EVRPC_STRUCT(Message)* rpc, void *arg)
  260. * @param cbarg an additional parameter that can be passed to the callback.
  261. * The parameter can be used to carry around state.
  262. */
  263. #define EVRPC_REGISTER(base, name, request, reply, callback, cbarg) \
  264. do { \
  265. struct evrpc* rpc = (struct evrpc *)calloc(1, sizeof(struct evrpc)); \
  266. EVRPC_REGISTER_OBJECT(rpc, name, request, reply); \
  267. evrpc_register_rpc(base, rpc, \
  268. (void (*)(struct evrpc_req_generic*, void *))callback, cbarg); \
  269. } while (0)
  270. int evrpc_register_rpc(struct evrpc_base *, struct evrpc *,
  271. void (*)(struct evrpc_req_generic*, void *), void *);
  272. /**
  273. * Unregisters an already registered RPC
  274. *
  275. * @param base the evrpc_base object from which to unregister an RPC
  276. * @param name the name of the rpc to unregister
  277. * @return -1 on error or 0 when successful.
  278. * @see EVRPC_REGISTER()
  279. */
  280. #define EVRPC_UNREGISTER(base, name) evrpc_unregister_rpc(base, #name)
  281. int evrpc_unregister_rpc(struct evrpc_base *base, const char *name);
  282. /*
  283. * Client-side RPC support
  284. */
  285. struct evrpc_pool;
  286. struct evhttp_connection;
  287. /**
  288. * provides information about the completed RPC request.
  289. */
  290. struct evrpc_status {
  291. #define EVRPC_STATUS_ERR_NONE 0
  292. #define EVRPC_STATUS_ERR_TIMEOUT 1
  293. #define EVRPC_STATUS_ERR_BADPAYLOAD 2
  294. #define EVRPC_STATUS_ERR_UNSTARTED 3
  295. #define EVRPC_STATUS_ERR_HOOKABORTED 4
  296. int error;
  297. /* for looking at headers or other information */
  298. struct evhttp_request *http_req;
  299. };
  300. struct evrpc_request_wrapper {
  301. TAILQ_ENTRY(evrpc_request_wrapper) next;
  302. /* pool on which this rpc request is being made */
  303. struct evrpc_pool *pool;
  304. /* connection on which the request is being sent */
  305. struct evhttp_connection *evcon;
  306. /* event for implementing request timeouts */
  307. struct event ev_timeout;
  308. /* the name of the rpc */
  309. char *name;
  310. /* callback */
  311. void (*cb)(struct evrpc_status*, void *request, void *reply, void *arg);
  312. void *cb_arg;
  313. void *request;
  314. void *reply;
  315. /* unmarshals the buffer into the proper request structure */
  316. void (*request_marshal)(struct evbuffer *, void *);
  317. /* removes all stored state in the reply */
  318. void (*reply_clear)(void *);
  319. /* marshals the reply into a buffer */
  320. int (*reply_unmarshal)(void *, struct evbuffer*);
  321. };
  322. /** launches an RPC and sends it to the server
  323. *
  324. * EVRPC_MAKE_REQUEST() is used by the client to send an RPC to the server.
  325. *
  326. * @param name the name of the RPC
  327. * @param pool the evrpc_pool that contains the connection objects over which
  328. * the request should be sent.
  329. * @param request a pointer to the RPC request structure - it contains the
  330. * data to be sent to the server.
  331. * @param reply a pointer to the RPC reply structure. It is going to be filled
  332. * if the request was answered successfully
  333. * @param cb the callback to invoke when the RPC request has been answered
  334. * @param cbarg an additional argument to be passed to the client
  335. * @return 0 on success, -1 on failure
  336. */
  337. #define EVRPC_MAKE_REQUEST(name, pool, request, reply, cb, cbarg) \
  338. evrpc_send_request_##name(pool, request, reply, cb, cbarg)
  339. int evrpc_make_request(struct evrpc_request_wrapper *);
  340. /** creates an rpc connection pool
  341. *
  342. * a pool has a number of connections associated with it.
  343. * rpc requests are always made via a pool.
  344. *
  345. * @param base a pointer to an struct event_based object; can be left NULL
  346. * in singled-threaded applications
  347. * @return a newly allocated struct evrpc_pool object
  348. * @see evrpc_pool_free()
  349. */
  350. struct evrpc_pool *evrpc_pool_new(struct event_base *base);
  351. /** frees an rpc connection pool
  352. *
  353. * @param pool a pointer to an evrpc_pool allocated via evrpc_pool_new()
  354. * @see evrpc_pool_new()
  355. */
  356. void evrpc_pool_free(struct evrpc_pool *pool);
  357. /*
  358. * adds a connection over which rpc can be dispatched. the connection
  359. * object must have been newly created.
  360. */
  361. void evrpc_pool_add_connection(struct evrpc_pool *,
  362. struct evhttp_connection *);
  363. /**
  364. * Sets the timeout in secs after which a request has to complete. The
  365. * RPC is completely aborted if it does not complete by then. Setting
  366. * the timeout to 0 means that it never timeouts and can be used to
  367. * implement callback type RPCs.
  368. *
  369. * Any connection already in the pool will be updated with the new
  370. * timeout. Connections added to the pool after set_timeout has be
  371. * called receive the pool timeout only if no timeout has been set
  372. * for the connection itself.
  373. *
  374. * @param pool a pointer to a struct evrpc_pool object
  375. * @param timeout_in_secs the number of seconds after which a request should
  376. * timeout and a failure be returned to the callback.
  377. */
  378. void evrpc_pool_set_timeout(struct evrpc_pool *pool, int timeout_in_secs);
  379. /**
  380. * Hooks for changing the input and output of RPCs; this can be used to
  381. * implement compression, authentication, encryption, ...
  382. */
  383. enum EVRPC_HOOK_TYPE {
  384. EVRPC_INPUT, /**< apply the function to an input hook */
  385. EVRPC_OUTPUT /**< apply the function to an output hook */
  386. };
  387. #ifndef WIN32
  388. /** Deprecated alias for EVRPC_INPUT. Not available on windows, where it
  389. * conflicts with platform headers. */
  390. #define INPUT EVRPC_INPUT
  391. /** Deprecated alias for EVRPC_OUTPUT. Not available on windows, where it
  392. * conflicts with platform headers. */
  393. #define OUTPUT EVRPC_OUTPUT
  394. #endif
  395. /** adds a processing hook to either an rpc base or rpc pool
  396. *
  397. * If a hook returns -1, the processing is aborted.
  398. *
  399. * The add functions return handles that can be used for removing hooks.
  400. *
  401. * @param vbase a pointer to either struct evrpc_base or struct evrpc_pool
  402. * @param hook_type either INPUT or OUTPUT
  403. * @param cb the callback to call when the hook is activated
  404. * @param cb_arg an additional argument for the callback
  405. * @return a handle to the hook so it can be removed later
  406. * @see evrpc_remove_hook()
  407. */
  408. void *evrpc_add_hook(void *vbase,
  409. enum EVRPC_HOOK_TYPE hook_type,
  410. int (*cb)(struct evhttp_request *, struct evbuffer *, void *),
  411. void *cb_arg);
  412. /** removes a previously added hook
  413. *
  414. * @param vbase a pointer to either struct evrpc_base or struct evrpc_pool
  415. * @param hook_type either INPUT or OUTPUT
  416. * @param handle a handle returned by evrpc_add_hook()
  417. * @return 1 on success or 0 on failure
  418. * @see evrpc_add_hook()
  419. */
  420. int evrpc_remove_hook(void *vbase,
  421. enum EVRPC_HOOK_TYPE hook_type,
  422. void *handle);
  423. #ifdef __cplusplus
  424. }
  425. #endif
  426. #endif /* _EVRPC_H_ */