1 /* $NetBSD: rpc.h,v 1.3 2015/01/29 07:26:02 spz Exp $ */
3 * Copyright (c) 2006-2007 Niels Provos <provos@citi.umich.edu>
4 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
28 #ifndef _EVENT2_RPC_H_
29 #define _EVENT2_RPC_H_
37 * This header files provides basic support for an RPC server and client.
39 * To support RPCs in a server, every supported RPC command needs to be
40 * defined and registered.
42 * EVRPC_HEADER(SendCommand, Request, Reply);
44 * SendCommand is the name of the RPC command.
45 * Request is the name of a structure generated by event_rpcgen.py.
46 * It contains all parameters relating to the SendCommand RPC. The
47 * server needs to fill in the Reply structure.
48 * Reply is the name of a structure generated by event_rpcgen.py. It
49 * contains the answer to the RPC.
51 * To register an RPC with an HTTP server, you need to first create an RPC
54 * struct evrpc_base *base = evrpc_init(http);
56 * A specific RPC can then be registered with
58 * EVRPC_REGISTER(base, SendCommand, Request, Reply, FunctionCB, arg);
60 * when the server receives an appropriately formatted RPC, the user callback
61 * is invoked. The callback needs to fill in the reply structure.
63 * void FunctionCB(EVRPC_STRUCT(SendCommand)* rpc, void *arg);
65 * To send the reply, call EVRPC_REQUEST_DONE(rpc);
67 * See the regression test for an example.
71 Determines if the member has been set in the message
73 @param msg the message to inspect
74 @param member the member variable to test for presences
75 @return 1 if it's present or 0 otherwise.
77 #define EVTAG_HAS(msg, member) \
78 ((msg)->member##_set == 1)
80 #ifndef _EVENT2_RPC_COMPAT_H_
83 Assigns a value to the member in the message.
85 @param msg the message to which to assign a value
86 @param member the name of the member variable
87 @param value the value to assign
89 #define EVTAG_ASSIGN(msg, member, value) \
90 (*(msg)->base->member##_assign)((msg), (value))
92 Assigns a value to the member in the message.
94 @param msg the message to which to assign a value
95 @param member the name of the member variable
96 @param value the value to assign
97 @param len the length of the value
99 #define EVTAG_ASSIGN_WITH_LEN(msg, member, value, len) \
100 (*(msg)->base->member##_assign)((msg), (value), (len))
102 Returns the value for a member.
104 @param msg the message from which to get the value
105 @param member the name of the member variable
106 @param pvalue a pointer to the variable to hold the value
107 @return 0 on success, -1 otherwise.
109 #define EVTAG_GET(msg, member, pvalue) \
110 (*(msg)->base->member##_get)((msg), (pvalue))
112 Returns the value for a member.
114 @param msg the message from which to get the value
115 @param member the name of the member variable
116 @param pvalue a pointer to the variable to hold the value
117 @param plen a pointer to the length of the value
118 @return 0 on success, -1 otherwise.
120 #define EVTAG_GET_WITH_LEN(msg, member, pvalue, plen) \
121 (*(msg)->base->member##_get)((msg), (pvalue), (plen))
123 #endif /* _EVENT2_RPC_COMPAT_H_ */
126 Adds a value to an array.
128 #define EVTAG_ARRAY_ADD_VALUE(msg, member, value) \
129 (*(msg)->base->member##_add)((msg), (value))
131 Allocates a new entry in the array and returns it.
133 #define EVTAG_ARRAY_ADD(msg, member) \
134 (*(msg)->base->member##_add)(msg)
136 Gets a variable at the specified offset from the array.
138 #define EVTAG_ARRAY_GET(msg, member, offset, pvalue) \
139 (*(msg)->base->member##_get)((msg), (offset), (pvalue))
141 Returns the number of entries in the array.
143 #define EVTAG_ARRAY_LEN(msg, member) ((msg)->member##_length)
148 struct evrpc_req_generic
;
149 struct evrpc_request_wrapper
;
152 /** The type of a specific RPC Message
154 * @param rpcname the name of the RPC message
156 #define EVRPC_STRUCT(rpcname) struct evrpc_req__##rpcname
158 struct evhttp_request
;
160 struct evrpc_hook_meta
;
162 /** Creates the definitions and prototypes for an RPC
164 * You need to use EVRPC_HEADER to create structures and function prototypes
165 * needed by the server and client implementation. The structures have to be
166 * defined in an .rpc file and converted to source code via event_rpcgen.py
168 * @param rpcname the name of the RPC
169 * @param reqstruct the name of the RPC request structure
170 * @param replystruct the name of the RPC reply structure
171 * @see EVRPC_GENERATE()
173 #define EVRPC_HEADER(rpcname, reqstruct, rplystruct) \
174 EVRPC_STRUCT(rpcname) { \
175 struct evrpc_hook_meta *hook_meta; \
176 struct reqstruct* request; \
177 struct rplystruct* reply; \
179 struct evhttp_request* http_req; \
180 struct evbuffer* rpc_data; \
182 int evrpc_send_request_##rpcname(struct evrpc_pool *, \
183 struct reqstruct *, struct rplystruct *, \
184 void (*)(struct evrpc_status *, \
185 struct reqstruct *, struct rplystruct *, void *cbarg), \
190 /** use EVRPC_GENERATE instead */
191 struct evrpc_request_wrapper
*evrpc_make_request_ctx(
192 struct evrpc_pool
*pool
, void *request
, void *reply
,
194 void (*req_marshal
)(struct evbuffer
*, void *),
195 void (*rpl_clear
)(void *),
196 int (*rpl_unmarshal
)(void *, struct evbuffer
*),
197 void (*cb
)(struct evrpc_status
*, void *, void *, void *),
200 /** Creates a context structure that contains rpc specific information.
202 * EVRPC_MAKE_CTX is used to populate a RPC specific context that
203 * contains information about marshaling the RPC data types.
205 * @param rpcname the name of the RPC
206 * @param reqstruct the name of the RPC request structure
207 * @param replystruct the name of the RPC reply structure
208 * @param pool the evrpc_pool over which to make the request
209 * @param request a pointer to the RPC request structure object
210 * @param reply a pointer to the RPC reply structure object
211 * @param cb the callback function to call when the RPC has completed
212 * @param cbarg the argument to supply to the callback
214 #define EVRPC_MAKE_CTX(rpcname, reqstruct, rplystruct, \
215 pool, request, reply, cb, cbarg) \
216 evrpc_make_request_ctx(pool, request, reply, \
218 (void (*)(struct evbuffer *, void *))reqstruct##_marshal, \
219 (void (*)(void *))rplystruct##_clear, \
220 (int (*)(void *, struct evbuffer *))rplystruct##_unmarshal, \
221 (void (*)(struct evrpc_status *, void *, void *, void *))cb, \
224 /** Generates the code for receiving and sending an RPC message
226 * EVRPC_GENERATE is used to create the code corresponding to sending
227 * and receiving a particular RPC message
229 * @param rpcname the name of the RPC
230 * @param reqstruct the name of the RPC request structure
231 * @param replystruct the name of the RPC reply structure
232 * @see EVRPC_HEADER()
234 #define EVRPC_GENERATE(rpcname, reqstruct, rplystruct) \
235 int evrpc_send_request_##rpcname(struct evrpc_pool *pool, \
236 struct reqstruct *request, struct rplystruct *reply, \
237 void (*cb)(struct evrpc_status *, \
238 struct reqstruct *, struct rplystruct *, void *cbarg), \
240 return evrpc_send_request_generic(pool, request, reply, \
241 (void (*)(struct evrpc_status *, void *, void *, void *))cb, \
244 (void (*)(struct evbuffer *, void *))reqstruct##_marshal, \
245 (void (*)(void *))rplystruct##_clear, \
246 (int (*)(void *, struct evbuffer *))rplystruct##_unmarshal); \
249 /** Provides access to the HTTP request object underlying an RPC
251 * Access to the underlying http object; can be used to look at headers or
252 * for getting the remote ip address
254 * @param rpc_req the rpc request structure provided to the server callback
255 * @return an struct evhttp_request object that can be inspected for
256 * HTTP headers or sender information.
258 #define EVRPC_REQUEST_HTTP(rpc_req) (rpc_req)->http_req
260 /** completes the server response to an rpc request */
261 void evrpc_request_done(struct evrpc_req_generic
*req
);
263 /** accessors for request and reply */
264 void *evrpc_get_request(struct evrpc_req_generic
*req
);
265 void *evrpc_get_reply(struct evrpc_req_generic
*req
);
267 /** Creates the reply to an RPC request
269 * EVRPC_REQUEST_DONE is used to answer a request; the reply is expected
270 * to have been filled in. The request and reply pointers become invalid
271 * after this call has finished.
273 * @param rpc_req the rpc request structure provided to the server callback
275 #define EVRPC_REQUEST_DONE(rpc_req) do { \
276 struct evrpc_req_generic *_req = (struct evrpc_req_generic *)(rpc_req); \
277 evrpc_request_done(_req); \
278 } while (/*CONSTCOND*/0)
284 /* functions to start up the rpc system */
286 /** Creates a new rpc base from which RPC requests can be received
288 * @param server a pointer to an existing HTTP server
289 * @return a newly allocated evrpc_base struct
292 struct evrpc_base
*evrpc_init(struct evhttp
*server
);
295 * Frees the evrpc base
297 * For now, you are responsible for making sure that no rpcs are ongoing.
299 * @param base the evrpc_base object to be freed
302 void evrpc_free(struct evrpc_base
*base
);
304 /** register RPCs with the HTTP Server
306 * registers a new RPC with the HTTP server, each RPC needs to have
307 * a unique name under which it can be identified.
309 * @param base the evrpc_base structure in which the RPC should be
311 * @param name the name of the RPC
312 * @param request the name of the RPC request structure
313 * @param reply the name of the RPC reply structure
314 * @param callback the callback that should be invoked when the RPC
315 * is received. The callback has the following prototype
316 * void (*callback)(EVRPC_STRUCT(Message)* rpc, void *arg)
317 * @param cbarg an additional parameter that can be passed to the callback.
318 * The parameter can be used to carry around state.
320 #define EVRPC_REGISTER(base, name, request, reply, callback, cbarg) \
321 evrpc_register_generic(base, #name, \
322 (void (*)(struct evrpc_req_generic *, void *))callback, cbarg, \
323 (void *(*)(void *))request##_new, NULL, \
324 (void (*)(void *))request##_free, \
325 (int (*)(void *, struct evbuffer *))request##_unmarshal, \
326 (void *(*)(void *))reply##_new, NULL, \
327 (void (*)(void *))reply##_free, \
328 (int (*)(void *))reply##_complete, \
329 (void (*)(struct evbuffer *, void *))reply##_marshal)
332 Low level function for registering an RPC with a server.
334 Use EVRPC_REGISTER() instead.
336 @see EVRPC_REGISTER()
338 int evrpc_register_rpc(struct evrpc_base
*, struct evrpc
*,
339 void (*)(struct evrpc_req_generic
*, void *), void *);
342 * Unregisters an already registered RPC
344 * @param base the evrpc_base object from which to unregister an RPC
345 * @param name the name of the rpc to unregister
346 * @return -1 on error or 0 when successful.
347 * @see EVRPC_REGISTER()
349 #define EVRPC_UNREGISTER(base, name) evrpc_unregister_rpc((base), #name)
351 int evrpc_unregister_rpc(struct evrpc_base
*base
, const char *name
);
354 * Client-side RPC support
357 struct evhttp_connection
;
360 /** launches an RPC and sends it to the server
362 * EVRPC_MAKE_REQUEST() is used by the client to send an RPC to the server.
364 * @param name the name of the RPC
365 * @param pool the evrpc_pool that contains the connection objects over which
366 * the request should be sent.
367 * @param request a pointer to the RPC request structure - it contains the
368 * data to be sent to the server.
369 * @param reply a pointer to the RPC reply structure. It is going to be filled
370 * if the request was answered successfully
371 * @param cb the callback to invoke when the RPC request has been answered
372 * @param cbarg an additional argument to be passed to the client
373 * @return 0 on success, -1 on failure
375 #define EVRPC_MAKE_REQUEST(name, pool, request, reply, cb, cbarg) \
376 evrpc_send_request_##name((pool), (request), (reply), (cb), (cbarg))
379 Makes an RPC request based on the provided context.
381 This is a low-level function and should not be used directly
382 unless a custom context object is provided. Use EVRPC_MAKE_REQUEST()
385 @param ctx a context from EVRPC_MAKE_CTX()
386 @returns 0 on success, -1 otherwise.
387 @see EVRPC_MAKE_REQUEST(), EVRPC_MAKE_CTX()
389 int evrpc_make_request(struct evrpc_request_wrapper
*ctx
);
391 /** creates an rpc connection pool
393 * a pool has a number of connections associated with it.
394 * rpc requests are always made via a pool.
396 * @param base a pointer to an struct event_based object; can be left NULL
397 * in singled-threaded applications
398 * @return a newly allocated struct evrpc_pool object
399 * @see evrpc_pool_free()
401 struct evrpc_pool
*evrpc_pool_new(struct event_base
*base
);
402 /** frees an rpc connection pool
404 * @param pool a pointer to an evrpc_pool allocated via evrpc_pool_new()
405 * @see evrpc_pool_new()
407 void evrpc_pool_free(struct evrpc_pool
*pool
);
410 * Adds a connection over which rpc can be dispatched to the pool.
412 * The connection object must have been newly created.
414 * @param pool the pool to which to add the connection
415 * @param evcon the connection to add to the pool.
417 void evrpc_pool_add_connection(struct evrpc_pool
*pool
,
418 struct evhttp_connection
*evcon
);
421 * Removes a connection from the pool.
423 * The connection object must have been newly created.
425 * @param pool the pool from which to remove the connection
426 * @param evcon the connection to remove from the pool.
428 void evrpc_pool_remove_connection(struct evrpc_pool
*pool
,
429 struct evhttp_connection
*evcon
);
432 * Sets the timeout in secs after which a request has to complete. The
433 * RPC is completely aborted if it does not complete by then. Setting
434 * the timeout to 0 means that it never timeouts and can be used to
435 * implement callback type RPCs.
437 * Any connection already in the pool will be updated with the new
438 * timeout. Connections added to the pool after set_timeout has be
439 * called receive the pool timeout only if no timeout has been set
440 * for the connection itself.
442 * @param pool a pointer to a struct evrpc_pool object
443 * @param timeout_in_secs the number of seconds after which a request should
444 * timeout and a failure be returned to the callback.
446 void evrpc_pool_set_timeout(struct evrpc_pool
*pool
, int timeout_in_secs
);
449 * Hooks for changing the input and output of RPCs; this can be used to
450 * implement compression, authentication, encryption, ...
453 enum EVRPC_HOOK_TYPE
{
454 EVRPC_INPUT
, /**< apply the function to an input hook */
455 EVRPC_OUTPUT
/**< apply the function to an output hook */
459 /** Deprecated alias for EVRPC_INPUT. Not available on windows, where it
460 * conflicts with platform headers. */
461 #define INPUT EVRPC_INPUT
462 /** Deprecated alias for EVRPC_OUTPUT. Not available on windows, where it
463 * conflicts with platform headers. */
464 #define OUTPUT EVRPC_OUTPUT
468 * Return value from hook processing functions
471 enum EVRPC_HOOK_RESULT
{
472 EVRPC_TERMINATE
= -1, /**< indicates the rpc should be terminated */
473 EVRPC_CONTINUE
= 0, /**< continue processing the rpc */
474 EVRPC_PAUSE
= 1 /**< pause processing request until resumed */
477 /** adds a processing hook to either an rpc base or rpc pool
479 * If a hook returns TERMINATE, the processing is aborted. On CONTINUE,
480 * the request is immediately processed after the hook returns. If the
481 * hook returns PAUSE, request processing stops until evrpc_resume_request()
484 * The add functions return handles that can be used for removing hooks.
486 * @param vbase a pointer to either struct evrpc_base or struct evrpc_pool
487 * @param hook_type either INPUT or OUTPUT
488 * @param cb the callback to call when the hook is activated
489 * @param cb_arg an additional argument for the callback
490 * @return a handle to the hook so it can be removed later
491 * @see evrpc_remove_hook()
493 void *evrpc_add_hook(void *vbase
,
494 enum EVRPC_HOOK_TYPE hook_type
,
495 int (*cb
)(void *, struct evhttp_request
*, struct evbuffer
*, void *),
498 /** removes a previously added hook
500 * @param vbase a pointer to either struct evrpc_base or struct evrpc_pool
501 * @param hook_type either INPUT or OUTPUT
502 * @param handle a handle returned by evrpc_add_hook()
503 * @return 1 on success or 0 on failure
504 * @see evrpc_add_hook()
506 int evrpc_remove_hook(void *vbase
,
507 enum EVRPC_HOOK_TYPE hook_type
,
510 /** resume a paused request
512 * @param vbase a pointer to either struct evrpc_base or struct evrpc_pool
513 * @param ctx the context pointer provided to the original hook call
516 evrpc_resume_request(void *vbase
, void *ctx
, enum EVRPC_HOOK_RESULT res
);
518 /** adds meta data to request
520 * evrpc_hook_add_meta() allows hooks to add meta data to a request. for
521 * a client request, the meta data can be inserted by an outgoing request hook
522 * and retrieved by the incoming request hook.
524 * @param ctx the context provided to the hook call
525 * @param key a NUL-terminated c-string
526 * @param data the data to be associated with the key
527 * @param data_size the size of the data
529 void evrpc_hook_add_meta(void *ctx
, const char *key
,
530 const void *data
, size_t data_size
);
532 /** retrieves meta data previously associated
534 * evrpc_hook_find_meta() can be used to retrieve meta data associated to a
535 * request by a previous hook.
536 * @param ctx the context provided to the hook call
537 * @param key a NUL-terminated c-string
538 * @param data pointer to a data pointer that will contain the retrieved data
539 * @param data_size pointer to the size of the data
540 * @return 0 on success or -1 on failure
542 int evrpc_hook_find_meta(void *ctx
, const char *key
,
543 void **data
, size_t *data_size
);
546 * returns the connection object associated with the request
548 * @param ctx the context provided to the hook call
549 * @return a pointer to the evhttp_connection object
551 struct evhttp_connection
*evrpc_hook_get_connection(void *ctx
);
554 Function for sending a generic RPC request.
556 Do not call this function directly, use EVRPC_MAKE_REQUEST() instead.
558 @see EVRPC_MAKE_REQUEST()
560 int evrpc_send_request_generic(struct evrpc_pool
*pool
,
561 void *request
, void *reply
,
562 void (*cb
)(struct evrpc_status
*, void *, void *, void *),
565 void (*req_marshal
)(struct evbuffer
*, void *),
566 void (*rpl_clear
)(void *),
567 int (*rpl_unmarshal
)(void *, struct evbuffer
*));
570 Function for registering a generic RPC with the RPC base.
572 Do not call this function directly, use EVRPC_REGISTER() instead.
574 @see EVRPC_REGISTER()
577 evrpc_register_generic(struct evrpc_base
*base
, const char *name
,
578 void (*callback
)(struct evrpc_req_generic
*, void *), void *cbarg
,
579 void *(*req_new
)(void *), void *req_new_arg
, void (*req_free
)(void *),
580 int (*req_unmarshal
)(void *, struct evbuffer
*),
581 void *(*rpl_new
)(void *), void *rpl_new_arg
, void (*rpl_free
)(void *),
582 int (*rpl_complete
)(void *),
583 void (*rpl_marshal
)(struct evbuffer
*, void *));
585 /** accessors for obscure and undocumented functionality */
586 struct evrpc_pool
* evrpc_request_get_pool(struct evrpc_request_wrapper
*ctx
);
587 void evrpc_request_set_pool(struct evrpc_request_wrapper
*ctx
,
588 struct evrpc_pool
*pool
);
589 void evrpc_request_set_cb(struct evrpc_request_wrapper
*ctx
,
590 void (*cb
)(struct evrpc_status
*, void *request
, void *reply
, void *arg
),
597 #endif /* _EVENT2_RPC_H_ */