The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Regexp::Common::dns - Common DNS Regular Expressions

SYNOPSIS

 use Regexp::Common qw/dns/;
  
 while (<>) {
     $RE{'dns'}{'data'}{'mx'}  and print 'an mx';
     $RE{'dns'}{'data'}{'soa'} and print 'a soa';
 }

 if ($host =~ $RE{'dns'}{'domain'}) {
     print "hostname found';
 }

PATTERNS

$RE{'dns'}{'int16'}

Matches a 16 bit unsigned integer in base 10 format.

$RE{'dns'}{'int32'}

Matches a 32 bit integer in base 10 format.

$RE{'dns'}{'domain'}

Matches a DNS domain name.

By default this regexp uses a pragmatic combination of rfc1035, and rfc2181. This is intended to be in sprit with current DNS operation practices. This hybrid approach is simlar to rfc1035, but allows for underscores, and for underscores and hyphens to begin or end a lable. It also allows for wilcarding.

The default rule can be set with the $Regexp::Common::dns::DEFAULT_RFC global, which is not exported by this package.

By default this regexp matches a domain name according to the rules of rfc1035 section 2.3.1:

 <domain>      ::= <subdomain> | " "
 
 <subdomain>   ::= <label> | <subdomain> "." <label>
 
 <label>       ::= <letter> [ [ <ldh-str> ] <let-dig> ]
 
 <ldh-str>     ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>

 <let-dig-hyp> ::= <let-dig> | "-"

 <let-dig>     ::= <letter> | <digit>

 <letter>      ::= <[A-Za-z]>

 <digit>       ::= <[0-9]>

Labels must be 63 characters or less.

Domain names must be 255 octets or less.

RFC 1035 does not allow for wildcarding (*.example.com). If you want to match a wildcarded domain name, use the -wildcard flag:

 $Regexp::Common::dns::DEFAULT_RFC = '1035'

 '*.example.com' =~ $RE{'dns'}{'domain'}{-wildcard};  # match
 '*.example.com' =~ $RE{'dns'}{'domain'};             # no match
 

RFC 1035 has been superseded by rfc2181 section 11:

  • Labels can be any character except a ..

  • Each label is no shorter than one octet.

  • Each lable is no longer than 63 octets.

  • A complete domain name may be no longer than 255 octets, including the separators.

For example:

 '_fancy.spf.record=4.org' =~ $RE{'dns'}{'domain'}{-rfc => 2181};

This regular expression does not match a single ..

The minimum number of lables can be specified with the -minlables flag:

 'org'    =~ $RE{'dns'}{'domain'}                  # match
 'org'    =~ $RE{'dns'}{'domain'}{-minlables => 2} # no match
 'co.org' =~ $RE{'dns'}{'domain'}{-minlables => 2} # match

The -rfc flag can be used to specify any of the three rule sets. The pragmatic ruleset discussed earlier is labled as hybrid.

$RE{'dns'}{'data'}{'a'}

Matches the data section of an A record. This is a dotted decimal representation of a IPv4 address.

$RE{'dns'}{'data'}{'cname'}

Matches the data section of a CNAME record. This pattern accepts the same flags as $RE{'dns'}{'domain'}.

$RE{'dns'}{'data'}{'mx'}

Matches the data section of a MX record. This pattern accepts the same flags as $RE{'dns'}{'domain'}.

If keep is turned on, then the $n variables are filled as follows:

$1

The entire data section.

$2

The preference.

$3

The exchange.

$RE{'dns'}{'data'}{'soa'}

Matches the data section of a MX record. This pattern accepts the -rfc flag.

If keep is turned on, then the $n variables are filled as follows:

$1

The entire data section.

$2

The mname.

$3

The rname.

$4

The serial number.

$5

The refresh time interval.

$6

The retry time interval.

$7

The expire time value.

$8

The minimum TTL.

TODO

Several RR data patterns are missing:

 HINFO
 MB
 MG
 MINFO
 MR
 NULL (easy!)
 PTR  
 TXT
 WKS
 RP
 LOC 
 AAAA
 OPT
 SRV
 DNAME

and more.

Patterns for whole RR records, TTLs, classes, and types are missing.

Ideally patterns for the various compenent of a data section would be provided, for example to match the mname section of a soa record:

 $RE{'dns'}{'data'}{'soa'}{'mname'}
 

The author is not sure that the $RE{'dns'}{'data'}{'rr'} namespace is needed, perhaps $RE{'dns'}{'rr'} would suffice.

AUTHOR

Chris Reinhardt cpan@triv.org

COPYRIGHT

Copyright (c) 2003 Chris Reinhardt.

All rights reserved. This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Regexp::Common, perl(1).