xref: /openbsd-src/usr.sbin/unbound/dynlibmod/examples/helloworld.c (revision cddcdaaab2c5c53a2ffc263683f6e1f4aaa4a1f3)
1d7b4a113Ssthen /**
2d7b4a113Ssthen  * \file
3d7b4a113Ssthen  *
4d7b4a113Ssthen  * This is an example to show how dynamic libraries can be made to work with
5d7b4a113Ssthen  * unbound. To build a .so file simply run:
6d7b4a113Ssthen  *   gcc -I../.. -shared -Wall -Werror -fpic  -o helloworld.so helloworld.c
7d7b4a113Ssthen  * And to build for windows, first make unbound with the --with-dynlibmod
8d7b4a113Ssthen  * switch, then use this command:
9d7b4a113Ssthen  *   x86_64-w64-mingw32-gcc -m64 -I../.. -shared -Wall -Werror -fpic
10*cddcdaaaSsthen  *      -o helloworld.dll helloworld.c -L../.. -l:libunbound.dll.a
11*cddcdaaaSsthen  * to cross-compile a 64-bit Windows DLL.  The libunbound.dll.a is produced
12*cddcdaaaSsthen  * by the compile step that makes unbound.exe and allows the dynlib dll to
13*cddcdaaaSsthen  * access definitions in unbound.exe.
14d7b4a113Ssthen  */
15d7b4a113Ssthen 
16d7b4a113Ssthen #include "../../config.h"
17d7b4a113Ssthen #include "../../util/module.h"
18d7b4a113Ssthen #include "../../sldns/parseutil.h"
19d7b4a113Ssthen #include "../dynlibmod.h"
20d7b4a113Ssthen 
21d7b4a113Ssthen /* Declare the EXPORT macro that expands to exporting the symbol for DLLs when
22d7b4a113Ssthen  * compiling for Windows. All procedures marked with EXPORT in this example are
23d7b4a113Ssthen  * called directly by the dynlib module and must be present for the module to
24d7b4a113Ssthen  * load correctly. */
25d7b4a113Ssthen #ifdef HAVE_WINDOWS_H
26d7b4a113Ssthen #define EXPORT __declspec(dllexport)
27d7b4a113Ssthen #else
28d7b4a113Ssthen #define EXPORT
29d7b4a113Ssthen #endif
30d7b4a113Ssthen 
31d7b4a113Ssthen /* Forward declare a callback, implemented at the bottom of this file */
32d7b4a113Ssthen int reply_callback(struct query_info* qinfo,
33d7b4a113Ssthen     struct module_qstate* qstate, struct reply_info* rep, int rcode,
34d7b4a113Ssthen     struct edns_data* edns, struct edns_option** opt_list_out,
35*cddcdaaaSsthen     struct comm_reply* repinfo, struct regional* region,
36*cddcdaaaSsthen     struct timeval* start_time, int id, void* callback);
37d7b4a113Ssthen 
38d7b4a113Ssthen /* Init is called when the module is first loaded. It should be used to set up
39d7b4a113Ssthen  * the environment for this module and do any other initialisation required. */
init(struct module_env * env,int id)40d7b4a113Ssthen EXPORT int init(struct module_env* env, int id) {
41d7b4a113Ssthen     log_info("dynlib: hello world from init");
42d7b4a113Ssthen     struct dynlibmod_env* de = (struct dynlibmod_env*) env->modinfo[id];
43d7b4a113Ssthen     de->inplace_cb_register_wrapped(&reply_callback,
44d7b4a113Ssthen                                     inplace_cb_reply,
45d7b4a113Ssthen                                     NULL, env, id);
46d7b4a113Ssthen     struct dynlibmod_env* local_env = env->modinfo[id];
47d7b4a113Ssthen     local_env->dyn_env = NULL;
48d7b4a113Ssthen     return 1;
49d7b4a113Ssthen }
50d7b4a113Ssthen 
51d7b4a113Ssthen /* Deinit is run as the program is shutting down. It should be used to clean up
52d7b4a113Ssthen  * the environment and any left over data. */
deinit(struct module_env * env,int id)53d7b4a113Ssthen EXPORT void deinit(struct module_env* env, int id) {
54d7b4a113Ssthen     log_info("dynlib: hello world from deinit");
55d7b4a113Ssthen     struct dynlibmod_env* de = (struct dynlibmod_env*) env->modinfo[id];
56d7b4a113Ssthen     de->inplace_cb_delete_wrapped(env, inplace_cb_reply, id);
57d7b4a113Ssthen     if (de->dyn_env != NULL) free(de->dyn_env);
58d7b4a113Ssthen }
59d7b4a113Ssthen 
60d7b4a113Ssthen /* Operate is called every time a query passes by this module. The event can be
61d7b4a113Ssthen  * used to determine which direction in the module chain it came from. */
operate(struct module_qstate * qstate,enum module_ev event,int id,struct outbound_entry * entry)62d7b4a113Ssthen EXPORT void operate(struct module_qstate* qstate, enum module_ev event,
63d7b4a113Ssthen                     int id, struct outbound_entry* entry) {
64d7b4a113Ssthen     log_info("dynlib: hello world from operate");
65d7b4a113Ssthen     log_info("dynlib: incoming query: %s %s(%d) %s(%d)",
66d7b4a113Ssthen             qstate->qinfo.qname,
67d7b4a113Ssthen             sldns_lookup_by_id(sldns_rr_classes, qstate->qinfo.qclass)->name,
68d7b4a113Ssthen             qstate->qinfo.qclass,
69d7b4a113Ssthen             sldns_rr_descript(qstate->qinfo.qtype)->_name,
70d7b4a113Ssthen             qstate->qinfo.qtype);
71d7b4a113Ssthen     if (event == module_event_new || event == module_event_pass) {
72d7b4a113Ssthen         qstate->ext_state[id] = module_wait_module;
73d7b4a113Ssthen         struct dynlibmod_env* env = qstate->env->modinfo[id];
74d7b4a113Ssthen         if (env->dyn_env == NULL) {
75d7b4a113Ssthen             env->dyn_env = calloc(3, sizeof(int));
76d7b4a113Ssthen             ((int *)env->dyn_env)[0] = 42;
77d7b4a113Ssthen             ((int *)env->dyn_env)[1] = 102;
78d7b4a113Ssthen             ((int *)env->dyn_env)[2] = 192;
79d7b4a113Ssthen         } else {
80d7b4a113Ssthen             log_err("dynlib: already has data!");
81d7b4a113Ssthen             qstate->ext_state[id] = module_error;
82d7b4a113Ssthen         }
83d7b4a113Ssthen     } else if (event == module_event_moddone) {
84d7b4a113Ssthen         qstate->ext_state[id] = module_finished;
85d7b4a113Ssthen     } else {
86d7b4a113Ssthen         qstate->ext_state[id] = module_error;
87d7b4a113Ssthen     }
88d7b4a113Ssthen }
89d7b4a113Ssthen 
90d7b4a113Ssthen /* Inform super is called when a query is completed or errors out, but only if
91d7b4a113Ssthen  * a sub-query has been registered to it by this module. Look at
92d7b4a113Ssthen  * mesh_attach_sub in services/mesh.h to see how this is done. */
inform_super(struct module_qstate * qstate,int id,struct module_qstate * super)93d7b4a113Ssthen EXPORT void inform_super(struct module_qstate* qstate, int id,
94d7b4a113Ssthen                          struct module_qstate* super) {
95d7b4a113Ssthen     log_info("dynlib: hello world from inform_super");
96d7b4a113Ssthen }
97d7b4a113Ssthen 
98d7b4a113Ssthen /* Clear is called once a query is complete and the response has been sent
99d7b4a113Ssthen  * back. It is used to clear up any per-query allocations. */
clear(struct module_qstate * qstate,int id)100d7b4a113Ssthen EXPORT void clear(struct module_qstate* qstate, int id) {
101d7b4a113Ssthen     log_info("dynlib: hello world from clear");
102d7b4a113Ssthen     struct dynlibmod_env* env = qstate->env->modinfo[id];
103d7b4a113Ssthen     if (env->dyn_env != NULL) {
104d7b4a113Ssthen         free(env->dyn_env);
105d7b4a113Ssthen         env->dyn_env = NULL;
106d7b4a113Ssthen     }
107d7b4a113Ssthen }
108d7b4a113Ssthen 
109d7b4a113Ssthen /* Get mem is called when Unbound is printing performance information. This
110d7b4a113Ssthen  * only happens explicitly and is only used to show memory usage to the user. */
get_mem(struct module_env * env,int id)111d7b4a113Ssthen EXPORT size_t get_mem(struct module_env* env, int id) {
112d7b4a113Ssthen     log_info("dynlib: hello world from get_mem");
113d7b4a113Ssthen     return 0;
114d7b4a113Ssthen }
115d7b4a113Ssthen 
116d7b4a113Ssthen /* The callback that was forward declared earlier. It is registered in the init
117d7b4a113Ssthen  * procedure to run when a query is being replied to. */
reply_callback(struct query_info * qinfo,struct module_qstate * qstate,struct reply_info * rep,int rcode,struct edns_data * edns,struct edns_option ** opt_list_out,struct comm_reply * repinfo,struct regional * region,struct timeval * start_time,int id,void * callback)118d7b4a113Ssthen int reply_callback(struct query_info* qinfo,
119d7b4a113Ssthen     struct module_qstate* qstate, struct reply_info* rep, int rcode,
120d7b4a113Ssthen     struct edns_data* edns, struct edns_option** opt_list_out,
121*cddcdaaaSsthen     struct comm_reply* repinfo, struct regional* region,
122*cddcdaaaSsthen     struct timeval* start_time, int id, void* callback) {
123d7b4a113Ssthen     log_info("dynlib: hello world from callback");
124d7b4a113Ssthen     struct dynlibmod_env* env = qstate->env->modinfo[id];
125d7b4a113Ssthen     if (env->dyn_env != NULL) {
126d7b4a113Ssthen         log_info("dynlib: numbers gotten from query: %d, %d, and %d",
127d7b4a113Ssthen             ((int *)env->dyn_env)[0],
128d7b4a113Ssthen             ((int *)env->dyn_env)[1],
129d7b4a113Ssthen             ((int *)env->dyn_env)[2]);
130d7b4a113Ssthen     }
131d7b4a113Ssthen     return 0;
132d7b4a113Ssthen }
133