############################################################################
# Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
############################################################################
############################################################################
# ans.py: See README.anspy for details.
############################################################################
import os
import sys
import signal
import socket
import select
import functools
from dns.rdataclass import *
############################################################################
# set up the RRs to be returned in the next answer
#
# the message contains up to two pipe-separated ('|') fields.
#
# the first field of the message is a comma-separated list
# of actions indicating what to put into the answer set
# (e.g., a dname, a cname, another cname, etc)
#
# supported actions:
# - cname (cname from the current name to a new one in the same domain)
# - dname (dname to a new domain, plus a synthesized cname)
# - xname ("external" cname, to a new name in a new domain)
#
# example: xname, dname, cname represents a CNAME to an external
# domain which is then answered by a DNAME and synthesized
# CNAME pointing to yet another domain, which is then answered
# by a CNAME within the same domain, and finally an answer
# to the query. each RR in the answer set has a corresponding
# RRSIG. these signatures are not valid, but will exercise the
# response parser.
#
# the second field is a comma-separated list of which RRs in the
# answer set to include in the answer, in which order. if prepended
# with 's', the number indicates which signature to include.
#
# examples: for the answer set "cname, cname, cname", an rr set
# '1, s1, 2, s2, 3, s3, 4, s4' indicates that all four RRs should
# be included in the answer, with siagntures, in the origninal
# order, while 4, s4, 3, s3, 2, s2, 1, s1' indicates the order
# should be reversed, 's3, s3, s3, s3' indicates that the third
# RRSIG should be repeated four times and everything else should
# be omitted, and so on.
#
# if there is no second field (i.e., no pipe symbol appears in
# the line) , the default is to send all answers and signatures.
# if a pipe symbol exists but the second field is empty, then
# nothing is sent at all.
############################################################################
actions = []
rrs = []
print ('received control message: %s' % msg)
return
rrs = []
for i in range(n):
return
rrs = []
if i > n:
print ('invalid index %d' + (i + 1))
continue
else:
if i > n:
print ('invalid index %d' % (i + 1))
continue
############################################################################
# Respond to a DNS query.
############################################################################
# get qtype
# for 'www.example.com.'...
# - name is 'www'
# - domain is 'example.com.'
# - sld is 'example'
# - tld is 'com.'
print ('domain: ' + domain)
# default answers, depending on QTYPE.
# currently only A, AAAA, TXT and NS are supported.
ttl = 86400
additionalA = '10.53.0.4'
additionalAAAA = 'fd92:7065:b8e:ffff::4'
if typename == 'A':
final = '10.53.0.4'
elif typename == 'AAAA':
final = 'fd92:7065:b8e:ffff::4'
elif typename == 'TXT':
final = 'Some\ text\ here'
elif typename == 'NS':
else:
final = None
# RRSIG rdata - won't validate but will exercise response parsing
sigdata='OCXH2De0yE4NMTl9UykvOsJ4IBGs/ZIpff2rpaVJrVG7jQfmj50otBAp A0Zo7dpBU4ofv0N/F2Ar6LznCncIojkWptEJIAKA5tHegf/jY39arEpO cevbGp6DKxFhlkLXNcw7k9o7DSw14OaRmgAjXdTFbrl4AiAa0zAttFko Tso='
# construct answer set.
answers = []
sigs = []
i = 0
if name != 'test':
continue
if action == b'xname':
newname = 'cname%d' % i
i += 1
i += 1
rrsig = 'CNAME 5 3 %d %s %s 12345 %s %s' % \
continue
if action == b'cname':
newname = 'cname%d' % i
i += 1
rrsig = 'CNAME 5 3 %d %s %s 12345 %s %s' % \
continue
if action == b'dname':
i += 1
rrsig = 'DNAME 5 3 %d %s %s 12345 %s %s' % \
rrsig = 'CNAME 5 3 %d %s %s 12345 %s %s' % \
continue
# now add the final answer
rrsig = '%s 5 3 %d %s %s 12345 %s %s' % \
# prepare the response and convert to wire format
if name != 'test':
if wantsigs:
else:
continue
elif sig:
else:
if typename != 'NS':
("ns1.%s" % domain)))
IN, A, additionalA))
r.use_edns()
return r.to_wire()
print ("Shutting down now...")
############################################################################
# Main
#
# Set up responder and control channel, open the pid file, and start
# the main loop, listening for queries on the query channel or commands
# on the control channel and acting on them.
############################################################################
try:
try:
except:
except:
f.close()
if havev6:
print ("Ctrl-c to quit")
if havev6:
else:
while running:
try:
break
break
except KeyboardInterrupt:
break
for s in inputready:
if s == ctrl_socket:
# Handle control channel input
print ("Control channel connected")
while True:
if not msg:
break
if s == query4_socket or s == query6_socket:
print ("Query received on %s" %
# Handle incoming queries
if rsp:
if not running:
break