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