1 /*	$NetBSD: rpc.h,v 1.2 2013/04/11 16:56:42 christos Exp $	*/
2 /*
3  * Copyright (c) 2006-2007 Niels Provos <provos@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 #ifndef _EVENT2_RPC_H_
29 #define _EVENT2_RPC_H_
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /** @file rpc.h
36  *
37  * This header files provides basic support for an RPC server and client.
38  *
39  * To support RPCs in a server, every supported RPC command needs to be
40  * defined and registered.
41  *
42  * EVRPC_HEADER(SendCommand, Request, Reply);
43  *
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.
50  *
51  * To register an RPC with an HTTP server, you need to first create an RPC
52  * base with:
53  *
54  *   struct evrpc_base *base = evrpc_init(http);
55  *
56  * A specific RPC can then be registered with
57  *
58  * EVRPC_REGISTER(base, SendCommand, Request, Reply,  FunctionCB, arg);
59  *
60  * when the server receives an appropriately formatted RPC, the user callback
61  * is invoked.   The callback needs to fill in the reply structure.
62  *
63  * void FunctionCB(EVRPC_STRUCT(SendCommand)* rpc, void *arg);
64  *
65  * To send the reply, call EVRPC_REQUEST_DONE(rpc);
66  *
67  * See the regression test for an example.
68  */
69 
70 /**
71    Determines if the member has been set in the message
72 
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.
76 */
77 #define EVTAG_HAS(msg, member) \
78 	((msg)->member##_set == 1)
79 
80 #ifndef _EVENT2_RPC_COMPAT_H_
81 
82 /**
83    Assigns a value to the member in the message.
84 
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
88 */
89 #define EVTAG_ASSIGN(msg, member, value) \
90 	(*(msg)->base->member##_assign)((msg), (value))
91 /**
92    Assigns a value to the member in the message.
93 
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
98 */
99 #define EVTAG_ASSIGN_WITH_LEN(msg, member, value, len)	\
100 	(*(msg)->base->member##_assign)((msg), (value), (len))
101 /**
102    Returns the value for a member.
103 
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.
108 */
109 #define EVTAG_GET(msg, member, pvalue) \
110 	(*(msg)->base->member##_get)((msg), (pvalue))
111 /**
112    Returns the value for a member.
113 
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.
119 */
120 #define EVTAG_GET_WITH_LEN(msg, member, pvalue, plen)	\
121 	(*(msg)->base->member##_get)((msg), (pvalue), (plen))
122 
123 #endif  /* _EVENT2_RPC_COMPAT_H_ */
124 
125 /**
126    Adds a value to an array.
127 */
128 #define EVTAG_ARRAY_ADD_VALUE(msg, member, value) \
129 	(*(msg)->base->member##_add)((msg), (value))
130 /**
131    Allocates a new entry in the array and returns it.
132 */
133 #define EVTAG_ARRAY_ADD(msg, member) \
134 	(*(msg)->base->member##_add)(msg)
135 /**
136    Gets a variable at the specified offset from the array.
137 */
138 #define EVTAG_ARRAY_GET(msg, member, offset, pvalue)	\
139 	(*(msg)->base->member##_get)((msg), (offset), (pvalue))
140 /**
141    Returns the number of entries in the array.
142 */
143 #define EVTAG_ARRAY_LEN(msg, member) ((msg)->member##_length)
144 
145 
146 struct evbuffer;
147 struct event_base;
148 struct evrpc_req_generic;
149 struct evrpc_request_wrapper;
150 struct evrpc;
151 
152 /** The type of a specific RPC Message
153  *
154  * @param rpcname the name of the RPC message
155  */
156 #define EVRPC_STRUCT(rpcname) struct evrpc_req__##rpcname
157 
158 struct evhttp_request;
159 struct evrpc_status;
160 struct evrpc_hook_meta;
161 
162 /** Creates the definitions and prototypes for an RPC
163  *
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
167  *
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()
172  */
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; \
178 	struct evrpc* rpc; \
179 	struct evhttp_request* http_req; \
180 	struct evbuffer* rpc_data; \
181 };								     \
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),	\
186     void *);
187 
188 struct evrpc_pool;
189 
190 /** use EVRPC_GENERATE instead */
191 struct evrpc_request_wrapper *evrpc_make_request_ctx(
192 	struct evrpc_pool *pool, void *request, void *reply,
193 	const char *rpcname,
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 *),
198 	void *cbarg);
199 
200 /** Creates a context structure that contains rpc specific information.
201  *
202  * EVRPC_MAKE_CTX is used to populate a RPC specific context that
203  * contains information about marshaling the RPC data types.
204  *
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
213  */
214 #define EVRPC_MAKE_CTX(rpcname, reqstruct, rplystruct, \
215     pool, request, reply, cb, cbarg)					\
216 	evrpc_make_request_ctx(pool, request, reply,			\
217 	    #rpcname,							\
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, \
222 	    cbarg)
223 
224 /** Generates the code for receiving and sending an RPC message
225  *
226  * EVRPC_GENERATE is used to create the code corresponding to sending
227  * and receiving a particular RPC message
228  *
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()
233  */
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),	\
239 	    void *cbarg) {						\
240 	return evrpc_send_request_generic(pool, request, reply,	\
241 	    (void (*)(struct evrpc_status *, void *, void *, void *))cb, \
242 	    cbarg,							\
243 	    #rpcname,							\
244 	    (void (*)(struct evbuffer *, void *))reqstruct##_marshal,	\
245 	    (void (*)(void *))rplystruct##_clear,			\
246 	    (int (*)(void *, struct evbuffer *))rplystruct##_unmarshal); \
247 }
248 
249 /** Provides access to the HTTP request object underlying an RPC
250  *
251  * Access to the underlying http object; can be used to look at headers or
252  * for getting the remote ip address
253  *
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.
257  */
258 #define EVRPC_REQUEST_HTTP(rpc_req) (rpc_req)->http_req
259 
260 /** completes the server response to an rpc request */
261 void evrpc_request_done(struct evrpc_req_generic *req);
262 
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);
266 
267 /** Creates the reply to an RPC request
268  *
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.
272  *
273  * @param rpc_req the rpc request structure provided to the server callback
274  */
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)
279 
280 
281 struct evrpc_base;
282 struct evhttp;
283 
284 /* functions to start up the rpc system */
285 
286 /** Creates a new rpc base from which RPC requests can be received
287  *
288  * @param server a pointer to an existing HTTP server
289  * @return a newly allocated evrpc_base struct
290  * @see evrpc_free()
291  */
292 struct evrpc_base *evrpc_init(struct evhttp *server);
293 
294 /**
295  * Frees the evrpc base
296  *
297  * For now, you are responsible for making sure that no rpcs are ongoing.
298  *
299  * @param base the evrpc_base object to be freed
300  * @see evrpc_init
301  */
302 void evrpc_free(struct evrpc_base *base);
303 
304 /** register RPCs with the HTTP Server
305  *
306  * registers a new RPC with the HTTP server, each RPC needs to have
307  * a unique name under which it can be identified.
308  *
309  * @param base the evrpc_base structure in which the RPC should be
310  *   registered.
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.
319  */
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)
330 
331 /**
332    Low level function for registering an RPC with a server.
333 
334    Use EVRPC_REGISTER() instead.
335 
336    @see EVRPC_REGISTER()
337 */
338 int evrpc_register_rpc(struct evrpc_base *, struct evrpc *,
339     void (*)(struct evrpc_req_generic*, void *), void *);
340 
341 /**
342  * Unregisters an already registered RPC
343  *
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()
348  */
349 #define EVRPC_UNREGISTER(base, name) evrpc_unregister_rpc((base), #name)
350 
351 int evrpc_unregister_rpc(struct evrpc_base *base, const char *name);
352 
353 /*
354  * Client-side RPC support
355  */
356 
357 struct evhttp_connection;
358 struct evrpc_status;
359 
360 /** launches an RPC and sends it to the server
361  *
362  * EVRPC_MAKE_REQUEST() is used by the client to send an RPC to the server.
363  *
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
374  */
375 #define EVRPC_MAKE_REQUEST(name, pool, request, reply, cb, cbarg)	\
376 	evrpc_send_request_##name((pool), (request), (reply), (cb), (cbarg))
377 
378 /**
379    Makes an RPC request based on the provided context.
380 
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()
383    instead.
384 
385    @param ctx a context from EVRPC_MAKE_CTX()
386    @returns 0 on success, -1 otherwise.
387    @see EVRPC_MAKE_REQUEST(), EVRPC_MAKE_CTX()
388 */
389 int evrpc_make_request(struct evrpc_request_wrapper *ctx);
390 
391 /** creates an rpc connection pool
392  *
393  * a pool has a number of connections associated with it.
394  * rpc requests are always made via a pool.
395  *
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()
400  */
401 struct evrpc_pool *evrpc_pool_new(struct event_base *base);
402 /** frees an rpc connection pool
403  *
404  * @param pool a pointer to an evrpc_pool allocated via evrpc_pool_new()
405  * @see evrpc_pool_new()
406  */
407 void evrpc_pool_free(struct evrpc_pool *pool);
408 
409 /**
410  * Adds a connection over which rpc can be dispatched to the pool.
411  *
412  * The connection object must have been newly created.
413  *
414  * @param pool the pool to which to add the connection
415  * @param evcon the connection to add to the pool.
416  */
417 void evrpc_pool_add_connection(struct evrpc_pool *pool,
418     struct evhttp_connection *evcon);
419 
420 /**
421  * Removes a connection from the pool.
422  *
423  * The connection object must have been newly created.
424  *
425  * @param pool the pool from which to remove the connection
426  * @param evcon the connection to remove from the pool.
427  */
428 void evrpc_pool_remove_connection(struct evrpc_pool *pool,
429     struct evhttp_connection *evcon);
430 
431 /**
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.
436  *
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.
441  *
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.
445  */
446 void evrpc_pool_set_timeout(struct evrpc_pool *pool, int timeout_in_secs);
447 
448 /**
449  * Hooks for changing the input and output of RPCs; this can be used to
450  * implement compression, authentication, encryption, ...
451  */
452 
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 */
456 };
457 
458 #ifndef WIN32
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
465 #endif
466 
467 /**
468  * Return value from hook processing functions
469  */
470 
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 */
475 };
476 
477 /** adds a processing hook to either an rpc base or rpc pool
478  *
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()
482  * has been called.
483  *
484  * The add functions return handles that can be used for removing hooks.
485  *
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()
492  */
493 void *evrpc_add_hook(void *vbase,
494     enum EVRPC_HOOK_TYPE hook_type,
495     int (*cb)(void *, struct evhttp_request *, struct evbuffer *, void *),
496     void *cb_arg);
497 
498 /** removes a previously added hook
499  *
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()
505  */
506 int evrpc_remove_hook(void *vbase,
507     enum EVRPC_HOOK_TYPE hook_type,
508     void *handle);
509 
510 /** resume a paused request
511  *
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
514  */
515 int
516 evrpc_resume_request(void *vbase, void *ctx, enum EVRPC_HOOK_RESULT res);
517 
518 /** adds meta data to request
519  *
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.
523  *
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
528  */
529 void evrpc_hook_add_meta(void *ctx, const char *key,
530     const void *data, size_t data_size);
531 
532 /** retrieves meta data previously associated
533  *
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
541  */
542 int evrpc_hook_find_meta(void *ctx, const char *key,
543     void **data, size_t *data_size);
544 
545 /**
546  * returns the connection object associated with the request
547  *
548  * @param ctx the context provided to the hook call
549  * @return a pointer to the evhttp_connection object
550  */
551 struct evhttp_connection *evrpc_hook_get_connection(void *ctx);
552 
553 /**
554    Function for sending a generic RPC request.
555 
556    Do not call this function directly, use EVRPC_MAKE_REQUEST() instead.
557 
558    @see EVRPC_MAKE_REQUEST()
559  */
560 int evrpc_send_request_generic(struct evrpc_pool *pool,
561     void *request, void *reply,
562     void (*cb)(struct evrpc_status *, void *, void *, void *),
563     void *cb_arg,
564     const char *rpcname,
565     void (*req_marshal)(struct evbuffer *, void *),
566     void (*rpl_clear)(void *),
567     int (*rpl_unmarshal)(void *, struct evbuffer *));
568 
569 /**
570    Function for registering a generic RPC with the RPC base.
571 
572    Do not call this function directly, use EVRPC_REGISTER() instead.
573 
574    @see EVRPC_REGISTER()
575  */
576 int
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 *));
584 
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),
591     void *cb_arg);
592 
593 #ifdef __cplusplus
594 }
595 #endif
596 
597 #endif /* _EVENT2_RPC_H_ */
598