Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 2006-2007 Niels Provos <provos@citi.umich.edu> |
michael@0 | 3 | * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson |
michael@0 | 4 | * |
michael@0 | 5 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 6 | * modification, are permitted provided that the following conditions |
michael@0 | 7 | * are met: |
michael@0 | 8 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 9 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 11 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 12 | * documentation and/or other materials provided with the distribution. |
michael@0 | 13 | * 3. The name of the author may not be used to endorse or promote products |
michael@0 | 14 | * derived from this software without specific prior written permission. |
michael@0 | 15 | * |
michael@0 | 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
michael@0 | 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
michael@0 | 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
michael@0 | 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
michael@0 | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
michael@0 | 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
michael@0 | 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 26 | */ |
michael@0 | 27 | #ifndef _EVRPC_INTERNAL_H_ |
michael@0 | 28 | #define _EVRPC_INTERNAL_H_ |
michael@0 | 29 | |
michael@0 | 30 | #include "http-internal.h" |
michael@0 | 31 | |
michael@0 | 32 | struct evrpc; |
michael@0 | 33 | struct evrpc_request_wrapper; |
michael@0 | 34 | |
michael@0 | 35 | #define EVRPC_URI_PREFIX "/.rpc." |
michael@0 | 36 | |
michael@0 | 37 | struct evrpc_hook { |
michael@0 | 38 | TAILQ_ENTRY(evrpc_hook) next; |
michael@0 | 39 | |
michael@0 | 40 | /* returns EVRPC_TERMINATE; if the rpc should be aborted. |
michael@0 | 41 | * a hook is is allowed to rewrite the evbuffer |
michael@0 | 42 | */ |
michael@0 | 43 | int (*process)(void *, struct evhttp_request *, |
michael@0 | 44 | struct evbuffer *, void *); |
michael@0 | 45 | void *process_arg; |
michael@0 | 46 | }; |
michael@0 | 47 | |
michael@0 | 48 | TAILQ_HEAD(evrpc_hook_list, evrpc_hook); |
michael@0 | 49 | |
michael@0 | 50 | /* |
michael@0 | 51 | * this is shared between the base and the pool, so that we can reuse |
michael@0 | 52 | * the hook adding functions; we alias both evrpc_pool and evrpc_base |
michael@0 | 53 | * to this common structure. |
michael@0 | 54 | */ |
michael@0 | 55 | |
michael@0 | 56 | struct evrpc_hook_ctx; |
michael@0 | 57 | TAILQ_HEAD(evrpc_pause_list, evrpc_hook_ctx); |
michael@0 | 58 | |
michael@0 | 59 | struct _evrpc_hooks { |
michael@0 | 60 | /* hooks for processing outbound and inbound rpcs */ |
michael@0 | 61 | struct evrpc_hook_list in_hooks; |
michael@0 | 62 | struct evrpc_hook_list out_hooks; |
michael@0 | 63 | |
michael@0 | 64 | struct evrpc_pause_list pause_requests; |
michael@0 | 65 | }; |
michael@0 | 66 | |
michael@0 | 67 | #define input_hooks common.in_hooks |
michael@0 | 68 | #define output_hooks common.out_hooks |
michael@0 | 69 | #define paused_requests common.pause_requests |
michael@0 | 70 | |
michael@0 | 71 | struct evrpc_base { |
michael@0 | 72 | struct _evrpc_hooks common; |
michael@0 | 73 | |
michael@0 | 74 | /* the HTTP server under which we register our RPC calls */ |
michael@0 | 75 | struct evhttp* http_server; |
michael@0 | 76 | |
michael@0 | 77 | /* a list of all RPCs registered with us */ |
michael@0 | 78 | TAILQ_HEAD(evrpc_list, evrpc) registered_rpcs; |
michael@0 | 79 | }; |
michael@0 | 80 | |
michael@0 | 81 | struct evrpc_req_generic; |
michael@0 | 82 | void evrpc_reqstate_free(struct evrpc_req_generic* rpc_state); |
michael@0 | 83 | |
michael@0 | 84 | /* A pool for holding evhttp_connection objects */ |
michael@0 | 85 | struct evrpc_pool { |
michael@0 | 86 | struct _evrpc_hooks common; |
michael@0 | 87 | |
michael@0 | 88 | struct event_base *base; |
michael@0 | 89 | |
michael@0 | 90 | struct evconq connections; |
michael@0 | 91 | |
michael@0 | 92 | int timeout; |
michael@0 | 93 | |
michael@0 | 94 | TAILQ_HEAD(evrpc_requestq, evrpc_request_wrapper) (requests); |
michael@0 | 95 | }; |
michael@0 | 96 | |
michael@0 | 97 | struct evrpc_hook_ctx { |
michael@0 | 98 | TAILQ_ENTRY(evrpc_hook_ctx) next; |
michael@0 | 99 | |
michael@0 | 100 | void *ctx; |
michael@0 | 101 | void (*cb)(void *, enum EVRPC_HOOK_RESULT); |
michael@0 | 102 | }; |
michael@0 | 103 | |
michael@0 | 104 | struct evrpc_meta { |
michael@0 | 105 | TAILQ_ENTRY(evrpc_meta) next; |
michael@0 | 106 | char *key; |
michael@0 | 107 | |
michael@0 | 108 | void *data; |
michael@0 | 109 | size_t data_size; |
michael@0 | 110 | }; |
michael@0 | 111 | |
michael@0 | 112 | TAILQ_HEAD(evrpc_meta_list, evrpc_meta); |
michael@0 | 113 | |
michael@0 | 114 | struct evrpc_hook_meta { |
michael@0 | 115 | struct evrpc_meta_list meta_data; |
michael@0 | 116 | struct evhttp_connection *evcon; |
michael@0 | 117 | }; |
michael@0 | 118 | |
michael@0 | 119 | /* allows association of meta data with a request */ |
michael@0 | 120 | static void evrpc_hook_associate_meta(struct evrpc_hook_meta **pctx, |
michael@0 | 121 | struct evhttp_connection *evcon); |
michael@0 | 122 | |
michael@0 | 123 | /* creates a new meta data store */ |
michael@0 | 124 | static struct evrpc_hook_meta *evrpc_hook_meta_new(void); |
michael@0 | 125 | |
michael@0 | 126 | /* frees the meta data associated with a request */ |
michael@0 | 127 | static void evrpc_hook_context_free(struct evrpc_hook_meta *ctx); |
michael@0 | 128 | |
michael@0 | 129 | /* the server side of an rpc */ |
michael@0 | 130 | |
michael@0 | 131 | /* We alias the RPC specific structs to this voided one */ |
michael@0 | 132 | struct evrpc_req_generic { |
michael@0 | 133 | /* |
michael@0 | 134 | * allows association of meta data via hooks - needs to be |
michael@0 | 135 | * synchronized with evrpc_request_wrapper |
michael@0 | 136 | */ |
michael@0 | 137 | struct evrpc_hook_meta *hook_meta; |
michael@0 | 138 | |
michael@0 | 139 | /* the unmarshaled request object */ |
michael@0 | 140 | void *request; |
michael@0 | 141 | |
michael@0 | 142 | /* the empty reply object that needs to be filled in */ |
michael@0 | 143 | void *reply; |
michael@0 | 144 | |
michael@0 | 145 | /* |
michael@0 | 146 | * the static structure for this rpc; that can be used to |
michael@0 | 147 | * automatically unmarshal and marshal the http buffers. |
michael@0 | 148 | */ |
michael@0 | 149 | struct evrpc *rpc; |
michael@0 | 150 | |
michael@0 | 151 | /* |
michael@0 | 152 | * the http request structure on which we need to answer. |
michael@0 | 153 | */ |
michael@0 | 154 | struct evhttp_request* http_req; |
michael@0 | 155 | |
michael@0 | 156 | /* |
michael@0 | 157 | * Temporary data store for marshaled data |
michael@0 | 158 | */ |
michael@0 | 159 | struct evbuffer* rpc_data; |
michael@0 | 160 | }; |
michael@0 | 161 | |
michael@0 | 162 | /* the client side of an rpc request */ |
michael@0 | 163 | struct evrpc_request_wrapper { |
michael@0 | 164 | /* |
michael@0 | 165 | * allows association of meta data via hooks - needs to be |
michael@0 | 166 | * synchronized with evrpc_req_generic. |
michael@0 | 167 | */ |
michael@0 | 168 | struct evrpc_hook_meta *hook_meta; |
michael@0 | 169 | |
michael@0 | 170 | TAILQ_ENTRY(evrpc_request_wrapper) next; |
michael@0 | 171 | |
michael@0 | 172 | /* pool on which this rpc request is being made */ |
michael@0 | 173 | struct evrpc_pool *pool; |
michael@0 | 174 | |
michael@0 | 175 | /* connection on which the request is being sent */ |
michael@0 | 176 | struct evhttp_connection *evcon; |
michael@0 | 177 | |
michael@0 | 178 | /* the actual request */ |
michael@0 | 179 | struct evhttp_request *req; |
michael@0 | 180 | |
michael@0 | 181 | /* event for implementing request timeouts */ |
michael@0 | 182 | struct event ev_timeout; |
michael@0 | 183 | |
michael@0 | 184 | /* the name of the rpc */ |
michael@0 | 185 | char *name; |
michael@0 | 186 | |
michael@0 | 187 | /* callback */ |
michael@0 | 188 | void (*cb)(struct evrpc_status*, void *request, void *reply, void *arg); |
michael@0 | 189 | void *cb_arg; |
michael@0 | 190 | |
michael@0 | 191 | void *request; |
michael@0 | 192 | void *reply; |
michael@0 | 193 | |
michael@0 | 194 | /* unmarshals the buffer into the proper request structure */ |
michael@0 | 195 | void (*request_marshal)(struct evbuffer *, void *); |
michael@0 | 196 | |
michael@0 | 197 | /* removes all stored state in the reply */ |
michael@0 | 198 | void (*reply_clear)(void *); |
michael@0 | 199 | |
michael@0 | 200 | /* marshals the reply into a buffer */ |
michael@0 | 201 | int (*reply_unmarshal)(void *, struct evbuffer*); |
michael@0 | 202 | }; |
michael@0 | 203 | |
michael@0 | 204 | #endif /* _EVRPC_INTERNAL_H_ */ |