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