1# Copyright (C) 2000-2018 Hajimu UMEMOTO <ume@mahoroba.org>.
2# All rights reserved.
3#
4# This module is based on perl5.005_55-v6-19990721 written by KAME
5# Project.
6#
7# Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
8# All rights reserved.
9#
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions
12# are met:
13# 1. Redistributions of source code must retain the above copyright
14#    notice, this list of conditions and the following disclaimer.
15# 2. Redistributions in binary form must reproduce the above copyright
16#    notice, this list of conditions and the following disclaimer in the
17#    documentation and/or other materials provided with the distribution.
18# 3. Neither the name of the project nor the names of its contributors
19#    may be used to endorse or promote products derived from this software
20#    without specific prior written permission.
21#
22# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32# SUCH DAMAGE.
33
34# $Id: Socket6.pm 688 2018-09-30 04:22:53Z ume $
35
36package Socket6;
37
38use strict;
39use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD);
40$VERSION = "0.29";
41
42=head1 NAME
43
44Socket6 - IPv6 related part of the C socket.h defines and structure manipulators
45
46=head1 SYNOPSIS
47
48    use Socket;
49    use Socket6;
50
51    @res = getaddrinfo('hishost.com', 'daytime', AF_UNSPEC, SOCK_STREAM);
52    $family = -1;
53    while (scalar(@res) >= 5) {
54	($family, $socktype, $proto, $saddr, $canonname, @res) = @res;
55
56	($host, $port) = getnameinfo($saddr, NI_NUMERICHOST | NI_NUMERICSERV);
57	print STDERR "Trying to connect to $host port $port...\n";
58
59	socket(Socket_Handle, $family, $socktype, $proto) || next;
60        connect(Socket_Handle, $saddr) && last;
61
62	close(Socket_Handle);
63	$family = -1;
64    }
65
66    if ($family != -1) {
67	print STDERR "connected to $host port $port\n";
68    } else {
69	die "connect attempt failed\n";
70    }
71
72=head1 DESCRIPTION
73
74This module provides glue routines to the various IPv6 functions.
75
76If you use the Socket6 module,
77be sure to specify "use Socket" as well as "use Socket6".
78
79Functions supplied are:
80
81=over
82
83=item inet_pton FAMILY, TEXT_ADDRESS
84
85    This function takes an IP address in presentation (or string) format
86    and converts it into numeric (or binary) format.
87    The type of IP address conversion (IPv4 versus IPv6) is controlled
88    by the FAMILY argument.
89
90=item inet_ntop FAMILY, BINARY_ADDRESS
91
92    This function takes an IP address in numeric (or binary) format
93    and converts it into presentation (or string) format
94    The type of IP address conversion (IPv4 versus IPv6) is controlled
95    by the FAMILY argument.
96
97=item pack_sockaddr_in6 PORT, ADDR
98
99    This function takes two arguments: a port number, and a 16-octet
100    IPv6 address structure (as returned by inet_pton()).
101    It returns the sockaddr_in6 structure with these arguments packed
102    into their correct fields, as well as the AF_INET6 family.
103    The other fields are not set and their values should not be relied upon.
104
105=item pack_sockaddr_in6_all PORT, FLOWINFO, ADDR, SCOPEID
106
107    This function takes four arguments: a port number, a 16-octet
108    IPv6 address structure (as returned by inet_pton), any
109    special flow information, and any specific scope information.
110    It returns a complete sockaddr_in6 structure with these arguments packed
111    into their correct fields, as well as the AF_INET6 family.
112
113=item unpack_sockaddr_in6 NAME
114
115    This function takes a sockaddr_in6 structure (as returned by
116    pack_sockaddr_in6()) and returns a list of two elements:
117    the port number and the 16-octet IP address.
118    This function will croak if it determines it has not been
119    passed an IPv6 structure.
120
121=item unpack_sockaddr_in6_all NAME
122
123    This function takes a sockaddr_in6 structure (as returned by
124    pack_sockaddr_in6()) and returns a list of four elements:
125    the port number, the flow information, the 16-octet IP address,
126    and the scope information.
127    This function will croak if it determines it has not been
128    passed an IPv6 structure.
129
130=item gethostbyname2 HOSTNAME, FAMILY
131
132=item getaddrinfo NODENAME, SERVICENAME, [FAMILY, SOCKTYPE, PROTOCOL, FLAGS]
133
134    This function converts node names to addresses and service names
135    to port numbers.
136    If the NODENAME argument is not a false value,
137    then a nodename to address lookup is performed;
138    otherwise a service name to port number lookup is performed.
139    At least one of NODENAME and SERVICENAME must have a true value.
140
141    If the lookup is successful, a list consisting of multiples of
142    five elements is returned.
143    Each group of five elements consists of the address family,
144    socket type, protocol, 16-octet IP address, and the canonical
145    name (undef if the node name passed is already the canonical name).
146
147    The arguments FAMILY, SOCKTYPE, PROTOCOL, and FLAGS are all optional.
148
149    This function will croak if it determines it has not been
150    passed an IPv6 structure.
151
152    If the lookup is unsuccessful, the function returns a single scalar.
153    This will contain the string version of that error in string context,
154    and the numeric value in numeric context.
155
156=item getnameinfo NAME, [FLAGS]
157
158    This function takes a socket address structure. If successful, it returns
159    two strings containing the node name and service name.
160
161    The optional FLAGS argument controls what kind of lookup is performed.
162
163    If the lookup is unsuccessful, the function returns a single scalar.
164    This will contain the string version of that error in string context,
165    and the numeric value in numeric context.
166
167=item getipnodebyname HOST, [FAMILY, FLAGS]
168
169    This function takes either a node name or an IP address string
170    and performs a lookup on that name (or conversion of the string).
171    It returns a list of five elements: the canonical host name,
172    the address family, the length in octets of the IP addresses
173    returned, a reference to a list of IP address structures, and
174    a reference to a list of aliases for the host name.
175
176    The arguments FAMILY and FLAGS are optional.
177    Note: This function does not handle IPv6 scope identifiers,
178    and should be used with care.
179    And, this function was deprecated in RFC3493.
180    The getnameinfo function should be used instead.
181
182=item getipnodebyaddr FAMILY, ADDRESS
183
184    This function takes an IP address family and an IP address structure
185    and performs a reverse lookup on that address.
186    It returns a list of five elements: the canonical host name,
187    the address family, the length in octets of the IP addresses
188    returned, a reference to a list of IP address structures, and
189    a reference to a list of aliases for the host name.
190
191    Note: This function does not handle IPv6 scope identifiers,
192    and should be used with care.
193    And, this function was deprecated in RFC3493.
194    The getaddrinfo function should be used instead.
195
196=item gai_strerror ERROR_NUMBER
197
198    This function returns a string corresponding to the error number
199    passed in as an argument.
200
201=item in6addr_any
202
203    This function returns the 16-octet wildcard address.
204
205=item in6addr_loopback
206
207    This function returns the 16-octet loopback address.
208
209=back
210
211=cut
212
213use Carp;
214
215use base qw(Exporter DynaLoader);
216
217@EXPORT = qw(
218	inet_pton inet_ntop pack_sockaddr_in6 pack_sockaddr_in6_all
219	unpack_sockaddr_in6 unpack_sockaddr_in6_all sockaddr_in6
220	gethostbyname2 getaddrinfo getnameinfo
221	in6addr_any in6addr_loopback
222	gai_strerror getipnodebyname getipnodebyaddr
223	AI_ADDRCONFIG
224	AI_ALL
225	AI_CANONNAME
226	AI_NUMERICHOST
227	AI_NUMERICSERV
228	AI_DEFAULT
229	AI_MASK
230	AI_PASSIVE
231	AI_V4MAPPED
232	AI_V4MAPPED_CFG
233	EAI_ADDRFAMILY
234	EAI_AGAIN
235	EAI_BADFLAGS
236	EAI_FAIL
237	EAI_FAMILY
238	EAI_MEMORY
239	EAI_NODATA
240	EAI_NONAME
241	EAI_SERVICE
242	EAI_SOCKTYPE
243	EAI_SYSTEM
244	EAI_BADHINTS
245	EAI_PROTOCOL
246	IP_AUTH_TRANS_LEVEL
247	IP_AUTH_NETWORK_LEVEL
248	IP_ESP_TRANS_LEVEL
249	IP_ESP_NETWORK_LEVEL
250	IPPROTO_IP
251	IPPROTO_IPV6
252	IPSEC_LEVEL_AVAIL
253	IPSEC_LEVEL_BYPASS
254	IPSEC_LEVEL_DEFAULT
255	IPSEC_LEVEL_NONE
256	IPSEC_LEVEL_REQUIRE
257	IPSEC_LEVEL_UNIQUE
258	IPSEC_LEVEL_USE
259	IPV6_AUTH_TRANS_LEVEL
260	IPV6_AUTH_NETWORK_LEVEL
261	IPV6_ESP_NETWORK_LEVEL
262	IPV6_ESP_TRANS_LEVEL
263	NI_NOFQDN
264	NI_NUMERICHOST
265	NI_NAMEREQD
266	NI_NUMERICSERV
267	NI_DGRAM
268	NI_WITHSCOPEID
269);
270push @EXPORT, qw(AF_INET6) unless defined eval {Socket::AF_INET6()};
271push @EXPORT, qw(PF_INET6) unless defined eval {Socket::PF_INET6()};
272
273@EXPORT_OK = qw(AF_INET6 PF_INET6);
274
275%EXPORT_TAGS = (
276    all     => [@EXPORT],
277);
278
279sub sockaddr_in6 {
280    if (wantarray) {
281	croak "usage:   (port,iaddr) = sockaddr_in6(sin_sv)" unless @_ == 1;
282        unpack_sockaddr_in6(@_);
283    } else {
284	croak "usage:   sin_sv = sockaddr_in6(port,iaddr))" unless @_ == 2;
285        pack_sockaddr_in6(@_);
286    }
287}
288
289sub AUTOLOAD {
290    my($constname);
291    ($constname = $AUTOLOAD) =~ s/.*:://o;
292    $! = 0;
293    my $val = constant($constname, @_ ? $_[0] : 0);
294    if ($! != 0) {
295	croak "Your vendor has not defined Socket macro $constname, used";
296    }
297    eval "sub $AUTOLOAD { $val }";
298    goto &$AUTOLOAD;
299}
300
301bootstrap Socket6 $VERSION;
302
3031;
304