blib/lib/Net/LibResolv.pm

filter: Statementstmt 100% 40 / 40 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 100% 47 / 47 CC 2 SCAR 2.2
CC
2
Cov
100%
_setup_constants
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
↑ Summary lib/Net/LibResolv.xs »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2011 -- leonerd@leonerd.org.uk
5  
6 package Net::LibResolv;
7  
8 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our $VERSION = '0.03';
12  
13 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 our @EXPORT_OK = qw(
15 res_query
16 res_search
17  
18 $h_errno
19 );
20 5 our %EXPORT_TAGS;
21  
22 5 our $h_errno;
23  
24 5 require XSLoader;
25 5 XSLoader::load( __PACKAGE__, $VERSION );
26  
27 5 $EXPORT_TAGS{"errors"} = [qw( HOST_NOT_FOUND NO_ADDRESS NO_DATA NO_RECOVERY TRY_AGAIN )];
28  
29 =head1 NAME
30  
31 C<Net::LibResolv> - a Perl wrapper around F<libresolv>
32  
33 =head1 SYNOPSIS
34  
35 use Net::LibResolv qw( res_query NS_C_IN NS_T_A $h_errno );
36 use Net::DNS::Packet;
37
38 my $answer = res_query( "www.cpan.org", NS_C_IN, NS_T_A );
39 defined $answer or die "DNS failure - $h_errno\n";
40
41 foreach my $rr ( Net::DNS::Packet->new( \$answer )->answer ) {
42 print $rr->string, "\n";
43 }
44  
45 =head1 DESCRIPTION
46  
47 The F<libresolv> library provides functions to use the platform's standard DNS
48 resolver to perform DNS queries. This Perl module provides a wrapping for the
49 two primary functions, C<res_query(3)> and C<res_search(3)>, allowing them to
50 be used from Perl.
51  
52 The return value from each function is a byte buffer containing the actual DNS
53 response packet. This will need to be parsed somehow to obtain the useful
54 information out of it; most likely by using L<Net::DNS>.
55  
56 =cut
57  
58 =head1 FUNCTIONS
59  
60 =cut
61  
62 # These functions are implemented in XS
63  
64 =head2 $answer = res_query( $dname, $class, $type )
65  
66 Calls the C<res_query(3)> function on the given domain name, class and type
67 number. Returns the answer byte buffer on success, or C<undef> on failure. On
68 failure sets the value of the C<$h_errno> package variable.
69  
70 C<$dname> should be a plain string. C<$class> and C<$type> should be numerical
71 codes. See the C<CONSTANTS> section for convenient definitions.
72  
73 =cut
74  
75 =head2 $answer = res_search( $dname, $class, $type )
76  
77 Calls the C<res_search(3)> function on the given domain name, class and type
78 number. Returns the answer byte buffer on success, or C<undef> on failure. On
79 failure sets the value of the C<$h_errno> package variable.
80  
81 C<$dname> should be a plain string. C<$class> and C<$type> should be numerical
82 codes. See the C<CONSTANTS> section for convenient definitions.
83  
84 =cut
85  
86 =head1 VARIABLES
87  
88 =head2 $h_errno
89  
90 After an error from C<res_query> or C<res_search>, this variable will be set
91 to the error value, as a dual-valued scalar. Its numerical value will be one
92 of the error constants (see below); it string value will be an error message
93 version of the same (similar to the C<$!> perl core variable).
94  
95 if( !defined( my $answer = res_query( ... ) ) ) {
96 print "Try again later...\n" if $h_errno == TRY_AGAIN;
97 }
98  
99 Z<>
100  
101 defined( my $answer = res_query( ... ) ) or
102 die "Cannot res_query() - $h_errno\n";
103  
104 =cut
105  
106 =head1 CONSTANTS
107  
108 =cut
109  
110 sub _setup_constants
111 10 ▶{
Subroutine _setup_constants: called CC=2 SCAR=6.9
112 10 my %args = @_;
113  
114 10 my $name = $args{name};
115 10 my $prefix = $args{prefix};
116 10 my $values = $args{values};
117 10 my $tag = $args{tag};
118  
119 10 my %name2value = %$values;
120 10 my %value2name = reverse %name2value;
121  
122 10 require constant;
123 10 constant->import( "$prefix$_" => $name2value{$_} ) for keys %name2value;
124  
125 10 push @EXPORT_OK, map "$prefix$_", keys %name2value;
126  
127 10 $EXPORT_TAGS{$tag} = [ map "$prefix$_", keys %name2value ];
128  
129 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
130 10 ▶ *{"${name}_name2value"} = sub { $name2value{uc shift} };
Subroutine __ANON__: called CC=1 SCAR=0.0
131 10 ▶ *{"${name}_value2name"} = sub { $value2name{+shift} };
Subroutine __ANON__: called CC=1 SCAR=0.0
132  
133 10 push @EXPORT_OK, "${name}_name2value", "${name}_value2name";
134 }
135  
136 # These constants are defined by RFCs, primarily RFC 1035 and friends. We
137 # shouldn't need to pull these out of platform .h files as they're portable
138 # constants
139  
140 =head2 Class IDs
141  
142 The following set of constants define values for the C<$class> parameter.
143 Typically only C<NS_C_IN> is actually used, for Internet.
144  
145 NS_C_IN NS_C_CHAOS NS_C_HS
146 NS_C_INVALD NS_C_NONE NS_C_ANY
147  
148 =head2 $id = class_name2value( $name )
149  
150 =head2 $name = class_value2name( $id )
151  
152 Functions to convert between class names and ID values.
153  
154 =cut
155  
156 5 _setup_constants
157 name => "class",
158 prefix => "NS_C_",
159 tag => "classes",
160 values => {
161 INVALID => 0,
162 IN => 1,
163 CHAOS => 3,
164 HS => 4,
165 NONE => 254,
166 ANY => 255,
167 };
168  
169 =head2 Type IDs
170  
171 The following are examples of constants define values for the C<$type>
172 parameter. (They all follow the same naming pattern, named after the record
173 type, so only a few are listed here.)
174  
175 NS_T_A NS_T_NS NS_T_CNAME NS_T_PTR NS_T_MX NS_T_TXT NS_T_SRV NS_T_AAAA
176 NS_T_INVALID NS_T_ANY
177  
178 =head2 $id = type_name2value( $name )
179  
180 =head2 $name = type_value2name( $id )
181  
182 Functions to convert between type names and ID values.
183  
184 =cut
185  
186 # The following list shamelessly stolen from <arpa/nameser.h>
187 5 _setup_constants
188 name => "type",
189 prefix => "NS_T_",
190 tag => "types",
191 values => {
192 INVALID => 0,
193 A => 1,
194 NS => 2,
195 MD => 3,
196 MF => 4,
197 CNAME => 5,
198 SOA => 6,
199 MB => 7,
200 MG => 8,
201 MR => 9,
202 NULL => 10,
203 WKS => 11,
204 PTR => 12,
205 HINFO => 13,
206 MINFO => 14,
207 MX => 15,
208 TXT => 16,
209 RP => 17,
210 AFSDB => 18,
211 X25 => 19,
212 ISDN => 20,
213 RT => 21,
214 NSAP => 22,
215 NSAP_PTR => 23,
216 SIG => 24,
217 KEY => 25,
218 PX => 26,
219 GPOS => 27,
220 AAAA => 28,
221 LOC => 29,
222 NXT => 30,
223 EID => 31,
224 NIMLOC => 32,
225 SRV => 33,
226 ATMA => 34,
227 NAPTR => 35,
228 KX => 36,
229 CERT => 37,
230 A6 => 38,
231 DNAME => 39,
232 SINK => 40,
233 OPT => 41,
234 APL => 42,
235 TKEY => 249,
236 TSIG => 250,
237 IXFR => 251,
238 AXFR => 252,
239 MAILB => 253,
240 MAILA => 254,
241 ANY => 255,
242 };
243  
244 =head2 Errors
245  
246 The following constants define error values for C<$h_errno>.
247  
248 HOST_NOT_FOUND NO_ADDRESS NO_DATA NO_RECOVERY TRY_AGAIN
249  
250 The values of C<NO_ADDRESS> and C<NO_DATA> may be the same.
251  
252 =head1 SEE ALSO
253  
254 =over 4
255  
256 =item *
257  
258 L<Net::DNS> - Perl interface to the DNS resolver
259  
260 =back
261  
262 =head1 AUTHOR
263  
264 Paul Evans <leonerd@leonerd.org.uk>
265  
266 =cut
267  
268 5 0x55AA;
↑ Summary lib/Net/LibResolv.xs »