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