xref: /libevent-2.1.12/evrpc-internal.h (revision 5e161c66)
1f554234fSNiels Provos /*
2b85b710cSNick Mathewson  * Copyright (c) 2006-2007 Niels Provos <[email protected]>
3e49e2891SNick Mathewson  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4f554234fSNiels Provos  *
5f554234fSNiels Provos  * Redistribution and use in source and binary forms, with or without
6f554234fSNiels Provos  * modification, are permitted provided that the following conditions
7f554234fSNiels Provos  * are met:
8f554234fSNiels Provos  * 1. Redistributions of source code must retain the above copyright
9f554234fSNiels Provos  *    notice, this list of conditions and the following disclaimer.
10f554234fSNiels Provos  * 2. Redistributions in binary form must reproduce the above copyright
11f554234fSNiels Provos  *    notice, this list of conditions and the following disclaimer in the
12f554234fSNiels Provos  *    documentation and/or other materials provided with the distribution.
13f554234fSNiels Provos  * 3. The name of the author may not be used to endorse or promote products
14f554234fSNiels Provos  *    derived from this software without specific prior written permission.
15f554234fSNiels Provos  *
16f554234fSNiels Provos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17f554234fSNiels Provos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18f554234fSNiels Provos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19f554234fSNiels Provos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20f554234fSNiels Provos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21f554234fSNiels Provos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22f554234fSNiels Provos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23f554234fSNiels Provos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24f554234fSNiels Provos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25f554234fSNiels Provos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26f554234fSNiels Provos  */
273f8c7cd0SNick Mathewson #ifndef EVRPC_INTERNAL_H_INCLUDED_
283f8c7cd0SNick Mathewson #define EVRPC_INTERNAL_H_INCLUDED_
29f554234fSNiels Provos 
30*5e161c66SNick Mathewson #include "event2/http.h"
31fda1216bSNiels Provos #include "http-internal.h"
32fda1216bSNiels Provos 
33f554234fSNiels Provos struct evrpc;
34aa4b9257SNiels Provos struct evrpc_request_wrapper;
35f554234fSNiels Provos 
3644bd5ab4SNiels Provos #define EVRPC_URI_PREFIX "/.rpc."
37f554234fSNiels Provos 
3865236aa8SNiels Provos struct evrpc_hook {
3960433a0aSGilad Benjamini 	TAILQ_ENTRY(evrpc_hook) next;
4065236aa8SNiels Provos 
415a5609c7SNiels Provos 	/* returns EVRPC_TERMINATE; if the rpc should be aborted.
425a5609c7SNiels Provos 	 * a hook is is allowed to rewrite the evbuffer
435a5609c7SNiels Provos 	 */
44819d4a33SNiels Provos 	int (*process)(void *, struct evhttp_request *,
45819d4a33SNiels Provos 	    struct evbuffer *, void *);
4665236aa8SNiels Provos 	void *process_arg;
4765236aa8SNiels Provos };
4865236aa8SNiels Provos 
491d3a008aSNiels Provos TAILQ_HEAD(evrpc_hook_list, evrpc_hook);
501d3a008aSNiels Provos 
511d3a008aSNiels Provos /*
521d3a008aSNiels Provos  * this is shared between the base and the pool, so that we can reuse
531d3a008aSNiels Provos  * the hook adding functions; we alias both evrpc_pool and evrpc_base
541d3a008aSNiels Provos  * to this common structure.
551d3a008aSNiels Provos  */
56819d4a33SNiels Provos 
57819d4a33SNiels Provos struct evrpc_hook_ctx;
58819d4a33SNiels Provos TAILQ_HEAD(evrpc_pause_list, evrpc_hook_ctx);
59819d4a33SNiels Provos 
60cb9da0bfSNick Mathewson struct evrpc_hooks_ {
611d3a008aSNiels Provos 	/* hooks for processing outbound and inbound rpcs */
621d3a008aSNiels Provos 	struct evrpc_hook_list in_hooks;
631d3a008aSNiels Provos 	struct evrpc_hook_list out_hooks;
64819d4a33SNiels Provos 
65819d4a33SNiels Provos 	struct evrpc_pause_list pause_requests;
661d3a008aSNiels Provos };
671d3a008aSNiels Provos 
681d3a008aSNiels Provos #define input_hooks common.in_hooks
691d3a008aSNiels Provos #define output_hooks common.out_hooks
70819d4a33SNiels Provos #define paused_requests common.pause_requests
711d3a008aSNiels Provos 
72f554234fSNiels Provos struct evrpc_base {
73cb9da0bfSNick Mathewson 	struct evrpc_hooks_ common;
741d3a008aSNiels Provos 
75f554234fSNiels Provos 	/* the HTTP server under which we register our RPC calls */
76f554234fSNiels Provos 	struct evhttp* http_server;
77f554234fSNiels Provos 
78f554234fSNiels Provos 	/* a list of all RPCs registered with us */
79f554234fSNiels Provos 	TAILQ_HEAD(evrpc_list, evrpc) registered_rpcs;
80f554234fSNiels Provos };
81f554234fSNiels Provos 
82f554234fSNiels Provos struct evrpc_req_generic;
838ac3c4c2SNick Mathewson void evrpc_reqstate_free_(struct evrpc_req_generic* rpc_state);
84f554234fSNiels Provos 
85fda1216bSNiels Provos /* A pool for holding evhttp_connection objects */
86fda1216bSNiels Provos struct evrpc_pool {
87cb9da0bfSNick Mathewson 	struct evrpc_hooks_ common;
881d3a008aSNiels Provos 
891d3a008aSNiels Provos 	struct event_base *base;
901d3a008aSNiels Provos 
91fda1216bSNiels Provos 	struct evconq connections;
92fda1216bSNiels Provos 
932d028ef6SNiels Provos 	int timeout;
942d028ef6SNiels Provos 
95819d4a33SNiels Provos 	TAILQ_HEAD(evrpc_requestq, evrpc_request_wrapper) (requests);
96fda1216bSNiels Provos };
97fda1216bSNiels Provos 
98819d4a33SNiels Provos struct evrpc_hook_ctx {
9960433a0aSGilad Benjamini 	TAILQ_ENTRY(evrpc_hook_ctx) next;
100819d4a33SNiels Provos 
101819d4a33SNiels Provos 	void *ctx;
102819d4a33SNiels Provos 	void (*cb)(void *, enum EVRPC_HOOK_RESULT);
103819d4a33SNiels Provos };
104fda1216bSNiels Provos 
1055a5609c7SNiels Provos struct evrpc_meta {
10660433a0aSGilad Benjamini 	TAILQ_ENTRY(evrpc_meta) next;
1075a5609c7SNiels Provos 	char *key;
1085a5609c7SNiels Provos 
1095a5609c7SNiels Provos 	void *data;
1105a5609c7SNiels Provos 	size_t data_size;
1115a5609c7SNiels Provos };
1125a5609c7SNiels Provos 
1135a5609c7SNiels Provos TAILQ_HEAD(evrpc_meta_list, evrpc_meta);
1145a5609c7SNiels Provos 
1152460aa59SNiels Provos struct evrpc_hook_meta {
1162460aa59SNiels Provos 	struct evrpc_meta_list meta_data;
1172460aa59SNiels Provos 	struct evhttp_connection *evcon;
1182460aa59SNiels Provos };
1192460aa59SNiels Provos 
1202460aa59SNiels Provos /* allows association of meta data with a request */
1218ac3c4c2SNick Mathewson static void evrpc_hook_associate_meta_(struct evrpc_hook_meta **pctx,
1222460aa59SNiels Provos     struct evhttp_connection *evcon);
1232460aa59SNiels Provos 
1242460aa59SNiels Provos /* creates a new meta data store */
1258ac3c4c2SNick Mathewson static struct evrpc_hook_meta *evrpc_hook_meta_new_(void);
1262460aa59SNiels Provos 
1275a5609c7SNiels Provos /* frees the meta data associated with a request */
1288ac3c4c2SNick Mathewson static void evrpc_hook_context_free_(struct evrpc_hook_meta *ctx);
1295a5609c7SNiels Provos 
130aa4b9257SNiels Provos /* the server side of an rpc */
131aa4b9257SNiels Provos 
132aa4b9257SNiels Provos /* We alias the RPC specific structs to this voided one */
133aa4b9257SNiels Provos struct evrpc_req_generic {
134aa4b9257SNiels Provos 	/*
135aa4b9257SNiels Provos 	 * allows association of meta data via hooks - needs to be
136aa4b9257SNiels Provos 	 * synchronized with evrpc_request_wrapper
137aa4b9257SNiels Provos 	 */
138aa4b9257SNiels Provos 	struct evrpc_hook_meta *hook_meta;
139aa4b9257SNiels Provos 
140aa4b9257SNiels Provos 	/* the unmarshaled request object */
141aa4b9257SNiels Provos 	void *request;
142aa4b9257SNiels Provos 
143aa4b9257SNiels Provos 	/* the empty reply object that needs to be filled in */
144aa4b9257SNiels Provos 	void *reply;
145aa4b9257SNiels Provos 
146aa4b9257SNiels Provos 	/*
147aa4b9257SNiels Provos 	 * the static structure for this rpc; that can be used to
148aa4b9257SNiels Provos 	 * automatically unmarshal and marshal the http buffers.
149aa4b9257SNiels Provos 	 */
150aa4b9257SNiels Provos 	struct evrpc *rpc;
151aa4b9257SNiels Provos 
152aa4b9257SNiels Provos 	/*
153aa4b9257SNiels Provos 	 * the http request structure on which we need to answer.
154aa4b9257SNiels Provos 	 */
155aa4b9257SNiels Provos 	struct evhttp_request* http_req;
156aa4b9257SNiels Provos 
157aa4b9257SNiels Provos 	/*
158aa4b9257SNiels Provos 	 * Temporary data store for marshaled data
159aa4b9257SNiels Provos 	 */
160aa4b9257SNiels Provos 	struct evbuffer* rpc_data;
161aa4b9257SNiels Provos };
162aa4b9257SNiels Provos 
163aa4b9257SNiels Provos /* the client side of an rpc request */
164aa4b9257SNiels Provos struct evrpc_request_wrapper {
165aa4b9257SNiels Provos 	/*
166aa4b9257SNiels Provos 	 * allows association of meta data via hooks - needs to be
167aa4b9257SNiels Provos 	 * synchronized with evrpc_req_generic.
168aa4b9257SNiels Provos 	 */
169aa4b9257SNiels Provos 	struct evrpc_hook_meta *hook_meta;
170aa4b9257SNiels Provos 
171aa4b9257SNiels Provos 	TAILQ_ENTRY(evrpc_request_wrapper) next;
172aa4b9257SNiels Provos 
173aa4b9257SNiels Provos 	/* pool on which this rpc request is being made */
174aa4b9257SNiels Provos 	struct evrpc_pool *pool;
175aa4b9257SNiels Provos 
176aa4b9257SNiels Provos 	/* connection on which the request is being sent */
177aa4b9257SNiels Provos 	struct evhttp_connection *evcon;
178aa4b9257SNiels Provos 
179aa4b9257SNiels Provos 	/* the actual  request */
180aa4b9257SNiels Provos 	struct evhttp_request *req;
181aa4b9257SNiels Provos 
182aa4b9257SNiels Provos 	/* event for implementing request timeouts */
183aa4b9257SNiels Provos 	struct event ev_timeout;
184aa4b9257SNiels Provos 
185aa4b9257SNiels Provos 	/* the name of the rpc */
186aa4b9257SNiels Provos 	char *name;
187aa4b9257SNiels Provos 
188aa4b9257SNiels Provos 	/* callback */
189aa4b9257SNiels Provos 	void (*cb)(struct evrpc_status*, void *request, void *reply, void *arg);
190aa4b9257SNiels Provos 	void *cb_arg;
191aa4b9257SNiels Provos 
192aa4b9257SNiels Provos 	void *request;
193aa4b9257SNiels Provos 	void *reply;
194aa4b9257SNiels Provos 
195aa4b9257SNiels Provos 	/* unmarshals the buffer into the proper request structure */
196aa4b9257SNiels Provos 	void (*request_marshal)(struct evbuffer *, void *);
197aa4b9257SNiels Provos 
198aa4b9257SNiels Provos 	/* removes all stored state in the reply */
199aa4b9257SNiels Provos 	void (*reply_clear)(void *);
200aa4b9257SNiels Provos 
201aa4b9257SNiels Provos 	/* marshals the reply into a buffer */
202aa4b9257SNiels Provos 	int (*reply_unmarshal)(void *, struct evbuffer*);
203aa4b9257SNiels Provos };
204aa4b9257SNiels Provos 
2053f8c7cd0SNick Mathewson #endif /* EVRPC_INTERNAL_H_INCLUDED_ */
206