1275970Scy/*
2275970Scy * Copyright (c) 2006-2007 Niels Provos <provos@citi.umich.edu>
3275970Scy * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4275970Scy *
5275970Scy * Redistribution and use in source and binary forms, with or without
6275970Scy * modification, are permitted provided that the following conditions
7275970Scy * are met:
8275970Scy * 1. Redistributions of source code must retain the above copyright
9275970Scy *    notice, this list of conditions and the following disclaimer.
10275970Scy * 2. Redistributions in binary form must reproduce the above copyright
11275970Scy *    notice, this list of conditions and the following disclaimer in the
12275970Scy *    documentation and/or other materials provided with the distribution.
13275970Scy * 3. The name of the author may not be used to endorse or promote products
14275970Scy *    derived from this software without specific prior written permission.
15275970Scy *
16275970Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17275970Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18275970Scy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19275970Scy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20275970Scy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21275970Scy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22275970Scy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23275970Scy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24275970Scy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25275970Scy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26275970Scy */
27275970Scy#ifndef EVRPC_INTERNAL_H_INCLUDED_
28275970Scy#define EVRPC_INTERNAL_H_INCLUDED_
29275970Scy
30282408Scy#include "event2/http.h"
31275970Scy#include "http-internal.h"
32275970Scy
33275970Scystruct evrpc;
34275970Scystruct evrpc_request_wrapper;
35275970Scy
36275970Scy#define EVRPC_URI_PREFIX "/.rpc."
37275970Scy
38275970Scystruct evrpc_hook {
39275970Scy	TAILQ_ENTRY(evrpc_hook) next;
40275970Scy
41275970Scy	/* returns EVRPC_TERMINATE; if the rpc should be aborted.
42275970Scy	 * a hook is is allowed to rewrite the evbuffer
43275970Scy	 */
44275970Scy	int (*process)(void *, struct evhttp_request *,
45275970Scy	    struct evbuffer *, void *);
46275970Scy	void *process_arg;
47275970Scy};
48275970Scy
49275970ScyTAILQ_HEAD(evrpc_hook_list, evrpc_hook);
50275970Scy
51275970Scy/*
52275970Scy * this is shared between the base and the pool, so that we can reuse
53275970Scy * the hook adding functions; we alias both evrpc_pool and evrpc_base
54275970Scy * to this common structure.
55275970Scy */
56275970Scy
57275970Scystruct evrpc_hook_ctx;
58275970ScyTAILQ_HEAD(evrpc_pause_list, evrpc_hook_ctx);
59275970Scy
60275970Scystruct evrpc_hooks_ {
61275970Scy	/* hooks for processing outbound and inbound rpcs */
62275970Scy	struct evrpc_hook_list in_hooks;
63275970Scy	struct evrpc_hook_list out_hooks;
64275970Scy
65275970Scy	struct evrpc_pause_list pause_requests;
66275970Scy};
67275970Scy
68275970Scy#define input_hooks common.in_hooks
69275970Scy#define output_hooks common.out_hooks
70275970Scy#define paused_requests common.pause_requests
71275970Scy
72275970Scystruct evrpc_base {
73275970Scy	struct evrpc_hooks_ common;
74275970Scy
75275970Scy	/* the HTTP server under which we register our RPC calls */
76275970Scy	struct evhttp* http_server;
77275970Scy
78275970Scy	/* a list of all RPCs registered with us */
79275970Scy	TAILQ_HEAD(evrpc_list, evrpc) registered_rpcs;
80275970Scy};
81275970Scy
82275970Scystruct evrpc_req_generic;
83275970Scyvoid evrpc_reqstate_free_(struct evrpc_req_generic* rpc_state);
84275970Scy
85275970Scy/* A pool for holding evhttp_connection objects */
86275970Scystruct evrpc_pool {
87275970Scy	struct evrpc_hooks_ common;
88275970Scy
89275970Scy	struct event_base *base;
90275970Scy
91275970Scy	struct evconq connections;
92275970Scy
93275970Scy	int timeout;
94275970Scy
95275970Scy	TAILQ_HEAD(evrpc_requestq, evrpc_request_wrapper) (requests);
96275970Scy};
97275970Scy
98275970Scystruct evrpc_hook_ctx {
99275970Scy	TAILQ_ENTRY(evrpc_hook_ctx) next;
100275970Scy
101275970Scy	void *ctx;
102275970Scy	void (*cb)(void *, enum EVRPC_HOOK_RESULT);
103275970Scy};
104275970Scy
105275970Scystruct evrpc_meta {
106275970Scy	TAILQ_ENTRY(evrpc_meta) next;
107275970Scy	char *key;
108275970Scy
109275970Scy	void *data;
110275970Scy	size_t data_size;
111275970Scy};
112275970Scy
113275970ScyTAILQ_HEAD(evrpc_meta_list, evrpc_meta);
114275970Scy
115275970Scystruct evrpc_hook_meta {
116275970Scy	struct evrpc_meta_list meta_data;
117275970Scy	struct evhttp_connection *evcon;
118275970Scy};
119275970Scy
120275970Scy/* allows association of meta data with a request */
121275970Scystatic void evrpc_hook_associate_meta_(struct evrpc_hook_meta **pctx,
122275970Scy    struct evhttp_connection *evcon);
123275970Scy
124275970Scy/* creates a new meta data store */
125275970Scystatic struct evrpc_hook_meta *evrpc_hook_meta_new_(void);
126275970Scy
127275970Scy/* frees the meta data associated with a request */
128275970Scystatic void evrpc_hook_context_free_(struct evrpc_hook_meta *ctx);
129275970Scy
130275970Scy/* the server side of an rpc */
131275970Scy
132275970Scy/* We alias the RPC specific structs to this voided one */
133275970Scystruct evrpc_req_generic {
134275970Scy	/*
135275970Scy	 * allows association of meta data via hooks - needs to be
136275970Scy	 * synchronized with evrpc_request_wrapper
137275970Scy	 */
138275970Scy	struct evrpc_hook_meta *hook_meta;
139275970Scy
140275970Scy	/* the unmarshaled request object */
141275970Scy	void *request;
142275970Scy
143275970Scy	/* the empty reply object that needs to be filled in */
144275970Scy	void *reply;
145275970Scy
146275970Scy	/*
147275970Scy	 * the static structure for this rpc; that can be used to
148275970Scy	 * automatically unmarshal and marshal the http buffers.
149275970Scy	 */
150275970Scy	struct evrpc *rpc;
151275970Scy
152275970Scy	/*
153275970Scy	 * the http request structure on which we need to answer.
154275970Scy	 */
155275970Scy	struct evhttp_request* http_req;
156275970Scy
157275970Scy	/*
158275970Scy	 * Temporary data store for marshaled data
159275970Scy	 */
160275970Scy	struct evbuffer* rpc_data;
161275970Scy};
162275970Scy
163275970Scy/* the client side of an rpc request */
164275970Scystruct evrpc_request_wrapper {
165275970Scy	/*
166275970Scy	 * allows association of meta data via hooks - needs to be
167275970Scy	 * synchronized with evrpc_req_generic.
168275970Scy	 */
169275970Scy	struct evrpc_hook_meta *hook_meta;
170275970Scy
171275970Scy	TAILQ_ENTRY(evrpc_request_wrapper) next;
172275970Scy
173275970Scy	/* pool on which this rpc request is being made */
174275970Scy	struct evrpc_pool *pool;
175275970Scy
176275970Scy	/* connection on which the request is being sent */
177275970Scy	struct evhttp_connection *evcon;
178275970Scy
179275970Scy	/* the actual  request */
180275970Scy	struct evhttp_request *req;
181275970Scy
182275970Scy	/* event for implementing request timeouts */
183275970Scy	struct event ev_timeout;
184275970Scy
185275970Scy	/* the name of the rpc */
186275970Scy	char *name;
187275970Scy
188275970Scy	/* callback */
189275970Scy	void (*cb)(struct evrpc_status*, void *request, void *reply, void *arg);
190275970Scy	void *cb_arg;
191275970Scy
192275970Scy	void *request;
193275970Scy	void *reply;
194275970Scy
195275970Scy	/* unmarshals the buffer into the proper request structure */
196275970Scy	void (*request_marshal)(struct evbuffer *, void *);
197275970Scy
198275970Scy	/* removes all stored state in the reply */
199275970Scy	void (*reply_clear)(void *);
200275970Scy
201275970Scy	/* marshals the reply into a buffer */
202275970Scy	int (*reply_unmarshal)(void *, struct evbuffer*);
203275970Scy};
204275970Scy
205275970Scy#endif /* EVRPC_INTERNAL_H_INCLUDED_ */
206