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