[Twisted-Python] Trouble with Record_TXT

Hi all, I am attempting to write a small DNS server that returns custom TXT record when I do: dig -t TXT -p 10053 @127.0.0.1 testdomain.com The code is as follows: ---------- #!/usr/bin/python from twisted.internet import reactor from twisted.names import client, dns, server class DynamicResolver(object): def query(self, query, timeout=None): name = query.name.name # answer = dns.RRHeader( # name = name, # payload = dns.Record_A(address=b'1.2.3.4')) answer = dns.RRHeader( name = name, payload = dns.Record_TXT(data='FirstSecond', ttl=3333)) answers = [answer] authority = [] additional = [] return answers, authority, additional def main(): factory = server.DNSServerFactory( clients = [DynamicResolver()] ) protocol = dns.DNSDatagramProtocol(controller=factory) reactor.listenUDP(10053, protocol) reactor.listenTCP(10053, factory) reactor.run() if __name__ == '__main__': main() ---------- You will notice three lines of code are commented out. They are sample code for an A record that work just fine. But the attempt at Record_TXT fails. I receive the following error when I attempt to perform the dig command: ----------- $ python servlet.py Unhandled Error Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/twisted/names/dns.py", line 2326, in datagramReceived self.controller.messageReceived(m, self, addr) File "/usr/local/lib/python2.7/dist-packages/twisted/names/server.py", line 192, in messageReceived self.handleQuery(message, proto, address) File "/usr/local/lib/python2.7/dist-packages/twisted/names/server.py", line 136, in handleQuery return self.resolver.query(query).addCallback( File "/usr/local/lib/python2.7/dist-packages/twisted/names/common.py", line 73, in query return defer.maybeDeferred(method, query.name.name, timeout) --- <exception caught here> --- File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 139, in maybeDeferred result = f(*args, **kw) File "/usr/local/lib/python2.7/dist-packages/twisted/names/common.py", line 81, in lookupAddress return self._lookup(name, dns.IN, dns.A, timeout) File "/usr/local/lib/python2.7/dist-packages/twisted/names/resolve.py", line 79, in _lookup d = self.resolvers[0].query(q, timeout) File "servlet.py", line 16, in query payload = dns.Record_TXT(data='FirstSecond', ttl=3333)) File "/usr/local/lib/python2.7/dist-packages/twisted/names/dns.py", line 867, in __init__ assert (payload is None) or isinstance(payload, UnknownRecord) or (payload.TYPE == type) exceptions.AssertionError: ----------- I'm a little confused, because I followed the documentation as referenced here: http://twistedmatrix.com/documents/current/api/twisted.names.dns.Record_TXT.... Any ideas what this error means and why it is happening? Thank you, Nahraf

On 11 June 2014 18:19, Nahraf34 <Nahraf34@janisary.us> wrote:
I receive the following error when I attempt to perform the dig command: assert (payload is None) or isinstance(payload, UnknownRecord) or (payload.TYPE == type) exceptions.AssertionError:
Hi, I tried to catch you on #twisted earlier, but you'd gone. I think the problem is that you need to pass a `type` argument to `RRHeader` which matches the payload you supply. eg * https://gist.github.com/wallrj/84c3815a0be52831e077 It happens to just work with A records because that's the default type. It would be nice if RRHeader would just examine the payload and automatically set it's type. Perhaps we can add a new constructor class method eg `RRHeader.fromPayload` which does this. Hope that helps. -RichardW,

On 05:19 pm, nahraf34@janisary.us wrote:
Hi all,
I am attempting to write a small DNS server that returns custom TXT record when I do:
dig -t TXT -p 10053 @127.0.0.1 testdomain.com
The code is as follows:
----------
#!/usr/bin/python
from twisted.internet import reactor from twisted.names import client, dns, server
class DynamicResolver(object): def query(self, query, timeout=None): name = query.name.name
# answer = dns.RRHeader( # name = name, # payload = dns.Record_A(address=b'1.2.3.4'))
answer = dns.RRHeader( name = name, payload = dns.Record_TXT(data='FirstSecond', ttl=3333))
answers = [answer] authority = [] additional = []
return answers, authority, additional
def main(): factory = server.DNSServerFactory( clients = [DynamicResolver()] )
protocol = dns.DNSDatagramProtocol(controller=factory)
reactor.listenUDP(10053, protocol) reactor.listenTCP(10053, factory)
reactor.run()
if __name__ == '__main__': main()
----------
You will notice three lines of code are commented out. They are sample code for an A record that work just fine. But the attempt at Record_TXT fails.
I receive the following error when I attempt to perform the dig command:
-----------
$ python servlet.py Unhandled Error Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/twisted/names/dns.py", line 2326, in datagramReceived self.controller.messageReceived(m, self, addr) File "/usr/local/lib/python2.7/dist-packages/twisted/names/server.py", line 192, in messageReceived self.handleQuery(message, proto, address) File "/usr/local/lib/python2.7/dist-packages/twisted/names/server.py", line 136, in handleQuery return self.resolver.query(query).addCallback( File "/usr/local/lib/python2.7/dist-packages/twisted/names/common.py", line 73, in query return defer.maybeDeferred(method, query.name.name, timeout) --- <exception caught here> --- File "/usr/local/lib/python2.7/dist- packages/twisted/internet/defer.py", line 139, in maybeDeferred result = f(*args, **kw) File "/usr/local/lib/python2.7/dist-packages/twisted/names/common.py", line 81, in lookupAddress return self._lookup(name, dns.IN, dns.A, timeout) File "/usr/local/lib/python2.7/dist- packages/twisted/names/resolve.py", line 79, in _lookup d = self.resolvers[0].query(q, timeout) File "servlet.py", line 16, in query payload = dns.Record_TXT(data='FirstSecond', ttl=3333)) File "/usr/local/lib/python2.7/dist-packages/twisted/names/dns.py", line 867, in __init__ assert (payload is None) or isinstance(payload, UnknownRecord) or (payload.TYPE == type) exceptions.AssertionError:
-----------
I'm a little confused, because I followed the documentation as referenced here: http://twistedmatrix.com/documents/current/api/twisted.names.dns.Record_TXT....
Any ideas what this error means and why it is happening?
The problem here is how you're using the `RRHeader` initializer. It (somewhat unhelpfully) accepts a separate `type` argument. The value of the `type` argument and the type of the payload must match. The default type is `A` (as in `Record_A`). If you change your code to do something like `RRHeader(type=payload.type, payload=payload)` then I think you'll get the behavior you're after. Jean-Paul
participants (3)
-
exarkun@twistedmatrix.com
-
Nahraf34
-
Richard Wall