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