How to append TXT record with Rdata to Additional section of a DNS Query?

175 views
Skip to first unread message

fairclothjm

unread,
Mar 24, 2020, 12:45:45 PM3/24/20
to dnspython-users
Hi,

I would like to append a TXT record with Rdata to the Additional section of a DNS Query. How can I do this?

So far I have:

from dns import flags
from dns import message
from dns import name
from dns import rdatatype
from dns import rdataclass

domain
= name.from_text("test.com")

req
= message.make_query(domain, rdatatype.A)

# TXT record data
test_name
= "test.name.com"
r_data
= "my-test-data"

# add TXT record to additional section of query
req
.flags |= flags.AD
req
.find_rrset(
    req
.additional,
    name
.from_text(test_name),
    rdataclass
.IN,
    rdatatype
.TXT,
    create
=True,
    force_unique
=True,
)

print("\nrequest:")
print(req)


Which outputs

request:
id
37746
opcode QUERY
rcode NOERROR
flags RD AD
;QUESTION
test
.com. IN A
;ANSWER
;AUTHORITY
;ADDITIONAL
test
.name.com. IN TXT

I have successfully added the TXT record name and type to the Additional section. However, I can't seem to find how to append the Rdata "my-test-data" as well. 

Is this even possible?


Thanks

Bob Halley

unread,
Mar 25, 2020, 11:07:36 AM3/25/20
to dnspython-users
You just need to make the rdata and add it to the empty rrset you made.  Here's the updated program:

from dns import flags
from dns import message
from dns import name
from dns import rdata
from dns import rdatatype
from dns import rdataclass

domain = name.from_text("test.com")

req = message.make_query(domain, rdatatype.A)

# TXT record data
test_name = "test.name.com"
r_data = "my-test-data"

# add TXT record to additional section of query
req.flags |= flags.AD
rrset = req.find_rrset(
    req.additional,
    name.from_text(test_name),
    rdataclass.IN,
    rdatatype.TXT,
    create=True,
    force_unique=True,
)
rd = rdata.from_text(rdataclass.IN, rdatatype.TXT, 'my-test-rdata')
rrset.add(rd, 300)   # 300 is the TTL

print("\nrequest:")
print(req)


fairclothjm

unread,
Mar 25, 2020, 11:26:14 AM3/25/20
to dnspython-users
Thanks!

I ended up doing the following last night:

rdata = dns.rdtypes.ANY.TXT.TXT(dns.rdataclass.IN, dns.rdatatype.TXT, value)
rdataset
.add(rdata, 300)

But your example is a bit cleaner.
Reply all
Reply to author
Forward
0 new messages