xref: /freebsd/contrib/unbound/dynlibmod/dynlibmod.h (revision f44e67d120ad78ef7894241b519ee79fd190a16e)
125039b37SCy Schubert /*
225039b37SCy Schubert  * dynlibmod.h: module header file
325039b37SCy Schubert  *
425039b37SCy Schubert  * Copyright (c) 2019, Peter Munch-Ellingsen (peterme AT peterme.net)
525039b37SCy Schubert  *
625039b37SCy Schubert  * This software is open source.
725039b37SCy Schubert  *
825039b37SCy Schubert  * Redistribution and use in source and binary forms, with or without
925039b37SCy Schubert  * modification, are permitted provided that the following conditions
1025039b37SCy Schubert  * are met:
1125039b37SCy Schubert  *
1225039b37SCy Schubert  *    * Redistributions of source code must retain the above copyright notice,
1325039b37SCy Schubert  *      this list of conditions and the following disclaimer.
1425039b37SCy Schubert  *
1525039b37SCy Schubert  *    * Redistributions in binary form must reproduce the above copyright notice,
1625039b37SCy Schubert  *      this list of conditions and the following disclaimer in the documentation
1725039b37SCy Schubert  *      and/or other materials provided with the distribution.
1825039b37SCy Schubert  *
1925039b37SCy Schubert  *    * Neither the name of the organization nor the names of its
2025039b37SCy Schubert  *      contributors may be used to endorse or promote products derived from this
2125039b37SCy Schubert  *      software without specific prior written permission.
2225039b37SCy Schubert  *
2325039b37SCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2425039b37SCy Schubert  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2525039b37SCy Schubert  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2625039b37SCy Schubert  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
2725039b37SCy Schubert  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2825039b37SCy Schubert  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2925039b37SCy Schubert  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3025039b37SCy Schubert  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3125039b37SCy Schubert  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3225039b37SCy Schubert  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3325039b37SCy Schubert  * POSSIBILITY OF SUCH DAMAGE.
3425039b37SCy Schubert  */
3525039b37SCy Schubert /**
3625039b37SCy Schubert  * \file
3725039b37SCy Schubert  * Dynamic loading module for unbound.  Loads dynamic library.
3825039b37SCy Schubert  */
3925039b37SCy Schubert #ifndef DYNLIBMOD_H
4025039b37SCy Schubert #define DYNLIBMOD_H
4125039b37SCy Schubert #include "util/module.h"
4225039b37SCy Schubert #include "services/outbound_list.h"
4325039b37SCy Schubert 
4425039b37SCy Schubert /**
4525039b37SCy Schubert  * Get the module function block.
4625039b37SCy Schubert  * @return: function block with function pointers to module methods.
4725039b37SCy Schubert  */
4825039b37SCy Schubert struct module_func_block* dynlibmod_get_funcblock(void);
4925039b37SCy Schubert 
5025039b37SCy Schubert /** dynlib module init */
5125039b37SCy Schubert int dynlibmod_init(struct module_env* env, int id);
5225039b37SCy Schubert 
5325039b37SCy Schubert /** dynlib module deinit */
5425039b37SCy Schubert void dynlibmod_deinit(struct module_env* env, int id);
5525039b37SCy Schubert 
5625039b37SCy Schubert /** dynlib module operate on a query */
5725039b37SCy Schubert void dynlibmod_operate(struct module_qstate* qstate, enum module_ev event,
5825039b37SCy Schubert 	int id, struct outbound_entry* outbound);
5925039b37SCy Schubert 
6025039b37SCy Schubert /** dynlib module  */
6125039b37SCy Schubert void dynlibmod_inform_super(struct module_qstate* qstate, int id,
6225039b37SCy Schubert 	struct module_qstate* super);
6325039b37SCy Schubert 
6425039b37SCy Schubert /** dynlib module cleanup query state */
6525039b37SCy Schubert void dynlibmod_clear(struct module_qstate* qstate, int id);
6625039b37SCy Schubert 
6725039b37SCy Schubert /** dynlib module alloc size routine */
6825039b37SCy Schubert size_t dynlibmod_get_mem(struct module_env* env, int id);
6925039b37SCy Schubert 
7025039b37SCy Schubert int dynlib_inplace_cb_reply_generic(struct query_info* qinfo,
7125039b37SCy Schubert     struct module_qstate* qstate, struct reply_info* rep, int rcode,
7225039b37SCy Schubert     struct edns_data* edns, struct edns_option** opt_list_out,
73*f44e67d1SCy Schubert     struct comm_reply* repinfo, struct regional* region,
74*f44e67d1SCy Schubert 	struct timeval* start_time, int id, void* callback);
7525039b37SCy Schubert 
7625039b37SCy Schubert int dynlib_inplace_cb_query_generic(struct query_info* qinfo, uint16_t flags,
7725039b37SCy Schubert     struct module_qstate* qstate, struct sockaddr_storage* addr,
7825039b37SCy Schubert     socklen_t addrlen, uint8_t* zone, size_t zonelen, struct regional* region,
7925039b37SCy Schubert     int id, void* callback);
8025039b37SCy Schubert 
8125039b37SCy Schubert int dynlib_inplace_cb_edns_back_parsed(struct module_qstate* qstate,
8225039b37SCy Schubert     int id, void* cb_args);
8325039b37SCy Schubert 
8425039b37SCy Schubert int dynlib_inplace_cb_query_response(struct module_qstate* qstate,
8525039b37SCy Schubert     struct dns_msg* response, int id, void* cb_args);
8625039b37SCy Schubert 
8725039b37SCy Schubert int
8825039b37SCy Schubert inplace_cb_register_wrapped(void* cb, enum inplace_cb_list_type type, void* cbarg,
8925039b37SCy Schubert   struct module_env* env, int id);
9025039b37SCy Schubert 
9125039b37SCy Schubert void
9225039b37SCy Schubert inplace_cb_delete_wrapped(struct module_env* env, enum inplace_cb_list_type type,
9325039b37SCy Schubert   int id);
9425039b37SCy Schubert 
9525039b37SCy Schubert struct cb_pair {
9625039b37SCy Schubert     void *cb;
9725039b37SCy Schubert     void *cb_arg;
9825039b37SCy Schubert };
9925039b37SCy Schubert 
10025039b37SCy Schubert /**
10125039b37SCy Schubert  * Global state for the module.
10225039b37SCy Schubert  */
10325039b37SCy Schubert 
10425039b37SCy Schubert typedef int (*func_init_t)(struct module_env*, int);
10525039b37SCy Schubert typedef void (*func_deinit_t)(struct module_env*, int);
10625039b37SCy Schubert typedef void (*func_operate_t)(struct module_qstate*, enum module_ev, int, struct outbound_entry*);
10725039b37SCy Schubert typedef void (*func_inform_t)(struct module_qstate*, int, struct module_qstate*);
10825039b37SCy Schubert typedef void (*func_clear_t)(struct module_qstate*, int);
10925039b37SCy Schubert typedef size_t (*func_get_mem_t)(struct module_env*, int);
11025039b37SCy Schubert typedef void (*inplace_cb_delete_wrapped_t)(struct module_env*, enum inplace_cb_list_type, int);
11125039b37SCy Schubert typedef int (*inplace_cb_register_wrapped_t)(void*, enum inplace_cb_list_type, void*, struct module_env*, int);
11225039b37SCy Schubert 
11325039b37SCy Schubert 
11425039b37SCy Schubert struct dynlibmod_env {
11525039b37SCy Schubert 	/** Dynamic library filename. */
11625039b37SCy Schubert 	const char* fname;
11725039b37SCy Schubert 	/** dynamic library handle */
11825039b37SCy Schubert 	void* dynamic_library;
11925039b37SCy Schubert 	/** Module init function */
12025039b37SCy Schubert 	func_init_t func_init;
12125039b37SCy Schubert 	/** Module deinit function */
12225039b37SCy Schubert 	func_deinit_t func_deinit;
12325039b37SCy Schubert 	/** Module operate function */
12425039b37SCy Schubert 	func_operate_t func_operate;
12525039b37SCy Schubert 	/** Module super_inform function */
12625039b37SCy Schubert 	func_inform_t func_inform;
12725039b37SCy Schubert 	/** Module clear function */
12825039b37SCy Schubert 	func_clear_t func_clear;
12925039b37SCy Schubert 	/** Module get_mem function */
13025039b37SCy Schubert 	func_get_mem_t func_get_mem;
13125039b37SCy Schubert   /** Wrapped inplace callback functions to circumvent callback whitelisting */
13225039b37SCy Schubert   inplace_cb_delete_wrapped_t inplace_cb_delete_wrapped;
13325039b37SCy Schubert   inplace_cb_register_wrapped_t inplace_cb_register_wrapped;
13425039b37SCy Schubert   /** Pointer to any data the dynamic library might want to keep */
13525039b37SCy Schubert   void *dyn_env;
13625039b37SCy Schubert };
13725039b37SCy Schubert 
13825039b37SCy Schubert 
13925039b37SCy Schubert #endif /* DYNLIBMOD_H */
140