@@ -65,10 +65,30 @@ def serve(udps):
65
65
query = question [:- len (config ['domain' ])]
66
66
else :
67
67
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
68
73
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...
69
77
if rcode == 0 and 'filters' in config :
70
78
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:\n AN: %s\n NS: %s\n AR: %s\n " % (an_resource_records , ns_resource_records , ar_resource_records )
72
92
resp_pkt = format_response (qid , question , qtype , qclass , rcode , an_resource_records , ns_resource_records , ar_resource_records )
73
93
found = True
74
94
break
@@ -84,11 +104,22 @@ def serve(udps):
84
104
continue
85
105
udps .sendto (resp_pkt , src_addr )
86
106
87
- def compute_name_server_resources (name_servers ):
107
+ def compute_name_server_resources (config ):
88
108
ns = []
89
109
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
+ })
92
123
ar .append ({'qtype' :1 , 'qclass' :1 , 'ttl' :ttl , 'rdata' :struct .pack ("!I" , ip )})
93
124
return ns , ar
94
125
0 commit comments