1*9034ec65Schristos /*	$NetBSD: evrpc-internal.h,v 1.6 2020/05/25 20:47:33 christos Exp $	*/
22b3787f6Schristos 
32b3787f6Schristos /*
42b3787f6Schristos  * Copyright (c) 2006-2007 Niels Provos <provos@citi.umich.edu>
52b3787f6Schristos  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
62b3787f6Schristos  *
72b3787f6Schristos  * Redistribution and use in source and binary forms, with or without
82b3787f6Schristos  * modification, are permitted provided that the following conditions
92b3787f6Schristos  * are met:
102b3787f6Schristos  * 1. Redistributions of source code must retain the above copyright
112b3787f6Schristos  *    notice, this list of conditions and the following disclaimer.
122b3787f6Schristos  * 2. Redistributions in binary form must reproduce the above copyright
132b3787f6Schristos  *    notice, this list of conditions and the following disclaimer in the
142b3787f6Schristos  *    documentation and/or other materials provided with the distribution.
152b3787f6Schristos  * 3. The name of the author may not be used to endorse or promote products
162b3787f6Schristos  *    derived from this software without specific prior written permission.
172b3787f6Schristos  *
182b3787f6Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
192b3787f6Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
202b3787f6Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
212b3787f6Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
222b3787f6Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
232b3787f6Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242b3787f6Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252b3787f6Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262b3787f6Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
272b3787f6Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282b3787f6Schristos  */
292b3787f6Schristos #ifndef EVRPC_INTERNAL_H_INCLUDED_
302b3787f6Schristos #define EVRPC_INTERNAL_H_INCLUDED_
312b3787f6Schristos 
3250cc4415Schristos #include "event2/http.h"
332b3787f6Schristos #include "http-internal.h"
342b3787f6Schristos 
352b3787f6Schristos struct evrpc;
362b3787f6Schristos struct evrpc_request_wrapper;
372b3787f6Schristos 
382b3787f6Schristos #define EVRPC_URI_PREFIX "/.rpc."
392b3787f6Schristos 
402b3787f6Schristos struct evrpc_hook {
412b3787f6Schristos 	TAILQ_ENTRY(evrpc_hook) next;
422b3787f6Schristos 
432b3787f6Schristos 	/* returns EVRPC_TERMINATE; if the rpc should be aborted.
442b3787f6Schristos 	 * a hook is is allowed to rewrite the evbuffer
452b3787f6Schristos 	 */
462b3787f6Schristos 	int (*process)(void *, struct evhttp_request *,
472b3787f6Schristos 	    struct evbuffer *, void *);
482b3787f6Schristos 	void *process_arg;
492b3787f6Schristos };
502b3787f6Schristos 
512b3787f6Schristos TAILQ_HEAD(evrpc_hook_list, evrpc_hook);
522b3787f6Schristos 
532b3787f6Schristos /*
542b3787f6Schristos  * this is shared between the base and the pool, so that we can reuse
552b3787f6Schristos  * the hook adding functions; we alias both evrpc_pool and evrpc_base
562b3787f6Schristos  * to this common structure.
572b3787f6Schristos  */
582b3787f6Schristos 
592b3787f6Schristos struct evrpc_hook_ctx;
602b3787f6Schristos TAILQ_HEAD(evrpc_pause_list, evrpc_hook_ctx);
612b3787f6Schristos 
622b3787f6Schristos struct evrpc_hooks_ {
632b3787f6Schristos 	/* hooks for processing outbound and inbound rpcs */
642b3787f6Schristos 	struct evrpc_hook_list in_hooks;
652b3787f6Schristos 	struct evrpc_hook_list out_hooks;
662b3787f6Schristos 
672b3787f6Schristos 	struct evrpc_pause_list pause_requests;
682b3787f6Schristos };
692b3787f6Schristos 
702b3787f6Schristos #define input_hooks common.in_hooks
712b3787f6Schristos #define output_hooks common.out_hooks
722b3787f6Schristos #define paused_requests common.pause_requests
732b3787f6Schristos 
742b3787f6Schristos struct evrpc_base {
752b3787f6Schristos 	struct evrpc_hooks_ common;
762b3787f6Schristos 
772b3787f6Schristos 	/* the HTTP server under which we register our RPC calls */
782b3787f6Schristos 	struct evhttp* http_server;
792b3787f6Schristos 
802b3787f6Schristos 	/* a list of all RPCs registered with us */
812b3787f6Schristos 	TAILQ_HEAD(evrpc_list, evrpc) registered_rpcs;
822b3787f6Schristos };
832b3787f6Schristos 
842b3787f6Schristos struct evrpc_req_generic;
852b3787f6Schristos void evrpc_reqstate_free_(struct evrpc_req_generic* rpc_state);
862b3787f6Schristos 
872b3787f6Schristos /* A pool for holding evhttp_connection objects */
882b3787f6Schristos struct evrpc_pool {
892b3787f6Schristos 	struct evrpc_hooks_ common;
902b3787f6Schristos 
912b3787f6Schristos 	struct event_base *base;
922b3787f6Schristos 
932b3787f6Schristos 	struct evconq connections;
942b3787f6Schristos 
952b3787f6Schristos 	int timeout;
962b3787f6Schristos 
972b3787f6Schristos 	TAILQ_HEAD(evrpc_requestq, evrpc_request_wrapper) (requests);
982b3787f6Schristos };
992b3787f6Schristos 
1002b3787f6Schristos struct evrpc_hook_ctx {
1012b3787f6Schristos 	TAILQ_ENTRY(evrpc_hook_ctx) next;
1022b3787f6Schristos 
1032b3787f6Schristos 	void *ctx;
1042b3787f6Schristos 	void (*cb)(void *, enum EVRPC_HOOK_RESULT);
1052b3787f6Schristos };
1062b3787f6Schristos 
1072b3787f6Schristos struct evrpc_meta {
1082b3787f6Schristos 	TAILQ_ENTRY(evrpc_meta) next;
1092b3787f6Schristos 	char *key;
1102b3787f6Schristos 
1112b3787f6Schristos 	void *data;
1122b3787f6Schristos 	size_t data_size;
1132b3787f6Schristos };
1142b3787f6Schristos 
1152b3787f6Schristos TAILQ_HEAD(evrpc_meta_list, evrpc_meta);
1162b3787f6Schristos 
1172b3787f6Schristos struct evrpc_hook_meta {
1182b3787f6Schristos 	struct evrpc_meta_list meta_data;
1192b3787f6Schristos 	struct evhttp_connection *evcon;
1202b3787f6Schristos };
1212b3787f6Schristos 
1222b3787f6Schristos /* allows association of meta data with a request */
1232b3787f6Schristos static void evrpc_hook_associate_meta_(struct evrpc_hook_meta **pctx,
1242b3787f6Schristos     struct evhttp_connection *evcon);
1252b3787f6Schristos 
1262b3787f6Schristos /* creates a new meta data store */
1272b3787f6Schristos static struct evrpc_hook_meta *evrpc_hook_meta_new_(void);
1282b3787f6Schristos 
1292b3787f6Schristos /* frees the meta data associated with a request */
1302b3787f6Schristos static void evrpc_hook_context_free_(struct evrpc_hook_meta *ctx);
1312b3787f6Schristos 
1322b3787f6Schristos /* the server side of an rpc */
1332b3787f6Schristos 
1342b3787f6Schristos /* We alias the RPC specific structs to this voided one */
1352b3787f6Schristos struct evrpc_req_generic {
1362b3787f6Schristos 	/*
1372b3787f6Schristos 	 * allows association of meta data via hooks - needs to be
1382b3787f6Schristos 	 * synchronized with evrpc_request_wrapper
1392b3787f6Schristos 	 */
1402b3787f6Schristos 	struct evrpc_hook_meta *hook_meta;
1412b3787f6Schristos 
1422b3787f6Schristos 	/* the unmarshaled request object */
1432b3787f6Schristos 	void *request;
1442b3787f6Schristos 
1452b3787f6Schristos 	/* the empty reply object that needs to be filled in */
1462b3787f6Schristos 	void *reply;
1472b3787f6Schristos 
1482b3787f6Schristos 	/*
1492b3787f6Schristos 	 * the static structure for this rpc; that can be used to
1502b3787f6Schristos 	 * automatically unmarshal and marshal the http buffers.
1512b3787f6Schristos 	 */
1522b3787f6Schristos 	struct evrpc *rpc;
1532b3787f6Schristos 
1542b3787f6Schristos 	/*
1552b3787f6Schristos 	 * the http request structure on which we need to answer.
1562b3787f6Schristos 	 */
1572b3787f6Schristos 	struct evhttp_request* http_req;
1582b3787f6Schristos 
1592b3787f6Schristos 	/*
1602b3787f6Schristos 	 * Temporary data store for marshaled data
1612b3787f6Schristos 	 */
1622b3787f6Schristos 	struct evbuffer* rpc_data;
1632b3787f6Schristos };
1642b3787f6Schristos 
1652b3787f6Schristos /* the client side of an rpc request */
1662b3787f6Schristos struct evrpc_request_wrapper {
1672b3787f6Schristos 	/*
1682b3787f6Schristos 	 * allows association of meta data via hooks - needs to be
1692b3787f6Schristos 	 * synchronized with evrpc_req_generic.
1702b3787f6Schristos 	 */
1712b3787f6Schristos 	struct evrpc_hook_meta *hook_meta;
1722b3787f6Schristos 
1732b3787f6Schristos 	TAILQ_ENTRY(evrpc_request_wrapper) next;
1742b3787f6Schristos 
1752b3787f6Schristos 	/* pool on which this rpc request is being made */
1762b3787f6Schristos 	struct evrpc_pool *pool;
1772b3787f6Schristos 
1782b3787f6Schristos 	/* connection on which the request is being sent */
1792b3787f6Schristos 	struct evhttp_connection *evcon;
1802b3787f6Schristos 
1812b3787f6Schristos 	/* the actual  request */
1822b3787f6Schristos 	struct evhttp_request *req;
1832b3787f6Schristos 
1842b3787f6Schristos 	/* event for implementing request timeouts */
1852b3787f6Schristos 	struct event ev_timeout;
1862b3787f6Schristos 
1872b3787f6Schristos 	/* the name of the rpc */
1882b3787f6Schristos 	char *name;
1892b3787f6Schristos 
1902b3787f6Schristos 	/* callback */
1912b3787f6Schristos 	void (*cb)(struct evrpc_status*, void *request, void *reply, void *arg);
1922b3787f6Schristos 	void *cb_arg;
1932b3787f6Schristos 
1942b3787f6Schristos 	void *request;
1952b3787f6Schristos 	void *reply;
1962b3787f6Schristos 
1972b3787f6Schristos 	/* unmarshals the buffer into the proper request structure */
1982b3787f6Schristos 	void (*request_marshal)(struct evbuffer *, void *);
1992b3787f6Schristos 
2002b3787f6Schristos 	/* removes all stored state in the reply */
2012b3787f6Schristos 	void (*reply_clear)(void *);
2022b3787f6Schristos 
2032b3787f6Schristos 	/* marshals the reply into a buffer */
2042b3787f6Schristos 	int (*reply_unmarshal)(void *, struct evbuffer*);
2052b3787f6Schristos };
2062b3787f6Schristos 
2072b3787f6Schristos #endif /* EVRPC_INTERNAL_H_INCLUDED_ */
208