1 /* 2 * Copyright (c) 2006-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 #ifndef _EVRPC_INTERNAL_H_ 28 #define _EVRPC_INTERNAL_H_ 29 30 #include "http-internal.h" 31 32 struct evrpc; 33 struct evrpc_request_wrapper; 34 35 #define EVRPC_URI_PREFIX "/.rpc." 36 37 struct evrpc_hook { 38 TAILQ_ENTRY(evrpc_hook) next; 39 40 /* returns EVRPC_TERMINATE; if the rpc should be aborted. 41 * a hook is is allowed to rewrite the evbuffer 42 */ 43 int (*process)(void *, struct evhttp_request *, 44 struct evbuffer *, void *); 45 void *process_arg; 46 }; 47 48 TAILQ_HEAD(evrpc_hook_list, evrpc_hook); 49 50 /* 51 * this is shared between the base and the pool, so that we can reuse 52 * the hook adding functions; we alias both evrpc_pool and evrpc_base 53 * to this common structure. 54 */ 55 56 struct evrpc_hook_ctx; 57 TAILQ_HEAD(evrpc_pause_list, evrpc_hook_ctx); 58 59 struct _evrpc_hooks { 60 /* hooks for processing outbound and inbound rpcs */ 61 struct evrpc_hook_list in_hooks; 62 struct evrpc_hook_list out_hooks; 63 64 struct evrpc_pause_list pause_requests; 65 }; 66 67 #define input_hooks common.in_hooks 68 #define output_hooks common.out_hooks 69 #define paused_requests common.pause_requests 70 71 struct evrpc_base { 72 struct _evrpc_hooks common; 73 74 /* the HTTP server under which we register our RPC calls */ 75 struct evhttp* http_server; 76 77 /* a list of all RPCs registered with us */ 78 TAILQ_HEAD(evrpc_list, evrpc) registered_rpcs; 79 }; 80 81 struct evrpc_req_generic; 82 void evrpc_reqstate_free(struct evrpc_req_generic* rpc_state); 83 84 /* A pool for holding evhttp_connection objects */ 85 struct evrpc_pool { 86 struct _evrpc_hooks common; 87 88 struct event_base *base; 89 90 struct evconq connections; 91 92 int timeout; 93 94 TAILQ_HEAD(evrpc_requestq, evrpc_request_wrapper) (requests); 95 }; 96 97 struct evrpc_hook_ctx { 98 TAILQ_ENTRY(evrpc_hook_ctx) next; 99 100 void *ctx; 101 void (*cb)(void *, enum EVRPC_HOOK_RESULT); 102 }; 103 104 struct evrpc_meta { 105 TAILQ_ENTRY(evrpc_meta) next; 106 char *key; 107 108 void *data; 109 size_t data_size; 110 }; 111 112 TAILQ_HEAD(evrpc_meta_list, evrpc_meta); 113 114 struct evrpc_hook_meta { 115 struct evrpc_meta_list meta_data; 116 struct evhttp_connection *evcon; 117 }; 118 119 /* allows association of meta data with a request */ 120 static void evrpc_hook_associate_meta(struct evrpc_hook_meta **pctx, 121 struct evhttp_connection *evcon); 122 123 /* creates a new meta data store */ 124 static struct evrpc_hook_meta *evrpc_hook_meta_new(void); 125 126 /* frees the meta data associated with a request */ 127 static void evrpc_hook_context_free(struct evrpc_hook_meta *ctx); 128 129 /* the server side of an rpc */ 130 131 /* We alias the RPC specific structs to this voided one */ 132 struct evrpc_req_generic { 133 /* 134 * allows association of meta data via hooks - needs to be 135 * synchronized with evrpc_request_wrapper 136 */ 137 struct evrpc_hook_meta *hook_meta; 138 139 /* the unmarshaled request object */ 140 void *request; 141 142 /* the empty reply object that needs to be filled in */ 143 void *reply; 144 145 /* 146 * the static structure for this rpc; that can be used to 147 * automatically unmarshal and marshal the http buffers. 148 */ 149 struct evrpc *rpc; 150 151 /* 152 * the http request structure on which we need to answer. 153 */ 154 struct evhttp_request* http_req; 155 156 /* 157 * Temporary data store for marshaled data 158 */ 159 struct evbuffer* rpc_data; 160 }; 161 162 /* the client side of an rpc request */ 163 struct evrpc_request_wrapper { 164 /* 165 * allows association of meta data via hooks - needs to be 166 * synchronized with evrpc_req_generic. 167 */ 168 struct evrpc_hook_meta *hook_meta; 169 170 TAILQ_ENTRY(evrpc_request_wrapper) next; 171 172 /* pool on which this rpc request is being made */ 173 struct evrpc_pool *pool; 174 175 /* connection on which the request is being sent */ 176 struct evhttp_connection *evcon; 177 178 /* the actual request */ 179 struct evhttp_request *req; 180 181 /* event for implementing request timeouts */ 182 struct event ev_timeout; 183 184 /* the name of the rpc */ 185 char *name; 186 187 /* callback */ 188 void (*cb)(struct evrpc_status*, void *request, void *reply, void *arg); 189 void *cb_arg; 190 191 void *request; 192 void *reply; 193 194 /* unmarshals the buffer into the proper request structure */ 195 void (*request_marshal)(struct evbuffer *, void *); 196 197 /* removes all stored state in the reply */ 198 void (*reply_clear)(void *); 199 200 /* marshals the reply into a buffer */ 201 int (*reply_unmarshal)(void *, struct evbuffer*); 202 }; 203 204 #endif /* _EVRPC_INTERNAL_H_ */ 205