michael@0: /* michael@0: * Copyright (c) 2006-2007 Niels Provos michael@0: * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: #ifndef _EVRPC_INTERNAL_H_ michael@0: #define _EVRPC_INTERNAL_H_ michael@0: michael@0: #include "http-internal.h" michael@0: michael@0: struct evrpc; michael@0: struct evrpc_request_wrapper; michael@0: michael@0: #define EVRPC_URI_PREFIX "/.rpc." michael@0: michael@0: struct evrpc_hook { michael@0: TAILQ_ENTRY(evrpc_hook) next; michael@0: michael@0: /* returns EVRPC_TERMINATE; if the rpc should be aborted. michael@0: * a hook is is allowed to rewrite the evbuffer michael@0: */ michael@0: int (*process)(void *, struct evhttp_request *, michael@0: struct evbuffer *, void *); michael@0: void *process_arg; michael@0: }; michael@0: michael@0: TAILQ_HEAD(evrpc_hook_list, evrpc_hook); michael@0: michael@0: /* michael@0: * this is shared between the base and the pool, so that we can reuse michael@0: * the hook adding functions; we alias both evrpc_pool and evrpc_base michael@0: * to this common structure. michael@0: */ michael@0: michael@0: struct evrpc_hook_ctx; michael@0: TAILQ_HEAD(evrpc_pause_list, evrpc_hook_ctx); michael@0: michael@0: struct _evrpc_hooks { michael@0: /* hooks for processing outbound and inbound rpcs */ michael@0: struct evrpc_hook_list in_hooks; michael@0: struct evrpc_hook_list out_hooks; michael@0: michael@0: struct evrpc_pause_list pause_requests; michael@0: }; michael@0: michael@0: #define input_hooks common.in_hooks michael@0: #define output_hooks common.out_hooks michael@0: #define paused_requests common.pause_requests michael@0: michael@0: struct evrpc_base { michael@0: struct _evrpc_hooks common; michael@0: michael@0: /* the HTTP server under which we register our RPC calls */ michael@0: struct evhttp* http_server; michael@0: michael@0: /* a list of all RPCs registered with us */ michael@0: TAILQ_HEAD(evrpc_list, evrpc) registered_rpcs; michael@0: }; michael@0: michael@0: struct evrpc_req_generic; michael@0: void evrpc_reqstate_free(struct evrpc_req_generic* rpc_state); michael@0: michael@0: /* A pool for holding evhttp_connection objects */ michael@0: struct evrpc_pool { michael@0: struct _evrpc_hooks common; michael@0: michael@0: struct event_base *base; michael@0: michael@0: struct evconq connections; michael@0: michael@0: int timeout; michael@0: michael@0: TAILQ_HEAD(evrpc_requestq, evrpc_request_wrapper) (requests); michael@0: }; michael@0: michael@0: struct evrpc_hook_ctx { michael@0: TAILQ_ENTRY(evrpc_hook_ctx) next; michael@0: michael@0: void *ctx; michael@0: void (*cb)(void *, enum EVRPC_HOOK_RESULT); michael@0: }; michael@0: michael@0: struct evrpc_meta { michael@0: TAILQ_ENTRY(evrpc_meta) next; michael@0: char *key; michael@0: michael@0: void *data; michael@0: size_t data_size; michael@0: }; michael@0: michael@0: TAILQ_HEAD(evrpc_meta_list, evrpc_meta); michael@0: michael@0: struct evrpc_hook_meta { michael@0: struct evrpc_meta_list meta_data; michael@0: struct evhttp_connection *evcon; michael@0: }; michael@0: michael@0: /* allows association of meta data with a request */ michael@0: static void evrpc_hook_associate_meta(struct evrpc_hook_meta **pctx, michael@0: struct evhttp_connection *evcon); michael@0: michael@0: /* creates a new meta data store */ michael@0: static struct evrpc_hook_meta *evrpc_hook_meta_new(void); michael@0: michael@0: /* frees the meta data associated with a request */ michael@0: static void evrpc_hook_context_free(struct evrpc_hook_meta *ctx); michael@0: michael@0: /* the server side of an rpc */ michael@0: michael@0: /* We alias the RPC specific structs to this voided one */ michael@0: struct evrpc_req_generic { michael@0: /* michael@0: * allows association of meta data via hooks - needs to be michael@0: * synchronized with evrpc_request_wrapper michael@0: */ michael@0: struct evrpc_hook_meta *hook_meta; michael@0: michael@0: /* the unmarshaled request object */ michael@0: void *request; michael@0: michael@0: /* the empty reply object that needs to be filled in */ michael@0: void *reply; michael@0: michael@0: /* michael@0: * the static structure for this rpc; that can be used to michael@0: * automatically unmarshal and marshal the http buffers. michael@0: */ michael@0: struct evrpc *rpc; michael@0: michael@0: /* michael@0: * the http request structure on which we need to answer. michael@0: */ michael@0: struct evhttp_request* http_req; michael@0: michael@0: /* michael@0: * Temporary data store for marshaled data michael@0: */ michael@0: struct evbuffer* rpc_data; michael@0: }; michael@0: michael@0: /* the client side of an rpc request */ michael@0: struct evrpc_request_wrapper { michael@0: /* michael@0: * allows association of meta data via hooks - needs to be michael@0: * synchronized with evrpc_req_generic. michael@0: */ michael@0: struct evrpc_hook_meta *hook_meta; michael@0: michael@0: TAILQ_ENTRY(evrpc_request_wrapper) next; michael@0: michael@0: /* pool on which this rpc request is being made */ michael@0: struct evrpc_pool *pool; michael@0: michael@0: /* connection on which the request is being sent */ michael@0: struct evhttp_connection *evcon; michael@0: michael@0: /* the actual request */ michael@0: struct evhttp_request *req; michael@0: michael@0: /* event for implementing request timeouts */ michael@0: struct event ev_timeout; michael@0: michael@0: /* the name of the rpc */ michael@0: char *name; michael@0: michael@0: /* callback */ michael@0: void (*cb)(struct evrpc_status*, void *request, void *reply, void *arg); michael@0: void *cb_arg; michael@0: michael@0: void *request; michael@0: void *reply; michael@0: michael@0: /* unmarshals the buffer into the proper request structure */ michael@0: void (*request_marshal)(struct evbuffer *, void *); michael@0: michael@0: /* removes all stored state in the reply */ michael@0: void (*reply_clear)(void *); michael@0: michael@0: /* marshals the reply into a buffer */ michael@0: int (*reply_unmarshal)(void *, struct evbuffer*); michael@0: }; michael@0: michael@0: #endif /* _EVRPC_INTERNAL_H_ */