xref: /openbsd/gnu/usr.bin/perl/cpan/Socket/Socket.pm (revision 3d61058a)
1898184e3Ssthenpackage Socket;
2898184e3Ssthen
3*3d61058aSafresh1use v5.6.1;
4898184e3Ssthenuse strict;
5898184e3Ssthen
6*3d61058aSafresh1our $VERSION = '2.038';
7898184e3Ssthen
8898184e3Ssthen=head1 NAME
9898184e3Ssthen
10898184e3SsthenC<Socket> - networking constants and support functions
11898184e3Ssthen
12898184e3Ssthen=head1 SYNOPSIS
13898184e3Ssthen
14898184e3SsthenC<Socket> a low-level module used by, among other things, the L<IO::Socket>
15898184e3Ssthenfamily of modules. The following examples demonstrate some low-level uses but
16898184e3Ssthena practical program would likely use the higher-level API provided by
17898184e3SsthenC<IO::Socket> or similar instead.
18898184e3Ssthen
19898184e3Ssthen    use Socket qw(PF_INET SOCK_STREAM pack_sockaddr_in inet_aton);
20898184e3Ssthen
21898184e3Ssthen    socket(my $socket, PF_INET, SOCK_STREAM, 0)
22898184e3Ssthen        or die "socket: $!";
23898184e3Ssthen
24898184e3Ssthen    my $port = getservbyname "echo", "tcp";
25898184e3Ssthen    connect($socket, pack_sockaddr_in($port, inet_aton("localhost")))
26898184e3Ssthen        or die "connect: $!";
27898184e3Ssthen
28898184e3Ssthen    print $socket "Hello, world!\n";
29898184e3Ssthen    print <$socket>;
30898184e3Ssthen
31898184e3SsthenSee also the L</EXAMPLES> section.
32898184e3Ssthen
33898184e3Ssthen=head1 DESCRIPTION
34898184e3Ssthen
35898184e3SsthenThis module provides a variety of constants, structure manipulators and other
36898184e3Ssthenfunctions related to socket-based networking. The values and functions
37898184e3Ssthenprovided are useful when used in conjunction with Perl core functions such as
38898184e3Ssthensocket(), setsockopt() and bind(). It also provides several other support
39898184e3Ssthenfunctions, mostly for dealing with conversions of network addresses between
40898184e3Ssthenhuman-readable and native binary forms, and for hostname resolver operations.
41898184e3Ssthen
42898184e3SsthenSome constants and functions are exported by default by this module; but for
43898184e3Ssthenbackward-compatibility any recently-added symbols are not exported by default
44898184e3Ssthenand must be requested explicitly. When an import list is provided to the
45898184e3SsthenC<use Socket> line, the default exports are not automatically imported. It is
46898184e3Ssthentherefore best practice to always to explicitly list all the symbols required.
47898184e3Ssthen
48898184e3SsthenAlso, some common socket "newline" constants are provided: the constants
49898184e3SsthenC<CR>, C<LF>, and C<CRLF>, as well as C<$CR>, C<$LF>, and C<$CRLF>, which map
50898184e3Ssthento C<\015>, C<\012>, and C<\015\012>. If you do not want to use the literal
51898184e3Ssthencharacters in your programs, then use the constants provided here. They are
52898184e3Ssthennot exported by default, but can be imported individually, and with the
53898184e3SsthenC<:crlf> export tag:
54898184e3Ssthen
55898184e3Ssthen    use Socket qw(:DEFAULT :crlf);
56898184e3Ssthen
57898184e3Ssthen    $sock->print("GET / HTTP/1.0$CRLF");
58898184e3Ssthen
59898184e3SsthenThe entire getaddrinfo() subsystem can be exported using the tag C<:addrinfo>;
60898184e3Ssthenthis exports the getaddrinfo() and getnameinfo() functions, and all the
61898184e3SsthenC<AI_*>, C<NI_*>, C<NIx_*> and C<EAI_*> constants.
62898184e3Ssthen
63898184e3Ssthen=cut
64898184e3Ssthen
65898184e3Ssthen=head1 CONSTANTS
66898184e3Ssthen
67898184e3SsthenIn each of the following groups, there may be many more constants provided
68898184e3Ssthenthan just the ones given as examples in the section heading. If the heading
69898184e3Ssthenends C<...> then this means there are likely more; the exact constants
70898184e3Ssthenprovided will depend on the OS and headers found at compile-time.
71898184e3Ssthen
72898184e3Ssthen=cut
73898184e3Ssthen
74898184e3Ssthen=head2 PF_INET, PF_INET6, PF_UNIX, ...
75898184e3Ssthen
76898184e3SsthenProtocol family constants to use as the first argument to socket() or the
77898184e3Ssthenvalue of the C<SO_DOMAIN> or C<SO_FAMILY> socket option.
78898184e3Ssthen
79898184e3Ssthen=head2 AF_INET, AF_INET6, AF_UNIX, ...
80898184e3Ssthen
81898184e3SsthenAddress family constants used by the socket address structures, to pass to
82898184e3Ssthensuch functions as inet_pton() or getaddrinfo(), or are returned by such
83898184e3Ssthenfunctions as sockaddr_family().
84898184e3Ssthen
85898184e3Ssthen=head2 SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, ...
86898184e3Ssthen
87898184e3SsthenSocket type constants to use as the second argument to socket(), or the value
88898184e3Ssthenof the C<SO_TYPE> socket option.
89898184e3Ssthen
9091f110e0Safresh1=head2 SOCK_NONBLOCK. SOCK_CLOEXEC
9191f110e0Safresh1
9291f110e0Safresh1Linux-specific shortcuts to specify the C<O_NONBLOCK> and C<FD_CLOEXEC> flags
9391f110e0Safresh1during a C<socket(2)> call.
9491f110e0Safresh1
9591f110e0Safresh1    socket( my $sockh, PF_INET, SOCK_DGRAM|SOCK_NONBLOCK, 0 )
9691f110e0Safresh1
97898184e3Ssthen=head2 SOL_SOCKET
98898184e3Ssthen
99898184e3SsthenSocket option level constant for setsockopt() and getsockopt().
100898184e3Ssthen
101898184e3Ssthen=head2 SO_ACCEPTCONN, SO_BROADCAST, SO_ERROR, ...
102898184e3Ssthen
103898184e3SsthenSocket option name constants for setsockopt() and getsockopt() at the
104898184e3SsthenC<SOL_SOCKET> level.
105898184e3Ssthen
106898184e3Ssthen=head2 IP_OPTIONS, IP_TOS, IP_TTL, ...
107898184e3Ssthen
108898184e3SsthenSocket option name constants for IPv4 socket options at the C<IPPROTO_IP>
109898184e3Ssthenlevel.
110898184e3Ssthen
1119f11ffb7Safresh1=head2 IP_PMTUDISC_WANT, IP_PMTUDISC_DONT, ...
1129f11ffb7Safresh1
113eac174f2Safresh1Socket option value constants for C<IP_MTU_DISCOVER> socket option.
1149f11ffb7Safresh1
1156fb12b70Safresh1=head2 IPTOS_LOWDELAY, IPTOS_THROUGHPUT, IPTOS_RELIABILITY, ...
1166fb12b70Safresh1
1176fb12b70Safresh1Socket option value constants for C<IP_TOS> socket option.
1186fb12b70Safresh1
119898184e3Ssthen=head2 MSG_BCAST, MSG_OOB, MSG_TRUNC, ...
120898184e3Ssthen
121898184e3SsthenMessage flag constants for send() and recv().
122898184e3Ssthen
123898184e3Ssthen=head2 SHUT_RD, SHUT_RDWR, SHUT_WR
124898184e3Ssthen
125898184e3SsthenDirection constants for shutdown().
126898184e3Ssthen
127898184e3Ssthen=head2 INADDR_ANY, INADDR_BROADCAST, INADDR_LOOPBACK, INADDR_NONE
128898184e3Ssthen
129898184e3SsthenConstants giving the special C<AF_INET> addresses for wildcard, broadcast,
130898184e3Ssthenlocal loopback, and invalid addresses.
131898184e3Ssthen
132898184e3SsthenNormally equivalent to inet_aton('0.0.0.0'), inet_aton('255.255.255.255'),
133898184e3Sstheninet_aton('localhost') and inet_aton('255.255.255.255') respectively.
134898184e3Ssthen
135898184e3Ssthen=head2 IPPROTO_IP, IPPROTO_IPV6, IPPROTO_TCP, ...
136898184e3Ssthen
137898184e3SsthenIP protocol constants to use as the third argument to socket(), the level
138898184e3Ssthenargument to getsockopt() or setsockopt(), or the value of the C<SO_PROTOCOL>
139898184e3Ssthensocket option.
140898184e3Ssthen
141898184e3Ssthen=head2 TCP_CORK, TCP_KEEPALIVE, TCP_NODELAY, ...
142898184e3Ssthen
143898184e3SsthenSocket option name constants for TCP socket options at the C<IPPROTO_TCP>
144898184e3Ssthenlevel.
145898184e3Ssthen
146898184e3Ssthen=head2 IN6ADDR_ANY, IN6ADDR_LOOPBACK
147898184e3Ssthen
148898184e3SsthenConstants giving the special C<AF_INET6> addresses for wildcard and local
149898184e3Ssthenloopback.
150898184e3Ssthen
151898184e3SsthenNormally equivalent to inet_pton(AF_INET6, "::") and
152898184e3Sstheninet_pton(AF_INET6, "::1") respectively.
153898184e3Ssthen
154898184e3Ssthen=head2 IPV6_ADD_MEMBERSHIP, IPV6_MTU, IPV6_V6ONLY, ...
155898184e3Ssthen
156898184e3SsthenSocket option name constants for IPv6 socket options at the C<IPPROTO_IPV6>
157898184e3Ssthenlevel.
158898184e3Ssthen
159898184e3Ssthen=cut
160898184e3Ssthen
161898184e3Ssthen# Still undocumented: SCM_*, SOMAXCONN, IOV_MAX, UIO_MAXIOV
162898184e3Ssthen
163898184e3Ssthen=head1 STRUCTURE MANIPULATORS
164898184e3Ssthen
165898184e3SsthenThe following functions convert between lists of Perl values and packed binary
166898184e3Ssthenstrings representing structures.
167898184e3Ssthen
168898184e3Ssthen=cut
169898184e3Ssthen
170898184e3Ssthen=head2 $family = sockaddr_family $sockaddr
171898184e3Ssthen
172898184e3SsthenTakes a packed socket address (as returned by pack_sockaddr_in(),
173898184e3Ssthenpack_sockaddr_un() or the perl builtin functions getsockname() and
174898184e3Ssthengetpeername()). Returns the address family tag. This will be one of the
175898184e3SsthenC<AF_*> constants, such as C<AF_INET> for a C<sockaddr_in> addresses or
176898184e3SsthenC<AF_UNIX> for a C<sockaddr_un>. It can be used to figure out what unpack to
177898184e3Ssthenuse for a sockaddr of unknown type.
178898184e3Ssthen
179898184e3Ssthen=head2 $sockaddr = pack_sockaddr_in $port, $ip_address
180898184e3Ssthen
181898184e3SsthenTakes two arguments, a port number and an opaque string (as returned by
182898184e3Sstheninet_aton(), or a v-string). Returns the C<sockaddr_in> structure with those
183898184e3Ssthenarguments packed in and C<AF_INET> filled in. For Internet domain sockets,
184898184e3Ssthenthis structure is normally what you need for the arguments in bind(),
185898184e3Ssthenconnect(), and send().
186898184e3Ssthen
1879f11ffb7Safresh1An undefined $port argument is taken as zero; an undefined $ip_address is
1889f11ffb7Safresh1considered a fatal error.
1899f11ffb7Safresh1
190898184e3Ssthen=head2 ($port, $ip_address) = unpack_sockaddr_in $sockaddr
191898184e3Ssthen
192898184e3SsthenTakes a C<sockaddr_in> structure (as returned by pack_sockaddr_in(),
193898184e3Ssthengetpeername() or recv()). Returns a list of two elements: the port and an
194898184e3Ssthenopaque string representing the IP address (you can use inet_ntoa() to convert
195898184e3Ssthenthe address to the four-dotted numeric format). Will croak if the structure
196898184e3Ssthendoes not represent an C<AF_INET> address.
197898184e3Ssthen
19891f110e0Safresh1In scalar context will return just the IP address.
19991f110e0Safresh1
200898184e3Ssthen=head2 $sockaddr = sockaddr_in $port, $ip_address
201898184e3Ssthen
202898184e3Ssthen=head2 ($port, $ip_address) = sockaddr_in $sockaddr
203898184e3Ssthen
204898184e3SsthenA wrapper of pack_sockaddr_in() or unpack_sockaddr_in(). In list context,
205898184e3Ssthenunpacks its argument and returns a list consisting of the port and IP address.
206898184e3SsthenIn scalar context, packs its port and IP address arguments as a C<sockaddr_in>
207898184e3Ssthenand returns it.
208898184e3Ssthen
209898184e3SsthenProvided largely for legacy compatibility; it is better to use
210898184e3Ssthenpack_sockaddr_in() or unpack_sockaddr_in() explicitly.
211898184e3Ssthen
212898184e3Ssthen=head2 $sockaddr = pack_sockaddr_in6 $port, $ip6_address, [$scope_id, [$flowinfo]]
213898184e3Ssthen
214898184e3SsthenTakes two to four arguments, a port number, an opaque string (as returned by
215898184e3Sstheninet_pton()), optionally a scope ID number, and optionally a flow label
216898184e3Ssthennumber. Returns the C<sockaddr_in6> structure with those arguments packed in
217898184e3Ssthenand C<AF_INET6> filled in. IPv6 equivalent of pack_sockaddr_in().
218898184e3Ssthen
2199f11ffb7Safresh1An undefined $port argument is taken as zero; an undefined $ip6_address is
2209f11ffb7Safresh1considered a fatal error.
2219f11ffb7Safresh1
222898184e3Ssthen=head2 ($port, $ip6_address, $scope_id, $flowinfo) = unpack_sockaddr_in6 $sockaddr
223898184e3Ssthen
224898184e3SsthenTakes a C<sockaddr_in6> structure. Returns a list of four elements: the port
225898184e3Ssthennumber, an opaque string representing the IPv6 address, the scope ID, and the
226898184e3Ssthenflow label. (You can use inet_ntop() to convert the address to the usual
227898184e3Ssthenstring format). Will croak if the structure does not represent an C<AF_INET6>
228898184e3Ssthenaddress.
229898184e3Ssthen
23091f110e0Safresh1In scalar context will return just the IP address.
23191f110e0Safresh1
232898184e3Ssthen=head2 $sockaddr = sockaddr_in6 $port, $ip6_address, [$scope_id, [$flowinfo]]
233898184e3Ssthen
234898184e3Ssthen=head2 ($port, $ip6_address, $scope_id, $flowinfo) = sockaddr_in6 $sockaddr
235898184e3Ssthen
236898184e3SsthenA wrapper of pack_sockaddr_in6() or unpack_sockaddr_in6(). In list context,
237898184e3Ssthenunpacks its argument according to unpack_sockaddr_in6(). In scalar context,
238898184e3Ssthenpacks its arguments according to pack_sockaddr_in6().
239898184e3Ssthen
240898184e3SsthenProvided largely for legacy compatibility; it is better to use
241898184e3Ssthenpack_sockaddr_in6() or unpack_sockaddr_in6() explicitly.
242898184e3Ssthen
243898184e3Ssthen=head2 $sockaddr = pack_sockaddr_un $path
244898184e3Ssthen
245898184e3SsthenTakes one argument, a pathname. Returns the C<sockaddr_un> structure with that
246898184e3Ssthenpath packed in with C<AF_UNIX> filled in. For C<PF_UNIX> sockets, this
247898184e3Ssthenstructure is normally what you need for the arguments in bind(), connect(),
248898184e3Ssthenand send().
249898184e3Ssthen
250898184e3Ssthen=head2 ($path) = unpack_sockaddr_un $sockaddr
251898184e3Ssthen
252898184e3SsthenTakes a C<sockaddr_un> structure (as returned by pack_sockaddr_un(),
253898184e3Ssthengetpeername() or recv()). Returns a list of one element: the pathname. Will
254898184e3Ssthencroak if the structure does not represent an C<AF_UNIX> address.
255898184e3Ssthen
256898184e3Ssthen=head2 $sockaddr = sockaddr_un $path
257898184e3Ssthen
258898184e3Ssthen=head2 ($path) = sockaddr_un $sockaddr
259898184e3Ssthen
260898184e3SsthenA wrapper of pack_sockaddr_un() or unpack_sockaddr_un(). In a list context,
261898184e3Ssthenunpacks its argument and returns a list consisting of the pathname. In a
262898184e3Ssthenscalar context, packs its pathname as a C<sockaddr_un> and returns it.
263898184e3Ssthen
264898184e3SsthenProvided largely for legacy compatibility; it is better to use
265898184e3Ssthenpack_sockaddr_un() or unpack_sockaddr_un() explicitly.
266898184e3Ssthen
267898184e3SsthenThese are only supported if your system has E<lt>F<sys/un.h>E<gt>.
268898184e3Ssthen
26991f110e0Safresh1=head2 $ip_mreq = pack_ip_mreq $multiaddr, $interface
270898184e3Ssthen
27191f110e0Safresh1Takes an IPv4 multicast address and optionally an interface address (or
27291f110e0Safresh1C<INADDR_ANY>). Returns the C<ip_mreq> structure with those arguments packed
27391f110e0Safresh1in. Suitable for use with the C<IP_ADD_MEMBERSHIP> and C<IP_DROP_MEMBERSHIP>
27491f110e0Safresh1sockopts.
275898184e3Ssthen
27691f110e0Safresh1=head2 ($multiaddr, $interface) = unpack_ip_mreq $ip_mreq
27791f110e0Safresh1
27891f110e0Safresh1Takes an C<ip_mreq> structure. Returns a list of two elements; the IPv4
27991f110e0Safresh1multicast address and interface address.
28091f110e0Safresh1
28191f110e0Safresh1=head2 $ip_mreq_source = pack_ip_mreq_source $multiaddr, $source, $interface
28291f110e0Safresh1
28391f110e0Safresh1Takes an IPv4 multicast address, source address, and optionally an interface
28491f110e0Safresh1address (or C<INADDR_ANY>). Returns the C<ip_mreq_source> structure with those
28591f110e0Safresh1arguments packed in. Suitable for use with the C<IP_ADD_SOURCE_MEMBERSHIP>
28691f110e0Safresh1and C<IP_DROP_SOURCE_MEMBERSHIP> sockopts.
28791f110e0Safresh1
28891f110e0Safresh1=head2 ($multiaddr, $source, $interface) = unpack_ip_mreq_source $ip_mreq
28991f110e0Safresh1
29091f110e0Safresh1Takes an C<ip_mreq_source> structure. Returns a list of three elements; the
29191f110e0Safresh1IPv4 multicast address, source address and interface address.
29291f110e0Safresh1
29391f110e0Safresh1=head2 $ipv6_mreq = pack_ipv6_mreq $multiaddr6, $ifindex
29491f110e0Safresh1
29591f110e0Safresh1Takes an IPv6 multicast address and an interface number. Returns the
29691f110e0Safresh1C<ipv6_mreq> structure with those arguments packed in. Suitable for use with
29791f110e0Safresh1the C<IPV6_ADD_MEMBERSHIP> and C<IPV6_DROP_MEMBERSHIP> sockopts.
29891f110e0Safresh1
29991f110e0Safresh1=head2 ($multiaddr6, $ifindex) = unpack_ipv6_mreq $ipv6_mreq
300898184e3Ssthen
301898184e3SsthenTakes an C<ipv6_mreq> structure. Returns a list of two elements; the IPv6
302898184e3Ssthenaddress and an interface number.
303898184e3Ssthen
304898184e3Ssthen=cut
305898184e3Ssthen
306898184e3Ssthen=head1 FUNCTIONS
307898184e3Ssthen
308898184e3Ssthen=cut
309898184e3Ssthen
310898184e3Ssthen=head2 $ip_address = inet_aton $string
311898184e3Ssthen
312898184e3SsthenTakes a string giving the name of a host, or a textual representation of an IP
313898184e3Ssthenaddress and translates that to an packed binary address structure suitable to
314898184e3Ssthenpass to pack_sockaddr_in(). If passed a hostname that cannot be resolved,
315898184e3Ssthenreturns C<undef>. For multi-homed hosts (hosts with more than one address),
316898184e3Ssthenthe first address found is returned.
317898184e3Ssthen
318898184e3SsthenFor portability do not assume that the result of inet_aton() is 32 bits wide,
319898184e3Ssthenin other words, that it would contain only the IPv4 address in network order.
320898184e3Ssthen
321898184e3SsthenThis IPv4-only function is provided largely for legacy reasons. Newly-written
322898184e3Ssthencode should use getaddrinfo() or inet_pton() instead for IPv6 support.
323898184e3Ssthen
324898184e3Ssthen=head2 $string = inet_ntoa $ip_address
325898184e3Ssthen
326898184e3SsthenTakes a packed binary address structure such as returned by
327898184e3Ssthenunpack_sockaddr_in() (or a v-string representing the four octets of the IPv4
328898184e3Ssthenaddress in network order) and translates it into a string of the form
329898184e3SsthenC<d.d.d.d> where the C<d>s are numbers less than 256 (the normal
330898184e3Ssthenhuman-readable four dotted number notation for Internet addresses).
331898184e3Ssthen
332898184e3SsthenThis IPv4-only function is provided largely for legacy reasons. Newly-written
333898184e3Ssthencode should use getnameinfo() or inet_ntop() instead for IPv6 support.
334898184e3Ssthen
335898184e3Ssthen=head2 $address = inet_pton $family, $string
336898184e3Ssthen
337898184e3SsthenTakes an address family (such as C<AF_INET> or C<AF_INET6>) and a string
338898184e3Ssthencontaining a textual representation of an address in that family and
339898184e3Ssthentranslates that to an packed binary address structure.
340898184e3Ssthen
341898184e3SsthenSee also getaddrinfo() for a more powerful and flexible function to look up
342898184e3Ssthensocket addresses given hostnames or textual addresses.
343898184e3Ssthen
344898184e3Ssthen=head2 $string = inet_ntop $family, $address
345898184e3Ssthen
346898184e3SsthenTakes an address family and a packed binary address structure and translates
347898184e3Ssthenit into a human-readable textual representation of the address; typically in
348898184e3SsthenC<d.d.d.d> form for C<AF_INET> or C<hhhh:hhhh::hhhh> form for C<AF_INET6>.
349898184e3Ssthen
350898184e3SsthenSee also getnameinfo() for a more powerful and flexible function to turn
351898184e3Ssthensocket addresses into human-readable textual representations.
352898184e3Ssthen
353898184e3Ssthen=head2 ($err, @result) = getaddrinfo $host, $service, [$hints]
354898184e3Ssthen
355898184e3SsthenGiven both a hostname and service name, this function attempts to resolve the
356898184e3Ssthenhost name into a list of network addresses, and the service name into a
357898184e3Ssthenprotocol and port number, and then returns a list of address structures
358898184e3Ssthensuitable to connect() to it.
359898184e3Ssthen
360898184e3SsthenGiven just a host name, this function attempts to resolve it to a list of
361898184e3Ssthennetwork addresses, and then returns a list of address structures giving these
362898184e3Ssthenaddresses.
363898184e3Ssthen
364898184e3SsthenGiven just a service name, this function attempts to resolve it to a protocol
365898184e3Ssthenand port number, and then returns a list of address structures that represent
366898184e3Ssthenit suitable to bind() to. This use should be combined with the C<AI_PASSIVE>
367898184e3Ssthenflag; see below.
368898184e3Ssthen
369898184e3SsthenGiven neither name, it generates an error.
370898184e3Ssthen
371898184e3SsthenIf present, $hints should be a reference to a hash, where the following keys
372898184e3Ssthenare recognised:
373898184e3Ssthen
374898184e3Ssthen=over 4
375898184e3Ssthen
376898184e3Ssthen=item flags => INT
377898184e3Ssthen
378898184e3SsthenA bitfield containing C<AI_*> constants; see below.
379898184e3Ssthen
380898184e3Ssthen=item family => INT
381898184e3Ssthen
382898184e3SsthenRestrict to only generating addresses in this address family
383898184e3Ssthen
384898184e3Ssthen=item socktype => INT
385898184e3Ssthen
386898184e3SsthenRestrict to only generating addresses of this socket type
387898184e3Ssthen
388898184e3Ssthen=item protocol => INT
389898184e3Ssthen
390898184e3SsthenRestrict to only generating addresses for this protocol
391898184e3Ssthen
392898184e3Ssthen=back
393898184e3Ssthen
394898184e3SsthenThe return value will be a list; the first value being an error indication,
395898184e3Ssthenfollowed by a list of address structures (if no error occurred).
396898184e3Ssthen
3979f11ffb7Safresh1The error value will be a dualvar; comparable to the C<EAI_*> error constants,
398898184e3Ssthenor printable as a human-readable error message string. If no error occurred it
399898184e3Ssthenwill be zero numerically and an empty string.
400898184e3Ssthen
401898184e3SsthenEach value in the results list will be a hash reference containing the following
402898184e3Ssthenfields:
403898184e3Ssthen
404898184e3Ssthen=over 4
405898184e3Ssthen
406898184e3Ssthen=item family => INT
407898184e3Ssthen
408898184e3SsthenThe address family (e.g. C<AF_INET>)
409898184e3Ssthen
410898184e3Ssthen=item socktype => INT
411898184e3Ssthen
412898184e3SsthenThe socket type (e.g. C<SOCK_STREAM>)
413898184e3Ssthen
414898184e3Ssthen=item protocol => INT
415898184e3Ssthen
416898184e3SsthenThe protocol (e.g. C<IPPROTO_TCP>)
417898184e3Ssthen
418898184e3Ssthen=item addr => STRING
419898184e3Ssthen
420898184e3SsthenThe address in a packed string (such as would be returned by
421898184e3Ssthenpack_sockaddr_in())
422898184e3Ssthen
423898184e3Ssthen=item canonname => STRING
424898184e3Ssthen
425898184e3SsthenThe canonical name for the host if the C<AI_CANONNAME> flag was provided, or
426898184e3SsthenC<undef> otherwise. This field will only be present on the first returned
427898184e3Ssthenaddress.
428898184e3Ssthen
429898184e3Ssthen=back
430898184e3Ssthen
431898184e3SsthenThe following flag constants are recognised in the $hints hash. Other flag
432898184e3Ssthenconstants may exist as provided by the OS.
433898184e3Ssthen
434898184e3Ssthen=over 4
435898184e3Ssthen
436898184e3Ssthen=item AI_PASSIVE
437898184e3Ssthen
438898184e3SsthenIndicates that this resolution is for a local bind() for a passive (i.e.
439898184e3Ssthenlistening) socket, rather than an active (i.e. connecting) socket.
440898184e3Ssthen
441898184e3Ssthen=item AI_CANONNAME
442898184e3Ssthen
443898184e3SsthenIndicates that the caller wishes the canonical hostname (C<canonname>) field
444898184e3Ssthenof the result to be filled in.
445898184e3Ssthen
446898184e3Ssthen=item AI_NUMERICHOST
447898184e3Ssthen
448898184e3SsthenIndicates that the caller will pass a numeric address, rather than a hostname,
449898184e3Ssthenand that getaddrinfo() must not perform a resolve operation on this name. This
450898184e3Ssthenflag will prevent a possibly-slow network lookup operation, and instead return
451898184e3Ssthenan error if a hostname is passed.
452898184e3Ssthen
453898184e3Ssthen=back
454898184e3Ssthen
455898184e3Ssthen=head2 ($err, $hostname, $servicename) = getnameinfo $sockaddr, [$flags, [$xflags]]
456898184e3Ssthen
457898184e3SsthenGiven a packed socket address (such as from getsockname(), getpeername(), or
458898184e3Ssthenreturned by getaddrinfo() in a C<addr> field), returns the hostname and
459898184e3Ssthensymbolic service name it represents. $flags may be a bitmask of C<NI_*>
460898184e3Ssthenconstants, or defaults to 0 if unspecified.
461898184e3Ssthen
462898184e3SsthenThe return value will be a list; the first value being an error condition,
463898184e3Ssthenfollowed by the hostname and service name.
464898184e3Ssthen
4659f11ffb7Safresh1The error value will be a dualvar; comparable to the C<EAI_*> error constants,
466898184e3Ssthenor printable as a human-readable error message string. The host and service
467898184e3Ssthennames will be plain strings.
468898184e3Ssthen
469898184e3SsthenThe following flag constants are recognised as $flags. Other flag constants may
470898184e3Ssthenexist as provided by the OS.
471898184e3Ssthen
472898184e3Ssthen=over 4
473898184e3Ssthen
474898184e3Ssthen=item NI_NUMERICHOST
475898184e3Ssthen
476898184e3SsthenRequests that a human-readable string representation of the numeric address be
477898184e3Ssthenreturned directly, rather than performing a name resolve operation that may
478898184e3Ssthenconvert it into a hostname. This will also avoid potentially-blocking network
479898184e3SsthenIO.
480898184e3Ssthen
481898184e3Ssthen=item NI_NUMERICSERV
482898184e3Ssthen
483898184e3SsthenRequests that the port number be returned directly as a number representation
484898184e3Ssthenrather than performing a name resolve operation that may convert it into a
485898184e3Ssthenservice name.
486898184e3Ssthen
487898184e3Ssthen=item NI_NAMEREQD
488898184e3Ssthen
489898184e3SsthenIf a name resolve operation fails to provide a name, then this flag will cause
490898184e3Ssthengetnameinfo() to indicate an error, rather than returning the numeric
491898184e3Ssthenrepresentation as a human-readable string.
492898184e3Ssthen
493898184e3Ssthen=item NI_DGRAM
494898184e3Ssthen
495898184e3SsthenIndicates that the socket address relates to a C<SOCK_DGRAM> socket, for the
496898184e3Ssthenservices whose name differs between TCP and UDP protocols.
497898184e3Ssthen
498898184e3Ssthen=back
499898184e3Ssthen
500898184e3SsthenThe following constants may be supplied as $xflags.
501898184e3Ssthen
502898184e3Ssthen=over 4
503898184e3Ssthen
504898184e3Ssthen=item NIx_NOHOST
505898184e3Ssthen
506898184e3SsthenIndicates that the caller is not interested in the hostname of the result, so
507898184e3Ssthenit does not have to be converted. C<undef> will be returned as the hostname.
508898184e3Ssthen
509898184e3Ssthen=item NIx_NOSERV
510898184e3Ssthen
511898184e3SsthenIndicates that the caller is not interested in the service name of the result,
512898184e3Ssthenso it does not have to be converted. C<undef> will be returned as the service
513898184e3Ssthenname.
514898184e3Ssthen
515898184e3Ssthen=back
516898184e3Ssthen
517898184e3Ssthen=head1 getaddrinfo() / getnameinfo() ERROR CONSTANTS
518898184e3Ssthen
519898184e3SsthenThe following constants may be returned by getaddrinfo() or getnameinfo().
520898184e3SsthenOthers may be provided by the OS.
521898184e3Ssthen
522898184e3Ssthen=over 4
523898184e3Ssthen
524898184e3Ssthen=item EAI_AGAIN
525898184e3Ssthen
526898184e3SsthenA temporary failure occurred during name resolution. The operation may be
527898184e3Ssthensuccessful if it is retried later.
528898184e3Ssthen
529898184e3Ssthen=item EAI_BADFLAGS
530898184e3Ssthen
531898184e3SsthenThe value of the C<flags> hint to getaddrinfo(), or the $flags parameter to
532898184e3Ssthengetnameinfo() contains unrecognised flags.
533898184e3Ssthen
534898184e3Ssthen=item EAI_FAMILY
535898184e3Ssthen
536898184e3SsthenThe C<family> hint to getaddrinfo(), or the family of the socket address
537898184e3Ssthenpassed to getnameinfo() is not supported.
538898184e3Ssthen
539898184e3Ssthen=item EAI_NODATA
540898184e3Ssthen
541898184e3SsthenThe host name supplied to getaddrinfo() did not provide any usable address
542898184e3Ssthendata.
543898184e3Ssthen
544898184e3Ssthen=item EAI_NONAME
545898184e3Ssthen
546898184e3SsthenThe host name supplied to getaddrinfo() does not exist, or the address
547898184e3Ssthensupplied to getnameinfo() is not associated with a host name and the
548898184e3SsthenC<NI_NAMEREQD> flag was supplied.
549898184e3Ssthen
550898184e3Ssthen=item EAI_SERVICE
551898184e3Ssthen
552898184e3SsthenThe service name supplied to getaddrinfo() is not available for the socket
553898184e3Ssthentype given in the $hints.
554898184e3Ssthen
555898184e3Ssthen=back
556898184e3Ssthen
557898184e3Ssthen=cut
558898184e3Ssthen
559898184e3Ssthen=head1 EXAMPLES
560898184e3Ssthen
561898184e3Ssthen=head2 Lookup for connect()
562898184e3Ssthen
563898184e3SsthenThe getaddrinfo() function converts a hostname and a service name into a list
564898184e3Ssthenof structures, each containing a potential way to connect() to the named
565898184e3Ssthenservice on the named host.
566898184e3Ssthen
567898184e3Ssthen    use IO::Socket;
568898184e3Ssthen    use Socket qw(SOCK_STREAM getaddrinfo);
569898184e3Ssthen
570898184e3Ssthen    my %hints = (socktype => SOCK_STREAM);
571898184e3Ssthen    my ($err, @res) = getaddrinfo("localhost", "echo", \%hints);
572898184e3Ssthen    die "Cannot getaddrinfo - $err" if $err;
573898184e3Ssthen
574898184e3Ssthen    my $sock;
575898184e3Ssthen
576898184e3Ssthen    foreach my $ai (@res) {
577898184e3Ssthen        my $candidate = IO::Socket->new();
578898184e3Ssthen
579898184e3Ssthen        $candidate->socket($ai->{family}, $ai->{socktype}, $ai->{protocol})
580898184e3Ssthen            or next;
581898184e3Ssthen
582898184e3Ssthen        $candidate->connect($ai->{addr})
583898184e3Ssthen            or next;
584898184e3Ssthen
585898184e3Ssthen        $sock = $candidate;
586898184e3Ssthen        last;
587898184e3Ssthen    }
588898184e3Ssthen
589898184e3Ssthen    die "Cannot connect to localhost:echo" unless $sock;
590898184e3Ssthen
591898184e3Ssthen    $sock->print("Hello, world!\n");
592898184e3Ssthen    print <$sock>;
593898184e3Ssthen
594898184e3SsthenBecause a list of potential candidates is returned, the C<while> loop tries
5956fb12b70Safresh1each in turn until it finds one that succeeds both the socket() and connect()
5966fb12b70Safresh1calls.
597898184e3Ssthen
598898184e3SsthenThis function performs the work of the legacy functions gethostbyname(),
599898184e3Ssthengetservbyname(), inet_aton() and pack_sockaddr_in().
600898184e3Ssthen
601898184e3SsthenIn practice this logic is better performed by L<IO::Socket::IP>.
602898184e3Ssthen
603898184e3Ssthen=head2 Making a human-readable string out of an address
604898184e3Ssthen
605898184e3SsthenThe getnameinfo() function converts a socket address, such as returned by
606898184e3Ssthengetsockname() or getpeername(), into a pair of human-readable strings
607898184e3Ssthenrepresenting the address and service name.
608898184e3Ssthen
609898184e3Ssthen    use IO::Socket::IP;
610898184e3Ssthen    use Socket qw(getnameinfo);
611898184e3Ssthen
612898184e3Ssthen    my $server = IO::Socket::IP->new(LocalPort => 12345, Listen => 1) or
613898184e3Ssthen        die "Cannot listen - $@";
614898184e3Ssthen
615898184e3Ssthen    my $socket = $server->accept or die "accept: $!";
616898184e3Ssthen
617898184e3Ssthen    my ($err, $hostname, $servicename) = getnameinfo($socket->peername);
618898184e3Ssthen    die "Cannot getnameinfo - $err" if $err;
619898184e3Ssthen
620898184e3Ssthen    print "The peer is connected from $hostname\n";
621898184e3Ssthen
622898184e3SsthenSince in this example only the hostname was used, the redundant conversion of
623898184e3Ssthenthe port number into a service name may be omitted by passing the
624898184e3SsthenC<NIx_NOSERV> flag.
625898184e3Ssthen
626898184e3Ssthen    use Socket qw(getnameinfo NIx_NOSERV);
627898184e3Ssthen
628898184e3Ssthen    my ($err, $hostname) = getnameinfo($socket->peername, 0, NIx_NOSERV);
629898184e3Ssthen
630898184e3SsthenThis function performs the work of the legacy functions unpack_sockaddr_in(),
631898184e3Sstheninet_ntoa(), gethostbyaddr() and getservbyport().
632898184e3Ssthen
633898184e3SsthenIn practice this logic is better performed by L<IO::Socket::IP>.
634898184e3Ssthen
635898184e3Ssthen=head2 Resolving hostnames into IP addresses
636898184e3Ssthen
637898184e3SsthenTo turn a hostname into a human-readable plain IP address use getaddrinfo()
638898184e3Ssthento turn the hostname into a list of socket structures, then getnameinfo() on
639898184e3Sstheneach one to make it a readable IP address again.
640898184e3Ssthen
641898184e3Ssthen    use Socket qw(:addrinfo SOCK_RAW);
642898184e3Ssthen
643898184e3Ssthen    my ($err, @res) = getaddrinfo($hostname, "", {socktype => SOCK_RAW});
644898184e3Ssthen    die "Cannot getaddrinfo - $err" if $err;
645898184e3Ssthen
646898184e3Ssthen    while( my $ai = shift @res ) {
647898184e3Ssthen        my ($err, $ipaddr) = getnameinfo($ai->{addr}, NI_NUMERICHOST, NIx_NOSERV);
648898184e3Ssthen        die "Cannot getnameinfo - $err" if $err;
649898184e3Ssthen
650898184e3Ssthen        print "$ipaddr\n";
651898184e3Ssthen    }
652898184e3Ssthen
653898184e3SsthenThe C<socktype> hint to getaddrinfo() filters the results to only include one
654898184e3Ssthensocket type and protocol. Without this most OSes return three combinations,
655898184e3Ssthenfor C<SOCK_STREAM>, C<SOCK_DGRAM> and C<SOCK_RAW>, resulting in triplicate
656898184e3Ssthenoutput of addresses. The C<NI_NUMERICHOST> flag to getnameinfo() causes it to
657898184e3Ssthenreturn a string-formatted plain IP address, rather than reverse resolving it
658898184e3Ssthenback into a hostname.
659898184e3Ssthen
660898184e3SsthenThis combination performs the work of the legacy functions gethostbyname()
661898184e3Ssthenand inet_ntoa().
662898184e3Ssthen
663898184e3Ssthen=head2 Accessing socket options
664898184e3Ssthen
665898184e3SsthenThe many C<SO_*> and other constants provide the socket option names for
666898184e3Ssthengetsockopt() and setsockopt().
667898184e3Ssthen
668898184e3Ssthen    use IO::Socket::INET;
669898184e3Ssthen    use Socket qw(SOL_SOCKET SO_RCVBUF IPPROTO_IP IP_TTL);
670898184e3Ssthen
671898184e3Ssthen    my $socket = IO::Socket::INET->new(LocalPort => 0, Proto => 'udp')
672898184e3Ssthen        or die "Cannot create socket: $@";
673898184e3Ssthen
674898184e3Ssthen    $socket->setsockopt(SOL_SOCKET, SO_RCVBUF, 64*1024) or
675898184e3Ssthen        die "setsockopt: $!";
676898184e3Ssthen
677898184e3Ssthen    print "Receive buffer is ", $socket->getsockopt(SOL_SOCKET, SO_RCVBUF),
678898184e3Ssthen        " bytes\n";
679898184e3Ssthen
680898184e3Ssthen    print "IP TTL is ", $socket->getsockopt(IPPROTO_IP, IP_TTL), "\n";
681898184e3Ssthen
682898184e3SsthenAs a convenience, L<IO::Socket>'s setsockopt() method will convert a number
683898184e3Sstheninto a packed byte buffer, and getsockopt() will unpack a byte buffer of the
684898184e3Ssthencorrect size back into a number.
685898184e3Ssthen
686898184e3Ssthen=cut
687898184e3Ssthen
688898184e3Ssthen=head1 AUTHOR
689898184e3Ssthen
690898184e3SsthenThis module was originally maintained in Perl core by the Perl 5 Porters.
691898184e3Ssthen
692898184e3SsthenIt was extracted to dual-life on CPAN at version 1.95 by
693898184e3SsthenPaul Evans <leonerd@leonerd.org.uk>
694898184e3Ssthen
695898184e3Ssthen=cut
696898184e3Ssthen
697898184e3Ssthenuse Carp;
698898184e3Ssthenuse warnings::register;
699898184e3Ssthen
700898184e3Ssthenrequire Exporter;
701898184e3Ssthenrequire XSLoader;
702898184e3Ssthenour @ISA = qw(Exporter);
703898184e3Ssthen
704898184e3Ssthen# <@Nicholas> you can't change @EXPORT without breaking the implicit API
705898184e3Ssthen# Please put any new constants in @EXPORT_OK!
706898184e3Ssthen
707898184e3Ssthen# List re-ordered to match documentation above. Try to keep the ordering
708898184e3Ssthen# consistent so it's easier to see which ones are or aren't documented.
709898184e3Ssthenour @EXPORT = qw(
710898184e3Ssthen    PF_802 PF_AAL PF_APPLETALK PF_CCITT PF_CHAOS PF_CTF PF_DATAKIT
711898184e3Ssthen    PF_DECnet PF_DLI PF_ECMA PF_GOSIP PF_HYLINK PF_IMPLINK PF_INET PF_INET6
712898184e3Ssthen    PF_ISO PF_KEY PF_LAST PF_LAT PF_LINK PF_MAX PF_NBS PF_NIT PF_NS PF_OSI
713898184e3Ssthen    PF_OSINET PF_PUP PF_ROUTE PF_SNA PF_UNIX PF_UNSPEC PF_USER PF_WAN
714898184e3Ssthen    PF_X25
715898184e3Ssthen
716898184e3Ssthen    AF_802 AF_AAL AF_APPLETALK AF_CCITT AF_CHAOS AF_CTF AF_DATAKIT
717898184e3Ssthen    AF_DECnet AF_DLI AF_ECMA AF_GOSIP AF_HYLINK AF_IMPLINK AF_INET AF_INET6
718898184e3Ssthen    AF_ISO AF_KEY AF_LAST AF_LAT AF_LINK AF_MAX AF_NBS AF_NIT AF_NS AF_OSI
719898184e3Ssthen    AF_OSINET AF_PUP AF_ROUTE AF_SNA AF_UNIX AF_UNSPEC AF_USER AF_WAN
720898184e3Ssthen    AF_X25
721898184e3Ssthen
722898184e3Ssthen    SOCK_DGRAM SOCK_RAW SOCK_RDM SOCK_SEQPACKET SOCK_STREAM
723898184e3Ssthen
724898184e3Ssthen    SOL_SOCKET
725898184e3Ssthen
726898184e3Ssthen    SO_ACCEPTCONN SO_ATTACH_FILTER SO_BACKLOG SO_BROADCAST SO_CHAMELEON
727898184e3Ssthen    SO_DEBUG SO_DETACH_FILTER SO_DGRAM_ERRIND SO_DOMAIN SO_DONTLINGER
728898184e3Ssthen    SO_DONTROUTE SO_ERROR SO_FAMILY SO_KEEPALIVE SO_LINGER SO_OOBINLINE
729898184e3Ssthen    SO_PASSCRED SO_PASSIFNAME SO_PEERCRED SO_PROTOCOL SO_PROTOTYPE
730898184e3Ssthen    SO_RCVBUF SO_RCVLOWAT SO_RCVTIMEO SO_REUSEADDR SO_REUSEPORT
731898184e3Ssthen    SO_SECURITY_AUTHENTICATION SO_SECURITY_ENCRYPTION_NETWORK
732898184e3Ssthen    SO_SECURITY_ENCRYPTION_TRANSPORT SO_SNDBUF SO_SNDLOWAT SO_SNDTIMEO
733898184e3Ssthen    SO_STATE SO_TYPE SO_USELOOPBACK SO_XOPEN SO_XSE
734898184e3Ssthen
7359f11ffb7Safresh1    IP_HDRINCL IP_OPTIONS IP_RECVOPTS IP_RECVRETOPTS IP_RETOPTS IP_TOS
7369f11ffb7Safresh1    IP_TTL
737898184e3Ssthen
738898184e3Ssthen    MSG_BCAST MSG_BTAG MSG_CTLFLAGS MSG_CTLIGNORE MSG_CTRUNC MSG_DONTROUTE
7399f11ffb7Safresh1    MSG_DONTWAIT MSG_EOF MSG_EOR MSG_ERRQUEUE MSG_ETAG MSG_FASTOPEN MSG_FIN
740898184e3Ssthen    MSG_MAXIOVLEN MSG_MCAST MSG_NOSIGNAL MSG_OOB MSG_PEEK MSG_PROXY MSG_RST
741898184e3Ssthen    MSG_SYN MSG_TRUNC MSG_URG MSG_WAITALL MSG_WIRE
742898184e3Ssthen
743898184e3Ssthen    SHUT_RD SHUT_RDWR SHUT_WR
744898184e3Ssthen
745898184e3Ssthen    INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
746898184e3Ssthen
747898184e3Ssthen    SCM_CONNECT SCM_CREDENTIALS SCM_CREDS SCM_RIGHTS SCM_TIMESTAMP
748898184e3Ssthen
749898184e3Ssthen    SOMAXCONN
750898184e3Ssthen
751898184e3Ssthen    IOV_MAX
752898184e3Ssthen    UIO_MAXIOV
753898184e3Ssthen
754898184e3Ssthen    sockaddr_family
755898184e3Ssthen    pack_sockaddr_in  unpack_sockaddr_in  sockaddr_in
756898184e3Ssthen    pack_sockaddr_in6 unpack_sockaddr_in6 sockaddr_in6
757898184e3Ssthen    pack_sockaddr_un  unpack_sockaddr_un  sockaddr_un
758898184e3Ssthen
759898184e3Ssthen    inet_aton inet_ntoa
760898184e3Ssthen);
761898184e3Ssthen
762898184e3Ssthen# List re-ordered to match documentation above. Try to keep the ordering
763898184e3Ssthen# consistent so it's easier to see which ones are or aren't documented.
764898184e3Ssthenour @EXPORT_OK = qw(
765898184e3Ssthen    CR LF CRLF $CR $LF $CRLF
766898184e3Ssthen
76791f110e0Safresh1    SOCK_NONBLOCK SOCK_CLOEXEC
76891f110e0Safresh1
7699f11ffb7Safresh1    IP_ADD_MEMBERSHIP IP_ADD_SOURCE_MEMBERSHIP IP_BIND_ADDRESS_NO_PORT
7709f11ffb7Safresh1    IP_DROP_MEMBERSHIP IP_DROP_SOURCE_MEMBERSHIP IP_FREEBIND
7719f11ffb7Safresh1    IP_MULTICAST_ALL IP_MULTICAST_IF IP_MULTICAST_LOOP IP_MULTICAST_TTL
7729f11ffb7Safresh1    IP_MTU IP_MTU_DISCOVER IP_NODEFRAG IP_RECVERR IP_TRANSPARENT
77391f110e0Safresh1
774b8851fccSafresh1    IPPROTO_IP IPPROTO_IPV6 IPPROTO_RAW IPPROTO_ICMP IPPROTO_IGMP
775b8851fccSafresh1    IPPROTO_TCP IPPROTO_UDP IPPROTO_GRE IPPROTO_ESP IPPROTO_AH
7769f11ffb7Safresh1    IPPROTO_ICMPV6 IPPROTO_SCTP
7779f11ffb7Safresh1
7789f11ffb7Safresh1    IP_PMTUDISC_DO IP_PMTUDISC_DONT IP_PMTUDISC_PROBE IP_PMTUDISC_WANT
779898184e3Ssthen
7806fb12b70Safresh1    IPTOS_LOWDELAY IPTOS_THROUGHPUT IPTOS_RELIABILITY IPTOS_MINCOST
7816fb12b70Safresh1
782*3d61058aSafresh1    TCP_CC_INFO TCP_CONGESTION TCP_CONNECTIONTIMEOUT TCP_CORK TCP_DEFER_ACCEPT
783*3d61058aSafresh1    TCP_FASTOPEN TCP_FASTOPEN_CONNECT TCP_FASTOPEN_KEY TCP_FASTOPEN_NO_COOKIE
784*3d61058aSafresh1    TCP_INFO TCP_INIT_CWND TCP_KEEPALIVE TCP_KEEPCNT TCP_KEEPIDLE TCP_KEEPINTVL
785*3d61058aSafresh1    TCP_LINGER2 TCP_MAXRT TCP_MAXSEG TCP_MD5SIG TCP_MD5SIG_EXT TCP_NODELAY
786*3d61058aSafresh1    TCP_NOOPT TCP_NOPUSH TCP_NOTSENT_LOWAT TCP_QUICKACK TCP_SACK_ENABLE
787*3d61058aSafresh1    TCP_STDURG TCP_SYNCNT TCP_TIMESTAMP TCP_TX_DELAY TCP_USER_TIMEOUT
788898184e3Ssthen    TCP_WINDOW_CLAMP
789898184e3Ssthen
790898184e3Ssthen    IN6ADDR_ANY IN6ADDR_LOOPBACK
791898184e3Ssthen
7929f11ffb7Safresh1    IPV6_ADDRFROM IPV6_ADD_MEMBERSHIP IPV6_DROP_MEMBERSHIP IPV6_JOIN_GROUP
79391f110e0Safresh1    IPV6_LEAVE_GROUP IPV6_MTU IPV6_MTU_DISCOVER IPV6_MULTICAST_HOPS
7949f11ffb7Safresh1    IPV6_MULTICAST_IF IPV6_MULTICAST_LOOP IPV6_RECVERR IPV6_ROUTER_ALERT
795*3d61058aSafresh1    IPV6_TCLASS IPV6_UNICAST_HOPS IPV6_V6ONLY
7969f11ffb7Safresh1
797e0680481Safresh1    SO_INCOMING_CPU SO_INCOMING_NAPI_ID SO_LOCK_FILTER SO_RCVBUFFORCE
798e0680481Safresh1    SO_SNDBUFFORCE
79991f110e0Safresh1
80091f110e0Safresh1    pack_ip_mreq unpack_ip_mreq pack_ip_mreq_source unpack_ip_mreq_source
801898184e3Ssthen
802898184e3Ssthen    pack_ipv6_mreq unpack_ipv6_mreq
803898184e3Ssthen
804898184e3Ssthen    inet_pton inet_ntop
805898184e3Ssthen
806898184e3Ssthen    getaddrinfo getnameinfo
807898184e3Ssthen
808898184e3Ssthen    AI_ADDRCONFIG AI_ALL AI_CANONIDN AI_CANONNAME AI_IDN
809898184e3Ssthen    AI_IDN_ALLOW_UNASSIGNED AI_IDN_USE_STD3_ASCII_RULES AI_NUMERICHOST
810898184e3Ssthen    AI_NUMERICSERV AI_PASSIVE AI_V4MAPPED
811898184e3Ssthen
812898184e3Ssthen    NI_DGRAM NI_IDN NI_IDN_ALLOW_UNASSIGNED NI_IDN_USE_STD3_ASCII_RULES
813898184e3Ssthen    NI_NAMEREQD NI_NOFQDN NI_NUMERICHOST NI_NUMERICSERV
814898184e3Ssthen
815898184e3Ssthen    NIx_NOHOST NIx_NOSERV
816898184e3Ssthen
817898184e3Ssthen    EAI_ADDRFAMILY EAI_AGAIN EAI_BADFLAGS EAI_BADHINTS EAI_FAIL EAI_FAMILY
818898184e3Ssthen    EAI_NODATA EAI_NONAME EAI_PROTOCOL EAI_SERVICE EAI_SOCKTYPE EAI_SYSTEM
819898184e3Ssthen);
820898184e3Ssthen
821898184e3Ssthenour %EXPORT_TAGS = (
822898184e3Ssthen    crlf     => [qw(CR LF CRLF $CR $LF $CRLF)],
823898184e3Ssthen    addrinfo => [qw(getaddrinfo getnameinfo), grep m/^(?:AI|NI|NIx|EAI)_/, @EXPORT_OK],
824898184e3Ssthen    all      => [@EXPORT, @EXPORT_OK],
825898184e3Ssthen);
826898184e3Ssthen
827898184e3SsthenBEGIN {
828898184e3Ssthen    sub CR   () {"\015"}
829898184e3Ssthen    sub LF   () {"\012"}
830898184e3Ssthen    sub CRLF () {"\015\012"}
831898184e3Ssthen
832898184e3Ssthen    # These are not gni() constants; they're extensions for the perl API
833898184e3Ssthen    # The definitions in Socket.pm and Socket.xs must match
834898184e3Ssthen    sub NIx_NOHOST() {1 << 0}
835898184e3Ssthen    sub NIx_NOSERV() {1 << 1}
836898184e3Ssthen}
837898184e3Ssthen
838898184e3Ssthen*CR   = \CR();
839898184e3Ssthen*LF   = \LF();
840898184e3Ssthen*CRLF = \CRLF();
841898184e3Ssthen
842eac174f2Safresh1# The four deprecated addrinfo constants
843eac174f2Safresh1foreach my $name (qw( AI_IDN_ALLOW_UNASSIGNED AI_IDN_USE_STD3_ASCII_RULES NI_IDN_ALLOW_UNASSIGNED NI_IDN_USE_STD3_ASCII_RULES )) {
844eac174f2Safresh1    no strict 'refs';
845eac174f2Safresh1    *$name = sub {
846eac174f2Safresh1        croak "The addrinfo constant $name is deprecated";
847eac174f2Safresh1    };
848eac174f2Safresh1}
849eac174f2Safresh1
850898184e3Ssthensub sockaddr_in {
851898184e3Ssthen    if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die
852898184e3Ssthen        my($af, $port, @quad) = @_;
853898184e3Ssthen        warnings::warn "6-ARG sockaddr_in call is deprecated"
854898184e3Ssthen            if warnings::enabled();
855898184e3Ssthen        pack_sockaddr_in($port, inet_aton(join('.', @quad)));
856898184e3Ssthen    } elsif (wantarray) {
857898184e3Ssthen        croak "usage:   (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1;
858898184e3Ssthen        unpack_sockaddr_in(@_);
859898184e3Ssthen    } else {
860898184e3Ssthen        croak "usage:   sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2;
861898184e3Ssthen        pack_sockaddr_in(@_);
862898184e3Ssthen    }
863898184e3Ssthen}
864898184e3Ssthen
865898184e3Ssthensub sockaddr_in6 {
866898184e3Ssthen    if (wantarray) {
867898184e3Ssthen        croak "usage:   (port,in6addr,scope_id,flowinfo) = sockaddr_in6(sin6_sv)" unless @_ == 1;
868898184e3Ssthen        unpack_sockaddr_in6(@_);
869898184e3Ssthen    }
870898184e3Ssthen    else {
871898184e3Ssthen        croak "usage:   sin6_sv = sockaddr_in6(port,in6addr,[scope_id,[flowinfo]])" unless @_ >= 2 and @_ <= 4;
872898184e3Ssthen        pack_sockaddr_in6(@_);
873898184e3Ssthen    }
874898184e3Ssthen}
875898184e3Ssthen
876898184e3Ssthensub sockaddr_un {
877898184e3Ssthen    if (wantarray) {
878898184e3Ssthen        croak "usage:   (filename) = sockaddr_un(sun_sv)" unless @_ == 1;
879898184e3Ssthen        unpack_sockaddr_un(@_);
880898184e3Ssthen    } else {
881898184e3Ssthen        croak "usage:   sun_sv = sockaddr_un(filename)" unless @_ == 1;
882898184e3Ssthen        pack_sockaddr_un(@_);
883898184e3Ssthen    }
884898184e3Ssthen}
885898184e3Ssthen
886898184e3SsthenXSLoader::load(__PACKAGE__, $VERSION);
887898184e3Ssthen
888898184e3Ssthenmy %errstr;
889898184e3Ssthen
890898184e3Ssthenif( defined &getaddrinfo ) {
891898184e3Ssthen    # These are not part of the API, nothing uses them, and deleting them
892898184e3Ssthen    # reduces the size of %Socket:: by about 12K
893898184e3Ssthen    delete $Socket::{fake_getaddrinfo};
894898184e3Ssthen    delete $Socket::{fake_getnameinfo};
895898184e3Ssthen} else {
896898184e3Ssthen    require Scalar::Util;
897898184e3Ssthen
898898184e3Ssthen    *getaddrinfo = \&fake_getaddrinfo;
899898184e3Ssthen    *getnameinfo = \&fake_getnameinfo;
900898184e3Ssthen
901898184e3Ssthen    # These numbers borrowed from GNU libc's implementation, but since
902898184e3Ssthen    # they're only used by our emulation, it doesn't matter if the real
903898184e3Ssthen    # platform's values differ
904898184e3Ssthen    my %constants = (
905898184e3Ssthen        AI_PASSIVE     => 1,
906898184e3Ssthen        AI_CANONNAME   => 2,
907898184e3Ssthen        AI_NUMERICHOST => 4,
908898184e3Ssthen        AI_V4MAPPED    => 8,
909898184e3Ssthen        AI_ALL         => 16,
910898184e3Ssthen        AI_ADDRCONFIG  => 32,
911898184e3Ssthen        # RFC 2553 doesn't define this but Linux does - lets be nice and
912898184e3Ssthen        # provide it since we can
913898184e3Ssthen        AI_NUMERICSERV => 1024,
914898184e3Ssthen
915898184e3Ssthen        EAI_BADFLAGS   => -1,
916898184e3Ssthen        EAI_NONAME     => -2,
917898184e3Ssthen        EAI_NODATA     => -5,
918898184e3Ssthen        EAI_FAMILY     => -6,
919898184e3Ssthen        EAI_SERVICE    => -8,
920898184e3Ssthen
921898184e3Ssthen        NI_NUMERICHOST => 1,
922898184e3Ssthen        NI_NUMERICSERV => 2,
923898184e3Ssthen        NI_NOFQDN      => 4,
924898184e3Ssthen        NI_NAMEREQD    => 8,
925898184e3Ssthen        NI_DGRAM       => 16,
926898184e3Ssthen
927898184e3Ssthen        # Constants we don't support. Export them, but croak if anyone tries to
928898184e3Ssthen        # use them
929898184e3Ssthen        AI_IDN      => 64,
930898184e3Ssthen        AI_CANONIDN => 128,
931898184e3Ssthen        NI_IDN      => 32,
932898184e3Ssthen
933898184e3Ssthen        # Error constants we'll never return, so it doesn't matter what value
934898184e3Ssthen        # these have, nor that we don't provide strings for them
935898184e3Ssthen        EAI_SYSTEM   => -11,
936898184e3Ssthen        EAI_BADHINTS => -1000,
937898184e3Ssthen        EAI_PROTOCOL => -1001
938898184e3Ssthen    );
939898184e3Ssthen
940898184e3Ssthen    foreach my $name ( keys %constants ) {
941898184e3Ssthen        my $value = $constants{$name};
942898184e3Ssthen
943898184e3Ssthen        no strict 'refs';
944898184e3Ssthen        defined &$name or *$name = sub () { $value };
945898184e3Ssthen    }
946898184e3Ssthen
947898184e3Ssthen    %errstr = (
948898184e3Ssthen        # These strings from RFC 2553
949898184e3Ssthen        EAI_BADFLAGS()   => "invalid value for ai_flags",
950898184e3Ssthen        EAI_NONAME()     => "nodename nor servname provided, or not known",
951898184e3Ssthen        EAI_NODATA()     => "no address associated with nodename",
952898184e3Ssthen        EAI_FAMILY()     => "ai_family not supported",
953898184e3Ssthen        EAI_SERVICE()    => "servname not supported for ai_socktype",
954898184e3Ssthen    );
955898184e3Ssthen}
956898184e3Ssthen
957898184e3Ssthen# The following functions are used if the system does not have a
958898184e3Ssthen# getaddrinfo(3) function in libc; and are used to emulate it for the AF_INET
959898184e3Ssthen# family
960898184e3Ssthen
961898184e3Ssthen# Borrowed from Regexp::Common::net
962b8851fccSafresh1my $REGEXP_IPv4_DECIMAL = qr/25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}/;
963898184e3Ssthenmy $REGEXP_IPv4_DOTTEDQUAD = qr/$REGEXP_IPv4_DECIMAL\.$REGEXP_IPv4_DECIMAL\.$REGEXP_IPv4_DECIMAL\.$REGEXP_IPv4_DECIMAL/;
964898184e3Ssthen
965898184e3Ssthensub fake_makeerr
966898184e3Ssthen{
967898184e3Ssthen    my ( $errno ) = @_;
968898184e3Ssthen    my $errstr = $errno == 0 ? "" : ( $errstr{$errno} || $errno );
969898184e3Ssthen    return Scalar::Util::dualvar( $errno, $errstr );
970898184e3Ssthen}
971898184e3Ssthen
972898184e3Ssthensub fake_getaddrinfo
973898184e3Ssthen{
974898184e3Ssthen    my ( $node, $service, $hints ) = @_;
975898184e3Ssthen
976898184e3Ssthen    $node = "" unless defined $node;
977898184e3Ssthen
978898184e3Ssthen    $service = "" unless defined $service;
979898184e3Ssthen
980898184e3Ssthen    my ( $family, $socktype, $protocol, $flags ) = @$hints{qw( family socktype protocol flags )};
981898184e3Ssthen
982898184e3Ssthen    $family ||= Socket::AF_INET(); # 0 == AF_UNSPEC, which we want too
983898184e3Ssthen    $family == Socket::AF_INET() or return fake_makeerr( EAI_FAMILY() );
984898184e3Ssthen
985898184e3Ssthen    $socktype ||= 0;
986898184e3Ssthen
987898184e3Ssthen    $protocol ||= 0;
988898184e3Ssthen
989898184e3Ssthen    $flags ||= 0;
990898184e3Ssthen
991898184e3Ssthen    my $flag_passive     = $flags & AI_PASSIVE();     $flags &= ~AI_PASSIVE();
992898184e3Ssthen    my $flag_canonname   = $flags & AI_CANONNAME();   $flags &= ~AI_CANONNAME();
993898184e3Ssthen    my $flag_numerichost = $flags & AI_NUMERICHOST(); $flags &= ~AI_NUMERICHOST();
994898184e3Ssthen    my $flag_numericserv = $flags & AI_NUMERICSERV(); $flags &= ~AI_NUMERICSERV();
995898184e3Ssthen
996898184e3Ssthen    # These constants don't apply to AF_INET-only lookups, so we might as well
997898184e3Ssthen    # just ignore them. For AI_ADDRCONFIG we just presume the host has ability
998898184e3Ssthen    # to talk AF_INET. If not we'd have to return no addresses at all. :)
999898184e3Ssthen    $flags &= ~(AI_V4MAPPED()|AI_ALL()|AI_ADDRCONFIG());
1000898184e3Ssthen
1001eac174f2Safresh1    $flags & (AI_IDN()|AI_CANONIDN()) and
1002898184e3Ssthen        croak "Socket::getaddrinfo() does not support IDN";
1003898184e3Ssthen
1004898184e3Ssthen    $flags == 0 or return fake_makeerr( EAI_BADFLAGS() );
1005898184e3Ssthen
1006898184e3Ssthen    $node eq "" and $service eq "" and return fake_makeerr( EAI_NONAME() );
1007898184e3Ssthen
1008898184e3Ssthen    my $canonname;
1009898184e3Ssthen    my @addrs;
1010898184e3Ssthen    if( $node ne "" ) {
1011898184e3Ssthen        return fake_makeerr( EAI_NONAME() ) if( $flag_numerichost and $node !~ m/^$REGEXP_IPv4_DOTTEDQUAD$/ );
1012898184e3Ssthen        ( $canonname, undef, undef, undef, @addrs ) = gethostbyname( $node );
1013898184e3Ssthen        defined $canonname or return fake_makeerr( EAI_NONAME() );
1014898184e3Ssthen
1015898184e3Ssthen        undef $canonname unless $flag_canonname;
1016898184e3Ssthen    }
1017898184e3Ssthen    else {
1018898184e3Ssthen        $addrs[0] = $flag_passive ? Socket::inet_aton( "0.0.0.0" )
1019898184e3Ssthen                                  : Socket::inet_aton( "127.0.0.1" );
1020898184e3Ssthen    }
1021898184e3Ssthen
1022898184e3Ssthen    my @ports; # Actually ARRAYrefs of [ socktype, protocol, port ]
1023898184e3Ssthen    my $protname = "";
1024898184e3Ssthen    if( $protocol ) {
1025b8851fccSafresh1        $protname = eval { getprotobynumber( $protocol ) };
1026898184e3Ssthen    }
1027898184e3Ssthen
1028898184e3Ssthen    if( $service ne "" and $service !~ m/^\d+$/ ) {
1029898184e3Ssthen        return fake_makeerr( EAI_NONAME() ) if( $flag_numericserv );
1030898184e3Ssthen        getservbyname( $service, $protname ) or return fake_makeerr( EAI_SERVICE() );
1031898184e3Ssthen    }
1032898184e3Ssthen
1033898184e3Ssthen    foreach my $this_socktype ( Socket::SOCK_STREAM(), Socket::SOCK_DGRAM(), Socket::SOCK_RAW() ) {
1034898184e3Ssthen        next if $socktype and $this_socktype != $socktype;
1035898184e3Ssthen
1036898184e3Ssthen        my $this_protname = "raw";
1037898184e3Ssthen        $this_socktype == Socket::SOCK_STREAM() and $this_protname = "tcp";
1038898184e3Ssthen        $this_socktype == Socket::SOCK_DGRAM()  and $this_protname = "udp";
1039898184e3Ssthen
1040898184e3Ssthen        next if $protname and $this_protname ne $protname;
1041898184e3Ssthen
1042898184e3Ssthen        my $port;
1043898184e3Ssthen        if( $service ne "" ) {
1044898184e3Ssthen            if( $service =~ m/^\d+$/ ) {
1045898184e3Ssthen                $port = "$service";
1046898184e3Ssthen            }
1047898184e3Ssthen            else {
1048898184e3Ssthen                ( undef, undef, $port, $this_protname ) = getservbyname( $service, $this_protname );
1049898184e3Ssthen                next unless defined $port;
1050898184e3Ssthen            }
1051898184e3Ssthen        }
1052898184e3Ssthen        else {
1053898184e3Ssthen            $port = 0;
1054898184e3Ssthen        }
1055898184e3Ssthen
1056b8851fccSafresh1        push @ports, [ $this_socktype, eval { scalar getprotobyname( $this_protname ) } || 0, $port ];
1057898184e3Ssthen    }
1058898184e3Ssthen
1059898184e3Ssthen    my @ret;
1060898184e3Ssthen    foreach my $addr ( @addrs ) {
1061898184e3Ssthen        foreach my $portspec ( @ports ) {
1062898184e3Ssthen            my ( $socktype, $protocol, $port ) = @$portspec;
1063898184e3Ssthen            push @ret, {
1064898184e3Ssthen                family    => $family,
1065898184e3Ssthen                socktype  => $socktype,
1066898184e3Ssthen                protocol  => $protocol,
1067898184e3Ssthen                addr      => Socket::pack_sockaddr_in( $port, $addr ),
1068898184e3Ssthen                canonname => undef,
1069898184e3Ssthen            };
1070898184e3Ssthen        }
1071898184e3Ssthen    }
1072898184e3Ssthen
1073898184e3Ssthen    # Only supply canonname for the first result
1074898184e3Ssthen    if( defined $canonname ) {
1075898184e3Ssthen        $ret[0]->{canonname} = $canonname;
1076898184e3Ssthen    }
1077898184e3Ssthen
1078898184e3Ssthen    return ( fake_makeerr( 0 ), @ret );
1079898184e3Ssthen}
1080898184e3Ssthen
1081898184e3Ssthensub fake_getnameinfo
1082898184e3Ssthen{
1083898184e3Ssthen    my ( $addr, $flags, $xflags ) = @_;
1084898184e3Ssthen
1085898184e3Ssthen    my ( $port, $inetaddr );
1086898184e3Ssthen    eval { ( $port, $inetaddr ) = Socket::unpack_sockaddr_in( $addr ) }
1087898184e3Ssthen        or return fake_makeerr( EAI_FAMILY() );
1088898184e3Ssthen
1089898184e3Ssthen    my $family = Socket::AF_INET();
1090898184e3Ssthen
1091898184e3Ssthen    $flags ||= 0;
1092898184e3Ssthen
1093898184e3Ssthen    my $flag_numerichost = $flags & NI_NUMERICHOST(); $flags &= ~NI_NUMERICHOST();
1094898184e3Ssthen    my $flag_numericserv = $flags & NI_NUMERICSERV(); $flags &= ~NI_NUMERICSERV();
1095898184e3Ssthen    my $flag_nofqdn      = $flags & NI_NOFQDN();      $flags &= ~NI_NOFQDN();
1096898184e3Ssthen    my $flag_namereqd    = $flags & NI_NAMEREQD();    $flags &= ~NI_NAMEREQD();
1097898184e3Ssthen    my $flag_dgram       = $flags & NI_DGRAM()   ;    $flags &= ~NI_DGRAM();
1098898184e3Ssthen
1099eac174f2Safresh1    $flags & NI_IDN() and
1100898184e3Ssthen        croak "Socket::getnameinfo() does not support IDN";
1101898184e3Ssthen
1102898184e3Ssthen    $flags == 0 or return fake_makeerr( EAI_BADFLAGS() );
1103898184e3Ssthen
1104898184e3Ssthen    $xflags ||= 0;
1105898184e3Ssthen
1106898184e3Ssthen    my $node;
1107898184e3Ssthen    if( $xflags & NIx_NOHOST ) {
1108898184e3Ssthen        $node = undef;
1109898184e3Ssthen    }
1110898184e3Ssthen    elsif( $flag_numerichost ) {
1111898184e3Ssthen        $node = Socket::inet_ntoa( $inetaddr );
1112898184e3Ssthen    }
1113898184e3Ssthen    else {
1114898184e3Ssthen        $node = gethostbyaddr( $inetaddr, $family );
1115898184e3Ssthen        if( !defined $node ) {
1116898184e3Ssthen            return fake_makeerr( EAI_NONAME() ) if $flag_namereqd;
1117898184e3Ssthen            $node = Socket::inet_ntoa( $inetaddr );
1118898184e3Ssthen        }
1119898184e3Ssthen        elsif( $flag_nofqdn ) {
1120898184e3Ssthen            my ( $shortname ) = split m/\./, $node;
1121898184e3Ssthen            my ( $fqdn ) = gethostbyname $shortname;
1122898184e3Ssthen            $node = $shortname if defined $fqdn and $fqdn eq $node;
1123898184e3Ssthen        }
1124898184e3Ssthen    }
1125898184e3Ssthen
1126898184e3Ssthen    my $service;
1127898184e3Ssthen    if( $xflags & NIx_NOSERV ) {
1128898184e3Ssthen        $service = undef;
1129898184e3Ssthen    }
1130898184e3Ssthen    elsif( $flag_numericserv ) {
1131898184e3Ssthen        $service = "$port";
1132898184e3Ssthen    }
1133898184e3Ssthen    else {
1134898184e3Ssthen        my $protname = $flag_dgram ? "udp" : "";
1135898184e3Ssthen        $service = getservbyport( $port, $protname );
1136898184e3Ssthen        if( !defined $service ) {
1137898184e3Ssthen            $service = "$port";
1138898184e3Ssthen        }
1139898184e3Ssthen    }
1140898184e3Ssthen
1141898184e3Ssthen    return ( fake_makeerr( 0 ), $node, $service );
1142898184e3Ssthen}
1143898184e3Ssthen
1144898184e3Ssthen1;
1145