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