Skip to content

Commit 0c5b7b3

Browse files
Handle NS records quite differently, so we can do Special Things with them
1 parent ffc86a1 commit 0c5b7b3

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

pymds

+35-4
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,30 @@ def serve(udps):
6565
query = question[:-len(config['domain'])]
6666
else:
6767
continue
68+
ns_resource_records, IGNORE_ar_resource_records = compute_name_server_resources(config)
69+
# Copy the NS data for later.
70+
original_ns_resource_records = ns_resource_records
71+
72+
# Perform the query against the backend plugin
6873
rcode, an_resource_records = config['source'].get_response(query, config['domain'], qtype, qclass, src_addr)
74+
75+
print "Got from get_response(): %s / %s" % (rcode, an_resource_records)
76+
# Run it against any filters we have available...
6977
if rcode == 0 and 'filters' in config:
7078
for f in config['filters']:
71-
an_resource_records = f.filter(query, config['domain'], qtype, qclass, src_addr, an_resource_records)
79+
print "Running filter!"
80+
an_resource_records, ns_resource_records = f.filter(query, config['domain'], qtype, qclass, src_addr, an_resource_records, ns_resource_records)
81+
print "Filter returned %s/%s" % (an_resource_records, ns_resource_records)
82+
83+
# If there's no error and no responses, wipe NS information
84+
# (or, if it's an NS query, move it to the answer section)
85+
# If we don't do this, it'd be considered a referral instead of an answer...
86+
if rcode == 0 and len(an_resource_records) == 0 and ns_resource_records == original_ns_resource_records:
87+
if qtype == 2:
88+
an_resource_records = ns_resource_records
89+
ns_resource_records = []
90+
91+
print "About to send back:\nAN: %s\nNS: %s\nAR: %s\n" % (an_resource_records, ns_resource_records, ar_resource_records)
7292
resp_pkt = format_response(qid, question, qtype, qclass, rcode, an_resource_records, ns_resource_records, ar_resource_records)
7393
found = True
7494
break
@@ -84,11 +104,22 @@ def serve(udps):
84104
continue
85105
udps.sendto(resp_pkt, src_addr)
86106

87-
def compute_name_server_resources(name_servers):
107+
def compute_name_server_resources(config):
88108
ns = []
89109
ar = []
90-
for name_server, ip, ttl in name_servers:
91-
ns.append({'qtype':2, 'qclass':1, 'ttl':ttl, 'rdata':labels2str(name_server)})
110+
111+
# Allow there to be zero nameservers defined...
112+
if not config.has_key('name_servers'):
113+
return ns, ar
114+
115+
for name_server, ip, ttl in config['name_servers']:
116+
ns.append({
117+
'qtype':2,
118+
'qclass':1,
119+
'ttl':ttl,
120+
'rdata':labels2str(name_server),
121+
'question': config['domain']
122+
})
92123
ar.append({'qtype':1, 'qclass':1, 'ttl':ttl, 'rdata':struct.pack("!I", ip)})
93124
return ns, ar
94125

pymdsrr.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ def __init__(self, netmask):
1313
base = 10
1414
self._netmask = int(netmask,base)
1515

16-
def filter(self, query, domain, qtype, qclass, src_addr, an_resource_records):
16+
def filter(self, query, domain, qtype, qclass, src_addr, an_resource_records, ns_resource_records):
1717
if qtype != 1 or len(an_resource_records) < 2:
18-
return an_resource_records
18+
return an_resource_records, ns_resource_records
1919
src_ip = ipstr2int(src_addr[0])
2020
result = []
2121
if self._netmask:
@@ -35,4 +35,4 @@ def filter(self, query, domain, qtype, qclass, src_addr, an_resource_records):
3535
if self._netmask:
3636
if key not in self._cache:
3737
self._cache[key] = result[0]
38-
return result
38+
return result, ns_resource_records

0 commit comments

Comments
 (0)