1NAME
2 `Socket::GetAddrInfo' - address-family independent name resolving
3 functions
4
5SYNOPSIS
6 use Socket qw( SOCK_STREAM );
7 use Socket::GetAddrInfo qw( getaddrinfo getnameinfo );
8 use IO::Socket;
9
10 my %hints = ( socktype => SOCK_STREAM );
11 my ( $err, @res ) = getaddrinfo( "www.google.com", "www", \%hints );
12
13 die "Cannot resolve name - $err" if $err;
14
15 my $sock;
16
17 foreach my $ai ( @res ) {
18 my $candidate = IO::Socket->new();
19
20 $candidate->socket( $ai->{family}, $ai->{socktype}, $ai->{protocol} )
21 or next;
22
23 $candidate->connect( $ai->{addr} )
24 or next;
25
26 $sock = $candidate;
27 last;
28 }
29
30 if( $sock ) {
31 my ( $err, $host, $service ) = getnameinfo( $sock->peername );
32 print "Connected to $host:$service\n" if !$err;
33 }
34
35DESCRIPTION
36 The RFC 2553 functions `getaddrinfo' and `getnameinfo' provide an
37 abstracted way to convert between a pair of host name/service name and
38 socket addresses, or vice versa. `getaddrinfo' converts names into a set
39 of arguments to pass to the `socket()' and `connect()' syscalls, and
40 `getnameinfo' converts a socket address back into its host name/service
41 name pair.
42
43 These functions provide a useful interface for performing either of
44 these name resolution operation, without having to deal with IPv4/IPv6
45 transparency, or whether the underlying host can support IPv6 at all, or
46 other such issues. However, not all platforms can support the underlying
47 calls at the C layer, which means a dilema for authors wishing to write
48 forward-compatible code. Either to support these functions, and cause
49 the code not to work on older platforms, or stick to the older "legacy"
50 resolvers such as `gethostbyname()', which means the code becomes more
51 portable.
52
53 This module attempts to solve this problem, by detecting at compiletime
54 whether the underlying OS will support these functions. If it does not,
55 the module will use pure-perl emulations of the functions using the
56 legacy resolver functions instead. The emulations support the same
57 interface as the real functions, and behave as close as is resonably
58 possible to emulate using the legacy resolvers. See
59 Socket::GetAddrInfo::Emul for details on the limits of this emulation.
60
61 As of Perl version 5.14.0, Perl already supports `getaddrinfo' in core.
62 On such a system, this module simply uses the functions provided by
63 `Socket', and does not need to use its own compiled XS, or pure-perl
64 legacy emulation.
65
66 As `Socket' in core now provides all the functions also provided by this
67 module, it is likely this may be the last released version of this
68 module. And code currently using this module would be advised to switch
69 to using core `Socket' instead.
70
71EXPORT TAGS
72 The following tags may be imported by `use Socket::GetAddrInfo qw( :tag
73 )':
74
75 AI Imports all of the `AI_*' constants for `getaddrinfo' flags
76
77 NI Imports all of the `NI_*' constants for `getnameinfo' flags
78
79 EAI Imports all of the `EAI_*' for error values
80
81 constants
82 Imports all of the above constants
83
84FUNCTIONS
85 ( $err, @res ) = getaddrinfo( $host, $service, $hints )
86 `getaddrinfo' turns human-readable text strings (containing hostnames,
87 numeric addresses, service names, or port numbers) into sets of binary
88 values containing socket-level representations of these addresses.
89
90 When given both host and service, this function attempts to resolve the
91 host name to a set of network addresses, and the service name into a
92 protocol and port number, and then returns a list of address structures
93 suitable to connect() to it.
94
95 When given just a host name, this function attempts to resolve it to a
96 set of network addresses, and then returns a list of these addresses in
97 the returned structures.
98
99 When given just a service name, this function attempts to resolve it to
100 a protocol and port number, and then returns a list of address
101 structures that represent it suitable to bind() to.
102
103 When given neither name, it generates an error.
104
105 The optional `$hints' parameter can be passed a HASH reference to
106 indicate how the results are generated. It may contain any of the
107 following four fields:
108
109 flags => INT
110 A bitfield containing `AI_*' constants. At least the following
111 flags will be available:
112
113 * `AI_PASSIVE'
114 Indicates that this resolution is for a local `bind()' for a
115 passive (i.e. listening) socket, rather than an active (i.e.
116 connecting) socket.
117
118 * `AI_CANONNAME'
119 Indicates that the caller wishes the canonical hostname
120 (`canonname') field of the result to be filled in.
121
122 * `AI_NUMERICHOST'
123 Indicates that the caller will pass a numeric address, rather
124 than a hostname, and that `getaddrinfo' must not perform a
125 resolve operation on this name. This flag will prevent a
126 possibly-slow network lookup operation, and instead return an
127 error, if a hostname is passed.
128
129 Other flags may be provided by the OS.
130
131 family => INT
132 Restrict to only generating addresses in this address family
133
134 socktype => INT
135 Restrict to only generating addresses of this socket type
136
137 protocol => INT
138 Restrict to only generating addresses for this protocol
139
140 Errors are indicated by the `$err' value returned; which will be
141 non-zero in numeric context, and contain a string error message as a
142 string. The value can be compared against any of the `EAI_*' constants
143 to determine what the error is. Rather than explicitly checking, see
144 also Socket::GetAddrInfo::Strict which provides functions that throw
145 exceptions on errors.
146
147 If no error occurs, `@res' will contain HASH references, each
148 representing one address. It will contain the following five fields:
149
150 family => INT
151 The address family (e.g. AF_INET)
152
153 socktype => INT
154 The socket type (e.g. SOCK_STREAM)
155
156 protocol => INT
157 The protocol (e.g. IPPROTO_TCP)
158
159 addr => STRING
160 The address in a packed string (such as would be returned by
161 pack_sockaddr_in)
162
163 canonname => STRING
164 The canonical name for the host if the `AI_CANONNAME' flag was
165 provided, or `undef' otherwise. This field will only be present
166 on the first returned address.
167
168 ( $err, $host, $service ) = getnameinfo( $addr, $flags, $xflags )
169 `getnameinfo' turns a binary socket address into a pair of
170 human-readable strings, containing the host name, numeric address,
171 service name, or port number.
172
173 The optional `$flags' parameter is a bitfield containing `NI_*'
174 constants. At least the following flags will be available:
175
176 * `NI_NUMERICHOST'
177 Requests that a human-readable string representation of the numeric
178 address is returned directly, rather than performing a name resolve
179 operation that may convert it into a hostname.
180
181 * `NI_NUMERICSERV'
182 Requests that the port number be returned directly as a number
183 representation rather than performing a name resolve operation that
184 may convert it into a service name.
185
186 * `NI_NAMEREQD'
187 If a name resolve operation fails to provide a name, then this flag
188 will cause `getnameinfo' to indicate an error, rather than returning
189 the numeric representation as a human-readable string.
190
191 * `NI_DGRAM'
192 Indicates that the socket address relates to a `SOCK_DGRAM' socket,
193 for the services whose name differs between `TCP' and `UDP' protocols.
194
195 Other flags may be provided by the OS.
196
197 The optional `$xflags' parameter is a bitfield containing `NIx_*'
198 constants. These are a Perl-level extension to the API, to indicate
199 extra information.
200
201 * `NIx_NOHOST'
202 Indicates that the caller is not interested in the hostname of the
203 result, so it does not have to be converted; `undef' will be returned
204 as the hostname.
205
206 * `NIx_NOSERV'
207 Indicates that the caller is not interested in the service name of the
208 result, so it does not have to be converted; `undef' will be returned
209 as the service name.
210
211 Errors are indicated by the `$err' value returned; which will be
212 non-zero in numeric context, and contain a string error message as a
213 string. The value can be compared against any of the `EAI_*' constants
214 to determine what the error is. Rather than explicitly checking, see
215 also Socket::GetAddrInfo::Strict which provides functions that throw
216 exceptions on errors.
217
218EXAMPLES
219 Lookup for `connect'
220 The `getaddrinfo' function converts a hostname and a service name into a
221 list of structures, each containing a potential way to `connect()' to
222 the named service on the named host.
223
224 my %hints = ( socktype => SOCK_STREAM );
225 my ( $err, @res ) = getaddrinfo( $hostname, $servicename, \%hints );
226 die "Cannot getaddrinfo - $err" if $err;
227
228 my $sock;
229
230 foreach my $ai ( @res ) {
231 my $candidate = IO::Socket->new();
232
233 $candidate->socket( $ai->{family}, $ai->{socktype}, $ai->{protocol} )
234 or next;
235
236 $candidate->connect( $ai->{addr} )
237 or next;
238
239 $sock = $candidate;
240 last;
241 }
242
243 Because a list of potential candidates is returned, the `while' loop
244 tries each in turn until it it finds one that succeeds both the
245 `socket()' and `connect()' calls.
246
247 This function performs the work of the legacy functions `gethostbyname',
248 `getservbyname', `inet_aton' and `pack_sockaddr_in'.
249
250 Making a human-readable string out of an address
251 The `getnameinfo' function converts a socket address, such as returned
252 by `getsockname' or `getpeername', into a pair of human-readable strings
253 representing the address and service name.
254
255 my ( $err, $hostname, $servicename ) = getnameinfo( $socket->peername );
256 die "Cannot getnameinfo - $err" if $err;
257
258 print "The peer is connected from $hostname\n";
259
260 Since in this example only the hostname was used, the redundant
261 conversion of the port number into a service name may be omitted by
262 passing the `NIx_NOSERV' flag.
263
264 my ( $err, $hostname ) = getnameinfo( $socket->peername, 0, NIx_NOSERV );
265
266 This function performs the work of the legacy functions
267 `unpack_sockaddr_in', `inet_ntoa', `gethostbyaddr' and `getservbyport'.
268
269 Resolving hostnames into IP addresses
270 To turn a hostname into a human-readable plain IP address use
271 `getaddrinfo' to turn the hostname into a list of socket structures,
272 then `getnameinfo' on each one to make it a readable IP address again.
273
274 my ( $err, @res ) = getaddrinfo( $hostname, "", { socktype => SOCK_RAW } );
275 die "Cannot getaddrinfo - $err" if $err;
276
277 while( my $ai = shift @res ) {
278 my ( $err, $ipaddr ) = getnameinfo( $ai->{addr}, NI_NUMERICHOST, NIx_NOSERV );
279 die "Cannot getnameinfo - $err" if $err;
280
281 print "$ipaddr\n";
282 }
283
284 The `socktype' hint to `getaddrinfo' filters the results to only include
285 one socket type and protocol. Without this most OSes return three
286 combinations, for `SOCK_STREAM', `SOCK_DGRAM' and `SOCK_RAW', resulting
287 in triplicate output of addresses. The `NI_NUMERICHOST' flag to
288 `getnameinfo' causes it to return a string-formatted plain IP address,
289 rather than reverse resolving it back into a hostname.
290
291 This combination performs the work of the legacy functions
292 `gethostbyname' and `inet_ntoa'.
293
294BUILDING WITHOUT XS CODE
295 In some environments it may be preferred not to build the XS
296 implementation, leaving a choice only of the core or pure-perl emulation
297 implementations.
298
299 $ perl Build.PL --pp
300
301 or
302
303 $ PERL_SOCKET_GETADDRINFO_NO_BUILD_XS=1 perl Build.PL
304
305BUGS
306 * Appears to FAIL on older Darwin machines (e.g. `osvers=8.11.1'). The
307 failure mode occurs in t/02getnameinfo.t and appears to relate to an
308 endian bug; expecting to receive `80' and instead receiving `20480'
309 (which is a 16-bit `80' byte-swapped).
310
311SEE ALSO
312 * http://tools.ietf.org/html/rfc2553 - Basic Socket Interface
313 Extensions for IPv6
314
315ACKNOWLEDGEMENTS
316 Christian Hansen <chansen@cpan.org> - for help with some XS features and
317 Win32 build fixes.
318
319 Zefram <zefram@fysh.org> - for help with fixing some bugs in the XS
320 code.
321
322 Reini Urban <rurban@cpan.org> - for help with older perls and more Win32
323 build fixes.
324
325AUTHOR
326 Paul Evans <leonerd@leonerd.org.uk>
327
328