xref: /openbsd-src/usr.sbin/unbound/dynlibmod/dynlibmod.h (revision cddcdaaab2c5c53a2ffc263683f6e1f4aaa4a1f3)
1d7b4a113Ssthen /*
2d7b4a113Ssthen  * dynlibmod.h: module header file
3d7b4a113Ssthen  *
4d7b4a113Ssthen  * Copyright (c) 2019, Peter Munch-Ellingsen (peterme AT peterme.net)
5d7b4a113Ssthen  *
6d7b4a113Ssthen  * This software is open source.
7d7b4a113Ssthen  *
8d7b4a113Ssthen  * Redistribution and use in source and binary forms, with or without
9d7b4a113Ssthen  * modification, are permitted provided that the following conditions
10d7b4a113Ssthen  * are met:
11d7b4a113Ssthen  *
12d7b4a113Ssthen  *    * Redistributions of source code must retain the above copyright notice,
13d7b4a113Ssthen  *      this list of conditions and the following disclaimer.
14d7b4a113Ssthen  *
15d7b4a113Ssthen  *    * Redistributions in binary form must reproduce the above copyright notice,
16d7b4a113Ssthen  *      this list of conditions and the following disclaimer in the documentation
17d7b4a113Ssthen  *      and/or other materials provided with the distribution.
18d7b4a113Ssthen  *
19d7b4a113Ssthen  *    * Neither the name of the organization nor the names of its
20d7b4a113Ssthen  *      contributors may be used to endorse or promote products derived from this
21d7b4a113Ssthen  *      software without specific prior written permission.
22d7b4a113Ssthen  *
23d7b4a113Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24d7b4a113Ssthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25d7b4a113Ssthen  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26d7b4a113Ssthen  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27d7b4a113Ssthen  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28d7b4a113Ssthen  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29d7b4a113Ssthen  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30d7b4a113Ssthen  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31d7b4a113Ssthen  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32d7b4a113Ssthen  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33d7b4a113Ssthen  * POSSIBILITY OF SUCH DAMAGE.
34d7b4a113Ssthen  */
35d7b4a113Ssthen /**
36d7b4a113Ssthen  * \file
37d7b4a113Ssthen  * Dynamic loading module for unbound.  Loads dynamic library.
38d7b4a113Ssthen  */
39d7b4a113Ssthen #ifndef DYNLIBMOD_H
40d7b4a113Ssthen #define DYNLIBMOD_H
41d7b4a113Ssthen #include "util/module.h"
42d7b4a113Ssthen #include "services/outbound_list.h"
43d7b4a113Ssthen 
44d7b4a113Ssthen /**
45d7b4a113Ssthen  * Get the module function block.
46d7b4a113Ssthen  * @return: function block with function pointers to module methods.
47d7b4a113Ssthen  */
48d7b4a113Ssthen struct module_func_block* dynlibmod_get_funcblock(void);
49d7b4a113Ssthen 
50d7b4a113Ssthen /** dynlib module init */
51d7b4a113Ssthen int dynlibmod_init(struct module_env* env, int id);
52d7b4a113Ssthen 
53d7b4a113Ssthen /** dynlib module deinit */
54d7b4a113Ssthen void dynlibmod_deinit(struct module_env* env, int id);
55d7b4a113Ssthen 
56d7b4a113Ssthen /** dynlib module operate on a query */
57d7b4a113Ssthen void dynlibmod_operate(struct module_qstate* qstate, enum module_ev event,
58d7b4a113Ssthen 	int id, struct outbound_entry* outbound);
59d7b4a113Ssthen 
60d7b4a113Ssthen /** dynlib module  */
61d7b4a113Ssthen void dynlibmod_inform_super(struct module_qstate* qstate, int id,
62d7b4a113Ssthen 	struct module_qstate* super);
63d7b4a113Ssthen 
64d7b4a113Ssthen /** dynlib module cleanup query state */
65d7b4a113Ssthen void dynlibmod_clear(struct module_qstate* qstate, int id);
66d7b4a113Ssthen 
67d7b4a113Ssthen /** dynlib module alloc size routine */
68d7b4a113Ssthen size_t dynlibmod_get_mem(struct module_env* env, int id);
69d7b4a113Ssthen 
70d7b4a113Ssthen int dynlib_inplace_cb_reply_generic(struct query_info* qinfo,
71d7b4a113Ssthen     struct module_qstate* qstate, struct reply_info* rep, int rcode,
72d7b4a113Ssthen     struct edns_data* edns, struct edns_option** opt_list_out,
73*cddcdaaaSsthen     struct comm_reply* repinfo, struct regional* region,
74*cddcdaaaSsthen 	struct timeval* start_time, int id, void* callback);
75d7b4a113Ssthen 
76d7b4a113Ssthen int dynlib_inplace_cb_query_generic(struct query_info* qinfo, uint16_t flags,
77d7b4a113Ssthen     struct module_qstate* qstate, struct sockaddr_storage* addr,
78d7b4a113Ssthen     socklen_t addrlen, uint8_t* zone, size_t zonelen, struct regional* region,
79d7b4a113Ssthen     int id, void* callback);
80d7b4a113Ssthen 
81d7b4a113Ssthen int dynlib_inplace_cb_edns_back_parsed(struct module_qstate* qstate,
82d7b4a113Ssthen     int id, void* cb_args);
83d7b4a113Ssthen 
84d7b4a113Ssthen int dynlib_inplace_cb_query_response(struct module_qstate* qstate,
85d7b4a113Ssthen     struct dns_msg* response, int id, void* cb_args);
86d7b4a113Ssthen 
87d7b4a113Ssthen int
88d7b4a113Ssthen inplace_cb_register_wrapped(void* cb, enum inplace_cb_list_type type, void* cbarg,
89d7b4a113Ssthen   struct module_env* env, int id);
90d7b4a113Ssthen 
91d7b4a113Ssthen void
92d7b4a113Ssthen inplace_cb_delete_wrapped(struct module_env* env, enum inplace_cb_list_type type,
93d7b4a113Ssthen   int id);
94d7b4a113Ssthen 
95d7b4a113Ssthen struct cb_pair {
96d7b4a113Ssthen     void *cb;
97d7b4a113Ssthen     void *cb_arg;
98d7b4a113Ssthen };
99d7b4a113Ssthen 
100d7b4a113Ssthen /**
101d7b4a113Ssthen  * Global state for the module.
102d7b4a113Ssthen  */
103d7b4a113Ssthen 
104d7b4a113Ssthen typedef int (*func_init_t)(struct module_env*, int);
105d7b4a113Ssthen typedef void (*func_deinit_t)(struct module_env*, int);
106d7b4a113Ssthen typedef void (*func_operate_t)(struct module_qstate*, enum module_ev, int, struct outbound_entry*);
107d7b4a113Ssthen typedef void (*func_inform_t)(struct module_qstate*, int, struct module_qstate*);
108d7b4a113Ssthen typedef void (*func_clear_t)(struct module_qstate*, int);
109d7b4a113Ssthen typedef size_t (*func_get_mem_t)(struct module_env*, int);
110d7b4a113Ssthen typedef void (*inplace_cb_delete_wrapped_t)(struct module_env*, enum inplace_cb_list_type, int);
111d7b4a113Ssthen typedef int (*inplace_cb_register_wrapped_t)(void*, enum inplace_cb_list_type, void*, struct module_env*, int);
112d7b4a113Ssthen 
113d7b4a113Ssthen 
114d7b4a113Ssthen struct dynlibmod_env {
115d7b4a113Ssthen 	/** Dynamic library filename. */
116d7b4a113Ssthen 	const char* fname;
117d7b4a113Ssthen 	/** dynamic library handle */
118d7b4a113Ssthen 	void* dynamic_library;
119d7b4a113Ssthen 	/** Module init function */
120d7b4a113Ssthen 	func_init_t func_init;
121d7b4a113Ssthen 	/** Module deinit function */
122d7b4a113Ssthen 	func_deinit_t func_deinit;
123d7b4a113Ssthen 	/** Module operate function */
124d7b4a113Ssthen 	func_operate_t func_operate;
125d7b4a113Ssthen 	/** Module super_inform function */
126d7b4a113Ssthen 	func_inform_t func_inform;
127d7b4a113Ssthen 	/** Module clear function */
128d7b4a113Ssthen 	func_clear_t func_clear;
129d7b4a113Ssthen 	/** Module get_mem function */
130d7b4a113Ssthen 	func_get_mem_t func_get_mem;
131d7b4a113Ssthen   /** Wrapped inplace callback functions to circumvent callback whitelisting */
132d7b4a113Ssthen   inplace_cb_delete_wrapped_t inplace_cb_delete_wrapped;
133d7b4a113Ssthen   inplace_cb_register_wrapped_t inplace_cb_register_wrapped;
134d7b4a113Ssthen   /** Pointer to any data the dynamic library might want to keep */
135d7b4a113Ssthen   void *dyn_env;
136d7b4a113Ssthen };
137d7b4a113Ssthen 
138d7b4a113Ssthen 
139d7b4a113Ssthen #endif /* DYNLIBMOD_H */
140