From 7d91d6345f5ab0766f1bf6cda1634bb552a47845 Mon Sep 17 00:00:00 2001 From: Yorgos Thessalonikefs Date: Tue, 20 Oct 2026 16:51:37 +0200 Subject: [PATCH] - Terminate inplace callback progression if one of them errors out; suggestion by Rio Fujita. --- doc/Changelog | 4 +- pythonmod/doc/examples/example6.rst | 127 +++++++++++++++++++++++- pythonmod/examples/inplace_callbacks.py | 38 +++++-- util/data/msgreply.c | 25 +++-- util/data/msgreply.h | 23 +++-- 5 files changed, 189 insertions(+), 28 deletions(-) diff --git a/doc/Changelog b/doc/Changelog index 54a77a41f..3160bab9c 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -1,6 +1,6 @@ 22 October 2026: Willem - Merge #1539: Send along ETag with auth zone xfrs over http(s), - so unbound will not fetch the whole zone every refresh time, + so Unbound will not fetch the whole zone every refresh time, but only when it has changed. 20 October 2026: Wouter @@ -13,6 +13,8 @@ - Add CONTRIBUTING.md, similar to other NLnet Labs projects. - Merge #1536: Better documentation and warning message for no configured trust anchors. + - Terminate inplace callback progression if one of them errors out; + suggestion by Rio Fujita. 19 October 2026: Wouter - Merge #1528 from Ar-maan05: Fix NSEC at apex detection for diff --git a/pythonmod/doc/examples/example6.rst b/pythonmod/doc/examples/example6.rst index fd6caf74d..18b92ab05 100644 --- a/pythonmod/doc/examples/example6.rst +++ b/pythonmod/doc/examples/example6.rst @@ -23,12 +23,15 @@ This example relies on the following functionalities: Registering inplace callback functions -------------------------------------- -There are four types of inplace callback functions: +There are seven types of inplace callback functions: - `inplace callback reply functions`_ - `inplace callback reply_cache functions`_ - `inplace callback reply_local functions`_ - `inplace callback reply_servfail functions`_ +- `inplace callback query functions`_ +- `inplace callback query_response functions`_ +- `inplace callback edns_back_parsed functions`_ Inplace callback reply functions @@ -61,7 +64,9 @@ The callback function's prototype is the following: release. Current parameters: ``repinfo``: Reply information for a communication point (comm_reply). - :return: True on success, False on failure. + :return: True on success, False on failure. Failure terminates any further + callback processing. If this module is ephemeral it is advised + to clean up and return success instead. """ @@ -105,7 +110,9 @@ The callback function's prototype is the following: release. Current parameters: ``repinfo``: Reply information for a communication point (comm_reply). - :return: True on success, False on failure. + :return: True on success, False on failure. Failure terminates any further + callback processing. If this module is ephemeral it is advised + to clean up and return success instead. For demonstration purposes we want to see if EDNS option 65002 is present and reply with a new value. @@ -152,7 +159,9 @@ The callback function's prototype is the following: release. Current parameters: ``repinfo``: Reply information for a communication point (comm_reply). - :return: True on success, False on failure. + :return: True on success, False on failure. Failure terminates any further + callback processing. If this module is ephemeral it is advised + to clean up and return success instead. """ @@ -197,7 +206,9 @@ The callback function's prototype is the following: release. Current parameters: ``repinfo``: Reply information for a communication point (comm_reply). - :return: True on success, False on failure. + :return: True on success, False on failure. Failure terminates any further + callback processing. If this module is ephemeral it is advised + to clean up and return success instead. For demonstration purposes we want to reply with an empty EDNS code '65003' and log the IP address(es) of the client(s). @@ -214,6 +225,112 @@ We can register such function as: log_info("python: Could not register inplace callback function.") +Inplace callback query functions +................................ + +Called before sending a query to a backend server. + +The callback function's prototype is the following: + +.. code-block:: python + + def inplace_query_callback(qinfo, flags, qstate, addr, zone, region, **kwargs): + """ + Function that will be registered as an inplace callback function. + It will be called before sending a query to a backend server. + + :param qinfo: query_info struct; + :param flags: flags of the query; + :param qstate: module qstate. opt_lists are available here; + :param addr: struct sockaddr_storage. Address of the backend server; + :param zone: zone name in binary; + :param region: region to allocate temporary data. Needs to be used when we + want to append a new option to opt_lists. + :param **kwargs: Dictionary that may contain parameters added in a future + release. + + :return: True on success, False on failure. Failure terminates any further + callback processing. If this module is ephemeral it is advised + to clean up and return success instead. + """ + +.. note:: The function's name is irrelevant. + +We can register such function as: + +.. code-block:: python + + if not register_inplace_cb_query(inplace_query_callback, env, id): + log_info("python: Could not register inplace callback function.") + + +Inplace callback query_response functions +......................................... + +Called after receiving a reply from a backend server. + +The callback function's prototype is the following: + +.. code-block:: python + + def inplace_query_response_callback(qstate, response, **kwargs): + """ + Function that will be registered as an inplace callback function. + It will be called after receiving a reply from a backend server. + + :param qstate: module qstate. opt_lists are available here; + :param response: struct dns_msg. The reply received from the backend server; + :param **kwargs: Dictionary that may contain parameters added in a future + release. + + :return: True on success, False on failure. Failure terminates any further + callback processing. If this module is ephemeral it is advised + to clean up and return success instead. + """ + +.. note:: The function's name is irrelevant. + +We can register such function as: + +.. code-block:: python + + if not register_inplace_cb_query_response(inplace_query_response_callback, env, id): + log_info("python: Could not register inplace callback function.") + + +Inplace callback edns_back_parsed functions +........................................... + +Called after EDNS is parsed on a reply from a backend server. + +The callback function's prototype is the following: + +.. code-block:: python + + def inplace_edns_back_parsed_call(qstate, **kwargs): + """ + Function that will be registered as an inplace callback function. + It will be called after EDNS is parsed on a reply from a backend server. + + :param qstate: module qstate. opt_lists are available here; + :param **kwargs: Dictionary that may contain parameters added in a future + release. + + :return: True on success, False on failure. Failure terminates any further + callback processing. If this module is ephemeral it is advised + to clean up and return success instead. + """ + +.. note:: The function's name is irrelevant. + +We can register such function as: + +.. code-block:: python + + if not register_inplace_cb_edns_back_parsed_call(inplace_edns_back_parsed_call, env, id): + log_info("python: Could not register inplace callback function.") + + Testing ~~~~~~~ diff --git a/pythonmod/examples/inplace_callbacks.py b/pythonmod/examples/inplace_callbacks.py index 42806daa1..d2a0cbc36 100644 --- a/pythonmod/examples/inplace_callbacks.py +++ b/pythonmod/examples/inplace_callbacks.py @@ -95,7 +95,9 @@ def inplace_reply_callback(qinfo, qstate, rep, rcode, edns, opt_list_out, release. Current parameters: ``repinfo``: Reply information for a communication point (comm_reply). - :return: True on success, False on failure. + :return: True on success, False on failure. Failure terminates any further + callback processing. If this module is ephemeral it is advised + to clean up and return success instead. """ log_info("python: called back while replying.") @@ -123,7 +125,9 @@ def inplace_cache_callback(qinfo, qstate, rep, rcode, edns, opt_list_out, release. Current parameters: ``repinfo``: Reply information for a communication point (comm_reply). - :return: True on success, False on failure. + :return: True on success, False on failure. Failure terminates any further + callback processing. If this module is ephemeral it is advised + to clean up and return success instead. For demonstration purposes we want to see if EDNS option 65002 is present and reply with a new value. @@ -147,6 +151,12 @@ def inplace_cache_callback(qinfo, qstate, rep, rcode, edns, opt_list_out, else: log_info("python: *failed to add new option code 65002*") return False + # If replying with another code is not crucial for operation + # we can soft fail instead and allow further callback + # processing by Unbound. + # else: + # log_info("python: *failed to add new option code 65002*") + # return True break return True @@ -173,7 +183,9 @@ def inplace_local_callback(qinfo, qstate, rep, rcode, edns, opt_list_out, release. Current parameters: ``repinfo``: Reply information for a communication point (comm_reply). - :return: True on success, False on failure. + :return: True on success, False on failure. Failure terminates any further + callback processing. If this module is ephemeral it is advised + to clean up and return success instead. """ log_info("python: called back while replying with local data or chaos" @@ -203,7 +215,9 @@ def inplace_servfail_callback(qinfo, qstate, rep, rcode, edns, opt_list_out, release. Current parameters: ``repinfo``: Reply information for a communication point (comm_reply). - :return: True on success, False on failure. + :return: True on success, False on failure. Failure terminates any further + callback processing. If this module is ephemeral it is advised + to clean up and return success instead. For demonstration purposes we want to reply with an empty EDNS code '65003' and log the IP address of the client. @@ -240,6 +254,10 @@ def inplace_query_callback(qinfo, flags, qstate, addr, zone, region, **kwargs): want to append a new option to opt_lists. :param **kwargs: Dictionary that may contain parameters added in a future release. + + :return: True on success, False on failure. Failure terminates any further + callback processing. If this module is ephemeral it is advised + to clean up and return success instead. """ log_info("python: outgoing query to {}@{}".format(addr.addr, addr.port)) return True @@ -254,6 +272,10 @@ def inplace_query_response_callback(qstate, response, **kwargs): :param response: struct dns_msg. The reply received from the backend server; :param **kwargs: Dictionary that may contain parameters added in a future release. + + :return: True on success, False on failure. Failure terminates any further + callback processing. If this module is ephemeral it is advised + to clean up and return success instead. """ log_dns_msg( "python: incoming reply from {}{}".format(qstate.reply.addr, os.linesep), @@ -265,11 +287,15 @@ def inplace_query_response_callback(qstate, response, **kwargs): def inplace_edns_back_parsed_call(qstate, **kwargs): """ Function that will be registered as an inplace callback function. - It will be called after EDNS is parsed on a reply from a backend server.. + It will be called after EDNS is parsed on a reply from a backend server. :param qstate: module qstate. opt_lists are available here; :param **kwargs: Dictionary that may contain parameters added in a future release. + + :return: True on success, False on failure. Failure terminates any further + callback processing. If this module is ephemeral it is advised + to clean up and return success instead. """ log_info("python: edns parsed") return True @@ -348,7 +374,7 @@ def inform_super(id, qstate, superqstate, qdata): return True def operate(id, event, qstate, qdata): if (event == MODULE_EVENT_NEW) or (event == MODULE_EVENT_PASS): - qstate.ext_state[id] = MODULE_WAIT_MODULE + qstate.ext_state[id] = MODULE_WAIT_MODULE return True elif event == MODULE_EVENT_MODDONE: diff --git a/util/data/msgreply.c b/util/data/msgreply.c index 86193a671..b5315ff72 100644 --- a/util/data/msgreply.c +++ b/util/data/msgreply.c @@ -1287,8 +1287,11 @@ static int inplace_cb_reply_call_generic( for(cb=callback_list; cb; cb=cb->next) { fptr_ok(fptr_whitelist_inplace_cb_reply_generic( (inplace_cb_reply_func_type*)cb->cb, type)); - (void)(*(inplace_cb_reply_func_type*)cb->cb)(qinfo, qstate, rep, - rcode, edns, &opt_list_out, repinfo, region, start_time, cb->id, cb->cb_arg); + if(!(*(inplace_cb_reply_func_type*)cb->cb)(qinfo, qstate, rep, + rcode, edns, &opt_list_out, repinfo, region, start_time, cb->id, cb->cb_arg)) { + edns->opt_list_inplace_cb_out = NULL; + return 0; + } } edns->opt_list_inplace_cb_out = opt_list_out; return 1; @@ -1350,9 +1353,11 @@ int inplace_cb_query_call(struct module_env* env, struct query_info* qinfo, for(; cb; cb=cb->next) { fptr_ok(fptr_whitelist_inplace_cb_query( (inplace_cb_query_func_type*)cb->cb)); - (void)(*(inplace_cb_query_func_type*)cb->cb)(qinfo, flags, + if(!(*(inplace_cb_query_func_type*)cb->cb)(qinfo, flags, qstate, addr, addrlen, zone, zonelen, region, - cb->id, cb->cb_arg); + cb->id, cb->cb_arg)) { + return 0; + } } return 1; } @@ -1365,8 +1370,10 @@ int inplace_cb_edns_back_parsed_call(struct module_env* env, for(; cb; cb=cb->next) { fptr_ok(fptr_whitelist_inplace_cb_edns_back_parsed( (inplace_cb_edns_back_parsed_func_type*)cb->cb)); - (void)(*(inplace_cb_edns_back_parsed_func_type*)cb->cb)(qstate, - cb->id, cb->cb_arg); + if(!(*(inplace_cb_edns_back_parsed_func_type*)cb->cb)(qstate, + cb->id, cb->cb_arg)) { + return 0; + } } return 1; } @@ -1378,8 +1385,10 @@ int inplace_cb_query_response_call(struct module_env* env, for(; cb; cb=cb->next) { fptr_ok(fptr_whitelist_inplace_cb_query_response( (inplace_cb_query_response_func_type*)cb->cb)); - (void)(*(inplace_cb_query_response_func_type*)cb->cb)(qstate, - response, cb->id, cb->cb_arg); + if(!(*(inplace_cb_query_response_func_type*)cb->cb)(qstate, + response, cb->id, cb->cb_arg)) { + return 0; + } } return 1; } diff --git a/util/data/msgreply.h b/util/data/msgreply.h index e4476660d..15451e0e8 100644 --- a/util/data/msgreply.h +++ b/util/data/msgreply.h @@ -668,7 +668,8 @@ struct edns_option* edns_opt_list_find(struct edns_option* list, uint16_t code); * @param region: region to store data. * @param start_time: the start time of recursion, when the packet arrived, * or the current time for cache responses. - * @return false on failure (a callback function returned an error). + * @return false on failure (a callback function returned an error and further + * callback processing was aborted). */ int inplace_cb_reply_call(struct module_env* env, struct query_info* qinfo, struct module_qstate* qstate, struct reply_info* rep, int rcode, @@ -688,7 +689,8 @@ int inplace_cb_reply_call(struct module_env* env, struct query_info* qinfo, * @param region: region to store data. * @param start_time: the start time of recursion, when the packet arrived, * or the current time for cache responses. - * @return false on failure (a callback function returned an error). + * @return false on failure (a callback function returned an error and further + * callback processing was aborted). */ int inplace_cb_reply_cache_call(struct module_env* env, struct query_info* qinfo, struct module_qstate* qstate, @@ -709,7 +711,8 @@ int inplace_cb_reply_cache_call(struct module_env* env, * @param region: region to store data. * @param start_time: the start time of recursion, when the packet arrived, * or the current time for cache responses. - * @return false on failure (a callback function returned an error). + * @return false on failure (a callback function returned an error and further + * callback processing was aborted). */ int inplace_cb_reply_local_call(struct module_env* env, struct query_info* qinfo, struct module_qstate* qstate, @@ -731,7 +734,8 @@ int inplace_cb_reply_local_call(struct module_env* env, * @param region: region to store data. * @param start_time: the start time of recursion, when the packet arrived, * or the current time for cache responses. - * @return false on failure (a callback function returned an error). + * @return false on failure (a callback function returned an error and further + * callback processing was aborted). */ int inplace_cb_reply_servfail_call(struct module_env* env, struct query_info* qinfo, struct module_qstate* qstate, @@ -754,7 +758,8 @@ int inplace_cb_reply_servfail_call(struct module_env* env, * @param zonelen: length of zone. * @param qstate: module qstate. * @param region: region to store data. - * @return false on failure (a callback function returned an error). + * @return false on failure (a callback function returned an error and further + * callback processing was aborted). */ int inplace_cb_query_call(struct module_env* env, struct query_info* qinfo, uint16_t flags, struct sockaddr_storage* addr, socklen_t addrlen, @@ -767,9 +772,10 @@ int inplace_cb_query_call(struct module_env* env, struct query_info* qinfo, * reply from a nameserver. * @param env: module environment. * @param qstate: module qstate. - * @return false on failure (a callback function returned an error). + * @return false on failure (a callback function returned an error and further + * callback processing was aborted). */ -int inplace_cb_edns_back_parsed_call(struct module_env* env, +int inplace_cb_edns_back_parsed_call(struct module_env* env, struct module_qstate* qstate); /** @@ -779,7 +785,8 @@ int inplace_cb_edns_back_parsed_call(struct module_env* env, * @param env: module environment. * @param qstate: module qstate. * @param response: received response - * @return false on failure (a callback function returned an error). + * @return false on failure (a callback function returned an error and further + * callback processing was aborted). */ int inplace_cb_query_response_call(struct module_env* env, struct module_qstate* qstate, struct dns_msg* response);