Skip to content

Instantly share code, notes, and snippets.

Last active Mar 8, 2019
What would you like to do?
An incomplete Python3 module for Unbound 1.9.0 showing a proof-of-concept partial implementation of like functionality in Unbound, and an extension of the idea to support dynamic delegation (which I'm not sure is even a sensible thing to do).
import base64
# Based on:
def init_standard(id, env):
return True
def deinit(id):
return True
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:
qdn = qstate.qinfo.qname_str.lower()
if qdn.endswith(""):
# The case: respond with an A record.
# Expects as input a dotted quad format IPv4 address sub-domain name.
ipv4 = '.'.join(qstate.qinfo.qname_list[0:4])
msg = DNSMessage(qdn, qstate.qinfo.qtype, qstate.qinfo.qclass, PKT_QR | PKT_AA)
msg.answer.append("{} 300 IN A {}".format(qdn, ipv4))
set_return_msg_and_finish(id, msg, qstate)
elif qdn.endswith(""):
# A proposed '' case: respond with NS and A glue records.
# Expects as input a base 32 encoded dotted quad format IPv4 address which "points" to a
# nameserver.
b32 = ''.join(qstate.qinfo.qname_list[-6:-5])
decoded = base64.b32decode(b32).decode('utf-8')
msg = DNSMessage(qdn, qstate.qinfo.qtype, qstate.qinfo.qclass, PKT_QR)
msg.authority.append("{} 60 IN NS {}".format(b32, decoded))
msg.additional.append("{} 60 IN A {}".format(b32, decoded))
set_return_msg_and_finish(id, msg, qstate)
# Pass the query to validator
qstate.ext_state[id] = MODULE_WAIT_MODULE
qstate.ext_state[id] = MODULE_FINISHED
qstate.ext_state[id] = MODULE_ERROR
return True
def set_return_msg_and_finish(id, msg, qstate):
qstate.return_rcode = RCODE_NOERROR
# Set qstate.return_msg
if not msg.set_return_msg(qstate):
qstate.ext_state[id] = MODULE_ERROR
# Indicate valid result = 2
qstate.ext_state[id] = MODULE_FINISHED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment