0
# DNS Records
1
2
Comprehensive DNS record type support including all standard and modern record types. Provides record type constants, class constants, and complete record data parsing and formatting functionality.
3
4
## Capabilities
5
6
### Record Type Constants
7
8
Complete set of DNS record type constants including legacy, standard, and modern record types.
9
10
```python { .api }
11
# Core record types
12
NONE = 0
13
A = 1 # IPv4 address
14
NS = 2 # Name server
15
MD = 3 # Mail destination (obsolete)
16
MF = 4 # Mail forwarder (obsolete)
17
CNAME = 5 # Canonical name
18
SOA = 6 # Start of authority
19
MB = 7 # Mailbox domain (experimental)
20
MG = 8 # Mail group member (experimental)
21
MR = 9 # Mail rename domain (experimental)
22
NULL = 10 # Null resource record (experimental)
23
WKS = 11 # Well known service description
24
PTR = 12 # Domain name pointer
25
HINFO = 13 # Host information
26
MINFO = 14 # Mailbox or mail list information
27
MX = 15 # Mail exchange
28
TXT = 16 # Text strings
29
30
# Extended record types
31
RP = 17 # Responsible person
32
AFSDB = 18 # AFS database location
33
X25 = 19 # X.25 PSDN address
34
ISDN = 20 # ISDN address
35
RT = 21 # Route through
36
NSAP = 22 # NSAP address
37
NSAP_PTR = 23 # Reverse NSAP lookup
38
SIG = 24 # Security signature
39
KEY = 25 # Security key
40
PX = 26 # X.400 mail mapping information
41
GPOS = 27 # Geographical position
42
AAAA = 28 # IPv6 address
43
LOC = 29 # Location information
44
NXT = 30 # Next domain (obsolete)
45
EID = 31 # Endpoint identifier
46
NIMLOC = 32 # Nimrod locator
47
SRV = 33 # Service location
48
ATMA = 34 # ATM address
49
NAPTR = 35 # Naming authority pointer
50
KX = 36 # Key exchanger
51
CERT = 37 # Certificate resource record
52
A6 = 38 # A6 (obsolete)
53
DNAME = 39 # DNAME redirection
54
SINK = 40 # Kitchen sink (obsolete)
55
OPT = 41 # EDNS option
56
APL = 42 # Address prefix list
57
DS = 43 # Delegation signer
58
SSHFP = 44 # SSH key fingerprint
59
IPSECKEY = 45 # IPSEC key
60
RRSIG = 46 # DNSSEC signature
61
NSEC = 47 # Next secure record
62
DNSKEY = 48 # DNS key
63
DHCID = 49 # DHCP identifier
64
NSEC3 = 50 # Next secure record version 3
65
NSEC3PARAM = 51 # NSEC3 parameters
66
TLSA = 52 # TLSA certificate association
67
68
# Modern record types
69
HIP = 55 # Host identity protocol
70
NINFO = 56 # NINFO
71
RKEY = 57 # RKEY
72
TALINK = 58 # Trust anchor link
73
CDS = 59 # Child delegation signer
74
CDNSKEY = 60 # Child DNS key
75
OPENPGPKEY = 61 # OpenPGP key
76
CSYNC = 62 # Child-to-parent synchronization
77
SPF = 99 # Sender policy framework
78
UINFO = 100 # IANA reserved
79
UID = 101 # IANA reserved
80
GID = 102 # IANA reserved
81
UNSPEC = 103 # IANA reserved
82
EUI48 = 108 # MAC address (EUI-48)
83
EUI64 = 109 # MAC address (EUI-64)
84
85
# Query types
86
TKEY = 249 # Transaction key
87
TSIG = 250 # Transaction signature
88
IXFR = 251 # Incremental zone transfer
89
AXFR = 252 # Authoritative zone transfer
90
MAILB = 253 # Mailbox-related RRs
91
MAILA = 254 # Mail agent RRs
92
ANY = 255 # Any record type
93
94
# Extended types
95
URI = 256 # Uniform resource identifier
96
CAA = 257 # Certification authority authorization
97
AVC = 258 # Application visibility and control
98
DLV = 32769 # DNSSEC lookaside validation
99
```
100
101
### Record Class Constants
102
103
DNS record class constants for different protocol classes.
104
105
```python { .api }
106
# Standard classes
107
RESERVED0 = 0 # Reserved
108
IN = 1 # Internet
109
CH = 3 # Chaos
110
HS = 4 # Hesiod
111
NONE = 254 # None
112
ANY = 255 # Any class
113
```
114
115
### Record Type Functions
116
117
Utility functions for working with record types and classes.
118
119
```python { .api }
120
def from_text(text):
121
"""
122
Convert text to record type value.
123
124
Args:
125
text (str): Record type name (e.g., 'A', 'AAAA', 'MX')
126
127
Returns:
128
int: Record type constant
129
130
Raises:
131
dns.rdatatype.UnknownRdatatype: If type is unknown
132
"""
133
134
def to_text(value):
135
"""
136
Convert record type value to text.
137
138
Args:
139
value (int): Record type constant
140
141
Returns:
142
str: Record type name
143
"""
144
145
def is_metatype(rdtype):
146
"""
147
Check if record type is a metatype.
148
149
Metatypes are types used in queries but not in zone data
150
(AXFR, IXFR, ANY, etc.)
151
152
Args:
153
rdtype (int): Record type
154
155
Returns:
156
bool: True if metatype
157
"""
158
159
def is_singleton(rdtype):
160
"""
161
Check if record type is a singleton.
162
163
Singleton types can have only one record per name
164
(SOA, CNAME, etc.)
165
166
Args:
167
rdtype (int): Record type
168
169
Returns:
170
bool: True if singleton type
171
"""
172
```
173
174
### Record Data Classes
175
176
Base classes and specific record data implementations for all supported record types.
177
178
```python { .api }
179
class Rdata:
180
"""
181
Base class for DNS record data.
182
183
All specific record types inherit from this class and implement
184
their own parsing, formatting, and wire format methods.
185
"""
186
187
def __init__(self, rdclass, rdtype):
188
"""Initialize record data."""
189
190
def to_text(self, origin=None, relativize=True):
191
"""Convert to text format."""
192
193
def to_wire(self, file, compress=None, origin=None):
194
"""Convert to wire format."""
195
196
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
197
"""Parse from text format."""
198
199
def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
200
"""Parse from wire format."""
201
202
# Common record data classes
203
class A(Rdata):
204
"""IPv4 address record."""
205
address: str # IPv4 address
206
207
class AAAA(Rdata):
208
"""IPv6 address record."""
209
address: str # IPv6 address
210
211
class CNAME(Rdata):
212
"""Canonical name record."""
213
target: dns.name.Name # Target name
214
215
class MX(Rdata):
216
"""Mail exchange record."""
217
preference: int # Preference value
218
exchange: dns.name.Name # Mail server name
219
220
class NS(Rdata):
221
"""Name server record."""
222
target: dns.name.Name # Name server name
223
224
class PTR(Rdata):
225
"""Pointer record."""
226
target: dns.name.Name # Target name
227
228
class SOA(Rdata):
229
"""Start of authority record."""
230
mname: dns.name.Name # Primary name server
231
rname: dns.name.Name # Responsible person email
232
serial: int # Serial number
233
refresh: int # Refresh interval
234
retry: int # Retry interval
235
expire: int # Expire time
236
minimum: int # Minimum TTL
237
238
class SRV(Rdata):
239
"""Service record."""
240
priority: int # Priority
241
weight: int # Weight
242
port: int # Port number
243
target: dns.name.Name # Target host
244
245
class TXT(Rdata):
246
"""Text record."""
247
strings: list # List of text strings
248
249
class NAPTR(Rdata):
250
"""Naming authority pointer record."""
251
order: int # Order
252
preference: int # Preference
253
flags: bytes # Flags
254
service: bytes # Service
255
regexp: bytes # Regular expression
256
replacement: dns.name.Name # Replacement
257
258
class TLSA(Rdata):
259
"""TLSA certificate association record."""
260
usage: int # Certificate usage
261
selector: int # Selector
262
mtype: int # Matching type
263
cert: bytes # Certificate data
264
265
class CAA(Rdata):
266
"""Certification authority authorization record."""
267
flags: int # Flags
268
tag: bytes # Property tag
269
value: bytes # Property value
270
271
class DS(Rdata):
272
"""Delegation signer record."""
273
key_tag: int # Key tag
274
algorithm: int # Algorithm
275
digest_type: int # Digest type
276
digest: bytes # Digest
277
278
class DNSKEY(Rdata):
279
"""DNS key record."""
280
flags: int # Flags
281
protocol: int # Protocol
282
algorithm: int # Algorithm
283
key: bytes # Public key
284
285
class RRSIG(Rdata):
286
"""Resource record signature."""
287
type_covered: int # Type covered
288
algorithm: int # Algorithm
289
labels: int # Original TTL
290
original_ttl: int # Original TTL
291
expiration: int # Signature expiration
292
inception: int # Signature inception
293
key_tag: int # Key tag
294
signer: dns.name.Name # Signer name
295
signature: bytes # Signature
296
297
class NSEC(Rdata):
298
"""Next secure record."""
299
next: dns.name.Name # Next domain name
300
windows: list # Type bit maps
301
302
class NSEC3(Rdata):
303
"""Next secure record version 3."""
304
algorithm: int # Hash algorithm
305
flags: int # Flags
306
iterations: int # Iterations
307
salt: bytes # Salt
308
next: bytes # Next hashed owner name
309
windows: list # Type bit maps
310
```
311
312
### RRset and Rdataset Classes
313
314
Classes for managing collections of resource records.
315
316
```python { .api }
317
class RRset:
318
"""
319
A resource record set (RRset).
320
321
An RRset contains all resource records at a particular name
322
with the same type and class.
323
324
Attributes:
325
name (dns.name.Name): Record name
326
rdclass (int): Record class
327
rdtype (int): Record type
328
ttl (int): Time to live
329
items (list): List of record data
330
"""
331
332
def __init__(self, name, rdclass, rdtype, covers='NONE'):
333
"""Initialize RRset."""
334
335
def __len__(self):
336
"""Return number of records."""
337
338
def __iter__(self):
339
"""Iterate over record data."""
340
341
def __getitem__(self, index):
342
"""Get record data by index."""
343
344
def add(self, rd, ttl=None):
345
"""Add record data to RRset."""
346
347
def union_update(self, other):
348
"""Update RRset with records from another RRset."""
349
350
def intersection_update(self, other):
351
"""Keep only records present in both RRsets."""
352
353
def update(self, other):
354
"""Replace RRset contents with another RRset."""
355
356
class Rdataset:
357
"""
358
A DNS rdataset (resource record data set).
359
360
An rdataset contains the resource record data for all records
361
at a name with the same type and class, but without the name itself.
362
363
Attributes:
364
rdclass (int): Record class
365
rdtype (int): Record type
366
covers (int): Covered type for RRSIG records
367
ttl (int): Time to live
368
items (list): List of record data
369
"""
370
371
def __init__(self, rdclass, rdtype, covers='NONE'):
372
"""Initialize rdataset."""
373
374
def __len__(self):
375
"""Return number of records."""
376
377
def __iter__(self):
378
"""Iterate over record data."""
379
380
def add(self, rd, ttl=None):
381
"""Add record data to rdataset."""
382
383
def union_update(self, other):
384
"""Update rdataset with records from another rdataset."""
385
386
def to_text(self, name=None, origin=None, relativize=True):
387
"""Convert rdataset to text format."""
388
389
def to_wire(self, name, file, compress=None, origin=None):
390
"""Convert rdataset to wire format."""
391
```
392
393
## Usage Examples
394
395
### Working with Record Types
396
397
```python
398
import dns.rdatatype
399
import dns.rdataclass
400
401
# Convert between text and numeric values
402
a_type = dns.rdatatype.from_text('A')
403
print(f"A record type: {a_type}") # 1
404
405
aaaa_text = dns.rdatatype.to_text(28)
406
print(f"Type 28: {aaaa_text}") # AAAA
407
408
# Check record type properties
409
print(f"SOA is singleton: {dns.rdatatype.is_singleton(dns.rdatatype.SOA)}")
410
print(f"AXFR is metatype: {dns.rdatatype.is_metatype(dns.rdatatype.AXFR)}")
411
412
# Work with classes
413
in_class = dns.rdataclass.from_text('IN')
414
print(f"IN class: {in_class}") # 1
415
```
416
417
### Creating and Parsing Record Data
418
419
```python
420
import dns.rdata
421
import dns.rdatatype
422
import dns.rdataclass
423
424
# Create A record
425
a_rdata = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '192.0.2.1')
426
print(f"A record: {a_rdata.address}")
427
428
# Create MX record
429
mx_rdata = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, '10 mail.example.com.')
430
print(f"MX preference: {mx_rdata.preference}")
431
print(f"MX exchange: {mx_rdata.exchange}")
432
433
# Create SRV record
434
srv_rdata = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SRV,
435
'10 20 80 www.example.com.')
436
print(f"SRV priority: {srv_rdata.priority}")
437
print(f"SRV weight: {srv_rdata.weight}")
438
print(f"SRV port: {srv_rdata.port}")
439
print(f"SRV target: {srv_rdata.target}")
440
441
# Create TXT record with multiple strings
442
txt_rdata = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.TXT,
443
'"v=spf1" "include:_spf.example.com" "~all"')
444
print(f"TXT strings: {txt_rdata.strings}")
445
```
446
447
### Working with RRsets
448
449
```python
450
import dns.rrset
451
import dns.name
452
import dns.rdata
453
import dns.rdatatype
454
import dns.rdataclass
455
456
# Create RRset
457
name = dns.name.from_text('example.com.')
458
rrset = dns.rrset.RRset(name, dns.rdataclass.IN, dns.rdatatype.A)
459
460
# Add records to RRset
461
a1 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '192.0.2.1')
462
a2 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '192.0.2.2')
463
rrset.add(a1, ttl=300)
464
rrset.add(a2, ttl=300)
465
466
print(f"RRset name: {rrset.name}")
467
print(f"RRset type: {rrset.rdtype}")
468
print(f"RRset TTL: {rrset.ttl}")
469
print(f"Record count: {len(rrset)}")
470
471
# Iterate over records
472
for rdata in rrset:
473
print(f" Address: {rdata.address}")
474
475
# Create from text
476
text_rrset = dns.rrset.from_text('example.com.', 300, 'IN', 'MX',
477
'10 mail1.example.com.', '20 mail2.example.com.')
478
```
479
480
### Modern Record Types
481
482
```python
483
import dns.rdata
484
import dns.rdatatype
485
import dns.rdataclass
486
487
# TLSA record for certificate association
488
tlsa_rdata = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.TLSA,
489
'3 1 1 1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef')
490
print(f"TLSA usage: {tlsa_rdata.usage}")
491
print(f"TLSA selector: {tlsa_rdata.selector}")
492
print(f"TLSA matching type: {tlsa_rdata.mtype}")
493
print(f"TLSA cert data: {tlsa_rdata.cert.hex()}")
494
495
# CAA record for certificate authority authorization
496
caa_rdata = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CAA,
497
'0 issue "letsencrypt.org"')
498
print(f"CAA flags: {caa_rdata.flags}")
499
print(f"CAA tag: {caa_rdata.tag}")
500
print(f"CAA value: {caa_rdata.value}")
501
502
# NAPTR record for naming authority pointer
503
naptr_rdata = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NAPTR,
504
'100 10 "u" "E2U+sip" "!^.*$!sip:info@example.com!" .')
505
print(f"NAPTR order: {naptr_rdata.order}")
506
print(f"NAPTR preference: {naptr_rdata.preference}")
507
print(f"NAPTR flags: {naptr_rdata.flags}")
508
print(f"NAPTR service: {naptr_rdata.service}")
509
print(f"NAPTR regexp: {naptr_rdata.regexp}")
510
```
511
512
### Wire Format Operations
513
514
```python
515
import dns.rdata
516
import dns.rdatatype
517
import dns.rdataclass
518
import io
519
520
# Create record data
521
mx_rdata = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX,
522
'10 mail.example.com.')
523
524
# Convert to wire format
525
wire_buffer = io.BytesIO()
526
mx_rdata.to_wire(wire_buffer)
527
wire_data = wire_buffer.getvalue()
528
print(f"Wire format length: {len(wire_data)} bytes")
529
530
# Parse from wire format
531
parsed_rdata = dns.rdata.from_wire(dns.rdataclass.IN, dns.rdatatype.MX,
532
wire_data, 0, len(wire_data))
533
print(f"Parsed MX preference: {parsed_rdata.preference}")
534
print(f"Parsed MX exchange: {parsed_rdata.exchange}")
535
```
536
537
## Record Type Categories
538
539
### Address Records
540
- `A` (1): IPv4 address
541
- `AAAA` (28): IPv6 address
542
543
### Name Records
544
- `NS` (2): Name server
545
- `CNAME` (5): Canonical name
546
- `PTR` (12): Pointer
547
- `DNAME` (39): Delegation name
548
549
### Mail Records
550
- `MX` (15): Mail exchange
551
- `TXT` (16): Text (often used for SPF)
552
553
### Service Records
554
- `SRV` (33): Service location
555
- `NAPTR` (35): Naming authority pointer
556
- `URI` (256): Uniform resource identifier
557
558
### Security Records
559
- `DS` (43): Delegation signer
560
- `DNSKEY` (48): DNS key
561
- `RRSIG` (46): Resource record signature
562
- `NSEC` (47): Next secure
563
- `NSEC3` (50): Next secure version 3
564
- `TLSA` (52): TLS association
565
- `CAA` (257): Certificate authority authorization
566
567
### Zone Records
568
- `SOA` (6): Start of authority
569
- `OPT` (41): EDNS options
570
571
### Legacy and Special
572
- `HINFO` (13): Host information
573
- `LOC` (29): Location
574
- `SSHFP` (44): SSH fingerprint
575
- `SPF` (99): Sender policy framework
576
577
## Exceptions
578
579
```python { .api }
580
class UnknownRdatatype(DNSException):
581
"""An unknown DNS record type was encountered."""
582
583
class UnknownRdataclass(DNSException):
584
"""An unknown DNS record class was encountered."""
585
```