mirror of
https://github.com/NLnetLabs/unbound.git
synced 2026-12-06 00:05:37 +00:00
- Terminate inplace callback progression if one of them errors out;
suggestion by Rio Fujita.
This commit is contained in:
1 parent
3fce3be652
commit
7d91d6345f
5 files changed
+189
-28
No files matched your search
@@ -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
|
||||
~~~~~~~
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in new issue
Block a user