line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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, 2007-2012 -- leonerd@leonerd.org.uk |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Socket::GetAddrInfo; |
7
|
|
|
|
|
|
|
|
8
|
6
|
|
|
6
|
|
83099
|
use strict; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
269
|
|
9
|
6
|
|
|
6
|
|
34
|
use warnings; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
187
|
|
10
|
|
|
|
|
|
|
|
11
|
6
|
|
|
6
|
|
41
|
use Carp; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
3630
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.22'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
require Exporter; |
16
|
|
|
|
|
|
|
our @EXPORT_OK; |
17
|
|
|
|
|
|
|
our %EXPORT_TAGS; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
foreach my $impl (qw( Core XS Emul )) { |
20
|
|
|
|
|
|
|
my $class = "Socket::GetAddrInfo::$impl"; |
21
|
|
|
|
|
|
|
my $file = "Socket/GetAddrInfo/$impl.pm"; |
22
|
|
|
|
|
|
|
eval { |
23
|
|
|
|
|
|
|
# Each of the impls puts its symbols directly in our package |
24
|
|
|
|
|
|
|
# Don't need to ->import |
25
|
|
|
|
|
|
|
require $file; |
26
|
|
|
|
|
|
|
}; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
last if defined &getaddrinfo; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 NAME |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
C - address-family independent name resolving functions |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 SYNOPSIS |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
use Socket qw( SOCK_STREAM ); |
38
|
|
|
|
|
|
|
use Socket::GetAddrInfo qw( getaddrinfo getnameinfo ); |
39
|
|
|
|
|
|
|
use IO::Socket; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my %hints = ( socktype => SOCK_STREAM ); |
42
|
|
|
|
|
|
|
my ( $err, @res ) = getaddrinfo( "www.google.com", "www", \%hints ); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
die "Cannot resolve name - $err" if $err; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $sock; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
foreach my $ai ( @res ) { |
49
|
|
|
|
|
|
|
my $candidate = IO::Socket->new(); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
$candidate->socket( $ai->{family}, $ai->{socktype}, $ai->{protocol} ) |
52
|
|
|
|
|
|
|
or next; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
$candidate->connect( $ai->{addr} ) |
55
|
|
|
|
|
|
|
or next; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
$sock = $candidate; |
58
|
|
|
|
|
|
|
last; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
if( $sock ) { |
62
|
|
|
|
|
|
|
my ( $err, $host, $service ) = getnameinfo( $sock->peername ); |
63
|
|
|
|
|
|
|
print "Connected to $host:$service\n" if !$err; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 DESCRIPTION |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
The RFC 2553 functions C and C provide an abstracted |
69
|
|
|
|
|
|
|
way to convert between a pair of host name/service name and socket addresses, |
70
|
|
|
|
|
|
|
or vice versa. C converts names into a set of arguments to pass |
71
|
|
|
|
|
|
|
to the C and C syscalls, and C converts a |
72
|
|
|
|
|
|
|
socket address back into its host name/service name pair. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
These functions provide a useful interface for performing either of these name |
75
|
|
|
|
|
|
|
resolution operation, without having to deal with IPv4/IPv6 transparency, or |
76
|
|
|
|
|
|
|
whether the underlying host can support IPv6 at all, or other such issues. |
77
|
|
|
|
|
|
|
However, not all platforms can support the underlying calls at the C layer, |
78
|
|
|
|
|
|
|
which means a dilema for authors wishing to write forward-compatible code. |
79
|
|
|
|
|
|
|
Either to support these functions, and cause the code not to work on older |
80
|
|
|
|
|
|
|
platforms, or stick to the older "legacy" resolvers such as |
81
|
|
|
|
|
|
|
C, which means the code becomes more portable. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
This module attempts to solve this problem, by detecting at compiletime |
84
|
|
|
|
|
|
|
whether the underlying OS will support these functions. If it does not, the |
85
|
|
|
|
|
|
|
module will use pure-perl emulations of the functions using the legacy |
86
|
|
|
|
|
|
|
resolver functions instead. The emulations support the same interface as the |
87
|
|
|
|
|
|
|
real functions, and behave as close as is resonably possible to emulate using |
88
|
|
|
|
|
|
|
the legacy resolvers. See L for details on the |
89
|
|
|
|
|
|
|
limits of this emulation. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
As of Perl version 5.14.0, Perl already supports C in core. On |
92
|
|
|
|
|
|
|
such a system, this module simply uses the functions provided by C, |
93
|
|
|
|
|
|
|
and does not need to use its own compiled XS, or pure-perl legacy emulation. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
As C in core now provides all the functions also provided by this |
96
|
|
|
|
|
|
|
module, it is likely this may be the last released version of this module. And |
97
|
|
|
|
|
|
|
code currently using this module would be advised to switch to using core |
98
|
|
|
|
|
|
|
C instead. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=cut |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 EXPORT TAGS |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
The following tags may be imported by C |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=over 8 |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item AI |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Imports all of the C constants for C flags |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=item NI |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Imports all of the C constants for C flags |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=item EAI |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Imports all of the C for error values |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=item constants |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Imports all of the above constants |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=back |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=cut |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
$EXPORT_TAGS{AI} = [ grep m/^AI_/, @EXPORT_OK ]; |
129
|
|
|
|
|
|
|
$EXPORT_TAGS{NI} = [ grep m/^NI(?:x)?_/, @EXPORT_OK ]; |
130
|
|
|
|
|
|
|
$EXPORT_TAGS{EAI} = [ grep m/^EAI_/, @EXPORT_OK ]; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
$EXPORT_TAGS{constants} = [ map @{$EXPORT_TAGS{$_}}, qw( AI NI EAI ) ]; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub import |
135
|
|
|
|
|
|
|
{ |
136
|
8
|
|
|
8
|
|
52
|
my $class = shift; |
137
|
8
|
|
|
|
|
19
|
my %symbols = map { $_ => 1 } @_; |
|
145
|
|
|
|
|
262
|
|
138
|
|
|
|
|
|
|
|
139
|
8
|
50
|
|
|
|
44
|
$symbols{':newapi'} and croak ":newapi tag is no longer supported by Socket::GetAddrInfo; just 'use' it directly"; |
140
|
8
|
50
|
|
|
|
26
|
$symbols{':Socket6api'} and croak ":Socket6api tag is no longer supported by Socket::GetAddrInfo; use Socket::GetAddrInfo::Socket6api instead"; |
141
|
|
|
|
|
|
|
|
142
|
8
|
|
|
|
|
21
|
local $Exporter::ExportLevel = $Exporter::ExportLevel + 1; |
143
|
8
|
|
|
|
|
1057
|
Exporter::import( $class, keys %symbols ); |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 FUNCTIONS |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=cut |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 ( $err, @res ) = getaddrinfo( $host, $service, $hints ) |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
C turns human-readable text strings (containing hostnames, |
153
|
|
|
|
|
|
|
numeric addresses, service names, or port numbers) into sets of binary values |
154
|
|
|
|
|
|
|
containing socket-level representations of these addresses. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
When given both host and service, this function attempts to resolve the host |
157
|
|
|
|
|
|
|
name to a set of network addresses, and the service name into a protocol and |
158
|
|
|
|
|
|
|
port number, and then returns a list of address structures suitable to |
159
|
|
|
|
|
|
|
connect() to it. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
When given just a host name, this function attempts to resolve it to a set of |
162
|
|
|
|
|
|
|
network addresses, and then returns a list of these addresses in the returned |
163
|
|
|
|
|
|
|
structures. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
When given just a service name, this function attempts to resolve it to a |
166
|
|
|
|
|
|
|
protocol and port number, and then returns a list of address structures that |
167
|
|
|
|
|
|
|
represent it suitable to bind() to. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
When given neither name, it generates an error. |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
The optional C<$hints> parameter can be passed a HASH reference to indicate |
172
|
|
|
|
|
|
|
how the results are generated. It may contain any of the following four |
173
|
|
|
|
|
|
|
fields: |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=over 8 |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=item flags => INT |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
A bitfield containing C constants. At least the following flags will be |
180
|
|
|
|
|
|
|
available: |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=over 2 |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=item * C |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Indicates that this resolution is for a local C for a passive (i.e. |
187
|
|
|
|
|
|
|
listening) socket, rather than an active (i.e. connecting) socket. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=item * C |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Indicates that the caller wishes the canonical hostname (C) field |
192
|
|
|
|
|
|
|
of the result to be filled in. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=item * C |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Indicates that the caller will pass a numeric address, rather than a hostname, |
197
|
|
|
|
|
|
|
and that C must not perform a resolve operation on this name. |
198
|
|
|
|
|
|
|
This flag will prevent a possibly-slow network lookup operation, and instead |
199
|
|
|
|
|
|
|
return an error, if a hostname is passed. |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=back |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Other flags may be provided by the OS. |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=item family => INT |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
Restrict to only generating addresses in this address family |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=item socktype => INT |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Restrict to only generating addresses of this socket type |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=item protocol => INT |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
Restrict to only generating addresses for this protocol |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=back |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
Errors are indicated by the C<$err> value returned; which will be non-zero in |
220
|
|
|
|
|
|
|
numeric context, and contain a string error message as a string. The value can |
221
|
|
|
|
|
|
|
be compared against any of the C constants to determine what the error |
222
|
|
|
|
|
|
|
is. Rather than explicitly checking, see also L |
223
|
|
|
|
|
|
|
which provides functions that throw exceptions on errors. |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
If no error occurs, C<@res> will contain HASH references, each representing |
226
|
|
|
|
|
|
|
one address. It will contain the following five fields: |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=over 8 |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=item family => INT |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
The address family (e.g. AF_INET) |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=item socktype => INT |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
The socket type (e.g. SOCK_STREAM) |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=item protocol => INT |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
The protocol (e.g. IPPROTO_TCP) |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=item addr => STRING |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
The address in a packed string (such as would be returned by pack_sockaddr_in) |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=item canonname => STRING |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
The canonical name for the host if the C flag was provided, or |
249
|
|
|
|
|
|
|
C otherwise. This field will only be present on the first returned |
250
|
|
|
|
|
|
|
address. |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=back |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=head2 ( $err, $host, $service ) = getnameinfo( $addr, $flags, $xflags ) |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
C turns a binary socket address into a pair of human-readable |
257
|
|
|
|
|
|
|
strings, containing the host name, numeric address, service name, or port |
258
|
|
|
|
|
|
|
number. |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
The optional C<$flags> parameter is a bitfield containing C constants. |
261
|
|
|
|
|
|
|
At least the following flags will be available: |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=over 2 |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=item * C |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
Requests that a human-readable string representation of the numeric address is |
268
|
|
|
|
|
|
|
returned directly, rather than performing a name resolve operation that may |
269
|
|
|
|
|
|
|
convert it into a hostname. |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=item * C |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
Requests that the port number be returned directly as a number representation |
274
|
|
|
|
|
|
|
rather than performing a name resolve operation that may convert it into a |
275
|
|
|
|
|
|
|
service name. |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=item * C |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
If a name resolve operation fails to provide a name, then this flag will cause |
280
|
|
|
|
|
|
|
C to indicate an error, rather than returning the numeric |
281
|
|
|
|
|
|
|
representation as a human-readable string. |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=item * C |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
Indicates that the socket address relates to a C socket, for the |
286
|
|
|
|
|
|
|
services whose name differs between C and C protocols. |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
=back |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
Other flags may be provided by the OS. |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
The optional C<$xflags> parameter is a bitfield containing C constants. |
293
|
|
|
|
|
|
|
These are a Perl-level extension to the API, to indicate extra information. |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=over 2 |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
=item * C |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
Indicates that the caller is not interested in the hostname of the result, so |
300
|
|
|
|
|
|
|
it does not have to be converted; C will be returned as the hostname. |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
=item * C |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
Indicates that the caller is not interested in the service name of the result, |
305
|
|
|
|
|
|
|
so it does not have to be converted; C will be returned as the service |
306
|
|
|
|
|
|
|
name. |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
=back |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
Errors are indicated by the C<$err> value returned; which will be non-zero in |
311
|
|
|
|
|
|
|
numeric context, and contain a string error message as a string. The value can |
312
|
|
|
|
|
|
|
be compared against any of the C constants to determine what the error |
313
|
|
|
|
|
|
|
is. Rather than explicitly checking, see also L |
314
|
|
|
|
|
|
|
which provides functions that throw exceptions on errors. |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=cut |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
=head1 EXAMPLES |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
=head2 Lookup for C |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
The C function converts a hostname and a service name into a list |
323
|
|
|
|
|
|
|
of structures, each containing a potential way to C to the named |
324
|
|
|
|
|
|
|
service on the named host. |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
my %hints = ( socktype => SOCK_STREAM ); |
327
|
|
|
|
|
|
|
my ( $err, @res ) = getaddrinfo( $hostname, $servicename, \%hints ); |
328
|
|
|
|
|
|
|
die "Cannot getaddrinfo - $err" if $err; |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
my $sock; |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
foreach my $ai ( @res ) { |
333
|
|
|
|
|
|
|
my $candidate = IO::Socket->new(); |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
$candidate->socket( $ai->{family}, $ai->{socktype}, $ai->{protocol} ) |
336
|
|
|
|
|
|
|
or next; |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
$candidate->connect( $ai->{addr} ) |
339
|
|
|
|
|
|
|
or next; |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
$sock = $candidate; |
342
|
|
|
|
|
|
|
last; |
343
|
|
|
|
|
|
|
} |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
Because a list of potential candidates is returned, the C loop tries |
346
|
|
|
|
|
|
|
each in turn until it it finds one that succeeds both the C and |
347
|
|
|
|
|
|
|
C calls. |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
This function performs the work of the legacy functions C, |
350
|
|
|
|
|
|
|
C, C and C. |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
=head2 Making a human-readable string out of an address |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
The C function converts a socket address, such as returned by |
355
|
|
|
|
|
|
|
C or C, into a pair of human-readable strings |
356
|
|
|
|
|
|
|
representing the address and service name. |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
my ( $err, $hostname, $servicename ) = getnameinfo( $socket->peername ); |
359
|
|
|
|
|
|
|
die "Cannot getnameinfo - $err" if $err; |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
print "The peer is connected from $hostname\n"; |
362
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
Since in this example only the hostname was used, the redundant conversion of |
364
|
|
|
|
|
|
|
the port number into a service name may be omitted by passing the |
365
|
|
|
|
|
|
|
C flag. |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
my ( $err, $hostname ) = getnameinfo( $socket->peername, 0, NIx_NOSERV ); |
368
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
This function performs the work of the legacy functions C, |
370
|
|
|
|
|
|
|
C, C and C. |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
=head2 Resolving hostnames into IP addresses |
373
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
To turn a hostname into a human-readable plain IP address use C |
375
|
|
|
|
|
|
|
to turn the hostname into a list of socket structures, then C on |
376
|
|
|
|
|
|
|
each one to make it a readable IP address again. |
377
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
my ( $err, @res ) = getaddrinfo( $hostname, "", { socktype => SOCK_RAW } ); |
379
|
|
|
|
|
|
|
die "Cannot getaddrinfo - $err" if $err; |
380
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
while( my $ai = shift @res ) { |
382
|
|
|
|
|
|
|
my ( $err, $ipaddr ) = getnameinfo( $ai->{addr}, NI_NUMERICHOST, NIx_NOSERV ); |
383
|
|
|
|
|
|
|
die "Cannot getnameinfo - $err" if $err; |
384
|
|
|
|
|
|
|
|
385
|
|
|
|
|
|
|
print "$ipaddr\n"; |
386
|
|
|
|
|
|
|
} |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
The C hint to C filters the results to only include one |
389
|
|
|
|
|
|
|
socket type and protocol. Without this most OSes return three combinations, |
390
|
|
|
|
|
|
|
for C, C and C, resulting in triplicate |
391
|
|
|
|
|
|
|
output of addresses. The C flag to C causes it to |
392
|
|
|
|
|
|
|
return a string-formatted plain IP address, rather than reverse resolving it |
393
|
|
|
|
|
|
|
back into a hostname. |
394
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
This combination performs the work of the legacy functions C |
396
|
|
|
|
|
|
|
and C. |
397
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
=cut |
399
|
|
|
|
|
|
|
|
400
|
|
|
|
|
|
|
=head1 BUILDING WITHOUT XS CODE |
401
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
In some environments it may be preferred not to build the XS implementation, |
403
|
|
|
|
|
|
|
leaving a choice only of the core or pure-perl emulation implementations. |
404
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
$ perl Build.PL --pp |
406
|
|
|
|
|
|
|
|
407
|
|
|
|
|
|
|
or |
408
|
|
|
|
|
|
|
|
409
|
|
|
|
|
|
|
$ PERL_SOCKET_GETADDRINFO_NO_BUILD_XS=1 perl Build.PL |
410
|
|
|
|
|
|
|
|
411
|
|
|
|
|
|
|
=head1 BUGS |
412
|
|
|
|
|
|
|
|
413
|
|
|
|
|
|
|
=over 4 |
414
|
|
|
|
|
|
|
|
415
|
|
|
|
|
|
|
=item * |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
Appears to FAIL on older Darwin machines (e.g. C). The failure |
418
|
|
|
|
|
|
|
mode occurs in F and appears to relate to an endian bug; |
419
|
|
|
|
|
|
|
expecting to receive C<80> and instead receiving C<20480> (which is a 16-bit |
420
|
|
|
|
|
|
|
C<80> byte-swapped). |
421
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
=back |
423
|
|
|
|
|
|
|
|
424
|
|
|
|
|
|
|
=head1 SEE ALSO |
425
|
|
|
|
|
|
|
|
426
|
|
|
|
|
|
|
=over 4 |
427
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
=item * |
429
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
L - Basic Socket Interface Extensions for |
431
|
|
|
|
|
|
|
IPv6 |
432
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
=back |
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
436
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
Christian Hansen - for help with some XS features and Win32 |
438
|
|
|
|
|
|
|
build fixes. |
439
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
Zefram - for help with fixing some bugs in the XS code. |
441
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
Reini Urban - for help with older perls and more Win32 |
443
|
|
|
|
|
|
|
build fixes. |
444
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
=head1 AUTHOR |
446
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
Paul Evans |
448
|
|
|
|
|
|
|
|
449
|
|
|
|
|
|
|
=cut |
450
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
0x55AA; |