This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add Larwan Berke (Apocalypse) to AUTHORS
[perl5.git] / ext / Socket / Socket.pm
CommitLineData
a0d0e21e 1package Socket;
73c78b0a 2
e71d7b85
PLE
3use strict;
4
17f410f9 5our($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
deec43ae 6$VERSION = "1.93";
3b35bae3
AD
7
8=head1 NAME
9
036d8bd4 10Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa, inet_pton, inet_ntop - load the C socket.h defines and structure manipulators
3b35bae3
AD
11
12=head1 SYNOPSIS
13
14 use Socket;
15
4633a7c4 16 $proto = getprotobyname('udp');
8e07c86e 17 socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto);
4633a7c4
LW
18 $iaddr = gethostbyname('hishost.com');
19 $port = getservbyname('time', 'udp');
20 $sin = sockaddr_in($port, $iaddr);
21 send(Socket_Handle, 0, 0, $sin);
22
23 $proto = getprotobyname('tcp');
24 socket(Socket_Handle, PF_INET, SOCK_STREAM, $proto);
67d7c167 25 $port = getservbyname('smtp', 'tcp');
4633a7c4
LW
26 $sin = sockaddr_in($port,inet_aton("127.1"));
27 $sin = sockaddr_in(7,inet_aton("localhost"));
28 $sin = sockaddr_in(7,INADDR_LOOPBACK);
29 connect(Socket_Handle,$sin);
30
31 ($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle));
32 $peer_host = gethostbyaddr($iaddr, AF_INET);
33 $peer_addr = inet_ntoa($iaddr);
34
35 $proto = getprotobyname('tcp');
36 socket(Socket_Handle, PF_UNIX, SOCK_STREAM, $proto);
2359510d
SD
37 unlink('/var/run/usock');
38 $sun = sockaddr_un('/var/run/usock');
4633a7c4 39 connect(Socket_Handle,$sun);
3b35bae3
AD
40
41=head1 DESCRIPTION
42
43This module is just a translation of the C F<socket.h> file.
44Unlike the old mechanism of requiring a translated F<socket.ph>
45file, this uses the B<h2xs> program (see the Perl source distribution)
46and your native C compiler. This means that it has a
4633a7c4
LW
47far more likely chance of getting the numbers right. This includes
48all of the commonly used pound-defines like AF_INET, SOCK_STREAM, etc.
3b35bae3 49
fdb41d65
CN
50Also, some common socket "newline" constants are provided: the
51constants C<CR>, C<LF>, and C<CRLF>, as well as C<$CR>, C<$LF>, and
52C<$CRLF>, which map to C<\015>, C<\012>, and C<\015\012>. If you do
53not want to use the literal characters in your programs, then use
54the constants provided here. They are not exported by default, but can
55be imported individually, and with the C<:crlf> export tag:
56
57 use Socket qw(:DEFAULT :crlf);
58
8e07c86e
AD
59In addition, some structure manipulation functions are available:
60
bbc7dcd2 61=over 4
2ae324a7 62
8e07c86e
AD
63=item inet_aton HOSTNAME
64
6fe628c6
JH
65Takes a string giving the name of a host, and translates that to an
66opaque string (if programming in C, struct in_addr). Takes arguments
67of both the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name
68cannot be resolved, returns undef. For multi-homed hosts (hosts with
69more than one address), the first address found is returned.
8e07c86e 70
2528d3bc 71For portability do not assume that the result of inet_aton() is 32
6fe628c6
JH
72bits wide, in other words, that it would contain only the IPv4 address
73in network order.
2528d3bc 74
8e07c86e
AD
75=item inet_ntoa IP_ADDRESS
76
6fe628c6
JH
77Takes a string (an opaque string as returned by inet_aton(),
78or a v-string representing the four octets of the IPv4 address in
2528d3bc 79network order) and translates it into a string of the form 'd.d.d.d'
6fe628c6
JH
80where the 'd's are numbers less than 256 (the normal human-readable
81four dotted number notation for Internet addresses).
8e07c86e
AD
82
83=item INADDR_ANY
84
4633a7c4 85Note: does not return a number, but a packed string.
8e07c86e
AD
86
87Returns the 4-byte wildcard ip address which specifies any
6fe628c6 88of the hosts ip addresses. (A particular machine can have
8e07c86e
AD
89more than one ip address, each address corresponding to
90a particular network interface. This wildcard address
91allows you to bind to all of them simultaneously.)
92Normally equivalent to inet_aton('0.0.0.0').
93
7e1af8bc
PP
94=item INADDR_BROADCAST
95
96Note: does not return a number, but a packed string.
97
98Returns the 4-byte 'this-lan' ip broadcast address.
99This can be useful for some protocols to solicit information
100from all servers on the same LAN cable.
101Normally equivalent to inet_aton('255.255.255.255').
102
8e07c86e
AD
103=item INADDR_LOOPBACK
104
105Note - does not return a number.
106
6fe628c6 107Returns the 4-byte loopback address. Normally equivalent
8e07c86e 108to inet_aton('localhost').
3b35bae3 109
8e07c86e
AD
110=item INADDR_NONE
111
112Note - does not return a number.
113
6fe628c6 114Returns the 4-byte 'invalid' ip address. Normally equivalent
8e07c86e
AD
115to inet_aton('255.255.255.255').
116
d3f248d3
PLE
117=item IN6ADDR_ANY
118
119Returns the 16-byte wildcard IPv6 address. Normally equivalent
120to inet_pton(AF_INET6, "::")
121
122=item IN6ADDR_LOOPBACK
123
124Returns the 16-byte loopback IPv6 address. Normally equivalent
125to inet_pton(AF_INET6, "::1")
126
2a84dff3
GA
127=item sockaddr_family SOCKADDR
128
129Takes a sockaddr structure (as returned by pack_sockaddr_in(),
130pack_sockaddr_un() or the perl builtin functions getsockname() and
131getpeername()) and returns the address family tag. It will match the
132constant AF_INET for a sockaddr_in and AF_UNIX for a sockaddr_un. It
133can be used to figure out what unpacker to use for a sockaddr of
134unknown type.
135
4633a7c4
LW
136=item sockaddr_in PORT, ADDRESS
137
138=item sockaddr_in SOCKADDR_IN
139
91e74348 140In a list context, unpacks its SOCKADDR_IN argument and returns an array
4633a7c4
LW
141consisting of (PORT, ADDRESS). In a scalar context, packs its (PORT,
142ADDRESS) arguments as a SOCKADDR_IN and returns it. If this is confusing,
143use pack_sockaddr_in() and unpack_sockaddr_in() explicitly.
144
145=item pack_sockaddr_in PORT, IP_ADDRESS
8e07c86e 146
6fe628c6
JH
147Takes two arguments, a port number and an opaque string, IP_ADDRESS
148(as returned by inet_aton(), or a v-string). Returns the sockaddr_in
149structure with those arguments packed in with AF_INET filled in. For
150Internet domain sockets, this structure is normally what you need for
151the arguments in bind(), connect(), and send(), and is also returned
152by getpeername(), getsockname() and recv().
8e07c86e
AD
153
154=item unpack_sockaddr_in SOCKADDR_IN
155
4633a7c4 156Takes a sockaddr_in structure (as returned by pack_sockaddr_in()) and
6fe628c6
JH
157returns an array of two elements: the port and an opaque string
158representing the IP address (you can use inet_ntoa() to convert the
159address to the four-dotted numeric format). Will croak if the
160structure does not have AF_INET in the right place.
4633a7c4 161
4bc6101e
PLE
162=item sockaddr_in6 PORT, IP6_ADDRESS, [ SCOPE_ID, [ FLOWINFO ] ]
163
164=item sockaddr_in6 SOCKADDR_IN6
165
166In list context, unpacks its SOCKADDR_IN6 argument according to
167unpack_sockaddr_in6(). In scalar context, packs its arguments according to
168pack_sockaddr_in6().
169
c73aa44c
PLE
170=item pack_sockaddr_in6 PORT, IP6_ADDRESS, [ SCOPE_ID, [ FLOWINFO ] ]
171
172Takes two to four arguments, a port number, an opaque string (as returned by
173inet_pton()), optionally a scope ID number, and optionally a flow label
174number. Returns the sockaddr_in6 structure with those arguments packed in
175with AF_INET6 filled in. IPv6 equivalent of pack_sockaddr_in().
176
177=item unpack_sockaddr_in6 SOCKADDR_IN6
178
179Takes a sockaddr_in6 structure (as returned by pack_sockaddr_in6()) and
180returns an array of four elements: the port number, an opaque string
181representing the IPv6 address, the scope ID, and the flow label. (You can
182use inet_ntop() to convert the address to the usual string format). Will
183croak if the structure does not have AF_INET6 in the right place.
184
4633a7c4
LW
185=item sockaddr_un PATHNAME
186
187=item sockaddr_un SOCKADDR_UN
188
91e74348 189In a list context, unpacks its SOCKADDR_UN argument and returns an array
1fef88e7 190consisting of (PATHNAME). In a scalar context, packs its PATHNAME
4633a7c4
LW
191arguments as a SOCKADDR_UN and returns it. If this is confusing, use
192pack_sockaddr_un() and unpack_sockaddr_un() explicitly.
1fef88e7 193These are only supported if your system has E<lt>F<sys/un.h>E<gt>.
4633a7c4
LW
194
195=item pack_sockaddr_un PATH
196
197Takes one argument, a pathname. Returns the sockaddr_un structure with
198that path packed in with AF_UNIX filled in. For unix domain sockets, this
199structure is normally what you need for the arguments in bind(),
200connect(), and send(), and is also returned by getpeername(),
201getsockname() and recv().
202
203=item unpack_sockaddr_un SOCKADDR_UN
204
205Takes a sockaddr_un structure (as returned by pack_sockaddr_un())
206and returns the pathname. Will croak if the structure does not
207have AF_UNIX in the right place.
3b35bae3 208
036d8bd4
SP
209=item inet_pton ADDRESS_FAMILY, HOSTNAME
210
211Takes an address family, either AF_INET or AF_INET6, and a string giving
212the name of a host, and translates that to an opaque string
213(if programming in C, struct in_addr or struct in6_addr depending on the
214address family passed in). The host string may be a string hostname, such
215as 'www.perl.org', or an IP address. If using an IP address, the type of
b7b1e41b 216IP address must be consistent with the address family passed into the function.
036d8bd4 217
d6896be3
JV
218This function is not exported by default.
219
036d8bd4
SP
220=item inet_ntop ADDRESS_FAMILY, IP_ADDRESS
221
222Takes an address family, either AF_INET or AF_INET6, and a string
223(an opaque string as returned by inet_aton() or inet_pton()) and
224translates it to an IPv4 or IPv6 address string.
225
d6896be3
JV
226This function is not exported by default.
227
e57a891d
PLE
228=item getaddrinfo HOST, SERVICE, [ HINTS ]
229
230Given at least one of a hostname and a service name, returns a list of address
231structures to listen on or connect to. HOST and SERVICE should be plain
232strings (or a numerical port number for SERVICE). If present, HINTS should be
233a reference to a HASH, where the following keys are recognised:
234
235=over 8
236
237=item flags => INT
238
239A bitfield containing C<AI_*> constants
240
241=item family => INT
242
243Restrict to only generating addresses in this address family
244
245=item socktype => INT
246
247Restrict to only generating addresses of this socket type
248
249=item protocol => INT
250
251Restrict to only generating addresses for this protocol
252
253=back
254
255The return value will be a list; the first value being an error indication,
256followed by a list of address structures (if no error occured).
257
258 my ( $err, @results ) = getaddrinfo( ... );
259
260The error value will be a dualvar; comparable to the C<EI_*> error constants,
261or printable as a human-readable error message string. Each value in the
262results list will be a HASH reference containing the following fields:
263
264=over 8
265
266=item family => INT
267
268The address family (e.g. AF_INET)
269
270=item socktype => INT
271
272The socket type (e.g. SOCK_STREAM)
273
274=item protocol => INT
275
276The protocol (e.g. IPPROTO_TCP)
277
278=item addr => STRING
279
280The address in a packed string (such as would be returned by pack_sockaddr_in)
281
282=item canonname => STRING
283
284The canonical name for the host if the C<AI_CANONNAME> flag was provided, or
285C<undef> otherwise.
286
287=back
288
289=item getnameinfo ADDR, FLAGS
290
291Given a packed socket address (such as from C<getsockname>, C<getpeername>, or
292returned by C<getaddrinfo> in a C<addr> field), returns the hostname and
293symbolic service name it represents. FLAGS may be a bitmask of C<NI_*>
294constants, or defaults to 0 if unspecified.
295
296The return value will be a list; the first value being an error condition,
297followed by the hostname and service name.
298
299 my ( $err, $host, $service ) = getnameinfo( ... );
300
301The error value will be a dualvar; comparable to the C<EI_*> error constants,
302or printable as a human-readable error message string. The host and service
303names will be plain strings.
304
2ae324a7
PP
305=back
306
3b35bae3
AD
307=cut
308
a0d0e21e 309use Carp;
d3a7d8c7 310use warnings::register;
a0d0e21e
LW
311
312require Exporter;
da4061d3 313require XSLoader;
9426adcd 314@ISA = qw(Exporter);
80531fbd
LB
315
316# <@Nicholas> you can't change @EXPORT without breaking the implicit API
317# Please put any new constants in @EXPORT_OK!
a0d0e21e 318@EXPORT = qw(
2a84dff3
GA
319 inet_aton inet_ntoa
320 sockaddr_family
321 pack_sockaddr_in unpack_sockaddr_in
4633a7c4 322 pack_sockaddr_un unpack_sockaddr_un
c73aa44c 323 pack_sockaddr_in6 unpack_sockaddr_in6
4bc6101e 324 sockaddr_in sockaddr_in6 sockaddr_un
7e1af8bc 325 INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
a0d0e21e 326 AF_802
7198d2fd 327 AF_AAL
a0d0e21e
LW
328 AF_APPLETALK
329 AF_CCITT
330 AF_CHAOS
7198d2fd 331 AF_CTF
a0d0e21e
LW
332 AF_DATAKIT
333 AF_DECnet
334 AF_DLI
335 AF_ECMA
336 AF_GOSIP
337 AF_HYLINK
338 AF_IMPLINK
339 AF_INET
7198d2fd
JH
340 AF_INET6
341 AF_ISO
342 AF_KEY
343 AF_LAST
a0d0e21e 344 AF_LAT
7198d2fd 345 AF_LINK
a0d0e21e
LW
346 AF_MAX
347 AF_NBS
348 AF_NIT
349 AF_NS
350 AF_OSI
351 AF_OSINET
352 AF_PUP
7198d2fd 353 AF_ROUTE
a0d0e21e
LW
354 AF_SNA
355 AF_UNIX
356 AF_UNSPEC
7198d2fd
JH
357 AF_USER
358 AF_WAN
a0d0e21e 359 AF_X25
6b1016b5 360 IOV_MAX
131c565a
SP
361 IP_OPTIONS
362 IP_HDRINCL
363 IP_TOS
364 IP_TTL
365 IP_RECVOPTS
366 IP_RECVRETOPTS
367 IP_RETOPTS
6b1016b5 368 MSG_BCAST
7198d2fd 369 MSG_BTAG
de4597cb
JH
370 MSG_CTLFLAGS
371 MSG_CTLIGNORE
372 MSG_CTRUNC
a0d0e21e 373 MSG_DONTROUTE
de4597cb
JH
374 MSG_DONTWAIT
375 MSG_EOF
376 MSG_EOR
377 MSG_ERRQUEUE
7198d2fd 378 MSG_ETAG
de4597cb 379 MSG_FIN
a0d0e21e 380 MSG_MAXIOVLEN
6b1016b5 381 MSG_MCAST
de4597cb 382 MSG_NOSIGNAL
a0d0e21e
LW
383 MSG_OOB
384 MSG_PEEK
de4597cb
JH
385 MSG_PROXY
386 MSG_RST
387 MSG_SYN
388 MSG_TRUNC
389 MSG_URG
390 MSG_WAITALL
7198d2fd 391 MSG_WIRE
a0d0e21e 392 PF_802
7198d2fd 393 PF_AAL
a0d0e21e
LW
394 PF_APPLETALK
395 PF_CCITT
396 PF_CHAOS
7198d2fd 397 PF_CTF
a0d0e21e
LW
398 PF_DATAKIT
399 PF_DECnet
400 PF_DLI
401 PF_ECMA
402 PF_GOSIP
403 PF_HYLINK
404 PF_IMPLINK
405 PF_INET
7198d2fd
JH
406 PF_INET6
407 PF_ISO
408 PF_KEY
409 PF_LAST
a0d0e21e 410 PF_LAT
7198d2fd 411 PF_LINK
a0d0e21e
LW
412 PF_MAX
413 PF_NBS
414 PF_NIT
415 PF_NS
416 PF_OSI
417 PF_OSINET
418 PF_PUP
7198d2fd 419 PF_ROUTE
a0d0e21e
LW
420 PF_SNA
421 PF_UNIX
422 PF_UNSPEC
7198d2fd
JH
423 PF_USER
424 PF_WAN
a0d0e21e 425 PF_X25
de4597cb
JH
426 SCM_CONNECT
427 SCM_CREDENTIALS
428 SCM_CREDS
429 SCM_RIGHTS
430 SCM_TIMESTAMP
ca6e1c26
JH
431 SHUT_RD
432 SHUT_RDWR
433 SHUT_WR
a0d0e21e
LW
434 SOCK_DGRAM
435 SOCK_RAW
436 SOCK_RDM
437 SOCK_SEQPACKET
438 SOCK_STREAM
439 SOL_SOCKET
440 SOMAXCONN
441 SO_ACCEPTCONN
7198d2fd
JH
442 SO_ATTACH_FILTER
443 SO_BACKLOG
a0d0e21e 444 SO_BROADCAST
7198d2fd 445 SO_CHAMELEON
a0d0e21e 446 SO_DEBUG
7198d2fd
JH
447 SO_DETACH_FILTER
448 SO_DGRAM_ERRIND
a0d0e21e
LW
449 SO_DONTLINGER
450 SO_DONTROUTE
451 SO_ERROR
7198d2fd 452 SO_FAMILY
a0d0e21e
LW
453 SO_KEEPALIVE
454 SO_LINGER
455 SO_OOBINLINE
7198d2fd
JH
456 SO_PASSCRED
457 SO_PASSIFNAME
458 SO_PEERCRED
459 SO_PROTOCOL
460 SO_PROTOTYPE
a0d0e21e
LW
461 SO_RCVBUF
462 SO_RCVLOWAT
463 SO_RCVTIMEO
464 SO_REUSEADDR
b2f54bf3 465 SO_REUSEPORT
7198d2fd
JH
466 SO_SECURITY_AUTHENTICATION
467 SO_SECURITY_ENCRYPTION_NETWORK
468 SO_SECURITY_ENCRYPTION_TRANSPORT
a0d0e21e
LW
469 SO_SNDBUF
470 SO_SNDLOWAT
471 SO_SNDTIMEO
7198d2fd 472 SO_STATE
a0d0e21e
LW
473 SO_TYPE
474 SO_USELOOPBACK
7198d2fd
JH
475 SO_XOPEN
476 SO_XSE
6b1016b5 477 UIO_MAXIOV
a0d0e21e
LW
478);
479
1494e794
JP
480@EXPORT_OK = qw(CR LF CRLF $CR $LF $CRLF
481
d6896be3
JV
482 inet_pton
483 inet_ntop
484
eccda089
PLE
485 getaddrinfo
486 getnameinfo
487
80531fbd
LB
488 IN6ADDR_ANY IN6ADDR_LOOPBACK
489
eccda089
PLE
490 AI_CANONNAME
491 AI_NUMERICHOST
492 AI_NUMERICSERV
493 AI_PASSIVE
494
495 EAI_ADDRFAMILY
496 EAI_AGAIN
497 EAI_BADFLAGS
498 EAI_FAIL
499 EAI_FAMILY
500 EAI_NODATA
501 EAI_NONAME
502 EAI_SERVICE
503 EAI_SOCKTYPE
504
131c565a
SP
505 IPPROTO_IP
506 IPPROTO_IPV6
507 IPPROTO_RAW
91c526f1 508 IPPROTO_ICMP
1494e794 509 IPPROTO_TCP
91c526f1
GA
510 IPPROTO_UDP
511
eccda089
PLE
512 NI_DGRAM
513 NI_NAMEREQD
514 NI_NUMERICHOST
515 NI_NUMERICSERV
516
1494e794
JP
517 TCP_KEEPALIVE
518 TCP_MAXRT
519 TCP_MAXSEG
520 TCP_NODELAY
f686127b
SF
521 TCP_STDURG
522 TCP_CORK
523 TCP_KEEPIDLE
524 TCP_KEEPINTVL
525 TCP_KEEPCNT
526 TCP_SYNCNT
527 TCP_LINGER2
528 TCP_DEFER_ACCEPT
529 TCP_WINDOW_CLAMP
530 TCP_INFO
531 TCP_QUICKACK
532 TCP_CONGESTION
533 TCP_MD5SIG);
fdb41d65
CN
534
535%EXPORT_TAGS = (
dde527fc 536 crlf => [qw(CR LF CRLF $CR $LF $CRLF)],
fdb41d65
CN
537 all => [@EXPORT, @EXPORT_OK],
538);
539
540BEGIN {
541 sub CR () {"\015"}
542 sub LF () {"\012"}
543 sub CRLF () {"\015\012"}
544}
545
546*CR = \CR();
547*LF = \LF();
548*CRLF = \CRLF();
549
4633a7c4
LW
550sub sockaddr_in {
551 if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die
552 my($af, $port, @quad) = @_;
d3a7d8c7
GS
553 warnings::warn "6-ARG sockaddr_in call is deprecated"
554 if warnings::enabled();
4633a7c4
LW
555 pack_sockaddr_in($port, inet_aton(join('.', @quad)));
556 } elsif (wantarray) {
557 croak "usage: (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1;
558 unpack_sockaddr_in(@_);
559 } else {
560 croak "usage: sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2;
561 pack_sockaddr_in(@_);
562 }
563}
564
4bc6101e
PLE
565sub sockaddr_in6 {
566 if (wantarray) {
567 croak "usage: (port,in6addr,scope_id,flowinfo) = sockaddr_in6(sin6_sv)" unless @_ == 1;
568 unpack_sockaddr_in6(@_);
569 }
570 else {
571 croak "usage: sin6_sv = sockaddr_in6(port,in6addr,[scope_id,[flowinfo]])" unless @_ >= 2 and @_ <= 4;
572 pack_sockaddr_in6(@_);
573 }
574}
575
4633a7c4
LW
576sub sockaddr_un {
577 if (wantarray) {
578 croak "usage: (filename) = sockaddr_un(sun_sv)" unless @_ == 1;
579 unpack_sockaddr_un(@_);
580 } else {
37120919
AD
581 croak "usage: sun_sv = sockaddr_un(filename)" unless @_ == 1;
582 pack_sockaddr_un(@_);
4633a7c4
LW
583 }
584}
585
da4061d3 586XSLoader::load();
a0d0e21e 587
c2219427
PLE
588my %errstr;
589
590if( !defined &getaddrinfo ) {
591 require Scalar::Util;
592
593 *getaddrinfo = \&fake_getaddrinfo;
594 *getnameinfo = \&fake_getnameinfo;
595
596 # These numbers borrowed from GNU libc's implementation, but since
597 # they're only used by our emulation, it doesn't matter if the real
598 # platform's values differ
599 my %constants = (
600 AI_PASSIVE => 1,
601 AI_CANONNAME => 2,
602 AI_NUMERICHOST => 4,
2a59724c
PLE
603 # RFC 2553 doesn't define this but Linux does - lets be nice and
604 # provide it since we can
605 AI_NUMERICSERV => 1024,
c2219427
PLE
606
607 EAI_BADFLAGS => -1,
608 EAI_NONAME => -2,
609 EAI_NODATA => -5,
610 EAI_FAMILY => -6,
611 EAI_SERVICE => -8,
612
613 NI_NUMERICHOST => 1,
614 NI_NUMERICSERV => 2,
615 NI_NAMEREQD => 8,
616 NI_DGRAM => 16,
617 );
618
619 foreach my $name ( keys %constants ) {
fd48e6fc 620 my $value = $constants{$name};
e71d7b85
PLE
621
622 no strict 'refs';
c2219427
PLE
623 defined &$name or *$name = sub () { $value };
624 }
625
626 %errstr = (
627 # These strings from RFC 2553
628 EAI_BADFLAGS() => "invalid value for ai_flags",
629 EAI_NONAME() => "nodename nor servname provided, or not known",
630 EAI_NODATA() => "no address associated with nodename",
631 EAI_FAMILY() => "ai_family not supported",
632 EAI_SERVICE() => "servname not supported for ai_socktype",
633 );
634}
635
636# The following functions are used if the system does not have a
637# getaddrinfo(3) function in libc; and are used to emulate it for the AF_INET
638# family
639
c2219427
PLE
640# Borrowed from Regexp::Common::net
641my $REGEXP_IPv4_DECIMAL = qr/25[0-5]|2[0-4][0-9]|1?[0-9][0-9]{1,2}/;
642my $REGEXP_IPv4_DOTTEDQUAD = qr/$REGEXP_IPv4_DECIMAL\.$REGEXP_IPv4_DECIMAL\.$REGEXP_IPv4_DECIMAL\.$REGEXP_IPv4_DECIMAL/;
643
644sub fake_makeerr
645{
646 my ( $errno ) = @_;
647 my $errstr = $errno == 0 ? "" : ( $errstr{$errno} || $errno );
648 return Scalar::Util::dualvar( $errno, $errstr );
649}
650
651sub fake_getaddrinfo
652{
653 my ( $node, $service, $hints ) = @_;
654
655 $node = "" unless defined $node;
656
657 $service = "" unless defined $service;
658
659 my ( $family, $socktype, $protocol, $flags ) = @$hints{qw( family socktype protocol flags )};
660
661 $family ||= Socket::AF_INET(); # 0 == AF_UNSPEC, which we want too
662 $family == Socket::AF_INET() or return fake_makeerr( EAI_FAMILY() );
663
664 $socktype ||= 0;
665
666 $protocol ||= 0;
667
668 $flags ||= 0;
669
670 my $flag_passive = $flags & AI_PASSIVE(); $flags &= ~AI_PASSIVE();
671 my $flag_canonname = $flags & AI_CANONNAME(); $flags &= ~AI_CANONNAME();
672 my $flag_numerichost = $flags & AI_NUMERICHOST(); $flags &= ~AI_NUMERICHOST();
2a59724c 673 my $flag_numericserv = $flags & AI_NUMERICSERV(); $flags &= ~AI_NUMERICSERV();
c2219427
PLE
674
675 $flags == 0 or return fake_makeerr( EAI_BADFLAGS() );
676
677 $node eq "" and $service eq "" and return fake_makeerr( EAI_NONAME() );
678
679 my $canonname;
680 my @addrs;
681 if( $node ne "" ) {
682 return fake_makeerr( EAI_NONAME() ) if( $flag_numerichost and $node !~ m/^$REGEXP_IPv4_DOTTEDQUAD$/ );
683 ( $canonname, undef, undef, undef, @addrs ) = gethostbyname( $node );
684 defined $canonname or return fake_makeerr( EAI_NONAME() );
685
686 undef $canonname unless $flag_canonname;
687 }
688 else {
689 $addrs[0] = $flag_passive ? Socket::inet_aton( "0.0.0.0" )
690 : Socket::inet_aton( "127.0.0.1" );
691 }
692
693 my @ports; # Actually ARRAYrefs of [ socktype, protocol, port ]
694 my $protname = "";
695 if( $protocol ) {
696 $protname = getprotobynumber( $protocol );
697 }
698
699 if( $service ne "" and $service !~ m/^\d+$/ ) {
2a59724c 700 return fake_makeerr( EAI_NONAME() ) if( $flag_numericserv );
c2219427
PLE
701 getservbyname( $service, $protname ) or return fake_makeerr( EAI_SERVICE() );
702 }
703
704 foreach my $this_socktype ( Socket::SOCK_STREAM(), Socket::SOCK_DGRAM(), Socket::SOCK_RAW() ) {
705 next if $socktype and $this_socktype != $socktype;
706
707 my $this_protname = "raw";
708 $this_socktype == Socket::SOCK_STREAM() and $this_protname = "tcp";
709 $this_socktype == Socket::SOCK_DGRAM() and $this_protname = "udp";
710
711 next if $protname and $this_protname ne $protname;
712
713 my $port;
714 if( $service ne "" ) {
715 if( $service =~ m/^\d+$/ ) {
716 $port = "$service";
717 }
718 else {
719 ( undef, undef, $port, $this_protname ) = getservbyname( $service, $this_protname );
720 next unless defined $port;
721 }
722 }
723 else {
724 $port = 0;
725 }
726
727 push @ports, [ $this_socktype, scalar getprotobyname( $this_protname ) || 0, $port ];
728 }
729
730 my @ret;
731 foreach my $addr ( @addrs ) {
732 foreach my $portspec ( @ports ) {
733 my ( $socktype, $protocol, $port ) = @$portspec;
734 push @ret, {
735 family => $family,
736 socktype => $socktype,
737 protocol => $protocol,
738 addr => Socket::pack_sockaddr_in( $port, $addr ),
739 canonname => $canonname,
740 };
741 }
742 }
743
744 return ( fake_makeerr( 0 ), @ret );
745}
746
747sub fake_getnameinfo
748{
749 my ( $addr, $flags ) = @_;
750
751 my ( $port, $inetaddr );
752 eval { ( $port, $inetaddr ) = Socket::unpack_sockaddr_in( $addr ) }
753 or return fake_makeerr( EAI_FAMILY() );
754
755 my $family = Socket::AF_INET();
756
757 $flags ||= 0;
758
759 my $flag_numerichost = $flags & NI_NUMERICHOST(); $flags &= ~NI_NUMERICHOST();
760 my $flag_numericserv = $flags & NI_NUMERICSERV(); $flags &= ~NI_NUMERICSERV();
761 my $flag_namereqd = $flags & NI_NAMEREQD(); $flags &= ~NI_NAMEREQD();
762 my $flag_dgram = $flags & NI_DGRAM() ; $flags &= ~NI_DGRAM();
763
764 $flags == 0 or return fake_makeerr( EAI_BADFLAGS() );
765
766 my $node;
767 if( $flag_numerichost ) {
768 $node = Socket::inet_ntoa( $inetaddr );
769 }
770 else {
771 $node = gethostbyaddr( $inetaddr, $family );
772 if( !defined $node ) {
773 return fake_makeerr( EAI_NONAME() ) if $flag_namereqd;
774 $node = Socket::inet_ntoa( $inetaddr );
775 }
776 }
777
778 my $service;
779 if( $flag_numericserv ) {
780 $service = "$port";
781 }
782 else {
783 my $protname = $flag_dgram ? "udp" : "";
784 $service = getservbyport( $port, $protname );
785 if( !defined $service ) {
786 $service = "$port";
787 }
788 }
789
790 return ( fake_makeerr( 0 ), $node, $service );
791}
792
a0d0e21e 7931;