Net::DNS - Perl interface to the DNS resolver
use Net::DNS;
Net::DNS is a collection of modules that act as a Domain Name System (DNS) resolver. It is a port of the perl Net::DNS package.
The programmer should be somewhat familiar with the format of a DNS packet and its various sections. See RFC 1035 or DNS and BIND (Albitz & Liu) for details.
A resolver object is an instance of the Net::DNS::Resolver class. A program can have multiple resolver objects, each maintaining its own state information such as the nameservers to be queried, whether recursion is desired, etc.
Net::DNS::Resolver queries return Net::DNS::Packet objects. Packet objects have five sections:
* The header section, a Net::DNS::Header object. * The question section, a list of Net::DNS::Question objects. * The answer section, a list of Net::DNS::RR objects. * The authority section, a list of Net::DNS::RR objects. * The additional section, a list of Net::DNS::RR objects.
The Net::DNS::Update package is a subclass of Net::DNS::Packet for creating packet objects to be used in dynamic updates.
Net::DNS::Header objects represent the header section of a DNS packet.
Net::DNS::Question objects represent the question section of a DNS packet.
Net::DNS::RR is the base class for DNS resource record (RR) objects in the answer, authority, and additional sections of a DNS packet.
Don‘t assume that RR objects will be of the type you requested — always check an RR object‘s type before calling any of its methods.
As of version 0.55 there is functionality to help you sort RR arrays. ‘rrsort()’ is the function that is available to do the sorting. In most cases rrsort will give you the answer that you want but you can specify your own sorting method by using the Net::DNS::RR::FOO.set_rrsort_func() class method. See Net::DNS::RR for details.
The following examples show how to use the Net::DNS modules. See the other manual pages and the demo scripts included with the source code for additional examples.
See the Net::DNS::Update manual page for an example of performing dynamic updates.
require 'Net/DNS'
res = Net::DNS::Resolver.new
query = res.search("host.example.com")
if (query)
query.answer.each do |rr|
next unless rr.class == Net::DNS::RR::A
print rr.address + "\n"
end
else
print "query failed: " + res.errorstring + "\n"
end
require 'Net/DNS'
res = Net::DNS::Resolver.new
query = res.query("example.com", "NS")
if (query)
(query.answer.select { |i| i.class == Net::DNS::RR::NS}).each do |rr|
print rr.nsdname + "\n"
end
else
print "query failed: " + res.errorstring + "\n"
end
require 'Net/DNS'
name='ENTER_NAME_HERE'
res = Net::DNS::Resolver.new
mx = Net::DNS.mx(name, res, 'IN')
if (mx)
mx.each do |rr|
print rr.preference, " ", rr.exchange, "\n"
end
else
print "Can't find MX records for #{name}: " + res.errorstring + "\n"
end
require 'Net/DNS'
res = Net::DNS::Resolver.new
query = res.query("example.com", "SOA")
if (query)
(query.answer)[0].print
else
print "query failed: ", res.errorstring, "\n"
end
require 'Net/DNS'
res = Net::DNS::Resolver.new
res.nameservers("ns.example.com")
zone = res.axfr("example.com")
zone.each do |rr|
rr.print
end
Net::DNS is slow.
For other items to be fixed, please see the "TODO" file included with the source distribution.
Copyright (c) 1997-2002 Michael Fuhr.
Portions Copyright (c) 2002-2004 Chris Reinhardt.
Portions Copyright (c) 2005 Olaf Kolkman (RIPE NCC)
Portions Copyright (c) 2006 Olaf Kolkman (NLnet Labs)
Portions Copyright (c) 2006 AlexD (Nominet UK)
All rights reserved.
Ruby port (2006) from Nominet UK by :
Alex D
alexd@nominet.org.uk
Port from perl Net::DNS (version 0.57) maintained at NLnet Labs (www.nlnetlabs.nl) by:
Olaf Kolkman
olaf@net-dns.org
Between 2002 and 2004 Net::DNS was maintained by:
Chris Reinhardt
Net::DNS was created by:
Michael Fuhr
mike@fuhr.org
For more information see:
http://www.net-dns.org/
Stay tuned and syncicate:
http://www.net-dns.org/blog/
Net::DNS::Resolver, Net::DNS::Packet, Net::DNS::Update,
Net::DNS::Header, Net::DNS::Question, Net::DNS::RR, RFC 1035, DNS and BIND by Paul Albitz & Cricket Liu
| VERSION | = | '0.0.1' | Returns the version of Net::DNS. | |
| PACKETSZ | = | 512 | Returns the default packet size | |
| HFIXEDSZ | = | 12 | Header size | |
| QFIXEDSZ | = | 4 | ||
| RRFIXEDSZ | = | 10 | ||
| INT32SZ | = | 4 | ||
| INT16SZ | = | 2 | ||
| HAVE_XS | = | false | ||
| DNSSEC | = | false | ||
| DN_EXPAND_ESCAPES | = | false | ||
| Typesbyname | = | { 'SIGZERO' => 0, # RFC2931 consider this a pseudo type 'A' => 1, # RFC 1035, Section 3.4.1 'NS' => 2, # RFC 1035, Section 3.3.11 'MD' => 3, # RFC 1035, Section 3.3.4 (obsolete) 'MF' => 4, # RFC 1035, Section 3.3.5 (obsolete) 'CNAME' => 5, # RFC 1035, Section 3.3.1 'SOA' => 6, # RFC 1035, Section 3.3.13 'MB' => 7, # RFC 1035, Section 3.3.3 'MG' => 8, # RFC 1035, Section 3.3.6 'MR' => 9, # RFC 1035, Section 3.3.8 'NULL' => 10, # RFC 1035, Section 3.3.10 'WKS' => 11, # RFC 1035, Section 3.4.2 (deprecated) 'PTR' => 12, # RFC 1035, Section 3.3.12 'HINFO' => 13, # RFC 1035, Section 3.3.2 'MINFO' => 14, # RFC 1035, Section 3.3.7 'MX' => 15, # RFC 1035, Section 3.3.9 'TXT' => 16, # RFC 1035, Section 3.3.14 'RP' => 17, # RFC 1183, Section 2.2 'AFSDB' => 18, # RFC 1183, Section 1 'X25' => 19, # RFC 1183, Section 3.1 'ISDN' => 20, # RFC 1183, Section 3.2 'RT' => 21, # RFC 1183, Section 3.3 'NSAP' => 22, # RFC 1706, Section 5 'NSAP_PTR' => 23, # RFC 1348 (obsolete) # The following 2 RRs are impemented in Net::DNS::SEC 'SIG' => 24, # RFC 2535, Section 4.1 'KEY' => 25, # RFC 2535, Section 3.1 'PX' => 26, # RFC 2163, 'GPOS' => 27, # RFC 1712 (obsolete) 'AAAA' => 28, # RFC 1886, Section 2.1 'LOC' => 29, # RFC 1876 # The following RR is impemented in Net::DNS::SEC 'NXT' => 30, # RFC 2535, Section 5.2 obsoleted by RFC3755 'EID' => 31, # draft-ietf-nimrod-dns-xx.txt 'NIMLOC' => 32, # draft-ietf-nimrod-dns-xx.txt 'SRV' => 33, # RFC 2052 'ATMA' => 34, # ??? 'NAPTR' => 35, # RFC 2168 'KX' => 36, # RFC 2230 'CERT' => 37, # RFC 2538 'DNAME' => 39, # RFC 2672 'OPT' => 41, # RFC 2671 # The following 4 RRs are impemented in Net::DNS::SEC 'DS' => 43, # RFC 4034 'SSHFP' => 44, # draft-ietf-secsh-dns (No RFC # yet at time of coding) # 'IPSECKEY' => 45, # RFC 4025 'RRSIG' => 46, # RFC 4034 'NSEC' => 47, # RFC 4034 'DNSKEY' => 48, # RFC 4034 'SPF' => 99, # rfc-schlitt-spf-classic-o2 (No RFC # yet at time of coding) 'UINFO' => 100, # non-standard 'UID' => 101, # non-standard 'GID' => 102, # non-standard 'UNSPEC' => 103, # non-standard 'TKEY' => 249, # RFC 2930 'TSIG' => 250, # RFC 2931 'IXFR' => 251, # RFC 1995 'AXFR' => 252, # RFC 1035 'MAILB' => 253, # RFC 1035 (MB, MG, MR) 'MAILA' => 254, # RFC 1035 (obsolete - see MX) 'ANY' => 255, # RFC 1035 } | Do not use these tybesby hashes directly. Use the interface functions, see below. | |
| Typesbyval | = | Typesbyname.invert; | ||
| Classesbyname | = | { 'IN' => 1, # RFC 1035 'CH' => 3, # RFC 1035 'CHAOS' => 3, # RFC 1035 'HS' => 4, # RFC 1035 'HESIOD' => 4, # RFC 1035 'NONE' => 254, # RFC 2136 'ANY' => 255, # RFC 1035 } | Do not use these classesby hashes directly. See below. | |
| Classesbyval | = | { 1 => 'IN', # RFC 1035 3 => 'CH', # RFC 1035 4 => 'HS', # RFC 1035 254 => 'NONE', # RFC 2136 255 => 'ANY', # RFC 1035 } | ||
| Qtypesbyname | = | { 'IXFR' => 251, # incremental transfer [RFC1995] 'AXFR' => 252, # transfer of an entire zone [RFC1035] 'MAILB' => 253, # mailbox-related RRs (MB, MG or MR) [RFC1035] 'MAILA' => 254, # mail agent RRs (Obsolete - see MX) [RFC1035] 'ANY' => 255, # all records [RFC1035] } | The qtypesbyval and metatypesbyval specify special typecodes See rfc2929 and the relevant IANA registry www.iana.org/assignments/dns-parameters | |
| Qtypesbyval | = | Qtypesbyname.invert; | ||
| Metatypesbyname | = | { 'TKEY' => 249, # Transaction Key [RFC2930] 'TSIG' => 250, # Transaction Signature [RFC2845] 'OPT' => 41, # RFC 2671 } | ||
| Metatypesbyval | = | Metatypesbyname.invert; | ||
| Opcodesbyname | = | { 'QUERY' => 0, # RFC 1035 'IQUERY' => 1, # RFC 1035 'STATUS' => 2, # RFC 1035 'NS_NOTIFY_OP' => 4, # RFC 1996 'UPDATE' => 5, # RFC 2136 } | ||
| Opcodesbyval | = | Opcodesbyname.invert; | ||
| Rcodesbyname | = | { 'NOERROR' => 0, # RFC 1035 'FORMERR' => 1, # RFC 1035 'SERVFAIL' => 2, # RFC 1035 'NXDOMAIN' => 3, # RFC 1035 'NOTIMP' => 4, # RFC 1035 'REFUSED' => 5, # RFC 1035 'YXDOMAIN' => 6, # RFC 2136 'YXRRSET' => 7, # RFC 2136 'NXRRSET' => 8, # RFC 2136 'NOTAUTH' => 9, # RFC 2136 'NOTZONE' => 10, # RFC 2136 } | ||
| Rcodesbyval | = | Rcodesbyname.invert; |
classesbyval and classesbyname functions are wrappers around the similarly named hashes. They are used for ‘unknown’ DNS RR classess (RFC3597) See typesbyval and typesbyname, these beasts have the same functionality
Usage:
mxes = mx('example.com', 'IN')
# Use a default resolver -- can't get an error string this way.
require 'Net/DNS'
mx = Net::DNS.mx("example.com")
# Use your own resolver object.
require 'Net/DNS'
res = Net::DNS::Resolver.new
mx = Net::DNS.mx("example.com", res)
Returns a list of Net::DNS::RR::MX objects representing the MX records for the specified name; the list will be sorted by preference. Returns an empty list if the query failed or no MX records were found.
This method does not look up A records — it only performs MX queries.
See EXAMPLES for a more complete example.
Utility function
name2labels to translate names from presentation format into an array of "wire-format" labels. in: dName a string with a domain name in presentation format (1035 sect 5.1) out: an array of labels in wire format.
Use this method to add a "name is not in use" prerequisite to a dynamic update packet.
packet.push('pre' => nxdomain("host.example.com"))
Meaning: No RR with the specified name can exist.
Returns a Net::DNS::RR object or nil if the object couldn‘t be created.
Use this method to add an "RRset does not exist" prerequisite to a dynamic update packet.
packet.push('pre' => nxrrset("host.example.com A"))
Meaning: No RRs with the specified name and type can exist.
Returns a Net::DNS::RR object or nil if the object couldn‘t be created.
wire,leftover=presentation2wire(leftover) Will parse the input presentation format and return everything before the first non-escaped "." in the first element of the return array and all that has not been parsed yet in the 2nd argument.
Use this method to add RRs to a zone.
packet.push('update' => rr_add("host.example.com A 10.1.2.3"))
Meaning: Add this RR to the zone.
RR objects created by this method should be added to the "update" section of a dynamic update packet. The TTL defaults to 86400 seconds (24 hours) if not specified.
Returns a C<Net::DNS::RR object or nil if the object couldn‘t be created.
Use this method to delete RRs from a zone. There are three forms: delete an RRset, delete all RRsets, and delete an RR.
# Delete an RRset.
packet.push(:update => rr_del("host.example.com A"))
Meaning: Delete all RRs having the specified name and type.
# Delete all RRsets.
packet.push(:update => rr_del("host.example.com"))
Meaning: Delete all RRs having the specified name.
# Delete an RR.
packet.push(:update => rr_del("host.example.com A 10.1.2.3"))
Meaning: Delete all RRs having the specified name, type, and data.
RR objects created by this method should be added to the "update" section of a dynamic update packet.
Returns a Net::DNS::RR object or nil if the object couldn‘t be created.
require 'Net::DNS'
prioritysorted=rrsort("SRV","priority",rr_array)
rrsort() selects all RRs from the input array that are of the type that are defined in the first argument. Those RRs are sorted based on the attribute that is specified as second argument.
There are a number of RRs for which the sorting function is specifically defined for certain attributes. If such sorting function is defined in the code (it can be set or overwritten using the set_rrsort_func() class method) that function is used.
For instance:
prioritysorted=rrsort("SRV","priority",rr_array)
returns the SRV records sorted from lowest to heighest priority and for equal priorities from heighes to lowes weight.
If the function does not exist then a numerical sort on the attribute value is performed.
portsorted=rrsort("SRV","port",rr_array)
If the attribute does not exist for a certain RR than the RRs are sorted on string comparrisson of the rdata.
If the attribute is not defined than either the default_sort function will be defined or "Canonical sorting" (as defined by DNSSEC) will be used.
rrsort() returns a sorted array with only elements of the specified RR type or undef.
rrsort() returns undef when arguments are incorrect.
typesbyval returns they TYPE mnemonic as a function of the TYPE code. If the TYPE mapping is not specified the generic mnemonic TYPE### is returned.
Use this method to add a "name is in use" prerequisite to a dynamic update packet.
packet.push('pre' => yxdomain("host.example.com"))
Meaning: At least one RR with the specified name must exist.
Returns a Net::DNS::RR object or nil if the object couldn‘t be created.
Use this method to add an "RRset exists" prerequisite to a dynamic update packet. There are two forms, value-independent and value-dependent:
# RRset exists (value-independent)
update.push('pre' => yxrrset("host.example.com A"))
Meaning: At least one RR with the specified name and type must exist.
# RRset exists (value-dependent)
packet.push('pre' => yxrrset("host.example.com A 10.1.2.3"))
Meaning: At least one RR with the specified name and type must exist and must have matching data.
Returns a Net::DNS::RR object or nil if the object couldn‘t be created.