From bd71e0a134e4707a8d678d30a1c445a5f9d9812c Mon Sep 17 00:00:00 2001 From: "Paul \\\"LeoNerd\\\" Evans" Date: Mon, 24 Oct 2011 17:51:13 +0100 Subject: Rewrite ext/Socket/t/Socket.t to use Test::More rather than manual printing --- ext/Socket/t/Socket.t | 223 ++++++++++++++++++++++---------------------------- 1 file changed, 97 insertions(+), 126 deletions(-) diff --git a/ext/Socket/t/Socket.t b/ext/Socket/t/Socket.t index 326fa7a9ef..be15c01fa1 100644 --- a/ext/Socket/t/Socket.t +++ b/ext/Socket/t/Socket.t @@ -11,8 +11,7 @@ BEGIN { } use Socket qw(:all); - -print "1..26\n"; +use Test::More tests => 26; $has_echo = $^O ne 'MSWin32'; $alarmed = 0; @@ -20,20 +19,28 @@ sub arm { $alarmed = 0; alarm(shift) if $has_alarm } sub alarmed { $alarmed = 1 } $SIG{ALRM} = 'alarmed' if $has_alarm; -if (socket(T, PF_INET, SOCK_STREAM, IPPROTO_TCP)) { - print "ok 1\n"; - - arm(5); - my $host = $^O eq 'MacOS' || ($^O eq 'irix' && $Config{osvers} == 5) ? - '127.0.0.1' : 'localhost'; - my $localhost = inet_aton($host); +SKIP: { + unless(socket(T, PF_INET, SOCK_STREAM, IPPROTO_TCP)) { + skip "No PF_INET", 3; + } + + pass "socket(PF_INET)"; + + arm(5); + my $host = $^O eq 'MacOS' || ($^O eq 'irix' && $Config{osvers} == 5) ? + '127.0.0.1' : 'localhost'; + my $localhost = inet_aton($host); + + SKIP: { + unless($has_echo && defined $localhost && connect(T,pack_sockaddr_in(7,$localhost))) { + skip "Unable to connect to localhost:7", 2; + } - if ($has_echo && defined $localhost && connect(T,pack_sockaddr_in(7,$localhost))){ arm(0); - print "ok 2\n"; + pass "PF_INET echo localhost connected"; - print "# Connected to " . + diag "Connected to " . inet_ntoa((unpack_sockaddr_in(getpeername(T)))[1])."\n"; arm(5); @@ -50,34 +57,29 @@ if (socket(T, PF_INET, SOCK_STREAM, IPPROTO_TCP)) { $read = sysread(T,$buff,10,length($buff)); arm(0); } - print(($read == 0 || $buff eq "hello") ? "ok 3\n" : "not ok 3\n"); - } - else { - print "# You're allowed to fail tests 2 and 3 if\n"; - print "# the echo service has been disabled or if your\n"; - print "# gethostbyname() cannot resolve your localhost.\n"; - print "# 'Connection refused' indicates disabled echo service.\n"; - print "# 'Interrupted system call' indicates a hanging echo service.\n"; - print "# Error: $!\n"; - print "ok 2 - skipped\n"; - print "ok 3 - skipped\n"; - } -} -else { - print "# Error: $!\n"; - print "not ok 1\n"; + + is(($read == 0 || $buff eq "hello"), "PF_INET echo localhost reply"); + } } -if( socket(S, PF_INET,SOCK_STREAM, IPPROTO_TCP) ){ - print "ok 4\n"; +SKIP: { + unless(socket(S, PF_INET, SOCK_STREAM, IPPROTO_TCP)) { + skip "No PF_INET", 3; + } + + pass "socket(PF_INET)"; + + SKIP: { + arm(5); + unless($has_echo && connect(S,pack_sockaddr_in(7,INADDR_LOOPBACK))) { + skip "Unable to connect to localhost:7", 2; + } - arm(5); - if ($has_echo && connect(S,pack_sockaddr_in(7,INADDR_LOOPBACK))){ arm(0); - print "ok 5\n"; + pass "PF_INET echo INADDR_LOOPBACK connected"; - print "# Connected to " . + diag "Connected to " . inet_ntoa((unpack_sockaddr_in(getpeername(S)))[1])."\n"; arm(5); @@ -94,120 +96,89 @@ if( socket(S, PF_INET,SOCK_STREAM, IPPROTO_TCP) ){ $read = sysread(S,$buff,10,length($buff)); arm(0); } - print(($read == 0 || $buff eq "olleh") ? "ok 6\n" : "not ok 6\n"); - } - else { - print "# You're allowed to fail tests 5 and 6 if\n"; - print "# the echo service has been disabled.\n"; - print "# 'Interrupted system call' indicates a hanging echo service.\n"; - print "# Error: $!\n"; - print "ok 5 - skipped\n"; - print "ok 6 - skipped\n"; - } -} -else { - print "# Error: $!\n"; - print "not ok 4\n"; + + is(($read == 0 || $buff eq "olleh"), "PF_INET echo INADDR_LOOPBACK reply"); + } } # warnings -$SIG{__WARN__} = sub { - ++ $w if $_[0] =~ /^6-ARG sockaddr_in call is deprecated/ ; -} ; -$w = 0 ; -sockaddr_in(1,2,3,4,5,6) ; -print ($w == 1 ? "not ok 7\n" : "ok 7\n") ; -use warnings 'Socket' ; -sockaddr_in(1,2,3,4,5,6) ; -print ($w == 1 ? "ok 8\n" : "not ok 8\n") ; - -# Thest that whatever we give into pack/unpack_sockaddr retains -# the value thru the entire chain. -if((inet_ntoa((unpack_sockaddr_in(pack_sockaddr_in(100,inet_aton("10.250.230.10"))))[1])) eq '10.250.230.10') { - print "ok 9\n"; -} else { - print "not ok 9\n"; +{ + my $w = 0; + local $SIG{__WARN__} = sub { + ++ $w if $_[0] =~ /^6-ARG sockaddr_in call is deprecated/ ; + }; + + no warnings 'Socket'; + sockaddr_in(1,2,3,4,5,6) ; + is($w, 0, "sockaddr_in deprecated form doesn't warn without lexical warnings"); + + use warnings 'Socket'; + sockaddr_in(1,2,3,4,5,6) ; + is($w, 1, "sockaddr_in deprecated form warns with lexical warnings"); } -print ((inet_ntoa(inet_aton("10.20.30.40")) eq "10.20.30.40") ? "ok 10\n" : "not ok 10\n"); -print ((inet_ntoa(v10.20.30.40) eq "10.20.30.40") ? "ok 11\n" : "not ok 11\n"); + +# Test that whatever we give into pack/unpack_sockaddr retains +# the value thru the entire chain. +is(inet_ntoa((unpack_sockaddr_in(pack_sockaddr_in(100,inet_aton("10.250.230.10"))))[1]), '10.250.230.10', + 'inet_aton->pack_sockaddr_in->unpack_sockaddr_in->inet_ntoa roundtrip'); + +is(inet_ntoa(inet_aton("10.20.30.40")), "10.20.30.40", 'inet_aton->inet_ntoa roundtrip'); +is(inet_ntoa(v10.20.30.40), "10.20.30.40", 'inet_ntoa from v-string'); + { my ($port,$addr) = unpack_sockaddr_in(pack_sockaddr_in(100,v10.10.10.10)); - print (($port == 100) ? "ok 12\n" : "not ok 12\n"); - print ((inet_ntoa($addr) eq "10.10.10.10") ? "ok 13\n" : "not ok 13\n"); + is($port, 100, 'pack_sockaddr_in->unpack_sockaddr_in port'); + is(inet_ntoa($addr), "10.10.10.10", 'pack_sockaddr_in->unpack_sockaddr_in addr'); } - -eval { inet_ntoa(v10.20.30.400) }; -print (($@ =~ /^Wide character in Socket::inet_ntoa at/) ? "ok 14\n" : "not ok 14\n"); - -if (sockaddr_family(pack_sockaddr_in(100,inet_aton("10.250.230.10"))) == AF_INET) { - print "ok 15\n"; -} else { - print "not ok 15\n"; + +{ + local $@; + eval { inet_ntoa(v10.20.30.400) }; + like($@, qr/^Wide character in Socket::inet_ntoa at/, 'inet_ntoa warns about wide characters'); } -eval { sockaddr_family("") }; -print (($@ =~ /^Bad arg length for Socket::sockaddr_family, length is 0, should be at least \d+/) ? "ok 16\n" : "not ok 16\n"); +is(sockaddr_family(pack_sockaddr_in(100,inet_aton("10.250.230.10"))), AF_INET, 'pack_sockaddr_in->sockaddr_family'); -if ($^O eq 'linux') { +{ + local $@; + eval { sockaddr_family("") }; + like($@, qr/^Bad arg length for Socket::sockaddr_family, length is 0, should be at least \d+/, 'sockaddr_family warns about argument length'); +} + +SKIP: { # see if we can handle abstract sockets + skip "Abstract AF_UNIX paths unsupported", 2 unless $^O eq "linux"; + my $test_abstract_socket = chr(0) . '/org/perl/hello'. chr(0) . 'world'; my $addr = sockaddr_un ($test_abstract_socket); my ($path) = sockaddr_un ($addr); - if ($test_abstract_socket eq $path) { - print "ok 17\n"; - } - else { - $path =~ s/\0/\\0/g; - print "# got <$path>\n"; - print "not ok 17\n"; - } + is($path, $test_abstract_socket, 'sockaddr_un can handle abstract AF_UNIX paths'); # see if we calculate the address structure length correctly - if (length ($test_abstract_socket) + 2 == length $addr) { - print "ok 18\n"; - } else { - print "# got ".(length $addr)."\n"; - print "not ok 18\n"; - } - -} else { - # doesn't have abstract socket support - print "ok 17 - skipped on this platform\n"; - print "ok 18 - skipped on this platform\n"; + is(length ($test_abstract_socket) + 2, length $addr, 'sockaddr_un abstract address length'); } -if($Config{d_inetntop} && $Config{d_inetaton}){ - print ((inet_ntop(AF_INET, inet_pton(AF_INET, "10.20.30.40")) eq "10.20.30.40") ? "ok 19\n" : "not ok 19\n"); - print ((inet_ntop(AF_INET, inet_aton("10.20.30.40")) eq "10.20.30.40") ? "ok 20\n" : "not ok 20\n"); - if(defined eval { AF_INET6() } ) { - print (lc(inet_ntop(AF_INET6, inet_pton(AF_INET6, "2001:503:BA3E::2:30")) eq "2001:503:ba3e::2:30") ? "ok 21\n" : "not ok 21\n"); - } - else { - print "ok 21 - skipped - no AF_INET6\n"; - } -} else { - # no IPv6 - print "ok $_ - skipped on this platform\n" for 19 .. 21; -} +SKIP: { + skip "No inet_ntop", 3 unless $Config{d_inetntop} && $Config{d_inetaton}; -if(defined eval { AF_INET6() } ) { - my $sin6 = pack_sockaddr_in6(0x1234, "0123456789abcdef", 0, 89); + is(inet_ntop(AF_INET, inet_pton(AF_INET, "10.20.30.40")), "10.20.30.40", 'inet_pton->inet_ntop AF_INET roundtrip'); + is(inet_ntop(AF_INET, inet_aton("10.20.30.40")), "10.20.30.40", 'inet_aton->inet_ntop AF_INET roundtrip'); - print "not " unless sockaddr_family($sin6) == AF_INET6; - print "ok 22\n"; + SKIP: { + skip "No AF_INET6", 1 unless defined eval { AF_INET6() }; + is(lc inet_ntop(AF_INET6, inet_pton(AF_INET6, "2001:503:BA3E::2:30")), "2001:503:ba3e::2:30", 'inet_pton->inet_ntop AF_INET6 roundtrip'); + } +} - print "not " unless (unpack_sockaddr_in6($sin6))[0] == 0x1234; - print "ok 23\n"; +SKIP: { + skip "No AF_INET6", 5 unless defined eval { AF_INET6() }; - print "not " unless (unpack_sockaddr_in6($sin6))[1] == "0123456789abcdef"; - print "ok 24\n"; + my $sin6 = pack_sockaddr_in6(0x1234, "0123456789abcdef", 0, 89); - print "not " unless (unpack_sockaddr_in6($sin6))[2] == 0; - print "ok 25\n"; + is(sockaddr_family($sin6), AF_INET6, 'sockaddr_family of pack_sockaddr_in6'); - print "not " unless (unpack_sockaddr_in6($sin6))[3] == 89; - print "ok 26\n"; -} -else { - print "ok $_ - skipped - no AF_INET6\n" for 22 .. 26; + is((unpack_sockaddr_in6($sin6))[0], 0x1234, 'pack_sockaddr_in6->unpack_sockaddr_in6 port'); + is((unpack_sockaddr_in6($sin6))[1], "0123456789abcdef", 'pack_sockaddr_in6->unpack_sockaddr_in6 addr'); + is((unpack_sockaddr_in6($sin6))[2], 0, 'pack_sockaddr_in6->unpack_sockaddr_in6 scope_id'); + is((unpack_sockaddr_in6($sin6))[3], 89, 'pack_sockaddr_in6->unpack_sockaddr_in6 flowinfo'); } -- cgit v1.2.1 From 4c3d8e12eba407ea54003da3b559af75fc927817 Mon Sep 17 00:00:00 2001 From: "Paul \\\"LeoNerd\\\" Evans" Date: Mon, 24 Oct 2011 18:07:42 +0100 Subject: Fixups for older Perl versions: * Pass package and version to XSLoader::load to keep older perl versions happy * Older perls do not define croak_sv; #define a workaround for them --- ext/Socket/Socket.pm | 2 +- ext/Socket/Socket.xs | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/ext/Socket/Socket.pm b/ext/Socket/Socket.pm index d82aa2bfaf..20ae15b07b 100644 --- a/ext/Socket/Socket.pm +++ b/ext/Socket/Socket.pm @@ -612,7 +612,7 @@ sub sockaddr_un { } } -XSLoader::load(); +XSLoader::load(__PACKAGE__, $VERSION); my %errstr; diff --git a/ext/Socket/Socket.xs b/ext/Socket/Socket.xs index e164e67259..d7e7a11eb6 100644 --- a/ext/Socket/Socket.xs +++ b/ext/Socket/Socket.xs @@ -71,6 +71,10 @@ NETINET_DEFINE_CONTEXT # define INADDR_LOOPBACK 0x7F000001 #endif /* INADDR_LOOPBACK */ +#ifndef croak_sv +# define croak_sv(sv) croak(SvPV_nolen(sv)) +#endif + #ifndef HAS_INET_ATON /* -- cgit v1.2.1 From 44559db1c23f895802dafc1174080c9a8c0f1bce Mon Sep 17 00:00:00 2001 From: "Paul \\\"LeoNerd\\\" Evans" Date: Mon, 24 Oct 2011 19:02:43 +0100 Subject: Prepare Socket.pm for dual-life * Moved from ext/Socket to cpan/Socket * Currently declared as PEVANS/Socket-1.95.tar.gz - which is what it will eventually become --- MANIFEST | 16 +- Porting/Maintainers.pl | 8 +- cpan/Socket/.gitignore | 1 + cpan/Socket/Makefile.PL | 93 +++++ cpan/Socket/Socket.pm | 832 +++++++++++++++++++++++++++++++++++++++++++ cpan/Socket/Socket.xs | 851 ++++++++++++++++++++++++++++++++++++++++++++ cpan/Socket/t/Socket.t | 184 ++++++++++ cpan/Socket/t/getaddrinfo.t | 125 +++++++ cpan/Socket/t/getnameinfo.t | 37 ++ cpan/Socket/t/ipv6_mreq.t | 26 ++ cpan/Socket/t/socketpair.t | 245 +++++++++++++ ext/Socket/.gitignore | 1 - ext/Socket/Makefile.PL | 93 ----- ext/Socket/Socket.pm | 832 ------------------------------------------- ext/Socket/Socket.xs | 851 -------------------------------------------- ext/Socket/t/Socket.t | 184 ---------- ext/Socket/t/getaddrinfo.t | 125 ------- ext/Socket/t/getnameinfo.t | 37 -- ext/Socket/t/ipv6_mreq.t | 26 -- ext/Socket/t/socketpair.t | 245 ------------- 20 files changed, 2407 insertions(+), 2405 deletions(-) create mode 100644 cpan/Socket/.gitignore create mode 100644 cpan/Socket/Makefile.PL create mode 100644 cpan/Socket/Socket.pm create mode 100644 cpan/Socket/Socket.xs create mode 100644 cpan/Socket/t/Socket.t create mode 100644 cpan/Socket/t/getaddrinfo.t create mode 100644 cpan/Socket/t/getnameinfo.t create mode 100644 cpan/Socket/t/ipv6_mreq.t create mode 100644 cpan/Socket/t/socketpair.t delete mode 100644 ext/Socket/.gitignore delete mode 100644 ext/Socket/Makefile.PL delete mode 100644 ext/Socket/Socket.pm delete mode 100644 ext/Socket/Socket.xs delete mode 100644 ext/Socket/t/Socket.t delete mode 100644 ext/Socket/t/getaddrinfo.t delete mode 100644 ext/Socket/t/getnameinfo.t delete mode 100644 ext/Socket/t/ipv6_mreq.t delete mode 100644 ext/Socket/t/socketpair.t diff --git a/MANIFEST b/MANIFEST index bda0e07f33..eeadfa91b2 100644 --- a/MANIFEST +++ b/MANIFEST @@ -2169,6 +2169,14 @@ cpan/Pod-Simple/t/xhtml05.t Pod::Simple test file cpan/Pod-Simple/t/xhtml10.t Pod::Simple test file cpan/Pod-Simple/t/xhtml15.t Pod::Simple test file cpan/Pod-Simple/t/x_nixer.t Pod::Simple test file +cpan/Socket/Makefile.PL Socket extension makefile writer +cpan/Socket/Socket.pm Socket extension Perl module +cpan/Socket/Socket.xs Socket extension external subroutines +cpan/Socket/t/getaddrinfo.t See if Socket::getaddrinfo works +cpan/Socket/t/getnameinfo.t See if Socket::getnameinfo works +cpan/Socket/t/ipv6_mreq.t See if (un)pack_ipv6_mreq work +cpan/Socket/t/socketpair.t See if socketpair works +cpan/Socket/t/Socket.t See if Socket works cpan/Sys-Syslog/Changes Changelog for Sys::Syslog cpan/Sys-Syslog/fallback/const-c.inc Sys::Syslog constants fallback file cpan/Sys-Syslog/fallback/const-xs.inc Sys::Syslog constants fallback file @@ -3859,14 +3867,6 @@ ext/SDBM_File/sdbm/tune.h SDBM kit ext/SDBM_File/sdbm/util.c SDBM kit ext/SDBM_File/t/sdbm.t See if SDBM_File works ext/SDBM_File/typemap SDBM extension interface types -ext/Socket/Makefile.PL Socket extension makefile writer -ext/Socket/Socket.pm Socket extension Perl module -ext/Socket/Socket.xs Socket extension external subroutines -ext/Socket/t/getaddrinfo.t See if Socket::getaddrinfo works -ext/Socket/t/getnameinfo.t See if Socket::getnameinfo works -ext/Socket/t/ipv6_mreq.t See if (un)pack_ipv6_mreq work -ext/Socket/t/socketpair.t See if socketpair works -ext/Socket/t/Socket.t See if Socket works ext/Sys-Hostname/Hostname.pm Sys::Hostname extension Perl module ext/Sys-Hostname/Hostname.xs Sys::Hostname extension external subroutines ext/Sys-Hostname/t/Hostname.t See if Sys::Hostname works diff --git a/Porting/Maintainers.pl b/Porting/Maintainers.pl index 97037c79fd..aa5dfc066a 100755 --- a/Porting/Maintainers.pl +++ b/Porting/Maintainers.pl @@ -65,6 +65,7 @@ use File::Glob qw(:case); 'p5p' => 'perl5-porters ', 'perlfaq' => 'perlfaq-workers ', 'petdance' => 'Andy Lester ', + 'pevans' => 'Paul Evans ', 'pjf' => 'Paul Fenwick ', 'pmqs' => 'Paul Marquess ', 'pvhp' => 'Peter Prymmer ', @@ -1663,9 +1664,10 @@ use File::Glob qw(:case); 'Socket' => { - 'MAINTAINER' => 'p5p', - 'FILES' => q[ext/Socket], - 'UPSTREAM' => 'blead', + 'MAINTAINER' => 'pevans', + 'DISTRIBUTION' => 'PEVANS/Socket-1.95.tar.gz', + 'FILES' => q[cpan/Socket], + 'UPSTREAM' => 'cpan', }, 'Storable' => diff --git a/cpan/Socket/.gitignore b/cpan/Socket/.gitignore new file mode 100644 index 0000000000..2a06e93b55 --- /dev/null +++ b/cpan/Socket/.gitignore @@ -0,0 +1 @@ +*.inc diff --git a/cpan/Socket/Makefile.PL b/cpan/Socket/Makefile.PL new file mode 100644 index 0000000000..e5daf93a98 --- /dev/null +++ b/cpan/Socket/Makefile.PL @@ -0,0 +1,93 @@ +use ExtUtils::MakeMaker; +use ExtUtils::Constant 0.23 'WriteConstants'; +use Config; +WriteMakefile( + NAME => 'Socket', + VERSION_FROM => 'Socket.pm', + ($Config{libs} =~ /(-lsocks\S*)/ ? (LIBS => [ "$1" ] ) : ()), + XSPROTOARG => '-noprototypes', # XXX remove later? + realclean => {FILES=> 'const-c.inc const-xs.inc'}, +); +my @names = (qw(AF_802 AF_AAL AF_APPLETALK AF_CCITT AF_CHAOS AF_CTF + AF_DATAKIT AF_DECnet AF_DLI AF_ECMA AF_GOSIP AF_HYLINK + AF_IMPLINK AF_INET AF_INET6 AF_ISO AF_KEY + AF_LAST AF_LAT AF_LINK AF_MAX AF_NBS AF_NIT AF_NS + AF_OSI AF_OSINET AF_PUP AF_ROUTE AF_SNA + AF_UNIX AF_UNSPEC AF_USER AF_WAN AF_X25 + AI_CANONNAME AI_NUMERICHOST AI_NUMERICSERV AI_PASSIVE + EAI_ADDRFAMILY EAI_AGAIN EAI_BADFLAGS EAI_FAIL EAI_FAMILY + EAI_NODATA EAI_NONAME EAI_SERVICE EAI_SOCKTYPE + IOV_MAX IP_OPTIONS IP_HDRINCL IP_TOS IP_TTL IP_RECVOPTS + IP_RECVRETOPTS IP_RETOPTS + IPV6_ADD_MEMBERSHIP IPV6_DROP_MEMBERSHIP IPV6_MTU + IPV6_MTU_DISCOVER IPV6_MULTICAST_HOPS IPV6_MULTICAST_IF + IPV6_MULTICAST_LOOP IPV6_UNICAST_HOPS IPV6_V6ONLY + MSG_BCAST MSG_BTAG MSG_CTLFLAGS MSG_CTLIGNORE MSG_DONTWAIT + MSG_EOF MSG_EOR MSG_ERRQUEUE MSG_ETAG MSG_FIN + MSG_MAXIOVLEN MSG_MCAST MSG_NOSIGNAL MSG_RST MSG_SYN + MSG_TRUNC MSG_URG MSG_WAITALL MSG_WIRE + NI_DGRAM NI_NAMEREQD NI_NUMERICHOST NI_NUMERICSERV + PF_802 PF_AAL PF_APPLETALK PF_CCITT PF_CHAOS PF_CTF + PF_DATAKIT PF_DECnet PF_DLI PF_ECMA PF_GOSIP PF_HYLINK + PF_IMPLINK PF_INET PF_INET6 PF_ISO PF_KEY + PF_LAST PF_LAT PF_LINK PF_MAX PF_NBS PF_NIT PF_NS + PF_OSI PF_OSINET PF_PUP PF_ROUTE PF_SNA + PF_UNIX PF_UNSPEC PF_USER PF_WAN PF_X25 + SCM_CONNECT SCM_CREDENTIALS SCM_CREDS SCM_TIMESTAMP + SOCK_DGRAM SOCK_RAW SOCK_RDM SOCK_SEQPACKET SOCK_STREAM + SOL_SOCKET SOMAXCONN + SO_ACCEPTCONN SO_ATTACH_FILTER SO_BACKLOG SO_BROADCAST + SO_CHAMELEON SO_DEBUG SO_DETACH_FILTER SO_DGRAM_ERRIND + SO_DONTLINGER SO_DONTROUTE SO_ERROR SO_FAMILY + SO_KEEPALIVE SO_LINGER SO_OOBINLINE + SO_PASSCRED SO_PASSIFNAME SO_PEERCRED + SO_PROTOCOL SO_PROTOTYPE + SO_RCVBUF SO_RCVLOWAT SO_RCVTIMEO + SO_REUSEADDR SO_REUSEPORT + SO_SECURITY_AUTHENTICATION + SO_SECURITY_ENCRYPTION_NETWORK + SO_SECURITY_ENCRYPTION_TRANSPORT + SO_SNDBUF SO_SNDLOWAT SO_SNDTIMEO + SO_STATE SO_TYPE SO_USELOOPBACK SO_XOPEN SO_XSE + TCP_KEEPALIVE TCP_MAXRT TCP_MAXSEG TCP_NODELAY TCP_STDURG + TCP_CORK TCP_KEEPIDLE TCP_KEEPINTVL TCP_KEEPCNT + TCP_SYNCNT TCP_LINGER2 TCP_DEFER_ACCEPT TCP_WINDOW_CLAMP + TCP_INFO TCP_QUICKACK TCP_CONGESTION TCP_MD5SIG + UIO_MAXIOV + ), + {name=>"IPPROTO_IP", type=>"IV", default=>["IV", 0]}, + {name=>"IPPROTO_IPV6", type=>"IV", default=>["IV", 41]}, + {name=>"IPPROTO_RAW", type=>"IV", default=>["IV", 255]}, + {name=>"IPPROTO_ICMP", type=>"IV", default=>["IV", 1]}, + {name=>"IPPROTO_TCP", type=>"IV", default=>["IV", 6]}, + {name=>"IPPROTO_UDP", type=>"IV", default=>["IV", 17]}, + {name=>"SHUT_RD", type=>"IV", default=>["IV", "0"]}, + {name=>"SHUT_WR", type=>"IV", default=>["IV", "1"]}, + {name=>"SHUT_RDWR", type=>"IV", default=>["IV", "2"]}, +); + +push @names, + {name=>$_, type=>"IV", + macro=>["#if defined($_) || defined(HAS_$_) /* might be an enum */\n", + "#endif\n"]} +foreach qw (MSG_CTRUNC MSG_DONTROUTE MSG_OOB MSG_PEEK MSG_PROXY SCM_RIGHTS); + +push @names, +{name => $_, type => "SV", + pre=>"struct in_addr ip_address; ip_address.s_addr = htonl($_);", + value => "newSVpvn_flags((char *)&ip_address,sizeof(ip_address), SVs_TEMP)",} + foreach qw(INADDR_ANY INADDR_LOOPBACK INADDR_NONE INADDR_BROADCAST); + +push @names, +{name => $_, type => "SV", + macro=>["#ifdef ${_}_INIT\n", + "#endif\n"], + pre=>"struct in6_addr ip6_address = ${_}_INIT;", + value => "newSVpvn_flags((char *)&ip6_address,sizeof(ip6_address), SVs_TEMP)",} + foreach qw(IN6ADDR_ANY IN6ADDR_LOOPBACK); + +WriteConstants( + PROXYSUBS => {autoload => 1}, + NAME => 'Socket', + NAMES => \@names, +); diff --git a/cpan/Socket/Socket.pm b/cpan/Socket/Socket.pm new file mode 100644 index 0000000000..5fff0d0150 --- /dev/null +++ b/cpan/Socket/Socket.pm @@ -0,0 +1,832 @@ +package Socket; + +use strict; + +our($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); +$VERSION = "1.94_03"; + +=head1 NAME + +Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa, inet_pton, inet_ntop - load the C socket.h defines and structure manipulators + +=head1 SYNOPSIS + + use Socket; + + $proto = getprotobyname('udp'); + socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto); + $iaddr = gethostbyname('hishost.com'); + $port = getservbyname('time', 'udp'); + $sin = sockaddr_in($port, $iaddr); + send(Socket_Handle, 0, 0, $sin); + + $proto = getprotobyname('tcp'); + socket(Socket_Handle, PF_INET, SOCK_STREAM, $proto); + $port = getservbyname('smtp', 'tcp'); + $sin = sockaddr_in($port,inet_aton("127.1")); + $sin = sockaddr_in(7,inet_aton("localhost")); + $sin = sockaddr_in(7,INADDR_LOOPBACK); + connect(Socket_Handle,$sin); + + ($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle)); + $peer_host = gethostbyaddr($iaddr, AF_INET); + $peer_addr = inet_ntoa($iaddr); + + $proto = getprotobyname('tcp'); + socket(Socket_Handle, PF_UNIX, SOCK_STREAM, $proto); + unlink('/var/run/usock'); + $sun = sockaddr_un('/var/run/usock'); + connect(Socket_Handle,$sun); + +=head1 DESCRIPTION + +This module is just a translation of the C F file. +Unlike the old mechanism of requiring a translated F +file, this uses the B program (see the Perl source distribution) +and your native C compiler. This means that it has a +far more likely chance of getting the numbers right. This includes +all of the commonly used pound-defines like AF_INET, SOCK_STREAM, etc. + +Also, some common socket "newline" constants are provided: the +constants C, C, and C, as well as C<$CR>, C<$LF>, and +C<$CRLF>, which map to C<\015>, C<\012>, and C<\015\012>. If you do +not want to use the literal characters in your programs, then use +the constants provided here. They are not exported by default, but can +be imported individually, and with the C<:crlf> export tag: + + use Socket qw(:DEFAULT :crlf); + +In addition, some structure manipulation functions are available: + +=over 4 + +=item inet_aton HOSTNAME + +Takes a string giving the name of a host, and translates that to an +opaque string (if programming in C, struct in_addr). Takes arguments +of both the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name +cannot be resolved, returns undef. For multi-homed hosts (hosts with +more than one address), the first address found is returned. + +For portability do not assume that the result of inet_aton() is 32 +bits wide, in other words, that it would contain only the IPv4 address +in network order. + +=item inet_ntoa IP_ADDRESS + +Takes a string (an opaque string as returned by inet_aton(), +or a v-string representing the four octets of the IPv4 address in +network order) and translates it into a string of the form 'd.d.d.d' +where the 'd's are numbers less than 256 (the normal human-readable +four dotted number notation for Internet addresses). + +=item INADDR_ANY + +Note: does not return a number, but a packed string. + +Returns the 4-byte wildcard ip address which specifies any +of the hosts ip addresses. (A particular machine can have +more than one ip address, each address corresponding to +a particular network interface. This wildcard address +allows you to bind to all of them simultaneously.) +Normally equivalent to inet_aton('0.0.0.0'). + +=item INADDR_BROADCAST + +Note: does not return a number, but a packed string. + +Returns the 4-byte 'this-lan' ip broadcast address. +This can be useful for some protocols to solicit information +from all servers on the same LAN cable. +Normally equivalent to inet_aton('255.255.255.255'). + +=item INADDR_LOOPBACK + +Note - does not return a number. + +Returns the 4-byte loopback address. Normally equivalent +to inet_aton('localhost'). + +=item INADDR_NONE + +Note - does not return a number. + +Returns the 4-byte 'invalid' ip address. Normally equivalent +to inet_aton('255.255.255.255'). + +=item IN6ADDR_ANY + +Returns the 16-byte wildcard IPv6 address. Normally equivalent +to inet_pton(AF_INET6, "::") + +=item IN6ADDR_LOOPBACK + +Returns the 16-byte loopback IPv6 address. Normally equivalent +to inet_pton(AF_INET6, "::1") + +=item sockaddr_family SOCKADDR + +Takes a sockaddr structure (as returned by pack_sockaddr_in(), +pack_sockaddr_un() or the perl builtin functions getsockname() and +getpeername()) and returns the address family tag. It will match the +constant AF_INET for a sockaddr_in and AF_UNIX for a sockaddr_un. It +can be used to figure out what unpacker to use for a sockaddr of +unknown type. + +=item sockaddr_in PORT, ADDRESS + +=item sockaddr_in SOCKADDR_IN + +In a list context, unpacks its SOCKADDR_IN argument and returns an array +consisting of (PORT, ADDRESS). In a scalar context, packs its (PORT, +ADDRESS) arguments as a SOCKADDR_IN and returns it. If this is confusing, +use pack_sockaddr_in() and unpack_sockaddr_in() explicitly. + +=item pack_sockaddr_in PORT, IP_ADDRESS + +Takes two arguments, a port number and an opaque string, IP_ADDRESS +(as returned by inet_aton(), or a v-string). Returns the sockaddr_in +structure with those arguments packed in with AF_INET filled in. For +Internet domain sockets, this structure is normally what you need for +the arguments in bind(), connect(), and send(), and is also returned +by getpeername(), getsockname() and recv(). + +=item unpack_sockaddr_in SOCKADDR_IN + +Takes a sockaddr_in structure (as returned by pack_sockaddr_in()) and +returns an array of two elements: the port and an opaque string +representing the IP address (you can use inet_ntoa() to convert the +address to the four-dotted numeric format). Will croak if the +structure does not have AF_INET in the right place. + +=item sockaddr_in6 PORT, IP6_ADDRESS, [ SCOPE_ID, [ FLOWINFO ] ] + +=item sockaddr_in6 SOCKADDR_IN6 + +In list context, unpacks its SOCKADDR_IN6 argument according to +unpack_sockaddr_in6(). In scalar context, packs its arguments according to +pack_sockaddr_in6(). + +=item pack_sockaddr_in6 PORT, IP6_ADDRESS, [ SCOPE_ID, [ FLOWINFO ] ] + +Takes two to four arguments, a port number, an opaque string (as returned by +inet_pton()), optionally a scope ID number, and optionally a flow label +number. Returns the sockaddr_in6 structure with those arguments packed in +with AF_INET6 filled in. IPv6 equivalent of pack_sockaddr_in(). + +=item unpack_sockaddr_in6 SOCKADDR_IN6 + +Takes a sockaddr_in6 structure (as returned by pack_sockaddr_in6()) and +returns an array of four elements: the port number, an opaque string +representing the IPv6 address, the scope ID, and the flow label. (You can +use inet_ntop() to convert the address to the usual string format). Will +croak if the structure does not have AF_INET6 in the right place. + +=item sockaddr_un PATHNAME + +=item sockaddr_un SOCKADDR_UN + +In a list context, unpacks its SOCKADDR_UN argument and returns an array +consisting of (PATHNAME). In a scalar context, packs its PATHNAME +arguments as a SOCKADDR_UN and returns it. If this is confusing, use +pack_sockaddr_un() and unpack_sockaddr_un() explicitly. +These are only supported if your system has EFE. + +=item pack_sockaddr_un PATH + +Takes one argument, a pathname. Returns the sockaddr_un structure with +that path packed in with AF_UNIX filled in. For unix domain sockets, this +structure is normally what you need for the arguments in bind(), +connect(), and send(), and is also returned by getpeername(), +getsockname() and recv(). + +=item unpack_sockaddr_un SOCKADDR_UN + +Takes a sockaddr_un structure (as returned by pack_sockaddr_un()) +and returns the pathname. Will croak if the structure does not +have AF_UNIX in the right place. + +=item inet_pton ADDRESS_FAMILY, HOSTNAME + +Takes an address family, either AF_INET or AF_INET6, and a string giving +the name of a host, and translates that to an opaque string +(if programming in C, struct in_addr or struct in6_addr depending on the +address family passed in). The host string may be a string hostname, such +as 'www.perl.org', or an IP address. If using an IP address, the type of +IP address must be consistent with the address family passed into the function. + +This function is not exported by default. + +=item inet_ntop ADDRESS_FAMILY, IP_ADDRESS + +Takes an address family, either AF_INET or AF_INET6, and a string +(an opaque string as returned by inet_aton() or inet_pton()) and +translates it to an IPv4 or IPv6 address string. + +This function is not exported by default. + +=item getaddrinfo HOST, SERVICE, [ HINTS ] + +Given at least one of a hostname and a service name, returns a list of address +structures to listen on or connect to. HOST and SERVICE should be plain +strings (or a numerical port number for SERVICE). If present, HINTS should be +a reference to a HASH, where the following keys are recognised: + +=over 8 + +=item flags => INT + +A bitfield containing C constants + +=item family => INT + +Restrict to only generating addresses in this address family + +=item socktype => INT + +Restrict to only generating addresses of this socket type + +=item protocol => INT + +Restrict to only generating addresses for this protocol + +=back + +The return value will be a list; the first value being an error indication, +followed by a list of address structures (if no error occured). + + my ( $err, @results ) = getaddrinfo( ... ); + +The error value will be a dualvar; comparable to the C error constants, +or printable as a human-readable error message string. Each value in the +results list will be a HASH reference containing the following fields: + +=over 8 + +=item family => INT + +The address family (e.g. AF_INET) + +=item socktype => INT + +The socket type (e.g. SOCK_STREAM) + +=item protocol => INT + +The protocol (e.g. IPPROTO_TCP) + +=item addr => STRING + +The address in a packed string (such as would be returned by pack_sockaddr_in) + +=item canonname => STRING + +The canonical name for the host if the C flag was provided, or +C otherwise. This field will only be present on the first returned +address. + +=back + +=item getnameinfo ADDR, FLAGS + +Given a packed socket address (such as from C, C, or +returned by C in a C field), returns the hostname and +symbolic service name it represents. FLAGS may be a bitmask of C +constants, or defaults to 0 if unspecified. + +The return value will be a list; the first value being an error condition, +followed by the hostname and service name. + + my ( $err, $host, $service ) = getnameinfo( ... ); + +The error value will be a dualvar; comparable to the C error constants, +or printable as a human-readable error message string. The host and service +names will be plain strings. + +=back + +=over 8 + +=item pack_ipv6_mreq IP6_MULTIADDR, INTERFACE + +Takes an IPv6 address and an interface number. Returns the ipv6_mreq structure +with those arguments packed in. Suitable for use with the +C and C sockopts. + +=item unpack_ipv6_mreq IPV6_MREQ + +Takes an ipv6_mreq structure and returns a list of two elements; the IPv6 +address and an interface number. + +=back + +=cut + +use Carp; +use warnings::register; + +require Exporter; +require XSLoader; +@ISA = qw(Exporter); + +# <@Nicholas> you can't change @EXPORT without breaking the implicit API +# Please put any new constants in @EXPORT_OK! +@EXPORT = qw( + inet_aton inet_ntoa + sockaddr_family + pack_sockaddr_in unpack_sockaddr_in + pack_sockaddr_un unpack_sockaddr_un + pack_sockaddr_in6 unpack_sockaddr_in6 + sockaddr_in sockaddr_in6 sockaddr_un + INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE + AF_802 + AF_AAL + AF_APPLETALK + AF_CCITT + AF_CHAOS + AF_CTF + AF_DATAKIT + AF_DECnet + AF_DLI + AF_ECMA + AF_GOSIP + AF_HYLINK + AF_IMPLINK + AF_INET + AF_INET6 + AF_ISO + AF_KEY + AF_LAST + AF_LAT + AF_LINK + AF_MAX + AF_NBS + AF_NIT + AF_NS + AF_OSI + AF_OSINET + AF_PUP + AF_ROUTE + AF_SNA + AF_UNIX + AF_UNSPEC + AF_USER + AF_WAN + AF_X25 + IOV_MAX + IP_OPTIONS + IP_HDRINCL + IP_TOS + IP_TTL + IP_RECVOPTS + IP_RECVRETOPTS + IP_RETOPTS + MSG_BCAST + MSG_BTAG + MSG_CTLFLAGS + MSG_CTLIGNORE + MSG_CTRUNC + MSG_DONTROUTE + MSG_DONTWAIT + MSG_EOF + MSG_EOR + MSG_ERRQUEUE + MSG_ETAG + MSG_FIN + MSG_MAXIOVLEN + MSG_MCAST + MSG_NOSIGNAL + MSG_OOB + MSG_PEEK + MSG_PROXY + MSG_RST + MSG_SYN + MSG_TRUNC + MSG_URG + MSG_WAITALL + MSG_WIRE + PF_802 + PF_AAL + PF_APPLETALK + PF_CCITT + PF_CHAOS + PF_CTF + PF_DATAKIT + PF_DECnet + PF_DLI + PF_ECMA + PF_GOSIP + PF_HYLINK + PF_IMPLINK + PF_INET + PF_INET6 + PF_ISO + PF_KEY + PF_LAST + PF_LAT + PF_LINK + PF_MAX + PF_NBS + PF_NIT + PF_NS + PF_OSI + PF_OSINET + PF_PUP + PF_ROUTE + PF_SNA + PF_UNIX + PF_UNSPEC + PF_USER + PF_WAN + PF_X25 + SCM_CONNECT + SCM_CREDENTIALS + SCM_CREDS + SCM_RIGHTS + SCM_TIMESTAMP + SHUT_RD + SHUT_RDWR + SHUT_WR + SOCK_DGRAM + SOCK_RAW + SOCK_RDM + SOCK_SEQPACKET + SOCK_STREAM + SOL_SOCKET + SOMAXCONN + SO_ACCEPTCONN + SO_ATTACH_FILTER + SO_BACKLOG + SO_BROADCAST + SO_CHAMELEON + SO_DEBUG + SO_DETACH_FILTER + SO_DGRAM_ERRIND + SO_DONTLINGER + SO_DONTROUTE + SO_ERROR + SO_FAMILY + SO_KEEPALIVE + SO_LINGER + SO_OOBINLINE + SO_PASSCRED + SO_PASSIFNAME + SO_PEERCRED + SO_PROTOCOL + SO_PROTOTYPE + SO_RCVBUF + SO_RCVLOWAT + SO_RCVTIMEO + SO_REUSEADDR + SO_REUSEPORT + SO_SECURITY_AUTHENTICATION + SO_SECURITY_ENCRYPTION_NETWORK + SO_SECURITY_ENCRYPTION_TRANSPORT + SO_SNDBUF + SO_SNDLOWAT + SO_SNDTIMEO + SO_STATE + SO_TYPE + SO_USELOOPBACK + SO_XOPEN + SO_XSE + UIO_MAXIOV +); + +@EXPORT_OK = qw(CR LF CRLF $CR $LF $CRLF + + inet_pton + inet_ntop + + getaddrinfo + getnameinfo + + pack_ipv6_mreq + unpack_ipv6_mreq + + IN6ADDR_ANY IN6ADDR_LOOPBACK + + AI_CANONNAME + AI_NUMERICHOST + AI_NUMERICSERV + AI_PASSIVE + + EAI_ADDRFAMILY + EAI_AGAIN + EAI_BADFLAGS + EAI_FAIL + EAI_FAMILY + EAI_NODATA + EAI_NONAME + EAI_SERVICE + EAI_SOCKTYPE + + IPPROTO_IP + IPPROTO_IPV6 + IPPROTO_RAW + IPPROTO_ICMP + IPPROTO_TCP + IPPROTO_UDP + + IPV6_ADD_MEMBERSHIP + IPV6_DROP_MEMBERSHIP + IPV6_MTU + IPV6_MTU_DISCOVER + IPV6_MULTICAST_HOPS + IPV6_MULTICAST_IF + IPV6_MULTICAST_LOOP + IPV6_UNICAST_HOPS + IPV6_V6ONLY + + NI_DGRAM + NI_NAMEREQD + NI_NUMERICHOST + NI_NUMERICSERV + + TCP_KEEPALIVE + TCP_MAXRT + TCP_MAXSEG + TCP_NODELAY + TCP_STDURG + TCP_CORK + TCP_KEEPIDLE + TCP_KEEPINTVL + TCP_KEEPCNT + TCP_SYNCNT + TCP_LINGER2 + TCP_DEFER_ACCEPT + TCP_WINDOW_CLAMP + TCP_INFO + TCP_QUICKACK + TCP_CONGESTION + TCP_MD5SIG); + +%EXPORT_TAGS = ( + crlf => [qw(CR LF CRLF $CR $LF $CRLF)], + all => [@EXPORT, @EXPORT_OK], +); + +BEGIN { + sub CR () {"\015"} + sub LF () {"\012"} + sub CRLF () {"\015\012"} +} + +*CR = \CR(); +*LF = \LF(); +*CRLF = \CRLF(); + +sub sockaddr_in { + if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die + my($af, $port, @quad) = @_; + warnings::warn "6-ARG sockaddr_in call is deprecated" + if warnings::enabled(); + pack_sockaddr_in($port, inet_aton(join('.', @quad))); + } elsif (wantarray) { + croak "usage: (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1; + unpack_sockaddr_in(@_); + } else { + croak "usage: sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2; + pack_sockaddr_in(@_); + } +} + +sub sockaddr_in6 { + if (wantarray) { + croak "usage: (port,in6addr,scope_id,flowinfo) = sockaddr_in6(sin6_sv)" unless @_ == 1; + unpack_sockaddr_in6(@_); + } + else { + croak "usage: sin6_sv = sockaddr_in6(port,in6addr,[scope_id,[flowinfo]])" unless @_ >= 2 and @_ <= 4; + pack_sockaddr_in6(@_); + } +} + +sub sockaddr_un { + if (wantarray) { + croak "usage: (filename) = sockaddr_un(sun_sv)" unless @_ == 1; + unpack_sockaddr_un(@_); + } else { + croak "usage: sun_sv = sockaddr_un(filename)" unless @_ == 1; + pack_sockaddr_un(@_); + } +} + +XSLoader::load(__PACKAGE__, $VERSION); + +my %errstr; + +if( defined &getaddrinfo ) { + # These are not part of the API, nothing uses them, and deleting them + # reduces the size of %Socket:: by about 12K + delete $Socket::{fake_getaddrinfo}; + delete $Socket::{fake_getnameinfo}; +} else { + require Scalar::Util; + + *getaddrinfo = \&fake_getaddrinfo; + *getnameinfo = \&fake_getnameinfo; + + # These numbers borrowed from GNU libc's implementation, but since + # they're only used by our emulation, it doesn't matter if the real + # platform's values differ + my %constants = ( + AI_PASSIVE => 1, + AI_CANONNAME => 2, + AI_NUMERICHOST => 4, + # RFC 2553 doesn't define this but Linux does - lets be nice and + # provide it since we can + AI_NUMERICSERV => 1024, + + EAI_BADFLAGS => -1, + EAI_NONAME => -2, + EAI_NODATA => -5, + EAI_FAMILY => -6, + EAI_SERVICE => -8, + + NI_NUMERICHOST => 1, + NI_NUMERICSERV => 2, + NI_NAMEREQD => 8, + NI_DGRAM => 16, + ); + + foreach my $name ( keys %constants ) { + my $value = $constants{$name}; + + no strict 'refs'; + defined &$name or *$name = sub () { $value }; + } + + %errstr = ( + # These strings from RFC 2553 + EAI_BADFLAGS() => "invalid value for ai_flags", + EAI_NONAME() => "nodename nor servname provided, or not known", + EAI_NODATA() => "no address associated with nodename", + EAI_FAMILY() => "ai_family not supported", + EAI_SERVICE() => "servname not supported for ai_socktype", + ); +} + +# The following functions are used if the system does not have a +# getaddrinfo(3) function in libc; and are used to emulate it for the AF_INET +# family + +# Borrowed from Regexp::Common::net +my $REGEXP_IPv4_DECIMAL = qr/25[0-5]|2[0-4][0-9]|1?[0-9][0-9]{1,2}/; +my $REGEXP_IPv4_DOTTEDQUAD = qr/$REGEXP_IPv4_DECIMAL\.$REGEXP_IPv4_DECIMAL\.$REGEXP_IPv4_DECIMAL\.$REGEXP_IPv4_DECIMAL/; + +sub fake_makeerr +{ + my ( $errno ) = @_; + my $errstr = $errno == 0 ? "" : ( $errstr{$errno} || $errno ); + return Scalar::Util::dualvar( $errno, $errstr ); +} + +sub fake_getaddrinfo +{ + my ( $node, $service, $hints ) = @_; + + $node = "" unless defined $node; + + $service = "" unless defined $service; + + my ( $family, $socktype, $protocol, $flags ) = @$hints{qw( family socktype protocol flags )}; + + $family ||= Socket::AF_INET(); # 0 == AF_UNSPEC, which we want too + $family == Socket::AF_INET() or return fake_makeerr( EAI_FAMILY() ); + + $socktype ||= 0; + + $protocol ||= 0; + + $flags ||= 0; + + my $flag_passive = $flags & AI_PASSIVE(); $flags &= ~AI_PASSIVE(); + my $flag_canonname = $flags & AI_CANONNAME(); $flags &= ~AI_CANONNAME(); + my $flag_numerichost = $flags & AI_NUMERICHOST(); $flags &= ~AI_NUMERICHOST(); + my $flag_numericserv = $flags & AI_NUMERICSERV(); $flags &= ~AI_NUMERICSERV(); + + $flags == 0 or return fake_makeerr( EAI_BADFLAGS() ); + + $node eq "" and $service eq "" and return fake_makeerr( EAI_NONAME() ); + + my $canonname; + my @addrs; + if( $node ne "" ) { + return fake_makeerr( EAI_NONAME() ) if( $flag_numerichost and $node !~ m/^$REGEXP_IPv4_DOTTEDQUAD$/ ); + ( $canonname, undef, undef, undef, @addrs ) = gethostbyname( $node ); + defined $canonname or return fake_makeerr( EAI_NONAME() ); + + undef $canonname unless $flag_canonname; + } + else { + $addrs[0] = $flag_passive ? Socket::inet_aton( "0.0.0.0" ) + : Socket::inet_aton( "127.0.0.1" ); + } + + my @ports; # Actually ARRAYrefs of [ socktype, protocol, port ] + my $protname = ""; + if( $protocol ) { + $protname = getprotobynumber( $protocol ); + } + + if( $service ne "" and $service !~ m/^\d+$/ ) { + return fake_makeerr( EAI_NONAME() ) if( $flag_numericserv ); + getservbyname( $service, $protname ) or return fake_makeerr( EAI_SERVICE() ); + } + + foreach my $this_socktype ( Socket::SOCK_STREAM(), Socket::SOCK_DGRAM(), Socket::SOCK_RAW() ) { + next if $socktype and $this_socktype != $socktype; + + my $this_protname = "raw"; + $this_socktype == Socket::SOCK_STREAM() and $this_protname = "tcp"; + $this_socktype == Socket::SOCK_DGRAM() and $this_protname = "udp"; + + next if $protname and $this_protname ne $protname; + + my $port; + if( $service ne "" ) { + if( $service =~ m/^\d+$/ ) { + $port = "$service"; + } + else { + ( undef, undef, $port, $this_protname ) = getservbyname( $service, $this_protname ); + next unless defined $port; + } + } + else { + $port = 0; + } + + push @ports, [ $this_socktype, scalar getprotobyname( $this_protname ) || 0, $port ]; + } + + my @ret; + foreach my $addr ( @addrs ) { + foreach my $portspec ( @ports ) { + my ( $socktype, $protocol, $port ) = @$portspec; + push @ret, { + family => $family, + socktype => $socktype, + protocol => $protocol, + addr => Socket::pack_sockaddr_in( $port, $addr ), + canonname => undef, + }; + } + } + + # Only supply canonname for the first result + if( defined $canonname ) { + $ret[0]->{canonname} = $canonname; + } + + return ( fake_makeerr( 0 ), @ret ); +} + +sub fake_getnameinfo +{ + my ( $addr, $flags ) = @_; + + my ( $port, $inetaddr ); + eval { ( $port, $inetaddr ) = Socket::unpack_sockaddr_in( $addr ) } + or return fake_makeerr( EAI_FAMILY() ); + + my $family = Socket::AF_INET(); + + $flags ||= 0; + + my $flag_numerichost = $flags & NI_NUMERICHOST(); $flags &= ~NI_NUMERICHOST(); + my $flag_numericserv = $flags & NI_NUMERICSERV(); $flags &= ~NI_NUMERICSERV(); + my $flag_namereqd = $flags & NI_NAMEREQD(); $flags &= ~NI_NAMEREQD(); + my $flag_dgram = $flags & NI_DGRAM() ; $flags &= ~NI_DGRAM(); + + $flags == 0 or return fake_makeerr( EAI_BADFLAGS() ); + + my $node; + if( $flag_numerichost ) { + $node = Socket::inet_ntoa( $inetaddr ); + } + else { + $node = gethostbyaddr( $inetaddr, $family ); + if( !defined $node ) { + return fake_makeerr( EAI_NONAME() ) if $flag_namereqd; + $node = Socket::inet_ntoa( $inetaddr ); + } + } + + my $service; + if( $flag_numericserv ) { + $service = "$port"; + } + else { + my $protname = $flag_dgram ? "udp" : ""; + $service = getservbyport( $port, $protname ); + if( !defined $service ) { + $service = "$port"; + } + } + + return ( fake_makeerr( 0 ), $node, $service ); +} + +1; diff --git a/cpan/Socket/Socket.xs b/cpan/Socket/Socket.xs new file mode 100644 index 0000000000..f571ac7e71 --- /dev/null +++ b/cpan/Socket/Socket.xs @@ -0,0 +1,851 @@ +#define PERL_NO_GET_CONTEXT +#include "EXTERN.h" +#include "perl.h" +#include "XSUB.h" + +#include + +#ifdef I_SYS_TYPES +# include +#endif +#if !defined(ultrix) /* Avoid double definition. */ +# include +#endif +#if defined(USE_SOCKS) && defined(I_SOCKS) +# include +#endif +#ifdef MPE +# define PF_INET AF_INET +# define PF_UNIX AF_UNIX +# define SOCK_RAW 3 +#endif +#ifdef I_SYS_UN +# include +#endif +/* XXX Configure test for +#endif +#if defined(__sgi) && !defined(AF_LINK) && defined(PF_LINK) && PF_LINK == AF_LNK +# undef PF_LINK +#endif +#if defined(I_NETINET_IN) || defined(__ultrix__) +# include +#endif +#ifdef I_NETDB +# if !defined(ultrix) /* Avoid double definition. */ +# include +# endif +#endif +#ifdef I_ARPA_INET +# include +#endif +#ifdef I_NETINET_TCP +# include +#endif + +#ifdef NETWARE +NETDB_DEFINE_CONTEXT +NETINET_DEFINE_CONTEXT +#endif + +#ifdef I_SYSUIO +# include +#endif + +#ifndef AF_NBS +# undef PF_NBS +#endif + +#ifndef AF_X25 +# undef PF_X25 +#endif + +#ifndef INADDR_NONE +# define INADDR_NONE 0xffffffff +#endif /* INADDR_NONE */ +#ifndef INADDR_BROADCAST +# define INADDR_BROADCAST 0xffffffff +#endif /* INADDR_BROADCAST */ +#ifndef INADDR_LOOPBACK +# define INADDR_LOOPBACK 0x7F000001 +#endif /* INADDR_LOOPBACK */ + +#ifndef croak_sv +# define croak_sv(sv) croak(SvPV_nolen(sv)) +#endif + +#ifndef HAS_INET_ATON + +/* + * Check whether "cp" is a valid ascii representation + * of an Internet address and convert to a binary address. + * Returns 1 if the address is valid, 0 if not. + * This replaces inet_addr, the return value from which + * cannot distinguish between failure and a local broadcast address. + */ +static int +my_inet_aton(register const char *cp, struct in_addr *addr) +{ + dTHX; + register U32 val; + register int base; + register char c; + int nparts; + const char *s; + unsigned int parts[4]; + register unsigned int *pp = parts; + + if (!cp || !*cp) + return 0; + for (;;) { + /* + * Collect number up to ".". + * Values are specified as for C: + * 0x=hex, 0=octal, other=decimal. + */ + val = 0; base = 10; + if (*cp == '0') { + if (*++cp == 'x' || *cp == 'X') + base = 16, cp++; + else + base = 8; + } + while ((c = *cp) != '\0') { + if (isDIGIT(c)) { + val = (val * base) + (c - '0'); + cp++; + continue; + } + if (base == 16 && (s=strchr(PL_hexdigit,c))) { + val = (val << 4) + + ((s - PL_hexdigit) & 15); + cp++; + continue; + } + break; + } + if (*cp == '.') { + /* + * Internet format: + * a.b.c.d + * a.b.c (with c treated as 16-bits) + * a.b (with b treated as 24 bits) + */ + if (pp >= parts + 3 || val > 0xff) + return 0; + *pp++ = val, cp++; + } else + break; + } + /* + * Check for trailing characters. + */ + if (*cp && !isSPACE(*cp)) + return 0; + /* + * Concoct the address according to + * the number of parts specified. + */ + nparts = pp - parts + 1; /* force to an int for switch() */ + switch (nparts) { + + case 1: /* a -- 32 bits */ + break; + + case 2: /* a.b -- 8.24 bits */ + if (val > 0xffffff) + return 0; + val |= parts[0] << 24; + break; + + case 3: /* a.b.c -- 8.8.16 bits */ + if (val > 0xffff) + return 0; + val |= (parts[0] << 24) | (parts[1] << 16); + break; + + case 4: /* a.b.c.d -- 8.8.8.8 bits */ + if (val > 0xff) + return 0; + val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); + break; + } + addr->s_addr = htonl(val); + return 1; +} + +#undef inet_aton +#define inet_aton my_inet_aton + +#endif /* ! HAS_INET_ATON */ + + +static int +not_here(const char *s) +{ + croak("Socket::%s not implemented on this architecture", s); + return -1; +} + +#define PERL_IN_ADDR_S_ADDR_SIZE 4 + +/* +* Bad assumptions possible here. +* +* Bad Assumption 1: struct in_addr has no other fields +* than the s_addr (which is the field we care about +* in here, really). However, we can be fed either 4-byte +* addresses (from pack("N", ...), or va.b.c.d, or ...), +* or full struct in_addrs (from e.g. pack_sockaddr_in()), +* which may or may not be 4 bytes in size. +* +* Bad Assumption 2: the s_addr field is a simple type +* (such as an int, u_int32_t). It can be a bit field, +* in which case using & (address-of) on it or taking sizeof() +* wouldn't go over too well. (Those are not attempted +* now but in case someone thinks to change the below code +* to use addr.s_addr instead of addr, you have been warned.) +* +* Bad Assumption 3: the s_addr is the first field in +* an in_addr, or that its bytes are the first bytes in +* an in_addr. +* +* These bad assumptions are wrong in UNICOS which has +* struct in_addr { struct { u_long st_addr:32; } s_da }; +* #define s_addr s_da.st_addr +* and u_long is 64 bits. +* +* --jhi */ + +#include "const-c.inc" + +#ifdef HAS_GETADDRINFO +static SV *err_to_SV(pTHX_ int err) +{ + SV *ret = sv_newmortal(); + SvUPGRADE(ret, SVt_PVNV); + + if(err) { + const char *error = gai_strerror(err); + sv_setpv(ret, error); + } + else { + sv_setpv(ret, ""); + } + + SvIV_set(ret, err); SvIOK_on(ret); + + return ret; +} + +static void xs_getaddrinfo(pTHX_ CV *cv) +{ + dVAR; + dXSARGS; + + SV *host; + SV *service; + SV *hints; + + char *hostname = NULL; + char *servicename = NULL; + STRLEN len; + struct addrinfo hints_s; + struct addrinfo *res; + struct addrinfo *res_iter; + int err; + int n_res; + + if(items > 3) + croak_xs_usage(cv, "host, service, hints"); + + SP -= items; + + if(items < 1) + host = &PL_sv_undef; + else + host = ST(0); + + if(items < 2) + service = &PL_sv_undef; + else + service = ST(1); + + if(items < 3) + hints = NULL; + else + hints = ST(2); + + SvGETMAGIC(host); + if(SvOK(host)) { + hostname = SvPV_nomg(host, len); + if (!len) + hostname = NULL; + } + + SvGETMAGIC(service); + if(SvOK(service)) { + servicename = SvPV_nomg(service, len); + if (!len) + servicename = NULL; + } + + Zero(&hints_s, sizeof hints_s, char); + hints_s.ai_family = PF_UNSPEC; + + if(hints && SvOK(hints)) { + HV *hintshash; + SV **valp; + + if(!SvROK(hints) || SvTYPE(SvRV(hints)) != SVt_PVHV) + croak("hints is not a HASH reference"); + + hintshash = (HV*)SvRV(hints); + + if((valp = hv_fetch(hintshash, "flags", 5, 0)) != NULL) + hints_s.ai_flags = SvIV(*valp); + if((valp = hv_fetch(hintshash, "family", 6, 0)) != NULL) + hints_s.ai_family = SvIV(*valp); + if((valp = hv_fetch(hintshash, "socktype", 8, 0)) != NULL) + hints_s.ai_socktype = SvIV(*valp); + if((valp = hv_fetch(hintshash, "protocol", 8, 0)) != NULL) + hints_s.ai_protocol = SvIV(*valp); + } + + err = getaddrinfo(hostname, servicename, &hints_s, &res); + + XPUSHs(err_to_SV(aTHX_ err)); + + if(err) + XSRETURN(1); + + n_res = 0; + for(res_iter = res; res_iter; res_iter = res_iter->ai_next) { + HV *res_hv = newHV(); + + (void)hv_stores(res_hv, "family", newSViv(res_iter->ai_family)); + (void)hv_stores(res_hv, "socktype", newSViv(res_iter->ai_socktype)); + (void)hv_stores(res_hv, "protocol", newSViv(res_iter->ai_protocol)); + + (void)hv_stores(res_hv, "addr", newSVpvn((char*)res_iter->ai_addr, res_iter->ai_addrlen)); + + if(res_iter->ai_canonname) + (void)hv_stores(res_hv, "canonname", newSVpv(res_iter->ai_canonname, 0)); + else + (void)hv_stores(res_hv, "canonname", newSV(0)); + + XPUSHs(sv_2mortal(newRV_noinc((SV*)res_hv))); + n_res++; + } + + freeaddrinfo(res); + + XSRETURN(1 + n_res); +} +#endif + +#ifdef HAS_GETNAMEINFO +static void xs_getnameinfo(pTHX_ CV *cv) +{ + dVAR; + dXSARGS; + + SV *addr; + int flags; + + char host[1024]; + char serv[256]; + char *sa; /* we'll cast to struct sockaddr * when necessary */ + STRLEN addr_len; + int err; + + if(items < 1 || items > 2) + croak_xs_usage(cv, "addr, flags=0"); + + SP -= items; + + addr = ST(0); + + if(items < 2) + flags = 0; + else + flags = SvIV(ST(1)); + + if(!SvPOK(addr)) + croak("addr is not a string"); + + addr_len = SvCUR(addr); + + /* We need to ensure the sockaddr is aligned, because a random SvPV might + * not be due to SvOOK */ + Newx(sa, addr_len, char); + Copy(SvPV_nolen(addr), sa, addr_len, char); +#ifdef HAS_SOCKADDR_SA_LEN + ((struct sockaddr *)sa)->sa_len = addr_len; +#endif + + err = getnameinfo((struct sockaddr *)sa, addr_len, + host, sizeof(host), + serv, sizeof(serv), + flags); + + Safefree(sa); + + XPUSHs(err_to_SV(aTHX_ err)); + + if(err) + XSRETURN(1); + + XPUSHs(sv_2mortal(newSVpv(host, 0))); + XPUSHs(sv_2mortal(newSVpv(serv, 0))); + + XSRETURN(3); +} +#endif + +MODULE = Socket PACKAGE = Socket + +INCLUDE: const-xs.inc + +BOOT: +#ifdef HAS_GETADDRINFO + newXS("Socket::getaddrinfo", xs_getaddrinfo, __FILE__); +#endif +#ifdef HAS_GETNAMEINFO + newXS("Socket::getnameinfo", xs_getnameinfo, __FILE__); +#endif + +void +inet_aton(host) + char * host + CODE: + { + struct in_addr ip_address; + struct hostent * phe; + + if ((*host != '\0') && inet_aton(host, &ip_address)) { + ST(0) = newSVpvn_flags((char *)&ip_address, sizeof ip_address, SVs_TEMP); + XSRETURN(1); + } + + phe = gethostbyname(host); + if (phe && phe->h_addrtype == AF_INET && phe->h_length == 4) { + ST(0) = newSVpvn_flags((char *)phe->h_addr, phe->h_length, SVs_TEMP); + XSRETURN(1); + } + + XSRETURN_UNDEF; + } + +void +inet_ntoa(ip_address_sv) + SV * ip_address_sv + CODE: + { + STRLEN addrlen; + struct in_addr addr; + char * ip_address; + if (DO_UTF8(ip_address_sv) && !sv_utf8_downgrade(ip_address_sv, 1)) + croak("Wide character in %s", "Socket::inet_ntoa"); + ip_address = SvPVbyte(ip_address_sv, addrlen); + if (addrlen == sizeof(addr) || addrlen == 4) + addr.s_addr = + (ip_address[0] & 0xFF) << 24 | + (ip_address[1] & 0xFF) << 16 | + (ip_address[2] & 0xFF) << 8 | + (ip_address[3] & 0xFF); + else + croak("Bad arg length for %s, length is %d, should be %d", + "Socket::inet_ntoa", + addrlen, sizeof(addr)); + /* We could use inet_ntoa() but that is broken + * in HP-UX + GCC + 64bitint (returns "0.0.0.0"), + * so let's use this sprintf() workaround everywhere. + * This is also more threadsafe than using inet_ntoa(). */ + ST(0) = sv_2mortal(Perl_newSVpvf(aTHX_ "%d.%d.%d.%d", /* IPv6? */ + ((addr.s_addr >> 24) & 0xFF), + ((addr.s_addr >> 16) & 0xFF), + ((addr.s_addr >> 8) & 0xFF), + ( addr.s_addr & 0xFF))); + } + +void +sockaddr_family(sockaddr) + SV * sockaddr + PREINIT: + STRLEN sockaddr_len; + char *sockaddr_pv = SvPVbyte(sockaddr, sockaddr_len); + CODE: + if (sockaddr_len < offsetof(struct sockaddr, sa_data)) { + croak("Bad arg length for %s, length is %d, should be at least %d", + "Socket::sockaddr_family", sockaddr_len, + offsetof(struct sockaddr, sa_data)); + } + ST(0) = sv_2mortal(newSViv(((struct sockaddr*)sockaddr_pv)->sa_family)); + +void +pack_sockaddr_un(pathname) + SV * pathname + CODE: + { +#ifdef I_SYS_UN + struct sockaddr_un sun_ad; /* fear using sun */ + STRLEN len; + char * pathname_pv; + int addr_len; + + Zero( &sun_ad, sizeof sun_ad, char ); + sun_ad.sun_family = AF_UNIX; + pathname_pv = SvPV(pathname,len); + if (len > sizeof(sun_ad.sun_path)) + len = sizeof(sun_ad.sun_path); +# ifdef OS2 /* Name should start with \socket\ and contain backslashes! */ + { + int off; + char *s, *e; + + if (pathname_pv[0] != '/' && pathname_pv[0] != '\\') + croak("Relative UNIX domain socket name '%s' unsupported", + pathname_pv); + else if (len < 8 + || pathname_pv[7] != '/' && pathname_pv[7] != '\\' + || !strnicmp(pathname_pv + 1, "socket", 6)) + off = 7; + else + off = 0; /* Preserve names starting with \socket\ */ + Copy( "\\socket", sun_ad.sun_path, off, char); + Copy( pathname_pv, sun_ad.sun_path + off, len, char ); + + s = sun_ad.sun_path + off - 1; + e = s + len + 1; + while (++s < e) + if (*s = '/') + *s = '\\'; + } +# else /* !( defined OS2 ) */ + Copy( pathname_pv, sun_ad.sun_path, len, char ); +# endif + if (0) not_here("dummy"); + if (len > 1 && sun_ad.sun_path[0] == '\0') { + /* Linux-style abstract-namespace socket. + * The name is not a file name, but an array of arbitrary + * character, starting with \0 and possibly including \0s, + * therefore the length of the structure must denote the + * end of that character array */ + addr_len = (char *)&(sun_ad.sun_path) - (char *)&sun_ad + len; + } else { + addr_len = sizeof sun_ad; + } +# ifdef HAS_SOCKADDR_SA_LEN + sun_ad.sun_len = addr_len; +# endif + ST(0) = newSVpvn_flags((char *)&sun_ad, addr_len, SVs_TEMP); +#else + ST(0) = (SV *) not_here("pack_sockaddr_un"); +#endif + + } + +void +unpack_sockaddr_un(sun_sv) + SV * sun_sv + CODE: + { +#ifdef I_SYS_UN + struct sockaddr_un addr; + STRLEN sockaddrlen; + char * sun_ad = SvPVbyte(sun_sv,sockaddrlen); + int addr_len; +# ifndef __linux__ + /* On Linux sockaddrlen on sockets returned by accept, recvfrom, + getpeername and getsockname is not equal to sizeof(addr). */ + if (sockaddrlen != sizeof(addr)) { + croak("Bad arg length for %s, length is %d, should be %d", + "Socket::unpack_sockaddr_un", + sockaddrlen, sizeof(addr)); + } +# endif + + Copy( sun_ad, &addr, sizeof addr, char ); + + if ( addr.sun_family != AF_UNIX ) { + croak("Bad address family for %s, got %d, should be %d", + "Socket::unpack_sockaddr_un", + addr.sun_family, + AF_UNIX); + } + + if (addr.sun_path[0] == '\0') { + /* Linux-style abstract socket address begins with a nul + * and can contain nuls. */ + addr_len = (char *)&addr - (char *)&(addr.sun_path) + sockaddrlen; + } else { + for (addr_len = 0; addr.sun_path[addr_len] + && addr_len < (int)sizeof(addr.sun_path); addr_len++); + } + + ST(0) = newSVpvn_flags(addr.sun_path, addr_len, SVs_TEMP); +#else + ST(0) = (SV *) not_here("unpack_sockaddr_un"); +#endif + } + +void +pack_sockaddr_in(port, ip_address_sv) + unsigned short port + SV * ip_address_sv + CODE: + { + struct sockaddr_in sin; + struct in_addr addr; + STRLEN addrlen; + char * ip_address; + if (DO_UTF8(ip_address_sv) && !sv_utf8_downgrade(ip_address_sv, 1)) + croak("Wide character in %s", "Socket::pack_sockaddr_in"); + ip_address = SvPVbyte(ip_address_sv, addrlen); + if (addrlen == sizeof(addr) || addrlen == 4) + addr.s_addr = + (ip_address[0] & 0xFF) << 24 | + (ip_address[1] & 0xFF) << 16 | + (ip_address[2] & 0xFF) << 8 | + (ip_address[3] & 0xFF); + else + croak("Bad arg length for %s, length is %d, should be %d", + "Socket::pack_sockaddr_in", + addrlen, sizeof(addr)); + Zero( &sin, sizeof sin, char ); + sin.sin_family = AF_INET; + sin.sin_port = htons(port); + sin.sin_addr.s_addr = htonl(addr.s_addr); +# ifdef HAS_SOCKADDR_SA_LEN + sin.sin_len = sizeof (sin); +# endif + ST(0) = newSVpvn_flags((char *)&sin, sizeof (sin), SVs_TEMP); + } + +void +unpack_sockaddr_in(sin_sv) + SV * sin_sv + PPCODE: + { + STRLEN sockaddrlen; + struct sockaddr_in addr; + unsigned short port; + struct in_addr ip_address; + char * sin = SvPVbyte(sin_sv,sockaddrlen); + if (sockaddrlen != sizeof(addr)) { + croak("Bad arg length for %s, length is %d, should be %d", + "Socket::unpack_sockaddr_in", + sockaddrlen, sizeof(addr)); + } + Copy( sin, &addr,sizeof addr, char ); + if ( addr.sin_family != AF_INET ) { + croak("Bad address family for %s, got %d, should be %d", + "Socket::unpack_sockaddr_in", + addr.sin_family, + AF_INET); + } + port = ntohs(addr.sin_port); + ip_address = addr.sin_addr; + + EXTEND(SP, 2); + PUSHs(sv_2mortal(newSViv((IV) port))); + PUSHs(newSVpvn_flags((char *)&ip_address, sizeof(ip_address), SVs_TEMP)); + } + +void +pack_sockaddr_in6(port, sin6_addr, scope_id=0, flowinfo=0) + unsigned short port + SV * sin6_addr + unsigned long scope_id + unsigned long flowinfo + CODE: + { +#ifdef AF_INET6 + struct sockaddr_in6 sin6; + char * addrbytes; + STRLEN addrlen; + if (DO_UTF8(sin6_addr) && !sv_utf8_downgrade(sin6_addr, 1)) + croak("Wide character in %s", "Socket::pack_sockaddr_in6"); + addrbytes = SvPVbyte(sin6_addr, addrlen); + if(addrlen != sizeof(sin6.sin6_addr)) + croak("Bad arg length %s, length is %d, should be %d", + "Socket::pack_sockaddr_in6", addrlen, sizeof(sin6.sin6_addr)); + Zero(&sin6, sizeof(sin6), char); + sin6.sin6_family = AF_INET6; + sin6.sin6_port = htons(port); + sin6.sin6_flowinfo = htonl(flowinfo); + Copy(addrbytes, &sin6.sin6_addr, sizeof(sin6.sin6_addr), char); +# ifdef HAS_SIN6_SCOPE_ID + sin6.sin6_scope_id = scope_id; +# else + if(scope_id != 0) + warn("%s cannot represent non-zero scope_id %d", + "Socket::pack_sockaddr_in6", scope_id); +# endif +# ifdef HAS_SOCKADDR_SA_LEN + sin6.sin6_len = sizeof(sin6); +# endif + ST(0) = newSVpvn_flags((char *)&sin6, sizeof(sin6), SVs_TEMP); +#else + ST(0) = (SV*)not_here("pack_sockaddr_in6"); +#endif + } + +void +unpack_sockaddr_in6(sin6_sv) + SV * sin6_sv + PPCODE: + { +#ifdef AF_INET6 + STRLEN addrlen; + struct sockaddr_in6 sin6; + char * addrbytes = SvPVbyte(sin6_sv, addrlen); + if (addrlen != sizeof(sin6)) + croak("Bad arg length for %s, length is %d, should be %d", + "Socket::unpack_sockaddr_in6", + addrlen, sizeof(sin6)); + Copy(addrbytes, &sin6, sizeof(sin6), char); + if(sin6.sin6_family != AF_INET6) + croak("Bad address family for %s, got %d, should be %d", + "Socket::unpack_sockaddr_in6", + sin6.sin6_family, AF_INET6); + EXTEND(SP, 4); + mPUSHi(ntohs(sin6.sin6_port)); + mPUSHp((char *)&sin6.sin6_addr, sizeof(sin6.sin6_addr)); +# ifdef HAS_SIN6_SCOPE_ID + mPUSHi(sin6.sin6_scope_id); +# else + mPUSHi(0); +# endif + mPUSHi(ntohl(sin6.sin6_flowinfo)); +#else + ST(0) = (SV*)not_here("pack_sockaddr_in6"); +#endif + } + +void +inet_ntop(af, ip_address_sv) + int af + SV * ip_address_sv + CODE: +#ifdef HAS_INETNTOP + STRLEN addrlen, struct_size; +#ifdef AF_INET6 + struct in6_addr addr; + char str[INET6_ADDRSTRLEN]; +#else + struct in_addr addr; + char str[INET_ADDRSTRLEN]; +#endif + char *ip_address = SvPV(ip_address_sv, addrlen); + + struct_size = sizeof(addr); + + if(af != AF_INET +#ifdef AF_INET6 + && af != AF_INET6 +#endif + ) { + croak("Bad address family for %s, got %d, should be" +#ifdef AF_INET6 + " either AF_INET or AF_INET6", +#else + " AF_INET", +#endif + "Socket::inet_ntop", + af); + } + + Copy( ip_address, &addr, sizeof addr, char ); + inet_ntop(af, &addr, str, sizeof str); + + ST(0) = newSVpvn_flags(str, strlen(str), SVs_TEMP); +#else + ST(0) = (SV *)not_here("inet_ntop"); +#endif + +void +inet_pton(af, host) + int af + const char * host + CODE: +#ifdef HAS_INETPTON + int ok; +#ifdef AF_INET6 + struct in6_addr ip_address; +#else + struct in_addr ip_address; +#endif + + if(af != AF_INET +#ifdef AF_INET6 + && af != AF_INET6 +#endif + ) { + croak("Bad address family for %s, got %d, should be" +#ifdef AF_INET6 + " either AF_INET or AF_INET6", +#else + " AF_INET", +#endif + "Socket::inet_pton", + af); + } + ok = (*host != '\0') && inet_pton(af, host, &ip_address); + + ST(0) = sv_newmortal(); + if (ok) { + sv_setpvn( ST(0), (char *)&ip_address, sizeof(ip_address) ); + } +#else + ST(0) = (SV *)not_here("inet_pton"); +#endif + +void +pack_ipv6_mreq(addr, interface) + SV * addr + unsigned int interface + CODE: + { +#ifdef AF_INET6 + struct ipv6_mreq mreq; + char * addrbytes; + STRLEN addrlen; + if (DO_UTF8(addr) && !sv_utf8_downgrade(addr, 1)) + croak("Wide character in %s", "Socket::pack_ipv6_mreq"); + addrbytes = SvPVbyte(addr, addrlen); + if(addrlen != sizeof(mreq.ipv6mr_multiaddr)) + croak("Bad arg length %s, length is %d, should be %d", + "Socket::pack_ipv6_mreq", addrlen, sizeof(mreq.ipv6mr_multiaddr)); + Zero(&mreq, sizeof(mreq), char); + Copy(addrbytes, &mreq.ipv6mr_multiaddr, sizeof(mreq.ipv6mr_multiaddr), char); + mreq.ipv6mr_interface = interface; + ST(0) = newSVpvn_flags((char *)&mreq, sizeof(mreq), SVs_TEMP); +#else + ST(0) = (SV*)not_here("pack_ipv6_mreq"); +#endif + } + +void +unpack_ipv6_mreq(mreq_sv) + SV * mreq_sv + PPCODE: + { +#ifdef AF_INET6 + struct ipv6_mreq mreq; + STRLEN mreqlen; + char * mreqbytes = SvPVbyte(mreq_sv, mreqlen); + if (mreqlen != sizeof(mreq)) + croak("Bad arg length for %s, length is %d, should be %d", + "Socket::unpack_ipv6_mreq", + mreqlen, sizeof(mreq)); + Copy(mreqbytes, &mreq, sizeof(mreq), char); + EXTEND(SP, 2); + mPUSHp((char *)&mreq.ipv6mr_multiaddr, sizeof(mreq.ipv6mr_multiaddr)); + mPUSHi(mreq.ipv6mr_interface); +#else + not_here("unpack_ipv6_mreq"); +#endif + } diff --git a/cpan/Socket/t/Socket.t b/cpan/Socket/t/Socket.t new file mode 100644 index 0000000000..f59c265c4c --- /dev/null +++ b/cpan/Socket/t/Socket.t @@ -0,0 +1,184 @@ +#!./perl + +BEGIN { + require Config; import Config; + if ($Config{'extensions'} !~ /\bSocket\b/ && + !(($^O eq 'VMS') && $Config{d_socket})) { + print "1..0\n"; + exit 0; + } + $has_alarm = $Config{d_alarm}; +} + +use Socket qw(:all); +use Test::More tests => 26; + +$has_echo = $^O ne 'MSWin32'; +$alarmed = 0; +sub arm { $alarmed = 0; alarm(shift) if $has_alarm } +sub alarmed { $alarmed = 1 } +$SIG{ALRM} = 'alarmed' if $has_alarm; + +SKIP: { + unless(socket(T, PF_INET, SOCK_STREAM, IPPROTO_TCP)) { + skip "No PF_INET", 3; + } + + pass "socket(PF_INET)"; + + arm(5); + my $host = $^O eq 'MacOS' || ($^O eq 'irix' && $Config{osvers} == 5) ? + '127.0.0.1' : 'localhost'; + my $localhost = inet_aton($host); + + SKIP: { + unless($has_echo && defined $localhost && connect(T,pack_sockaddr_in(7,$localhost))) { + skip "Unable to connect to localhost:7", 2; + } + + arm(0); + + pass "PF_INET echo localhost connected"; + + diag "Connected to " . + inet_ntoa((unpack_sockaddr_in(getpeername(T)))[1])."\n"; + + arm(5); + syswrite(T,"hello",5); + arm(0); + + arm(5); + $read = sysread(T,$buff,10); # Connection may be granted, then closed! + arm(0); + + while ($read > 0 && length($buff) < 5) { + # adjust for fact that TCP doesn't guarantee size of reads/writes + arm(5); + $read = sysread(T,$buff,10,length($buff)); + arm(0); + } + + is(($read == 0 || $buff eq "hello"), "PF_INET echo localhost reply"); + } +} + +SKIP: { + unless(socket(S, PF_INET, SOCK_STREAM, IPPROTO_TCP)) { + skip "No PF_INET", 3; + } + + pass "socket(PF_INET)"; + + SKIP: { + arm(5); + unless($has_echo && connect(S,pack_sockaddr_in(7,INADDR_LOOPBACK))) { + skip "Unable to connect to localhost:7", 2; + } + + arm(0); + + pass "PF_INET echo INADDR_LOOPBACK connected"; + + diag "Connected to " . + inet_ntoa((unpack_sockaddr_in(getpeername(S)))[1])."\n"; + + arm(5); + syswrite(S,"olleh",5); + arm(0); + + arm(5); + $read = sysread(S,$buff,10); # Connection may be granted, then closed! + arm(0); + + while ($read > 0 && length($buff) < 5) { + # adjust for fact that TCP doesn't guarantee size of reads/writes + arm(5); + $read = sysread(S,$buff,10,length($buff)); + arm(0); + } + + is(($read == 0 || $buff eq "olleh"), "PF_INET echo INADDR_LOOPBACK reply"); + } +} + +# warnings +{ + my $w = 0; + local $SIG{__WARN__} = sub { + ++ $w if $_[0] =~ /^6-ARG sockaddr_in call is deprecated/ ; + }; + + no warnings 'Socket'; + sockaddr_in(1,2,3,4,5,6) ; + is($w, 0, "sockaddr_in deprecated form doesn't warn without lexical warnings"); + + use warnings 'Socket'; + sockaddr_in(1,2,3,4,5,6) ; + is($w, 1, "sockaddr_in deprecated form warns with lexical warnings"); +} + +# Test that whatever we give into pack/unpack_sockaddr retains +# the value thru the entire chain. +is(inet_ntoa((unpack_sockaddr_in(pack_sockaddr_in(100,inet_aton("10.250.230.10"))))[1]), '10.250.230.10', + 'inet_aton->pack_sockaddr_in->unpack_sockaddr_in->inet_ntoa roundtrip'); + +is(inet_ntoa(inet_aton("10.20.30.40")), "10.20.30.40", 'inet_aton->inet_ntoa roundtrip'); +is(inet_ntoa(v10.20.30.40), "10.20.30.40", 'inet_ntoa from v-string'); + +{ + my ($port,$addr) = unpack_sockaddr_in(pack_sockaddr_in(100,v10.10.10.10)); + is($port, 100, 'pack_sockaddr_in->unpack_sockaddr_in port'); + is(inet_ntoa($addr), "10.10.10.10", 'pack_sockaddr_in->unpack_sockaddr_in addr'); +} + +{ + local $@; + eval { inet_ntoa(v10.20.30.400) }; + like($@, qr/^Wide character in Socket::inet_ntoa at/, 'inet_ntoa warns about wide characters'); +} + +is(sockaddr_family(pack_sockaddr_in(100,inet_aton("10.250.230.10"))), AF_INET, 'pack_sockaddr_in->sockaddr_family'); + +{ + local $@; + eval { sockaddr_family("") }; + like($@, qr/^Bad arg length for Socket::sockaddr_family, length is 0, should be at least \d+/, 'sockaddr_family warns about argument length'); +} + +SKIP: { + # see if we can handle abstract sockets + skip "Abstract AF_UNIX paths unsupported", 2 unless $^O eq "linux"; + + my $test_abstract_socket = chr(0) . '/org/perl/hello'. chr(0) . 'world'; + my $addr = sockaddr_un ($test_abstract_socket); + my ($path) = sockaddr_un ($addr); + is($path, $test_abstract_socket, 'sockaddr_un can handle abstract AF_UNIX paths'); + + # see if we calculate the address structure length correctly + is(length ($test_abstract_socket) + 2, length $addr, 'sockaddr_un abstract address length'); +} + +SKIP: { + skip "No inet_ntop", 3 unless $Config{d_inetntop} && $Config{d_inetaton}; + + is(inet_ntop(AF_INET, inet_pton(AF_INET, "10.20.30.40")), "10.20.30.40", 'inet_pton->inet_ntop AF_INET roundtrip'); + is(inet_ntop(AF_INET, inet_aton("10.20.30.40")), "10.20.30.40", 'inet_aton->inet_ntop AF_INET roundtrip'); + + SKIP: { + skip "No AF_INET6", 1 unless defined eval { AF_INET6() }; + is(lc inet_ntop(AF_INET6, inet_pton(AF_INET6, "2001:503:BA3E::2:30")), "2001:503:ba3e::2:30", 'inet_pton->inet_ntop AF_INET6 roundtrip'); + } +} + +SKIP: { + skip "No AF_INET6", 5 unless defined eval { AF_INET6() }; + + my $sin6 = pack_sockaddr_in6(0x1234, "0123456789abcdef", 0, 89); + + is(sockaddr_family($sin6), AF_INET6, 'sockaddr_family of pack_sockaddr_in6'); + + is((unpack_sockaddr_in6($sin6))[0], 0x1234, 'pack_sockaddr_in6->unpack_sockaddr_in6 port'); + is((unpack_sockaddr_in6($sin6))[1], "0123456789abcdef", 'pack_sockaddr_in6->unpack_sockaddr_in6 addr'); + is((unpack_sockaddr_in6($sin6))[2], 0, 'pack_sockaddr_in6->unpack_sockaddr_in6 scope_id'); + is((unpack_sockaddr_in6($sin6))[3], 89, 'pack_sockaddr_in6->unpack_sockaddr_in6 flowinfo'); +} diff --git a/cpan/Socket/t/getaddrinfo.t b/cpan/Socket/t/getaddrinfo.t new file mode 100644 index 0000000000..b85af38254 --- /dev/null +++ b/cpan/Socket/t/getaddrinfo.t @@ -0,0 +1,125 @@ +use strict; +use warnings; +use Test::More tests => 30; + +use Socket qw( + AI_NUMERICHOST AF_INET SOCK_STREAM IPPROTO_TCP + unpack_sockaddr_in inet_aton getaddrinfo +); + +my ( $err, @res ); + +( $err, @res ) = getaddrinfo( "127.0.0.1", "80", { socktype => SOCK_STREAM } ); +cmp_ok( $err, "==", 0, '$err == 0 for host=127.0.0.1/service=80/socktype=STREAM' ); +cmp_ok( $err, "eq", "", '$err eq "" for host=127.0.0.1/service=80/socktype=STREAM' ); +is( scalar @res, 1, + '@res has 1 result' ); + +is( $res[0]->{family}, AF_INET, + '$res[0] family is AF_INET' ); +is( $res[0]->{socktype}, SOCK_STREAM, + '$res[0] socktype is SOCK_STREAM' ); +ok( $res[0]->{protocol} == 0 || $res[0]->{protocol} == IPPROTO_TCP, + '$res[0] protocol is 0 or IPPROTO_TCP' ); +ok( defined $res[0]->{addr}, + '$res[0] addr is defined' ); +if (length $res[0]->{addr}) { + is_deeply( [ unpack_sockaddr_in $res[0]->{addr} ], + [ 80, inet_aton( "127.0.0.1" ) ], + '$res[0] addr is {"127.0.0.1", 80}' ); +} else { + fail( '$res[0] addr is empty: check $socksizetype' ); +} + +# Check actual IV integers work just as well as PV strings +( $err, @res ) = getaddrinfo( "127.0.0.1", 80, { socktype => SOCK_STREAM } ); +cmp_ok( $err, "==", 0, '$err == 0 for host=127.0.0.1/service=80/socktype=STREAM' ); +is_deeply( [ unpack_sockaddr_in $res[0]->{addr} ], + [ 80, inet_aton( "127.0.0.1" ) ], + '$res[0] addr is {"127.0.0.1", 80}' ); + +( $err, @res ) = getaddrinfo( "127.0.0.1", "" ); +cmp_ok( $err, "==", 0, '$err == 0 for host=127.0.0.1' ); +# Might get more than one; e.g. different socktypes +ok( scalar @res > 0, '@res has results' ); + +( $err, @res ) = getaddrinfo( "127.0.0.1", undef ); +cmp_ok( $err, "==", 0, '$err == 0 for host=127.0.0.1/service=undef' ); + +# Test GETMAGIC +{ + "127.0.0.1" =~ /(.+)/; + ( $err, @res ) = getaddrinfo($1, undef); + cmp_ok( $err, "==", 0, '$err == 0 for host=$1' ); + ok( scalar @res > 0, '@res has results' ); + is( (unpack_sockaddr_in $res[0]->{addr})[1], + inet_aton( "127.0.0.1" ), + '$res[0] addr is {"127.0.0.1", ??}' ); +} + +( $err, @res ) = getaddrinfo( "", "80", { family => AF_INET, socktype => SOCK_STREAM } ); +cmp_ok( $err, "==", 0, '$err == 0 for service=80/family=AF_INET/socktype=STREAM' ); +is( scalar @res, 1, '@res has 1 result' ); + +# Just pick the first one +is( $res[0]->{family}, AF_INET, + '$res[0] family is AF_INET' ); +is( $res[0]->{socktype}, SOCK_STREAM, + '$res[0] socktype is SOCK_STREAM' ); +ok( $res[0]->{protocol} == 0 || $res[0]->{protocol} == IPPROTO_TCP, + '$res[0] protocol is 0 or IPPROTO_TCP' ); + +# Now some tests of a few well-known internet hosts +my $goodhost = "cpan.perl.org"; + +SKIP: { + skip "Resolver has no answer for $goodhost", 2 unless gethostbyname( $goodhost ); + + ( $err, @res ) = getaddrinfo( "cpan.perl.org", "ftp", { socktype => SOCK_STREAM } ); + cmp_ok( $err, "==", 0, '$err == 0 for host=cpan.perl.org/service=ftp/socktype=STREAM' ); + # Might get more than one; e.g. different families + ok( scalar @res > 0, '@res has results' ); +} + +# Now something I hope doesn't exist - we put it in a known-missing TLD +my $missinghost = "TbK4jM2M0OS.lm57DWIyu4i"; + +# Some CPAN testing machines seem to have wildcard DNS servers that reply to +# any request. We'd better check for them + +SKIP: { + skip "Resolver has an answer for $missinghost", 1 if gethostbyname( $missinghost ); + + # Some OSes return $err == 0 but no results + ( $err, @res ) = getaddrinfo( $missinghost, "ftp", { socktype => SOCK_STREAM } ); + ok( $err != 0 || ( $err == 0 && @res == 0 ), + '$err != 0 or @res == 0 for host=TbK4jM2M0OS.lm57DWIyu4i/service=ftp/socktype=SOCK_STREAM' ); + if( @res ) { + # Diagnostic that might help + while( my $r = shift @res ) { + diag( "family=$r->{family} socktype=$r->{socktype} protocol=$r->{protocol} addr=[" . length( $r->{addr} ) . " bytes]" ); + diag( " addr=" . join( ", ", map { sprintf '0x%02x', ord $_ } split m//, $r->{addr} ) ); + } + } +} + +# Now check that names with AI_NUMERICHOST fail + +( $err, @res ) = getaddrinfo( "localhost", "ftp", { flags => AI_NUMERICHOST, socktype => SOCK_STREAM } ); +ok( $err != 0, '$err != 0 for host=localhost/service=ftp/flags=AI_NUMERICHOST/socktype=SOCK_STREAM' ); + +# Some sanity checking on the hints hash +ok( defined eval { getaddrinfo( "127.0.0.1", "80", undef ); 1 }, + 'getaddrinfo() with undef hints works' ); +ok( !defined eval { getaddrinfo( "127.0.0.1", "80", "hints" ); 1 }, + 'getaddrinfo() with string hints dies' ); +ok( !defined eval { getaddrinfo( "127.0.0.1", "80", [] ); 1 }, + 'getaddrinfo() with ARRAY hints dies' ); + +# Ensure it doesn't segfault if args are missing + +( $err, @res ) = getaddrinfo(); +ok( defined $err, '$err defined for getaddrinfo()' ); + +( $err, @res ) = getaddrinfo( "127.0.0.1" ); +ok( defined $err, '$err defined for getaddrinfo("127.0.0.1")' ); diff --git a/cpan/Socket/t/getnameinfo.t b/cpan/Socket/t/getnameinfo.t new file mode 100644 index 0000000000..803e8c0c57 --- /dev/null +++ b/cpan/Socket/t/getnameinfo.t @@ -0,0 +1,37 @@ +use strict; +use warnings; +use Test::More tests => 10; + +use Socket qw( + AF_INET NI_NUMERICHOST NI_NUMERICSERV + getnameinfo pack_sockaddr_in inet_aton +); + +my ( $err, $host, $service ); + +( $err, $host, $service ) = getnameinfo( pack_sockaddr_in( 80, inet_aton( "127.0.0.1" ) ), NI_NUMERICHOST|NI_NUMERICSERV ); +cmp_ok( $err, "==", 0, '$err == 0 for {family=AF_INET,port=80,sinaddr=127.0.0.1}/NI_NUMERICHOST|NI_NUMERICSERV' ); +cmp_ok( $err, "eq", "", '$err eq "" for {family=AF_INET,port=80,sinaddr=127.0.0.1}/NI_NUMERICHOST|NI_NUMERICSERV' ); + +is( $host, "127.0.0.1", '$host is 127.0.0.1 for NH/NS' ); +is( $service, "80", '$service is 80 for NH/NS' ); + +# Probably "localhost" but we'd better ask the system to be sure +my $expect_host = gethostbyaddr( inet_aton( "127.0.0.1" ), AF_INET ); +defined $expect_host or $expect_host = "127.0.0.1"; + +( $err, $host, $service ) = getnameinfo( pack_sockaddr_in( 80, inet_aton( "127.0.0.1" ) ), NI_NUMERICSERV ); +cmp_ok( $err, "==", 0, '$err == 0 for {family=AF_INET,port=80,sinaddr=127.0.0.1}/NI_NUMERICSERV' ); + +is( $host, $expect_host, "\$host is $expect_host for NS" ); +is( $service, "80", '$service is 80 for NS' ); + +# Probably "www" but we'd better ask the system to be sure +my $expect_service = getservbyport( 80, "tcp" ); +defined $expect_service or $expect_service = "80"; + +( $err, $host, $service ) = getnameinfo( pack_sockaddr_in( 80, inet_aton( "127.0.0.1" ) ), NI_NUMERICHOST ); +cmp_ok( $err, "==", 0, '$err == 0 for {family=AF_INET,port=80,sinaddr=127.0.0.1}/NI_NUMERICHOST' ); + +is( $host, "127.0.0.1", '$host is 127.0.0.1 for NH' ); +is( $service, $expect_service, "\$service is $expect_service for NH" ); diff --git a/cpan/Socket/t/ipv6_mreq.t b/cpan/Socket/t/ipv6_mreq.t new file mode 100644 index 0000000000..43fb8f804b --- /dev/null +++ b/cpan/Socket/t/ipv6_mreq.t @@ -0,0 +1,26 @@ +use strict; +use warnings; +use Test::More; + +use Socket qw( + pack_ipv6_mreq unpack_ipv6_mreq +); + +# Check that pack/unpack_ipv6_mreq either croak with "Not implemented", or +# roundtrip as identity + +my $packed; +eval { + $packed = pack_ipv6_mreq "ANADDRESSIN16CHR", 123; +}; +if( !defined $packed ) { + plan skip_all => "No pack_ipv6_mreq" if $@ =~ m/ not implemented /; + die $@; +} + +plan tests => 2; + +my @unpacked = unpack_ipv6_mreq $packed; + +is( $unpacked[0], "ANADDRESSIN16CHR", 'unpack_ipv6_mreq multiaddr' ); +is( $unpacked[1], 123, 'unpack_ipv6_mreq ifindex' ); diff --git a/cpan/Socket/t/socketpair.t b/cpan/Socket/t/socketpair.t new file mode 100644 index 0000000000..997628c3bd --- /dev/null +++ b/cpan/Socket/t/socketpair.t @@ -0,0 +1,245 @@ +#!./perl -w + +my $child; +my $can_fork; +my $has_perlio; + +BEGIN { + require Config; import Config; + $can_fork = $Config{'d_fork'} || $Config{'d_pseudofork'}; + + if ($^O eq "hpux" or $Config{'extensions'} !~ /\bSocket\b/ && + !(($^O eq 'VMS') && $Config{d_socket})) { + print "1..0\n"; + exit 0; + } +} + +{ + # This was in the BEGIN block, but since Test::More 0.47 added support to + # detect forking, we don't need to fork before Test::More initialises. + + # Too many things in this test will hang forever if something is wrong, + # so we need a self destruct timer. And IO can hang despite an alarm. + + if( $can_fork) { + my $parent = $$; + $child = fork; + die "Fork failed" unless defined $child; + if (!$child) { + $SIG{INT} = sub {exit 0}; # You have 60 seconds. Your time starts now. + my $must_finish_by = time + 60; + my $remaining; + while (($remaining = $must_finish_by - time) > 0) { + sleep $remaining; + } + warn "Something unexpectedly hung during testing"; + kill "INT", $parent or die "Kill failed: $!"; + exit 1; + } + } + unless ($has_perlio = find PerlIO::Layer 'perlio') { + print < "alarm() not implemented on this platform"; +} elsif( !$can_fork ) { + plan skip_all => "fork() not implemented on this platform"; +} else { + # This should fail but not die if there is real socketpair + eval {socketpair LEFT, RIGHT, -1, -1, -1}; + if ($@ =~ /^Unsupported socket function "socketpair" called/ || + $! =~ /^The operation requested is not supported./) { # Stratus VOS + plan skip_all => 'No socketpair (real or emulated)'; + } else { + eval {AF_UNIX}; + if ($@ =~ /^Your vendor has not defined Socket macro AF_UNIX/) { + plan skip_all => 'No AF_UNIX'; + } else { + plan tests => 45; + } + } +} + +# But we'll install an alarm handler in case any of the races below fail. +$SIG{ALRM} = sub {die "Unexpected alarm during testing"}; + +ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC), + "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)") + or print "# \$\! = $!\n"; + +if ($has_perlio) { + binmode(LEFT, ":bytes"); + binmode(RIGHT, ":bytes"); +} + +my @left = ("hello ", "world\n"); +my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here. + +foreach (@left) { + # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left"); + is (syswrite (LEFT, $_), length $_, "syswrite to left"); +} +foreach (@right) { + # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right"); + is (syswrite (RIGHT, $_), length $_, "syswrite to right"); +} + +# stream socket, so our writes will become joined: +my ($buffer, $expect); +$expect = join '', @right; +undef $buffer; +is (read (LEFT, $buffer, length $expect), length $expect, "read on left"); +is ($buffer, $expect, "content what we expected?"); +$expect = join '', @left; +undef $buffer; +is (read (RIGHT, $buffer, length $expect), length $expect, "read on right"); +is ($buffer, $expect, "content what we expected?"); + +ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing"); +# This will hang forever if eof is buggy, and alarm doesn't interrupt system +# Calls. Hence the child process minder. +SKIP: { + skip "SCO Unixware / OSR have a bug with shutdown",2 if $^O =~ /^(?:svr|sco)/; + local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" }; + local $TODO = "Known problems with unix sockets on $^O" + if $^O eq 'hpux' || $^O eq 'super-ux'; + alarm 3; + $! = 0; + ok (eof RIGHT, "right is at EOF"); + local $TODO = "Known problems with unix sockets on $^O" + if $^O eq 'unicos' || $^O eq 'unicosmk'; + is ($!, '', 'and $! should report no error'); + alarm 60; +} + +my $err = $!; +$SIG{PIPE} = 'IGNORE'; +{ + local $SIG{ALRM} + = sub { warn "syswrite to left didn't fail within 3 seconds" }; + alarm 3; + # Split the system call from the is() - is() does IO so + # (say) a flush may do a seek which on a pipe may disturb errno + my $ans = syswrite (LEFT, "void"); + $err = $!; + is ($ans, undef, "syswrite to shutdown left should fail"); + alarm 60; +} +{ + # This may need skipping on some OSes - restoring value saved above + # should help + $! = $err; + ok (($!{EPIPE} or $!{ESHUTDOWN}), '$! should be EPIPE or ESHUTDOWN') + or printf "\$\!=%d(%s)\n", $err, $err; +} + +my @gripping = (chr 255, chr 127); +foreach (@gripping) { + is (syswrite (RIGHT, $_), length $_, "syswrite to right"); +} + +ok (!eof LEFT, "left is not at EOF"); + +$expect = join '', @gripping; +undef $buffer; +is (read (LEFT, $buffer, length $expect), length $expect, "read on left"); +is ($buffer, $expect, "content what we expected?"); + +ok (close LEFT, "close left"); +ok (close RIGHT, "close right"); + + +# And now datagrams +# I suspect we also need a self destruct time-bomb for these, as I don't see any +# guarantee that the stack won't drop a UDP packet, even if it is for localhost. + +SKIP: { + skip "No usable SOCK_DGRAM for socketpair", 24 if ($^O =~ /^(MSWin32|os2)\z/); + local $TODO = "socketpair not supported on $^O" if $^O eq 'nto'; + +ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC), + "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)") + or print "# \$\! = $!\n"; + +if ($has_perlio) { + binmode(LEFT, ":bytes"); + binmode(RIGHT, ":bytes"); +} + +foreach (@left) { + # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left"); + is (syswrite (LEFT, $_), length $_, "syswrite to left"); +} +foreach (@right) { + # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right"); + is (syswrite (RIGHT, $_), length $_, "syswrite to right"); +} + +# stream socket, so our writes will become joined: +my ($total); +$total = join '', @right; +foreach $expect (@right) { + undef $buffer; + is (sysread (LEFT, $buffer, length $total), length $expect, "read on left"); + is ($buffer, $expect, "content what we expected?"); +} +$total = join '', @left; +foreach $expect (@left) { + undef $buffer; + is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right"); + is ($buffer, $expect, "content what we expected?"); +} + +ok (shutdown(LEFT, 1), "shutdown left for writing"); + +# eof uses buffering. eof is indicated by a sysread of zero. +# but for a datagram socket there's no way it can know nothing will ever be +# sent +SKIP: { + skip "$^O does length 0 udp reads", 2 if ($^O eq 'os390'); + + my $alarmed = 0; + local $SIG{ALRM} = sub { $alarmed = 1; }; + print "# Approximate forever as 3 seconds. Wait 'forever'...\n"; + alarm 3; + undef $buffer; + is (sysread (RIGHT, $buffer, 1), undef, + "read on right should be interrupted"); + is ($alarmed, 1, "alarm should have fired"); +} + +alarm 30; + +#ok (eof RIGHT, "right is at EOF"); + +foreach (@gripping) { + is (syswrite (RIGHT, $_), length $_, "syswrite to right"); +} + +$total = join '', @gripping; +foreach $expect (@gripping) { + undef $buffer; + is (sysread (LEFT, $buffer, length $total), length $expect, "read on left"); + is ($buffer, $expect, "content what we expected?"); +} + +ok (close LEFT, "close left"); +ok (close RIGHT, "close right"); + +} # end of DGRAM SKIP + +kill "INT", $child or warn "Failed to kill child process $child: $!"; +exit 0; diff --git a/ext/Socket/.gitignore b/ext/Socket/.gitignore deleted file mode 100644 index 2a06e93b55..0000000000 --- a/ext/Socket/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.inc diff --git a/ext/Socket/Makefile.PL b/ext/Socket/Makefile.PL deleted file mode 100644 index 83908116f3..0000000000 --- a/ext/Socket/Makefile.PL +++ /dev/null @@ -1,93 +0,0 @@ -use ExtUtils::MakeMaker; -use ExtUtils::Constant 0.23 'WriteConstants'; -use Config; -WriteMakefile( - NAME => 'Socket', - VERSION_FROM => 'Socket.pm', - ($Config{libs} =~ /(-lsocks\S*)/ ? (LIBS => [ "$1" ] ) : ()), - XSPROTOARG => '-noprototypes', # XXX remove later? - realclean => {FILES=> 'const-c.inc const-xs.inc'}, -); -my @names = (qw(AF_802 AF_AAL AF_APPLETALK AF_CCITT AF_CHAOS AF_CTF - AF_DATAKIT AF_DECnet AF_DLI AF_ECMA AF_GOSIP AF_HYLINK - AF_IMPLINK AF_INET AF_INET6 AF_ISO AF_KEY - AF_LAST AF_LAT AF_LINK AF_MAX AF_NBS AF_NIT AF_NS - AF_OSI AF_OSINET AF_PUP AF_ROUTE AF_SNA - AF_UNIX AF_UNSPEC AF_USER AF_WAN AF_X25 - AI_CANONNAME AI_NUMERICHOST AI_NUMERICSERV AI_PASSIVE - EAI_ADDRFAMILY EAI_AGAIN EAI_BADFLAGS EAI_FAIL EAI_FAMILY - EAI_NODATA EAI_NONAME EAI_SERVICE EAI_SOCKTYPE - IOV_MAX IP_OPTIONS IP_HDRINCL IP_TOS IP_TTL IP_RECVOPTS - IP_RECVRETOPTS IP_RETOPTS - IPV6_ADD_MEMBERSHIP IPV6_DROP_MEMBERSHIP IPV6_MTU - IPV6_MTU_DISCOVER IPV6_MULTICAST_HOPS IPV6_MULTICAST_IF - IPV6_MULTICAST_LOOP IPV6_UNICAST_HOPS IPV6_V6ONLY - MSG_BCAST MSG_BTAG MSG_CTLFLAGS MSG_CTLIGNORE MSG_DONTWAIT - MSG_EOF MSG_EOR MSG_ERRQUEUE MSG_ETAG MSG_FIN - MSG_MAXIOVLEN MSG_MCAST MSG_NOSIGNAL MSG_RST MSG_SYN - MSG_TRUNC MSG_URG MSG_WAITALL MSG_WIRE - NI_DGRAM NI_NAMEREQD NI_NUMERICHOST NI_NUMERICSERV - PF_802 PF_AAL PF_APPLETALK PF_CCITT PF_CHAOS PF_CTF - PF_DATAKIT PF_DECnet PF_DLI PF_ECMA PF_GOSIP PF_HYLINK - PF_IMPLINK PF_INET PF_INET6 PF_ISO PF_KEY - PF_LAST PF_LAT PF_LINK PF_MAX PF_NBS PF_NIT PF_NS - PF_OSI PF_OSINET PF_PUP PF_ROUTE PF_SNA - PF_UNIX PF_UNSPEC PF_USER PF_WAN PF_X25 - SCM_CONNECT SCM_CREDENTIALS SCM_CREDS SCM_TIMESTAMP - SOCK_DGRAM SOCK_RAW SOCK_RDM SOCK_SEQPACKET SOCK_STREAM - SOL_SOCKET SOMAXCONN - SO_ACCEPTCONN SO_ATTACH_FILTER SO_BACKLOG SO_BROADCAST - SO_CHAMELEON SO_DEBUG SO_DETACH_FILTER SO_DGRAM_ERRIND - SO_DONTLINGER SO_DONTROUTE SO_ERROR SO_FAMILY - SO_KEEPALIVE SO_LINGER SO_OOBINLINE - SO_PASSCRED SO_PASSIFNAME SO_PEERCRED - SO_PROTOCOL SO_PROTOTYPE - SO_RCVBUF SO_RCVLOWAT SO_RCVTIMEO - SO_REUSEADDR SO_REUSEPORT - SO_SECURITY_AUTHENTICATION - SO_SECURITY_ENCRYPTION_NETWORK - SO_SECURITY_ENCRYPTION_TRANSPORT - SO_SNDBUF SO_SNDLOWAT SO_SNDTIMEO - SO_STATE SO_TYPE SO_USELOOPBACK SO_XOPEN SO_XSE - TCP_KEEPALIVE TCP_MAXRT TCP_MAXSEG TCP_NODELAY TCP_STDURG - TCP_CORK TCP_KEEPIDLE TCP_KEEPINTVL TCP_KEEPCNT - TCP_SYNCNT TCP_LINGER2 TCP_DEFER_ACCEPT TCP_WINDOW_CLAMP - TCP_INFO TCP_QUICKACK TCP_CONGESTION TCP_MD5SIG - UIO_MAXIOV - ), - {name=>"IPPROTO_IP", type=>"IV", default=>["IV", 0]}, - {name=>"IPPROTO_IPV6", type=>"IV", default=>["IV", 41]}, - {name=>"IPPROTO_RAW", type=>"IV", default=>["IV", 255]}, - {name=>"IPPROTO_ICMP", type=>"IV", default=>["IV", 1]}, - {name=>"IPPROTO_TCP", type=>"IV", default=>["IV", 6]}, - {name=>"IPPROTO_UDP", type=>"IV", default=>["IV", 17]}, - {name=>"SHUT_RD", type=>"IV", default=>["IV", "0"]}, - {name=>"SHUT_WR", type=>"IV", default=>["IV", "1"]}, - {name=>"SHUT_RDWR", type=>"IV", default=>["IV", "2"]}, -); - -push @names, - {name=>$_, type=>"IV", - macro=>["#if defined($_) || defined(HAS_$_) /* might be an enum */\n", - "#endif\n"]} -foreach qw (MSG_CTRUNC MSG_DONTROUTE MSG_OOB MSG_PEEK MSG_PROXY SCM_RIGHTS); - -push @names, -{name => $_, type => "SV", - pre=>"struct in_addr ip_address; ip_address.s_addr = htonl($_);", - value => "newSVpvn_flags((char *)&ip_address,sizeof(ip_address), SVs_TEMP)",} - foreach qw(INADDR_ANY INADDR_LOOPBACK INADDR_NONE INADDR_BROADCAST); - -push @names, -{name => $_, type => "SV", - macro=>["#ifdef ${_}_INIT\n", - "#endif\n"], - pre=>"struct in6_addr ip6_address = ${_}_INIT;", - value => "newSVpvn_flags((char *)&ip6_address,sizeof(ip6_address), SVs_TEMP)",} - foreach qw(IN6ADDR_ANY IN6ADDR_LOOPBACK); - -WriteConstants( - PROXYSUBS => {autoload => 1}, - NAME => 'Socket', - NAMES => \@names, -); diff --git a/ext/Socket/Socket.pm b/ext/Socket/Socket.pm deleted file mode 100644 index 20ae15b07b..0000000000 --- a/ext/Socket/Socket.pm +++ /dev/null @@ -1,832 +0,0 @@ -package Socket; - -use strict; - -our($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); -$VERSION = "1.94_03"; - -=head1 NAME - -Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa, inet_pton, inet_ntop - load the C socket.h defines and structure manipulators - -=head1 SYNOPSIS - - use Socket; - - $proto = getprotobyname('udp'); - socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto); - $iaddr = gethostbyname('hishost.com'); - $port = getservbyname('time', 'udp'); - $sin = sockaddr_in($port, $iaddr); - send(Socket_Handle, 0, 0, $sin); - - $proto = getprotobyname('tcp'); - socket(Socket_Handle, PF_INET, SOCK_STREAM, $proto); - $port = getservbyname('smtp', 'tcp'); - $sin = sockaddr_in($port,inet_aton("127.1")); - $sin = sockaddr_in(7,inet_aton("localhost")); - $sin = sockaddr_in(7,INADDR_LOOPBACK); - connect(Socket_Handle,$sin); - - ($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle)); - $peer_host = gethostbyaddr($iaddr, AF_INET); - $peer_addr = inet_ntoa($iaddr); - - $proto = getprotobyname('tcp'); - socket(Socket_Handle, PF_UNIX, SOCK_STREAM, $proto); - unlink('/var/run/usock'); - $sun = sockaddr_un('/var/run/usock'); - connect(Socket_Handle,$sun); - -=head1 DESCRIPTION - -This module is just a translation of the C F file. -Unlike the old mechanism of requiring a translated F -file, this uses the B program (see the Perl source distribution) -and your native C compiler. This means that it has a -far more likely chance of getting the numbers right. This includes -all of the commonly used pound-defines like AF_INET, SOCK_STREAM, etc. - -Also, some common socket "newline" constants are provided: the -constants C, C, and C, as well as C<$CR>, C<$LF>, and -C<$CRLF>, which map to C<\015>, C<\012>, and C<\015\012>. If you do -not want to use the literal characters in your programs, then use -the constants provided here. They are not exported by default, but can -be imported individually, and with the C<:crlf> export tag: - - use Socket qw(:DEFAULT :crlf); - -In addition, some structure manipulation functions are available: - -=over 4 - -=item inet_aton HOSTNAME - -Takes a string giving the name of a host, and translates that to an -opaque string (if programming in C, struct in_addr). Takes arguments -of both the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name -cannot be resolved, returns undef. For multi-homed hosts (hosts with -more than one address), the first address found is returned. - -For portability do not assume that the result of inet_aton() is 32 -bits wide, in other words, that it would contain only the IPv4 address -in network order. - -=item inet_ntoa IP_ADDRESS - -Takes a string (an opaque string as returned by inet_aton(), -or a v-string representing the four octets of the IPv4 address in -network order) and translates it into a string of the form 'd.d.d.d' -where the 'd's are numbers less than 256 (the normal human-readable -four dotted number notation for Internet addresses). - -=item INADDR_ANY - -Note: does not return a number, but a packed string. - -Returns the 4-byte wildcard ip address which specifies any -of the hosts ip addresses. (A particular machine can have -more than one ip address, each address corresponding to -a particular network interface. This wildcard address -allows you to bind to all of them simultaneously.) -Normally equivalent to inet_aton('0.0.0.0'). - -=item INADDR_BROADCAST - -Note: does not return a number, but a packed string. - -Returns the 4-byte 'this-lan' ip broadcast address. -This can be useful for some protocols to solicit information -from all servers on the same LAN cable. -Normally equivalent to inet_aton('255.255.255.255'). - -=item INADDR_LOOPBACK - -Note - does not return a number. - -Returns the 4-byte loopback address. Normally equivalent -to inet_aton('localhost'). - -=item INADDR_NONE - -Note - does not return a number. - -Returns the 4-byte 'invalid' ip address. Normally equivalent -to inet_aton('255.255.255.255'). - -=item IN6ADDR_ANY - -Returns the 16-byte wildcard IPv6 address. Normally equivalent -to inet_pton(AF_INET6, "::") - -=item IN6ADDR_LOOPBACK - -Returns the 16-byte loopback IPv6 address. Normally equivalent -to inet_pton(AF_INET6, "::1") - -=item sockaddr_family SOCKADDR - -Takes a sockaddr structure (as returned by pack_sockaddr_in(), -pack_sockaddr_un() or the perl builtin functions getsockname() and -getpeername()) and returns the address family tag. It will match the -constant AF_INET for a sockaddr_in and AF_UNIX for a sockaddr_un. It -can be used to figure out what unpacker to use for a sockaddr of -unknown type. - -=item sockaddr_in PORT, ADDRESS - -=item sockaddr_in SOCKADDR_IN - -In a list context, unpacks its SOCKADDR_IN argument and returns an array -consisting of (PORT, ADDRESS). In a scalar context, packs its (PORT, -ADDRESS) arguments as a SOCKADDR_IN and returns it. If this is confusing, -use pack_sockaddr_in() and unpack_sockaddr_in() explicitly. - -=item pack_sockaddr_in PORT, IP_ADDRESS - -Takes two arguments, a port number and an opaque string, IP_ADDRESS -(as returned by inet_aton(), or a v-string). Returns the sockaddr_in -structure with those arguments packed in with AF_INET filled in. For -Internet domain sockets, this structure is normally what you need for -the arguments in bind(), connect(), and send(), and is also returned -by getpeername(), getsockname() and recv(). - -=item unpack_sockaddr_in SOCKADDR_IN - -Takes a sockaddr_in structure (as returned by pack_sockaddr_in()) and -returns an array of two elements: the port and an opaque string -representing the IP address (you can use inet_ntoa() to convert the -address to the four-dotted numeric format). Will croak if the -structure does not have AF_INET in the right place. - -=item sockaddr_in6 PORT, IP6_ADDRESS, [ SCOPE_ID, [ FLOWINFO ] ] - -=item sockaddr_in6 SOCKADDR_IN6 - -In list context, unpacks its SOCKADDR_IN6 argument according to -unpack_sockaddr_in6(). In scalar context, packs its arguments according to -pack_sockaddr_in6(). - -=item pack_sockaddr_in6 PORT, IP6_ADDRESS, [ SCOPE_ID, [ FLOWINFO ] ] - -Takes two to four arguments, a port number, an opaque string (as returned by -inet_pton()), optionally a scope ID number, and optionally a flow label -number. Returns the sockaddr_in6 structure with those arguments packed in -with AF_INET6 filled in. IPv6 equivalent of pack_sockaddr_in(). - -=item unpack_sockaddr_in6 SOCKADDR_IN6 - -Takes a sockaddr_in6 structure (as returned by pack_sockaddr_in6()) and -returns an array of four elements: the port number, an opaque string -representing the IPv6 address, the scope ID, and the flow label. (You can -use inet_ntop() to convert the address to the usual string format). Will -croak if the structure does not have AF_INET6 in the right place. - -=item sockaddr_un PATHNAME - -=item sockaddr_un SOCKADDR_UN - -In a list context, unpacks its SOCKADDR_UN argument and returns an array -consisting of (PATHNAME). In a scalar context, packs its PATHNAME -arguments as a SOCKADDR_UN and returns it. If this is confusing, use -pack_sockaddr_un() and unpack_sockaddr_un() explicitly. -These are only supported if your system has EFE. - -=item pack_sockaddr_un PATH - -Takes one argument, a pathname. Returns the sockaddr_un structure with -that path packed in with AF_UNIX filled in. For unix domain sockets, this -structure is normally what you need for the arguments in bind(), -connect(), and send(), and is also returned by getpeername(), -getsockname() and recv(). - -=item unpack_sockaddr_un SOCKADDR_UN - -Takes a sockaddr_un structure (as returned by pack_sockaddr_un()) -and returns the pathname. Will croak if the structure does not -have AF_UNIX in the right place. - -=item inet_pton ADDRESS_FAMILY, HOSTNAME - -Takes an address family, either AF_INET or AF_INET6, and a string giving -the name of a host, and translates that to an opaque string -(if programming in C, struct in_addr or struct in6_addr depending on the -address family passed in). The host string may be a string hostname, such -as 'www.perl.org', or an IP address. If using an IP address, the type of -IP address must be consistent with the address family passed into the function. - -This function is not exported by default. - -=item inet_ntop ADDRESS_FAMILY, IP_ADDRESS - -Takes an address family, either AF_INET or AF_INET6, and a string -(an opaque string as returned by inet_aton() or inet_pton()) and -translates it to an IPv4 or IPv6 address string. - -This function is not exported by default. - -=item getaddrinfo HOST, SERVICE, [ HINTS ] - -Given at least one of a hostname and a service name, returns a list of address -structures to listen on or connect to. HOST and SERVICE should be plain -strings (or a numerical port number for SERVICE). If present, HINTS should be -a reference to a HASH, where the following keys are recognised: - -=over 8 - -=item flags => INT - -A bitfield containing C constants - -=item family => INT - -Restrict to only generating addresses in this address family - -=item socktype => INT - -Restrict to only generating addresses of this socket type - -=item protocol => INT - -Restrict to only generating addresses for this protocol - -=back - -The return value will be a list; the first value being an error indication, -followed by a list of address structures (if no error occured). - - my ( $err, @results ) = getaddrinfo( ... ); - -The error value will be a dualvar; comparable to the C error constants, -or printable as a human-readable error message string. Each value in the -results list will be a HASH reference containing the following fields: - -=over 8 - -=item family => INT - -The address family (e.g. AF_INET) - -=item socktype => INT - -The socket type (e.g. SOCK_STREAM) - -=item protocol => INT - -The protocol (e.g. IPPROTO_TCP) - -=item addr => STRING - -The address in a packed string (such as would be returned by pack_sockaddr_in) - -=item canonname => STRING - -The canonical name for the host if the C flag was provided, or -C otherwise. This field will only be present on the first returned -address. - -=back - -=item getnameinfo ADDR, FLAGS - -Given a packed socket address (such as from C, C, or -returned by C in a C field), returns the hostname and -symbolic service name it represents. FLAGS may be a bitmask of C -constants, or defaults to 0 if unspecified. - -The return value will be a list; the first value being an error condition, -followed by the hostname and service name. - - my ( $err, $host, $service ) = getnameinfo( ... ); - -The error value will be a dualvar; comparable to the C error constants, -or printable as a human-readable error message string. The host and service -names will be plain strings. - -=back - -=over 8 - -=item pack_ipv6_mreq IP6_MULTIADDR, INTERFACE - -Takes an IPv6 address and an interface number. Returns the ipv6_mreq structure -with those arguments packed in. Suitable for use with the -C and C sockopts. - -=item unpack_ipv6_mreq IPV6_MREQ - -Takes an ipv6_mreq structure and returns a list of two elements; the IPv6 -address and an interface number. - -=back - -=cut - -use Carp; -use warnings::register; - -require Exporter; -require XSLoader; -@ISA = qw(Exporter); - -# <@Nicholas> you can't change @EXPORT without breaking the implicit API -# Please put any new constants in @EXPORT_OK! -@EXPORT = qw( - inet_aton inet_ntoa - sockaddr_family - pack_sockaddr_in unpack_sockaddr_in - pack_sockaddr_un unpack_sockaddr_un - pack_sockaddr_in6 unpack_sockaddr_in6 - sockaddr_in sockaddr_in6 sockaddr_un - INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE - AF_802 - AF_AAL - AF_APPLETALK - AF_CCITT - AF_CHAOS - AF_CTF - AF_DATAKIT - AF_DECnet - AF_DLI - AF_ECMA - AF_GOSIP - AF_HYLINK - AF_IMPLINK - AF_INET - AF_INET6 - AF_ISO - AF_KEY - AF_LAST - AF_LAT - AF_LINK - AF_MAX - AF_NBS - AF_NIT - AF_NS - AF_OSI - AF_OSINET - AF_PUP - AF_ROUTE - AF_SNA - AF_UNIX - AF_UNSPEC - AF_USER - AF_WAN - AF_X25 - IOV_MAX - IP_OPTIONS - IP_HDRINCL - IP_TOS - IP_TTL - IP_RECVOPTS - IP_RECVRETOPTS - IP_RETOPTS - MSG_BCAST - MSG_BTAG - MSG_CTLFLAGS - MSG_CTLIGNORE - MSG_CTRUNC - MSG_DONTROUTE - MSG_DONTWAIT - MSG_EOF - MSG_EOR - MSG_ERRQUEUE - MSG_ETAG - MSG_FIN - MSG_MAXIOVLEN - MSG_MCAST - MSG_NOSIGNAL - MSG_OOB - MSG_PEEK - MSG_PROXY - MSG_RST - MSG_SYN - MSG_TRUNC - MSG_URG - MSG_WAITALL - MSG_WIRE - PF_802 - PF_AAL - PF_APPLETALK - PF_CCITT - PF_CHAOS - PF_CTF - PF_DATAKIT - PF_DECnet - PF_DLI - PF_ECMA - PF_GOSIP - PF_HYLINK - PF_IMPLINK - PF_INET - PF_INET6 - PF_ISO - PF_KEY - PF_LAST - PF_LAT - PF_LINK - PF_MAX - PF_NBS - PF_NIT - PF_NS - PF_OSI - PF_OSINET - PF_PUP - PF_ROUTE - PF_SNA - PF_UNIX - PF_UNSPEC - PF_USER - PF_WAN - PF_X25 - SCM_CONNECT - SCM_CREDENTIALS - SCM_CREDS - SCM_RIGHTS - SCM_TIMESTAMP - SHUT_RD - SHUT_RDWR - SHUT_WR - SOCK_DGRAM - SOCK_RAW - SOCK_RDM - SOCK_SEQPACKET - SOCK_STREAM - SOL_SOCKET - SOMAXCONN - SO_ACCEPTCONN - SO_ATTACH_FILTER - SO_BACKLOG - SO_BROADCAST - SO_CHAMELEON - SO_DEBUG - SO_DETACH_FILTER - SO_DGRAM_ERRIND - SO_DONTLINGER - SO_DONTROUTE - SO_ERROR - SO_FAMILY - SO_KEEPALIVE - SO_LINGER - SO_OOBINLINE - SO_PASSCRED - SO_PASSIFNAME - SO_PEERCRED - SO_PROTOCOL - SO_PROTOTYPE - SO_RCVBUF - SO_RCVLOWAT - SO_RCVTIMEO - SO_REUSEADDR - SO_REUSEPORT - SO_SECURITY_AUTHENTICATION - SO_SECURITY_ENCRYPTION_NETWORK - SO_SECURITY_ENCRYPTION_TRANSPORT - SO_SNDBUF - SO_SNDLOWAT - SO_SNDTIMEO - SO_STATE - SO_TYPE - SO_USELOOPBACK - SO_XOPEN - SO_XSE - UIO_MAXIOV -); - -@EXPORT_OK = qw(CR LF CRLF $CR $LF $CRLF - - inet_pton - inet_ntop - - getaddrinfo - getnameinfo - - pack_ipv6_mreq - unpack_ipv6_mreq - - IN6ADDR_ANY IN6ADDR_LOOPBACK - - AI_CANONNAME - AI_NUMERICHOST - AI_NUMERICSERV - AI_PASSIVE - - EAI_ADDRFAMILY - EAI_AGAIN - EAI_BADFLAGS - EAI_FAIL - EAI_FAMILY - EAI_NODATA - EAI_NONAME - EAI_SERVICE - EAI_SOCKTYPE - - IPPROTO_IP - IPPROTO_IPV6 - IPPROTO_RAW - IPPROTO_ICMP - IPPROTO_TCP - IPPROTO_UDP - - IPV6_ADD_MEMBERSHIP - IPV6_DROP_MEMBERSHIP - IPV6_MTU - IPV6_MTU_DISCOVER - IPV6_MULTICAST_HOPS - IPV6_MULTICAST_IF - IPV6_MULTICAST_LOOP - IPV6_UNICAST_HOPS - IPV6_V6ONLY - - NI_DGRAM - NI_NAMEREQD - NI_NUMERICHOST - NI_NUMERICSERV - - TCP_KEEPALIVE - TCP_MAXRT - TCP_MAXSEG - TCP_NODELAY - TCP_STDURG - TCP_CORK - TCP_KEEPIDLE - TCP_KEEPINTVL - TCP_KEEPCNT - TCP_SYNCNT - TCP_LINGER2 - TCP_DEFER_ACCEPT - TCP_WINDOW_CLAMP - TCP_INFO - TCP_QUICKACK - TCP_CONGESTION - TCP_MD5SIG); - -%EXPORT_TAGS = ( - crlf => [qw(CR LF CRLF $CR $LF $CRLF)], - all => [@EXPORT, @EXPORT_OK], -); - -BEGIN { - sub CR () {"\015"} - sub LF () {"\012"} - sub CRLF () {"\015\012"} -} - -*CR = \CR(); -*LF = \LF(); -*CRLF = \CRLF(); - -sub sockaddr_in { - if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die - my($af, $port, @quad) = @_; - warnings::warn "6-ARG sockaddr_in call is deprecated" - if warnings::enabled(); - pack_sockaddr_in($port, inet_aton(join('.', @quad))); - } elsif (wantarray) { - croak "usage: (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1; - unpack_sockaddr_in(@_); - } else { - croak "usage: sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2; - pack_sockaddr_in(@_); - } -} - -sub sockaddr_in6 { - if (wantarray) { - croak "usage: (port,in6addr,scope_id,flowinfo) = sockaddr_in6(sin6_sv)" unless @_ == 1; - unpack_sockaddr_in6(@_); - } - else { - croak "usage: sin6_sv = sockaddr_in6(port,in6addr,[scope_id,[flowinfo]])" unless @_ >= 2 and @_ <= 4; - pack_sockaddr_in6(@_); - } -} - -sub sockaddr_un { - if (wantarray) { - croak "usage: (filename) = sockaddr_un(sun_sv)" unless @_ == 1; - unpack_sockaddr_un(@_); - } else { - croak "usage: sun_sv = sockaddr_un(filename)" unless @_ == 1; - pack_sockaddr_un(@_); - } -} - -XSLoader::load(__PACKAGE__, $VERSION); - -my %errstr; - -if( defined &getaddrinfo ) { - # These are not part of the API, nothing uses them, and deleting them - # reduces the size of %Socket:: by about 12K - delete $Socket::{fake_getaddrinfo}; - delete $Socket::{fake_getnameinfo}; -} else { - require Scalar::Util; - - *getaddrinfo = \&fake_getaddrinfo; - *getnameinfo = \&fake_getnameinfo; - - # These numbers borrowed from GNU libc's implementation, but since - # they're only used by our emulation, it doesn't matter if the real - # platform's values differ - my %constants = ( - AI_PASSIVE => 1, - AI_CANONNAME => 2, - AI_NUMERICHOST => 4, - # RFC 2553 doesn't define this but Linux does - lets be nice and - # provide it since we can - AI_NUMERICSERV => 1024, - - EAI_BADFLAGS => -1, - EAI_NONAME => -2, - EAI_NODATA => -5, - EAI_FAMILY => -6, - EAI_SERVICE => -8, - - NI_NUMERICHOST => 1, - NI_NUMERICSERV => 2, - NI_NAMEREQD => 8, - NI_DGRAM => 16, - ); - - foreach my $name ( keys %constants ) { - my $value = $constants{$name}; - - no strict 'refs'; - defined &$name or *$name = sub () { $value }; - } - - %errstr = ( - # These strings from RFC 2553 - EAI_BADFLAGS() => "invalid value for ai_flags", - EAI_NONAME() => "nodename nor servname provided, or not known", - EAI_NODATA() => "no address associated with nodename", - EAI_FAMILY() => "ai_family not supported", - EAI_SERVICE() => "servname not supported for ai_socktype", - ); -} - -# The following functions are used if the system does not have a -# getaddrinfo(3) function in libc; and are used to emulate it for the AF_INET -# family - -# Borrowed from Regexp::Common::net -my $REGEXP_IPv4_DECIMAL = qr/25[0-5]|2[0-4][0-9]|1?[0-9][0-9]{1,2}/; -my $REGEXP_IPv4_DOTTEDQUAD = qr/$REGEXP_IPv4_DECIMAL\.$REGEXP_IPv4_DECIMAL\.$REGEXP_IPv4_DECIMAL\.$REGEXP_IPv4_DECIMAL/; - -sub fake_makeerr -{ - my ( $errno ) = @_; - my $errstr = $errno == 0 ? "" : ( $errstr{$errno} || $errno ); - return Scalar::Util::dualvar( $errno, $errstr ); -} - -sub fake_getaddrinfo -{ - my ( $node, $service, $hints ) = @_; - - $node = "" unless defined $node; - - $service = "" unless defined $service; - - my ( $family, $socktype, $protocol, $flags ) = @$hints{qw( family socktype protocol flags )}; - - $family ||= Socket::AF_INET(); # 0 == AF_UNSPEC, which we want too - $family == Socket::AF_INET() or return fake_makeerr( EAI_FAMILY() ); - - $socktype ||= 0; - - $protocol ||= 0; - - $flags ||= 0; - - my $flag_passive = $flags & AI_PASSIVE(); $flags &= ~AI_PASSIVE(); - my $flag_canonname = $flags & AI_CANONNAME(); $flags &= ~AI_CANONNAME(); - my $flag_numerichost = $flags & AI_NUMERICHOST(); $flags &= ~AI_NUMERICHOST(); - my $flag_numericserv = $flags & AI_NUMERICSERV(); $flags &= ~AI_NUMERICSERV(); - - $flags == 0 or return fake_makeerr( EAI_BADFLAGS() ); - - $node eq "" and $service eq "" and return fake_makeerr( EAI_NONAME() ); - - my $canonname; - my @addrs; - if( $node ne "" ) { - return fake_makeerr( EAI_NONAME() ) if( $flag_numerichost and $node !~ m/^$REGEXP_IPv4_DOTTEDQUAD$/ ); - ( $canonname, undef, undef, undef, @addrs ) = gethostbyname( $node ); - defined $canonname or return fake_makeerr( EAI_NONAME() ); - - undef $canonname unless $flag_canonname; - } - else { - $addrs[0] = $flag_passive ? Socket::inet_aton( "0.0.0.0" ) - : Socket::inet_aton( "127.0.0.1" ); - } - - my @ports; # Actually ARRAYrefs of [ socktype, protocol, port ] - my $protname = ""; - if( $protocol ) { - $protname = getprotobynumber( $protocol ); - } - - if( $service ne "" and $service !~ m/^\d+$/ ) { - return fake_makeerr( EAI_NONAME() ) if( $flag_numericserv ); - getservbyname( $service, $protname ) or return fake_makeerr( EAI_SERVICE() ); - } - - foreach my $this_socktype ( Socket::SOCK_STREAM(), Socket::SOCK_DGRAM(), Socket::SOCK_RAW() ) { - next if $socktype and $this_socktype != $socktype; - - my $this_protname = "raw"; - $this_socktype == Socket::SOCK_STREAM() and $this_protname = "tcp"; - $this_socktype == Socket::SOCK_DGRAM() and $this_protname = "udp"; - - next if $protname and $this_protname ne $protname; - - my $port; - if( $service ne "" ) { - if( $service =~ m/^\d+$/ ) { - $port = "$service"; - } - else { - ( undef, undef, $port, $this_protname ) = getservbyname( $service, $this_protname ); - next unless defined $port; - } - } - else { - $port = 0; - } - - push @ports, [ $this_socktype, scalar getprotobyname( $this_protname ) || 0, $port ]; - } - - my @ret; - foreach my $addr ( @addrs ) { - foreach my $portspec ( @ports ) { - my ( $socktype, $protocol, $port ) = @$portspec; - push @ret, { - family => $family, - socktype => $socktype, - protocol => $protocol, - addr => Socket::pack_sockaddr_in( $port, $addr ), - canonname => undef, - }; - } - } - - # Only supply canonname for the first result - if( defined $canonname ) { - $ret[0]->{canonname} = $canonname; - } - - return ( fake_makeerr( 0 ), @ret ); -} - -sub fake_getnameinfo -{ - my ( $addr, $flags ) = @_; - - my ( $port, $inetaddr ); - eval { ( $port, $inetaddr ) = Socket::unpack_sockaddr_in( $addr ) } - or return fake_makeerr( EAI_FAMILY() ); - - my $family = Socket::AF_INET(); - - $flags ||= 0; - - my $flag_numerichost = $flags & NI_NUMERICHOST(); $flags &= ~NI_NUMERICHOST(); - my $flag_numericserv = $flags & NI_NUMERICSERV(); $flags &= ~NI_NUMERICSERV(); - my $flag_namereqd = $flags & NI_NAMEREQD(); $flags &= ~NI_NAMEREQD(); - my $flag_dgram = $flags & NI_DGRAM() ; $flags &= ~NI_DGRAM(); - - $flags == 0 or return fake_makeerr( EAI_BADFLAGS() ); - - my $node; - if( $flag_numerichost ) { - $node = Socket::inet_ntoa( $inetaddr ); - } - else { - $node = gethostbyaddr( $inetaddr, $family ); - if( !defined $node ) { - return fake_makeerr( EAI_NONAME() ) if $flag_namereqd; - $node = Socket::inet_ntoa( $inetaddr ); - } - } - - my $service; - if( $flag_numericserv ) { - $service = "$port"; - } - else { - my $protname = $flag_dgram ? "udp" : ""; - $service = getservbyport( $port, $protname ); - if( !defined $service ) { - $service = "$port"; - } - } - - return ( fake_makeerr( 0 ), $node, $service ); -} - -1; diff --git a/ext/Socket/Socket.xs b/ext/Socket/Socket.xs deleted file mode 100644 index d7e7a11eb6..0000000000 --- a/ext/Socket/Socket.xs +++ /dev/null @@ -1,851 +0,0 @@ -#define PERL_NO_GET_CONTEXT -#include "EXTERN.h" -#include "perl.h" -#include "XSUB.h" - -#include - -#ifdef I_SYS_TYPES -# include -#endif -#if !defined(ultrix) /* Avoid double definition. */ -# include -#endif -#if defined(USE_SOCKS) && defined(I_SOCKS) -# include -#endif -#ifdef MPE -# define PF_INET AF_INET -# define PF_UNIX AF_UNIX -# define SOCK_RAW 3 -#endif -#ifdef I_SYS_UN -# include -#endif -/* XXX Configure test for -#endif -#if defined(__sgi) && !defined(AF_LINK) && defined(PF_LINK) && PF_LINK == AF_LNK -# undef PF_LINK -#endif -#if defined(I_NETINET_IN) || defined(__ultrix__) -# include -#endif -#ifdef I_NETDB -# if !defined(ultrix) /* Avoid double definition. */ -# include -# endif -#endif -#ifdef I_ARPA_INET -# include -#endif -#ifdef I_NETINET_TCP -# include -#endif - -#ifdef NETWARE -NETDB_DEFINE_CONTEXT -NETINET_DEFINE_CONTEXT -#endif - -#ifdef I_SYSUIO -# include -#endif - -#ifndef AF_NBS -# undef PF_NBS -#endif - -#ifndef AF_X25 -# undef PF_X25 -#endif - -#ifndef INADDR_NONE -# define INADDR_NONE 0xffffffff -#endif /* INADDR_NONE */ -#ifndef INADDR_BROADCAST -# define INADDR_BROADCAST 0xffffffff -#endif /* INADDR_BROADCAST */ -#ifndef INADDR_LOOPBACK -# define INADDR_LOOPBACK 0x7F000001 -#endif /* INADDR_LOOPBACK */ - -#ifndef croak_sv -# define croak_sv(sv) croak(SvPV_nolen(sv)) -#endif - -#ifndef HAS_INET_ATON - -/* - * Check whether "cp" is a valid ascii representation - * of an Internet address and convert to a binary address. - * Returns 1 if the address is valid, 0 if not. - * This replaces inet_addr, the return value from which - * cannot distinguish between failure and a local broadcast address. - */ -static int -my_inet_aton(register const char *cp, struct in_addr *addr) -{ - dTHX; - register U32 val; - register int base; - register char c; - int nparts; - const char *s; - unsigned int parts[4]; - register unsigned int *pp = parts; - - if (!cp || !*cp) - return 0; - for (;;) { - /* - * Collect number up to ".". - * Values are specified as for C: - * 0x=hex, 0=octal, other=decimal. - */ - val = 0; base = 10; - if (*cp == '0') { - if (*++cp == 'x' || *cp == 'X') - base = 16, cp++; - else - base = 8; - } - while ((c = *cp) != '\0') { - if (isDIGIT(c)) { - val = (val * base) + (c - '0'); - cp++; - continue; - } - if (base == 16 && (s=strchr(PL_hexdigit,c))) { - val = (val << 4) + - ((s - PL_hexdigit) & 15); - cp++; - continue; - } - break; - } - if (*cp == '.') { - /* - * Internet format: - * a.b.c.d - * a.b.c (with c treated as 16-bits) - * a.b (with b treated as 24 bits) - */ - if (pp >= parts + 3 || val > 0xff) - return 0; - *pp++ = val, cp++; - } else - break; - } - /* - * Check for trailing characters. - */ - if (*cp && !isSPACE(*cp)) - return 0; - /* - * Concoct the address according to - * the number of parts specified. - */ - nparts = pp - parts + 1; /* force to an int for switch() */ - switch (nparts) { - - case 1: /* a -- 32 bits */ - break; - - case 2: /* a.b -- 8.24 bits */ - if (val > 0xffffff) - return 0; - val |= parts[0] << 24; - break; - - case 3: /* a.b.c -- 8.8.16 bits */ - if (val > 0xffff) - return 0; - val |= (parts[0] << 24) | (parts[1] << 16); - break; - - case 4: /* a.b.c.d -- 8.8.8.8 bits */ - if (val > 0xff) - return 0; - val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); - break; - } - addr->s_addr = htonl(val); - return 1; -} - -#undef inet_aton -#define inet_aton my_inet_aton - -#endif /* ! HAS_INET_ATON */ - - -static int -not_here(const char *s) -{ - croak("Socket::%s not implemented on this architecture", s); - return -1; -} - -#define PERL_IN_ADDR_S_ADDR_SIZE 4 - -/* -* Bad assumptions possible here. -* -* Bad Assumption 1: struct in_addr has no other fields -* than the s_addr (which is the field we care about -* in here, really). However, we can be fed either 4-byte -* addresses (from pack("N", ...), or va.b.c.d, or ...), -* or full struct in_addrs (from e.g. pack_sockaddr_in()), -* which may or may not be 4 bytes in size. -* -* Bad Assumption 2: the s_addr field is a simple type -* (such as an int, u_int32_t). It can be a bit field, -* in which case using & (address-of) on it or taking sizeof() -* wouldn't go over too well. (Those are not attempted -* now but in case someone thinks to change the below code -* to use addr.s_addr instead of addr, you have been warned.) -* -* Bad Assumption 3: the s_addr is the first field in -* an in_addr, or that its bytes are the first bytes in -* an in_addr. -* -* These bad assumptions are wrong in UNICOS which has -* struct in_addr { struct { u_long st_addr:32; } s_da }; -* #define s_addr s_da.st_addr -* and u_long is 64 bits. -* -* --jhi */ - -#include "const-c.inc" - -#ifdef HAS_GETADDRINFO -static SV *err_to_SV(pTHX_ int err) -{ - SV *ret = sv_newmortal(); - SvUPGRADE(ret, SVt_PVNV); - - if(err) { - const char *error = gai_strerror(err); - sv_setpv(ret, error); - } - else { - sv_setpv(ret, ""); - } - - SvIV_set(ret, err); SvIOK_on(ret); - - return ret; -} - -static void xs_getaddrinfo(pTHX_ CV *cv) -{ - dVAR; - dXSARGS; - - SV *host; - SV *service; - SV *hints; - - char *hostname = NULL; - char *servicename = NULL; - STRLEN len; - struct addrinfo hints_s; - struct addrinfo *res; - struct addrinfo *res_iter; - int err; - int n_res; - - if(items > 3) - croak_xs_usage(cv, "host, service, hints"); - - SP -= items; - - if(items < 1) - host = &PL_sv_undef; - else - host = ST(0); - - if(items < 2) - service = &PL_sv_undef; - else - service = ST(1); - - if(items < 3) - hints = NULL; - else - hints = ST(2); - - SvGETMAGIC(host); - if(SvOK(host)) { - hostname = SvPV_nomg(host, len); - if (!len) - hostname = NULL; - } - - SvGETMAGIC(service); - if(SvOK(service)) { - servicename = SvPV_nomg(service, len); - if (!len) - servicename = NULL; - } - - Zero(&hints_s, sizeof hints_s, char); - hints_s.ai_family = PF_UNSPEC; - - if(hints && SvOK(hints)) { - HV *hintshash; - SV **valp; - - if(!SvROK(hints) || SvTYPE(SvRV(hints)) != SVt_PVHV) - croak("hints is not a HASH reference"); - - hintshash = (HV*)SvRV(hints); - - if((valp = hv_fetch(hintshash, "flags", 5, 0)) != NULL) - hints_s.ai_flags = SvIV(*valp); - if((valp = hv_fetch(hintshash, "family", 6, 0)) != NULL) - hints_s.ai_family = SvIV(*valp); - if((valp = hv_fetch(hintshash, "socktype", 8, 0)) != NULL) - hints_s.ai_socktype = SvIV(*valp); - if((valp = hv_fetch(hintshash, "protocol", 8, 0)) != NULL) - hints_s.ai_protocol = SvIV(*valp); - } - - err = getaddrinfo(hostname, servicename, &hints_s, &res); - - XPUSHs(err_to_SV(aTHX_ err)); - - if(err) - XSRETURN(1); - - n_res = 0; - for(res_iter = res; res_iter; res_iter = res_iter->ai_next) { - HV *res_hv = newHV(); - - (void)hv_stores(res_hv, "family", newSViv(res_iter->ai_family)); - (void)hv_stores(res_hv, "socktype", newSViv(res_iter->ai_socktype)); - (void)hv_stores(res_hv, "protocol", newSViv(res_iter->ai_protocol)); - - (void)hv_stores(res_hv, "addr", newSVpvn((char*)res_iter->ai_addr, res_iter->ai_addrlen)); - - if(res_iter->ai_canonname) - (void)hv_stores(res_hv, "canonname", newSVpv(res_iter->ai_canonname, 0)); - else - (void)hv_stores(res_hv, "canonname", newSV(0)); - - XPUSHs(sv_2mortal(newRV_noinc((SV*)res_hv))); - n_res++; - } - - freeaddrinfo(res); - - XSRETURN(1 + n_res); -} -#endif - -#ifdef HAS_GETNAMEINFO -static void xs_getnameinfo(pTHX_ CV *cv) -{ - dVAR; - dXSARGS; - - SV *addr; - int flags; - - char host[1024]; - char serv[256]; - char *sa; /* we'll cast to struct sockaddr * when necessary */ - STRLEN addr_len; - int err; - - if(items < 1 || items > 2) - croak_xs_usage(cv, "addr, flags=0"); - - SP -= items; - - addr = ST(0); - - if(items < 2) - flags = 0; - else - flags = SvIV(ST(1)); - - if(!SvPOK(addr)) - croak("addr is not a string"); - - addr_len = SvCUR(addr); - - /* We need to ensure the sockaddr is aligned, because a random SvPV might - * not be due to SvOOK */ - Newx(sa, addr_len, char); - Copy(SvPV_nolen(addr), sa, addr_len, char); -#ifdef HAS_SOCKADDR_SA_LEN - ((struct sockaddr *)sa)->sa_len = addr_len; -#endif - - err = getnameinfo((struct sockaddr *)sa, addr_len, - host, sizeof(host), - serv, sizeof(serv), - flags); - - Safefree(sa); - - XPUSHs(err_to_SV(aTHX_ err)); - - if(err) - XSRETURN(1); - - XPUSHs(sv_2mortal(newSVpv(host, 0))); - XPUSHs(sv_2mortal(newSVpv(serv, 0))); - - XSRETURN(3); -} -#endif - -MODULE = Socket PACKAGE = Socket - -INCLUDE: const-xs.inc - -BOOT: -#ifdef HAS_GETADDRINFO - newXS("Socket::getaddrinfo", xs_getaddrinfo, __FILE__); -#endif -#ifdef HAS_GETNAMEINFO - newXS("Socket::getnameinfo", xs_getnameinfo, __FILE__); -#endif - -void -inet_aton(host) - char * host - CODE: - { - struct in_addr ip_address; - struct hostent * phe; - - if ((*host != '\0') && inet_aton(host, &ip_address)) { - ST(0) = newSVpvn_flags((char *)&ip_address, sizeof ip_address, SVs_TEMP); - XSRETURN(1); - } - - phe = gethostbyname(host); - if (phe && phe->h_addrtype == AF_INET && phe->h_length == 4) { - ST(0) = newSVpvn_flags((char *)phe->h_addr, phe->h_length, SVs_TEMP); - XSRETURN(1); - } - - XSRETURN_UNDEF; - } - -void -inet_ntoa(ip_address_sv) - SV * ip_address_sv - CODE: - { - STRLEN addrlen; - struct in_addr addr; - char * ip_address; - if (DO_UTF8(ip_address_sv) && !sv_utf8_downgrade(ip_address_sv, 1)) - croak("Wide character in %s", "Socket::inet_ntoa"); - ip_address = SvPVbyte(ip_address_sv, addrlen); - if (addrlen == sizeof(addr) || addrlen == 4) - addr.s_addr = - (ip_address[0] & 0xFF) << 24 | - (ip_address[1] & 0xFF) << 16 | - (ip_address[2] & 0xFF) << 8 | - (ip_address[3] & 0xFF); - else - croak("Bad arg length for %s, length is %d, should be %d", - "Socket::inet_ntoa", - addrlen, sizeof(addr)); - /* We could use inet_ntoa() but that is broken - * in HP-UX + GCC + 64bitint (returns "0.0.0.0"), - * so let's use this sprintf() workaround everywhere. - * This is also more threadsafe than using inet_ntoa(). */ - ST(0) = sv_2mortal(Perl_newSVpvf(aTHX_ "%d.%d.%d.%d", /* IPv6? */ - ((addr.s_addr >> 24) & 0xFF), - ((addr.s_addr >> 16) & 0xFF), - ((addr.s_addr >> 8) & 0xFF), - ( addr.s_addr & 0xFF))); - } - -void -sockaddr_family(sockaddr) - SV * sockaddr - PREINIT: - STRLEN sockaddr_len; - char *sockaddr_pv = SvPVbyte(sockaddr, sockaddr_len); - CODE: - if (sockaddr_len < offsetof(struct sockaddr, sa_data)) { - croak("Bad arg length for %s, length is %d, should be at least %d", - "Socket::sockaddr_family", sockaddr_len, - offsetof(struct sockaddr, sa_data)); - } - ST(0) = sv_2mortal(newSViv(((struct sockaddr*)sockaddr_pv)->sa_family)); - -void -pack_sockaddr_un(pathname) - SV * pathname - CODE: - { -#ifdef I_SYS_UN - struct sockaddr_un sun_ad; /* fear using sun */ - STRLEN len; - char * pathname_pv; - int addr_len; - - Zero( &sun_ad, sizeof sun_ad, char ); - sun_ad.sun_family = AF_UNIX; - pathname_pv = SvPV(pathname,len); - if (len > sizeof(sun_ad.sun_path)) - len = sizeof(sun_ad.sun_path); -# ifdef OS2 /* Name should start with \socket\ and contain backslashes! */ - { - int off; - char *s, *e; - - if (pathname_pv[0] != '/' && pathname_pv[0] != '\\') - croak("Relative UNIX domain socket name '%s' unsupported", - pathname_pv); - else if (len < 8 - || pathname_pv[7] != '/' && pathname_pv[7] != '\\' - || !strnicmp(pathname_pv + 1, "socket", 6)) - off = 7; - else - off = 0; /* Preserve names starting with \socket\ */ - Copy( "\\socket", sun_ad.sun_path, off, char); - Copy( pathname_pv, sun_ad.sun_path + off, len, char ); - - s = sun_ad.sun_path + off - 1; - e = s + len + 1; - while (++s < e) - if (*s = '/') - *s = '\\'; - } -# else /* !( defined OS2 ) */ - Copy( pathname_pv, sun_ad.sun_path, len, char ); -# endif - if (0) not_here("dummy"); - if (len > 1 && sun_ad.sun_path[0] == '\0') { - /* Linux-style abstract-namespace socket. - * The name is not a file name, but an array of arbitrary - * character, starting with \0 and possibly including \0s, - * therefore the length of the structure must denote the - * end of that character array */ - addr_len = (char *)&(sun_ad.sun_path) - (char *)&sun_ad + len; - } else { - addr_len = sizeof sun_ad; - } -# ifdef HAS_SOCKADDR_SA_LEN - sun_ad.sun_len = addr_len; -# endif - ST(0) = newSVpvn_flags((char *)&sun_ad, addr_len, SVs_TEMP); -#else - ST(0) = (SV *) not_here("pack_sockaddr_un"); -#endif - - } - -void -unpack_sockaddr_un(sun_sv) - SV * sun_sv - CODE: - { -#ifdef I_SYS_UN - struct sockaddr_un addr; - STRLEN sockaddrlen; - char * sun_ad = SvPVbyte(sun_sv,sockaddrlen); - int addr_len; -# ifndef __linux__ - /* On Linux sockaddrlen on sockets returned by accept, recvfrom, - getpeername and getsockname is not equal to sizeof(addr). */ - if (sockaddrlen != sizeof(addr)) { - croak("Bad arg length for %s, length is %d, should be %d", - "Socket::unpack_sockaddr_un", - sockaddrlen, sizeof(addr)); - } -# endif - - Copy( sun_ad, &addr, sizeof addr, char ); - - if ( addr.sun_family != AF_UNIX ) { - croak("Bad address family for %s, got %d, should be %d", - "Socket::unpack_sockaddr_un", - addr.sun_family, - AF_UNIX); - } - - if (addr.sun_path[0] == '\0') { - /* Linux-style abstract socket address begins with a nul - * and can contain nuls. */ - addr_len = (char *)&addr - (char *)&(addr.sun_path) + sockaddrlen; - } else { - for (addr_len = 0; addr.sun_path[addr_len] - && addr_len < (int)sizeof(addr.sun_path); addr_len++); - } - - ST(0) = newSVpvn_flags(addr.sun_path, addr_len, SVs_TEMP); -#else - ST(0) = (SV *) not_here("unpack_sockaddr_un"); -#endif - } - -void -pack_sockaddr_in(port, ip_address_sv) - unsigned short port - SV * ip_address_sv - CODE: - { - struct sockaddr_in sin; - struct in_addr addr; - STRLEN addrlen; - char * ip_address; - if (DO_UTF8(ip_address_sv) && !sv_utf8_downgrade(ip_address_sv, 1)) - croak("Wide character in %s", "Socket::pack_sockaddr_in"); - ip_address = SvPVbyte(ip_address_sv, addrlen); - if (addrlen == sizeof(addr) || addrlen == 4) - addr.s_addr = - (ip_address[0] & 0xFF) << 24 | - (ip_address[1] & 0xFF) << 16 | - (ip_address[2] & 0xFF) << 8 | - (ip_address[3] & 0xFF); - else - croak("Bad arg length for %s, length is %d, should be %d", - "Socket::pack_sockaddr_in", - addrlen, sizeof(addr)); - Zero( &sin, sizeof sin, char ); - sin.sin_family = AF_INET; - sin.sin_port = htons(port); - sin.sin_addr.s_addr = htonl(addr.s_addr); -# ifdef HAS_SOCKADDR_SA_LEN - sin.sin_len = sizeof (sin); -# endif - ST(0) = newSVpvn_flags((char *)&sin, sizeof (sin), SVs_TEMP); - } - -void -unpack_sockaddr_in(sin_sv) - SV * sin_sv - PPCODE: - { - STRLEN sockaddrlen; - struct sockaddr_in addr; - unsigned short port; - struct in_addr ip_address; - char * sin = SvPVbyte(sin_sv,sockaddrlen); - if (sockaddrlen != sizeof(addr)) { - croak("Bad arg length for %s, length is %d, should be %d", - "Socket::unpack_sockaddr_in", - sockaddrlen, sizeof(addr)); - } - Copy( sin, &addr,sizeof addr, char ); - if ( addr.sin_family != AF_INET ) { - croak("Bad address family for %s, got %d, should be %d", - "Socket::unpack_sockaddr_in", - addr.sin_family, - AF_INET); - } - port = ntohs(addr.sin_port); - ip_address = addr.sin_addr; - - EXTEND(SP, 2); - PUSHs(sv_2mortal(newSViv((IV) port))); - PUSHs(newSVpvn_flags((char *)&ip_address, sizeof(ip_address), SVs_TEMP)); - } - -void -pack_sockaddr_in6(port, sin6_addr, scope_id=0, flowinfo=0) - unsigned short port - SV * sin6_addr - unsigned long scope_id - unsigned long flowinfo - CODE: - { -#ifdef AF_INET6 - struct sockaddr_in6 sin6; - char * addrbytes; - STRLEN addrlen; - if (DO_UTF8(sin6_addr) && !sv_utf8_downgrade(sin6_addr, 1)) - croak("Wide character in %s", "Socket::pack_sockaddr_in6"); - addrbytes = SvPVbyte(sin6_addr, addrlen); - if(addrlen != sizeof(sin6.sin6_addr)) - croak("Bad arg length %s, length is %d, should be %d", - "Socket::pack_sockaddr_in6", addrlen, sizeof(sin6.sin6_addr)); - Zero(&sin6, sizeof(sin6), char); - sin6.sin6_family = AF_INET6; - sin6.sin6_port = htons(port); - sin6.sin6_flowinfo = htonl(flowinfo); - Copy(addrbytes, &sin6.sin6_addr, sizeof(sin6.sin6_addr), char); -# ifdef HAS_SIN6_SCOPE_ID - sin6.sin6_scope_id = scope_id; -# else - if(scope_id != 0) - warn("%s cannot represent non-zero scope_id %d", - "Socket::pack_sockaddr_in6", scope_id); -# endif -# ifdef HAS_SOCKADDR_SA_LEN - sin6.sin6_len = sizeof(sin6); -# endif - ST(0) = newSVpvn_flags((char *)&sin6, sizeof(sin6), SVs_TEMP); -#else - ST(0) = (SV*)not_here("pack_sockaddr_in6"); -#endif - } - -void -unpack_sockaddr_in6(sin6_sv) - SV * sin6_sv - PPCODE: - { -#ifdef AF_INET6 - STRLEN addrlen; - struct sockaddr_in6 sin6; - char * addrbytes = SvPVbyte(sin6_sv, addrlen); - if (addrlen != sizeof(sin6)) - croak("Bad arg length for %s, length is %d, should be %d", - "Socket::unpack_sockaddr_in6", - addrlen, sizeof(sin6)); - Copy(addrbytes, &sin6, sizeof(sin6), char); - if(sin6.sin6_family != AF_INET6) - croak("Bad address family for %s, got %d, should be %d", - "Socket::unpack_sockaddr_in6", - sin6.sin6_family, AF_INET6); - EXTEND(SP, 4); - mPUSHi(ntohs(sin6.sin6_port)); - mPUSHp((char *)&sin6.sin6_addr, sizeof(sin6.sin6_addr)); -# ifdef HAS_SIN6_SCOPE_ID - mPUSHi(sin6.sin6_scope_id); -# else - mPUSHi(0); -# endif - mPUSHi(ntohl(sin6.sin6_flowinfo)); -#else - ST(0) = (SV*)not_here("pack_sockaddr_in6"); -#endif - } - -void -inet_ntop(af, ip_address_sv) - int af - SV * ip_address_sv - CODE: -#ifdef HAS_INETNTOP - STRLEN addrlen, struct_size; -#ifdef AF_INET6 - struct in6_addr addr; - char str[INET6_ADDRSTRLEN]; -#else - struct in_addr addr; - char str[INET_ADDRSTRLEN]; -#endif - char *ip_address = SvPV(ip_address_sv, addrlen); - - struct_size = sizeof(addr); - - if(af != AF_INET -#ifdef AF_INET6 - && af != AF_INET6 -#endif - ) { - croak("Bad address family for %s, got %d, should be" -#ifdef AF_INET6 - " either AF_INET or AF_INET6", -#else - " AF_INET", -#endif - "Socket::inet_ntop", - af); - } - - Copy( ip_address, &addr, sizeof addr, char ); - inet_ntop(af, &addr, str, sizeof str); - - ST(0) = newSVpvn_flags(str, strlen(str), SVs_TEMP); -#else - ST(0) = (SV *)not_here("inet_ntop"); -#endif - -void -inet_pton(af, host) - int af - const char * host - CODE: -#ifdef HAS_INETPTON - int ok; -#ifdef AF_INET6 - struct in6_addr ip_address; -#else - struct in_addr ip_address; -#endif - - if(af != AF_INET -#ifdef AF_INET6 - && af != AF_INET6 -#endif - ) { - croak("Bad address family for %s, got %d, should be" -#ifdef AF_INET6 - " either AF_INET or AF_INET6", -#else - " AF_INET", -#endif - "Socket::inet_pton", - af); - } - ok = (*host != '\0') && inet_pton(af, host, &ip_address); - - ST(0) = sv_newmortal(); - if (ok) { - sv_setpvn( ST(0), (char *)&ip_address, sizeof(ip_address) ); - } -#else - ST(0) = (SV *)not_here("inet_pton"); -#endif - -void -pack_ipv6_mreq(addr, interface) - SV * addr - unsigned int interface - CODE: - { -#ifdef AF_INET6 - struct ipv6_mreq mreq; - char * addrbytes; - STRLEN addrlen; - if (DO_UTF8(addr) && !sv_utf8_downgrade(addr, 1)) - croak("Wide character in %s", "Socket::pack_ipv6_mreq"); - addrbytes = SvPVbyte(addr, addrlen); - if(addrlen != sizeof(mreq.ipv6mr_multiaddr)) - croak("Bad arg length %s, length is %d, should be %d", - "Socket::pack_ipv6_mreq", addrlen, sizeof(mreq.ipv6mr_multiaddr)); - Zero(&mreq, sizeof(mreq), char); - Copy(addrbytes, &mreq.ipv6mr_multiaddr, sizeof(mreq.ipv6mr_multiaddr), char); - mreq.ipv6mr_interface = interface; - ST(0) = newSVpvn_flags((char *)&mreq, sizeof(mreq), SVs_TEMP); -#else - ST(0) = (SV*)not_here("pack_ipv6_mreq"); -#endif - } - -void -unpack_ipv6_mreq(mreq_sv) - SV * mreq_sv - PPCODE: - { -#ifdef AF_INET6 - struct ipv6_mreq mreq; - STRLEN mreqlen; - char * mreqbytes = SvPVbyte(mreq_sv, mreqlen); - if (mreqlen != sizeof(mreq)) - croak("Bad arg length for %s, length is %d, should be %d", - "Socket::unpack_ipv6_mreq", - mreqlen, sizeof(mreq)); - Copy(mreqbytes, &mreq, sizeof(mreq), char); - EXTEND(SP, 2); - mPUSHp((char *)&mreq.ipv6mr_multiaddr, sizeof(mreq.ipv6mr_multiaddr)); - mPUSHi(mreq.ipv6mr_interface); -#else - not_here("unpack_ipv6_mreq"); -#endif - } diff --git a/ext/Socket/t/Socket.t b/ext/Socket/t/Socket.t deleted file mode 100644 index be15c01fa1..0000000000 --- a/ext/Socket/t/Socket.t +++ /dev/null @@ -1,184 +0,0 @@ -#!./perl - -BEGIN { - require Config; import Config; - if ($Config{'extensions'} !~ /\bSocket\b/ && - !(($^O eq 'VMS') && $Config{d_socket})) { - print "1..0\n"; - exit 0; - } - $has_alarm = $Config{d_alarm}; -} - -use Socket qw(:all); -use Test::More tests => 26; - -$has_echo = $^O ne 'MSWin32'; -$alarmed = 0; -sub arm { $alarmed = 0; alarm(shift) if $has_alarm } -sub alarmed { $alarmed = 1 } -$SIG{ALRM} = 'alarmed' if $has_alarm; - -SKIP: { - unless(socket(T, PF_INET, SOCK_STREAM, IPPROTO_TCP)) { - skip "No PF_INET", 3; - } - - pass "socket(PF_INET)"; - - arm(5); - my $host = $^O eq 'MacOS' || ($^O eq 'irix' && $Config{osvers} == 5) ? - '127.0.0.1' : 'localhost'; - my $localhost = inet_aton($host); - - SKIP: { - unless($has_echo && defined $localhost && connect(T,pack_sockaddr_in(7,$localhost))) { - skip "Unable to connect to localhost:7", 2; - } - - arm(0); - - pass "PF_INET echo localhost connected"; - - diag "Connected to " . - inet_ntoa((unpack_sockaddr_in(getpeername(T)))[1])."\n"; - - arm(5); - syswrite(T,"hello",5); - arm(0); - - arm(5); - $read = sysread(T,$buff,10); # Connection may be granted, then closed! - arm(0); - - while ($read > 0 && length($buff) < 5) { - # adjust for fact that TCP doesn't guarantee size of reads/writes - arm(5); - $read = sysread(T,$buff,10,length($buff)); - arm(0); - } - - is(($read == 0 || $buff eq "hello"), "PF_INET echo localhost reply"); - } -} - -SKIP: { - unless(socket(S, PF_INET, SOCK_STREAM, IPPROTO_TCP)) { - skip "No PF_INET", 3; - } - - pass "socket(PF_INET)"; - - SKIP: { - arm(5); - unless($has_echo && connect(S,pack_sockaddr_in(7,INADDR_LOOPBACK))) { - skip "Unable to connect to localhost:7", 2; - } - - arm(0); - - pass "PF_INET echo INADDR_LOOPBACK connected"; - - diag "Connected to " . - inet_ntoa((unpack_sockaddr_in(getpeername(S)))[1])."\n"; - - arm(5); - syswrite(S,"olleh",5); - arm(0); - - arm(5); - $read = sysread(S,$buff,10); # Connection may be granted, then closed! - arm(0); - - while ($read > 0 && length($buff) < 5) { - # adjust for fact that TCP doesn't guarantee size of reads/writes - arm(5); - $read = sysread(S,$buff,10,length($buff)); - arm(0); - } - - is(($read == 0 || $buff eq "olleh"), "PF_INET echo INADDR_LOOPBACK reply"); - } -} - -# warnings -{ - my $w = 0; - local $SIG{__WARN__} = sub { - ++ $w if $_[0] =~ /^6-ARG sockaddr_in call is deprecated/ ; - }; - - no warnings 'Socket'; - sockaddr_in(1,2,3,4,5,6) ; - is($w, 0, "sockaddr_in deprecated form doesn't warn without lexical warnings"); - - use warnings 'Socket'; - sockaddr_in(1,2,3,4,5,6) ; - is($w, 1, "sockaddr_in deprecated form warns with lexical warnings"); -} - -# Test that whatever we give into pack/unpack_sockaddr retains -# the value thru the entire chain. -is(inet_ntoa((unpack_sockaddr_in(pack_sockaddr_in(100,inet_aton("10.250.230.10"))))[1]), '10.250.230.10', - 'inet_aton->pack_sockaddr_in->unpack_sockaddr_in->inet_ntoa roundtrip'); - -is(inet_ntoa(inet_aton("10.20.30.40")), "10.20.30.40", 'inet_aton->inet_ntoa roundtrip'); -is(inet_ntoa(v10.20.30.40), "10.20.30.40", 'inet_ntoa from v-string'); - -{ - my ($port,$addr) = unpack_sockaddr_in(pack_sockaddr_in(100,v10.10.10.10)); - is($port, 100, 'pack_sockaddr_in->unpack_sockaddr_in port'); - is(inet_ntoa($addr), "10.10.10.10", 'pack_sockaddr_in->unpack_sockaddr_in addr'); -} - -{ - local $@; - eval { inet_ntoa(v10.20.30.400) }; - like($@, qr/^Wide character in Socket::inet_ntoa at/, 'inet_ntoa warns about wide characters'); -} - -is(sockaddr_family(pack_sockaddr_in(100,inet_aton("10.250.230.10"))), AF_INET, 'pack_sockaddr_in->sockaddr_family'); - -{ - local $@; - eval { sockaddr_family("") }; - like($@, qr/^Bad arg length for Socket::sockaddr_family, length is 0, should be at least \d+/, 'sockaddr_family warns about argument length'); -} - -SKIP: { - # see if we can handle abstract sockets - skip "Abstract AF_UNIX paths unsupported", 2 unless $^O eq "linux"; - - my $test_abstract_socket = chr(0) . '/org/perl/hello'. chr(0) . 'world'; - my $addr = sockaddr_un ($test_abstract_socket); - my ($path) = sockaddr_un ($addr); - is($path, $test_abstract_socket, 'sockaddr_un can handle abstract AF_UNIX paths'); - - # see if we calculate the address structure length correctly - is(length ($test_abstract_socket) + 2, length $addr, 'sockaddr_un abstract address length'); -} - -SKIP: { - skip "No inet_ntop", 3 unless $Config{d_inetntop} && $Config{d_inetaton}; - - is(inet_ntop(AF_INET, inet_pton(AF_INET, "10.20.30.40")), "10.20.30.40", 'inet_pton->inet_ntop AF_INET roundtrip'); - is(inet_ntop(AF_INET, inet_aton("10.20.30.40")), "10.20.30.40", 'inet_aton->inet_ntop AF_INET roundtrip'); - - SKIP: { - skip "No AF_INET6", 1 unless defined eval { AF_INET6() }; - is(lc inet_ntop(AF_INET6, inet_pton(AF_INET6, "2001:503:BA3E::2:30")), "2001:503:ba3e::2:30", 'inet_pton->inet_ntop AF_INET6 roundtrip'); - } -} - -SKIP: { - skip "No AF_INET6", 5 unless defined eval { AF_INET6() }; - - my $sin6 = pack_sockaddr_in6(0x1234, "0123456789abcdef", 0, 89); - - is(sockaddr_family($sin6), AF_INET6, 'sockaddr_family of pack_sockaddr_in6'); - - is((unpack_sockaddr_in6($sin6))[0], 0x1234, 'pack_sockaddr_in6->unpack_sockaddr_in6 port'); - is((unpack_sockaddr_in6($sin6))[1], "0123456789abcdef", 'pack_sockaddr_in6->unpack_sockaddr_in6 addr'); - is((unpack_sockaddr_in6($sin6))[2], 0, 'pack_sockaddr_in6->unpack_sockaddr_in6 scope_id'); - is((unpack_sockaddr_in6($sin6))[3], 89, 'pack_sockaddr_in6->unpack_sockaddr_in6 flowinfo'); -} diff --git a/ext/Socket/t/getaddrinfo.t b/ext/Socket/t/getaddrinfo.t deleted file mode 100644 index b85af38254..0000000000 --- a/ext/Socket/t/getaddrinfo.t +++ /dev/null @@ -1,125 +0,0 @@ -use strict; -use warnings; -use Test::More tests => 30; - -use Socket qw( - AI_NUMERICHOST AF_INET SOCK_STREAM IPPROTO_TCP - unpack_sockaddr_in inet_aton getaddrinfo -); - -my ( $err, @res ); - -( $err, @res ) = getaddrinfo( "127.0.0.1", "80", { socktype => SOCK_STREAM } ); -cmp_ok( $err, "==", 0, '$err == 0 for host=127.0.0.1/service=80/socktype=STREAM' ); -cmp_ok( $err, "eq", "", '$err eq "" for host=127.0.0.1/service=80/socktype=STREAM' ); -is( scalar @res, 1, - '@res has 1 result' ); - -is( $res[0]->{family}, AF_INET, - '$res[0] family is AF_INET' ); -is( $res[0]->{socktype}, SOCK_STREAM, - '$res[0] socktype is SOCK_STREAM' ); -ok( $res[0]->{protocol} == 0 || $res[0]->{protocol} == IPPROTO_TCP, - '$res[0] protocol is 0 or IPPROTO_TCP' ); -ok( defined $res[0]->{addr}, - '$res[0] addr is defined' ); -if (length $res[0]->{addr}) { - is_deeply( [ unpack_sockaddr_in $res[0]->{addr} ], - [ 80, inet_aton( "127.0.0.1" ) ], - '$res[0] addr is {"127.0.0.1", 80}' ); -} else { - fail( '$res[0] addr is empty: check $socksizetype' ); -} - -# Check actual IV integers work just as well as PV strings -( $err, @res ) = getaddrinfo( "127.0.0.1", 80, { socktype => SOCK_STREAM } ); -cmp_ok( $err, "==", 0, '$err == 0 for host=127.0.0.1/service=80/socktype=STREAM' ); -is_deeply( [ unpack_sockaddr_in $res[0]->{addr} ], - [ 80, inet_aton( "127.0.0.1" ) ], - '$res[0] addr is {"127.0.0.1", 80}' ); - -( $err, @res ) = getaddrinfo( "127.0.0.1", "" ); -cmp_ok( $err, "==", 0, '$err == 0 for host=127.0.0.1' ); -# Might get more than one; e.g. different socktypes -ok( scalar @res > 0, '@res has results' ); - -( $err, @res ) = getaddrinfo( "127.0.0.1", undef ); -cmp_ok( $err, "==", 0, '$err == 0 for host=127.0.0.1/service=undef' ); - -# Test GETMAGIC -{ - "127.0.0.1" =~ /(.+)/; - ( $err, @res ) = getaddrinfo($1, undef); - cmp_ok( $err, "==", 0, '$err == 0 for host=$1' ); - ok( scalar @res > 0, '@res has results' ); - is( (unpack_sockaddr_in $res[0]->{addr})[1], - inet_aton( "127.0.0.1" ), - '$res[0] addr is {"127.0.0.1", ??}' ); -} - -( $err, @res ) = getaddrinfo( "", "80", { family => AF_INET, socktype => SOCK_STREAM } ); -cmp_ok( $err, "==", 0, '$err == 0 for service=80/family=AF_INET/socktype=STREAM' ); -is( scalar @res, 1, '@res has 1 result' ); - -# Just pick the first one -is( $res[0]->{family}, AF_INET, - '$res[0] family is AF_INET' ); -is( $res[0]->{socktype}, SOCK_STREAM, - '$res[0] socktype is SOCK_STREAM' ); -ok( $res[0]->{protocol} == 0 || $res[0]->{protocol} == IPPROTO_TCP, - '$res[0] protocol is 0 or IPPROTO_TCP' ); - -# Now some tests of a few well-known internet hosts -my $goodhost = "cpan.perl.org"; - -SKIP: { - skip "Resolver has no answer for $goodhost", 2 unless gethostbyname( $goodhost ); - - ( $err, @res ) = getaddrinfo( "cpan.perl.org", "ftp", { socktype => SOCK_STREAM } ); - cmp_ok( $err, "==", 0, '$err == 0 for host=cpan.perl.org/service=ftp/socktype=STREAM' ); - # Might get more than one; e.g. different families - ok( scalar @res > 0, '@res has results' ); -} - -# Now something I hope doesn't exist - we put it in a known-missing TLD -my $missinghost = "TbK4jM2M0OS.lm57DWIyu4i"; - -# Some CPAN testing machines seem to have wildcard DNS servers that reply to -# any request. We'd better check for them - -SKIP: { - skip "Resolver has an answer for $missinghost", 1 if gethostbyname( $missinghost ); - - # Some OSes return $err == 0 but no results - ( $err, @res ) = getaddrinfo( $missinghost, "ftp", { socktype => SOCK_STREAM } ); - ok( $err != 0 || ( $err == 0 && @res == 0 ), - '$err != 0 or @res == 0 for host=TbK4jM2M0OS.lm57DWIyu4i/service=ftp/socktype=SOCK_STREAM' ); - if( @res ) { - # Diagnostic that might help - while( my $r = shift @res ) { - diag( "family=$r->{family} socktype=$r->{socktype} protocol=$r->{protocol} addr=[" . length( $r->{addr} ) . " bytes]" ); - diag( " addr=" . join( ", ", map { sprintf '0x%02x', ord $_ } split m//, $r->{addr} ) ); - } - } -} - -# Now check that names with AI_NUMERICHOST fail - -( $err, @res ) = getaddrinfo( "localhost", "ftp", { flags => AI_NUMERICHOST, socktype => SOCK_STREAM } ); -ok( $err != 0, '$err != 0 for host=localhost/service=ftp/flags=AI_NUMERICHOST/socktype=SOCK_STREAM' ); - -# Some sanity checking on the hints hash -ok( defined eval { getaddrinfo( "127.0.0.1", "80", undef ); 1 }, - 'getaddrinfo() with undef hints works' ); -ok( !defined eval { getaddrinfo( "127.0.0.1", "80", "hints" ); 1 }, - 'getaddrinfo() with string hints dies' ); -ok( !defined eval { getaddrinfo( "127.0.0.1", "80", [] ); 1 }, - 'getaddrinfo() with ARRAY hints dies' ); - -# Ensure it doesn't segfault if args are missing - -( $err, @res ) = getaddrinfo(); -ok( defined $err, '$err defined for getaddrinfo()' ); - -( $err, @res ) = getaddrinfo( "127.0.0.1" ); -ok( defined $err, '$err defined for getaddrinfo("127.0.0.1")' ); diff --git a/ext/Socket/t/getnameinfo.t b/ext/Socket/t/getnameinfo.t deleted file mode 100644 index 803e8c0c57..0000000000 --- a/ext/Socket/t/getnameinfo.t +++ /dev/null @@ -1,37 +0,0 @@ -use strict; -use warnings; -use Test::More tests => 10; - -use Socket qw( - AF_INET NI_NUMERICHOST NI_NUMERICSERV - getnameinfo pack_sockaddr_in inet_aton -); - -my ( $err, $host, $service ); - -( $err, $host, $service ) = getnameinfo( pack_sockaddr_in( 80, inet_aton( "127.0.0.1" ) ), NI_NUMERICHOST|NI_NUMERICSERV ); -cmp_ok( $err, "==", 0, '$err == 0 for {family=AF_INET,port=80,sinaddr=127.0.0.1}/NI_NUMERICHOST|NI_NUMERICSERV' ); -cmp_ok( $err, "eq", "", '$err eq "" for {family=AF_INET,port=80,sinaddr=127.0.0.1}/NI_NUMERICHOST|NI_NUMERICSERV' ); - -is( $host, "127.0.0.1", '$host is 127.0.0.1 for NH/NS' ); -is( $service, "80", '$service is 80 for NH/NS' ); - -# Probably "localhost" but we'd better ask the system to be sure -my $expect_host = gethostbyaddr( inet_aton( "127.0.0.1" ), AF_INET ); -defined $expect_host or $expect_host = "127.0.0.1"; - -( $err, $host, $service ) = getnameinfo( pack_sockaddr_in( 80, inet_aton( "127.0.0.1" ) ), NI_NUMERICSERV ); -cmp_ok( $err, "==", 0, '$err == 0 for {family=AF_INET,port=80,sinaddr=127.0.0.1}/NI_NUMERICSERV' ); - -is( $host, $expect_host, "\$host is $expect_host for NS" ); -is( $service, "80", '$service is 80 for NS' ); - -# Probably "www" but we'd better ask the system to be sure -my $expect_service = getservbyport( 80, "tcp" ); -defined $expect_service or $expect_service = "80"; - -( $err, $host, $service ) = getnameinfo( pack_sockaddr_in( 80, inet_aton( "127.0.0.1" ) ), NI_NUMERICHOST ); -cmp_ok( $err, "==", 0, '$err == 0 for {family=AF_INET,port=80,sinaddr=127.0.0.1}/NI_NUMERICHOST' ); - -is( $host, "127.0.0.1", '$host is 127.0.0.1 for NH' ); -is( $service, $expect_service, "\$service is $expect_service for NH" ); diff --git a/ext/Socket/t/ipv6_mreq.t b/ext/Socket/t/ipv6_mreq.t deleted file mode 100644 index 43fb8f804b..0000000000 --- a/ext/Socket/t/ipv6_mreq.t +++ /dev/null @@ -1,26 +0,0 @@ -use strict; -use warnings; -use Test::More; - -use Socket qw( - pack_ipv6_mreq unpack_ipv6_mreq -); - -# Check that pack/unpack_ipv6_mreq either croak with "Not implemented", or -# roundtrip as identity - -my $packed; -eval { - $packed = pack_ipv6_mreq "ANADDRESSIN16CHR", 123; -}; -if( !defined $packed ) { - plan skip_all => "No pack_ipv6_mreq" if $@ =~ m/ not implemented /; - die $@; -} - -plan tests => 2; - -my @unpacked = unpack_ipv6_mreq $packed; - -is( $unpacked[0], "ANADDRESSIN16CHR", 'unpack_ipv6_mreq multiaddr' ); -is( $unpacked[1], 123, 'unpack_ipv6_mreq ifindex' ); diff --git a/ext/Socket/t/socketpair.t b/ext/Socket/t/socketpair.t deleted file mode 100644 index 997628c3bd..0000000000 --- a/ext/Socket/t/socketpair.t +++ /dev/null @@ -1,245 +0,0 @@ -#!./perl -w - -my $child; -my $can_fork; -my $has_perlio; - -BEGIN { - require Config; import Config; - $can_fork = $Config{'d_fork'} || $Config{'d_pseudofork'}; - - if ($^O eq "hpux" or $Config{'extensions'} !~ /\bSocket\b/ && - !(($^O eq 'VMS') && $Config{d_socket})) { - print "1..0\n"; - exit 0; - } -} - -{ - # This was in the BEGIN block, but since Test::More 0.47 added support to - # detect forking, we don't need to fork before Test::More initialises. - - # Too many things in this test will hang forever if something is wrong, - # so we need a self destruct timer. And IO can hang despite an alarm. - - if( $can_fork) { - my $parent = $$; - $child = fork; - die "Fork failed" unless defined $child; - if (!$child) { - $SIG{INT} = sub {exit 0}; # You have 60 seconds. Your time starts now. - my $must_finish_by = time + 60; - my $remaining; - while (($remaining = $must_finish_by - time) > 0) { - sleep $remaining; - } - warn "Something unexpectedly hung during testing"; - kill "INT", $parent or die "Kill failed: $!"; - exit 1; - } - } - unless ($has_perlio = find PerlIO::Layer 'perlio') { - print < "alarm() not implemented on this platform"; -} elsif( !$can_fork ) { - plan skip_all => "fork() not implemented on this platform"; -} else { - # This should fail but not die if there is real socketpair - eval {socketpair LEFT, RIGHT, -1, -1, -1}; - if ($@ =~ /^Unsupported socket function "socketpair" called/ || - $! =~ /^The operation requested is not supported./) { # Stratus VOS - plan skip_all => 'No socketpair (real or emulated)'; - } else { - eval {AF_UNIX}; - if ($@ =~ /^Your vendor has not defined Socket macro AF_UNIX/) { - plan skip_all => 'No AF_UNIX'; - } else { - plan tests => 45; - } - } -} - -# But we'll install an alarm handler in case any of the races below fail. -$SIG{ALRM} = sub {die "Unexpected alarm during testing"}; - -ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC), - "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)") - or print "# \$\! = $!\n"; - -if ($has_perlio) { - binmode(LEFT, ":bytes"); - binmode(RIGHT, ":bytes"); -} - -my @left = ("hello ", "world\n"); -my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here. - -foreach (@left) { - # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left"); - is (syswrite (LEFT, $_), length $_, "syswrite to left"); -} -foreach (@right) { - # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right"); - is (syswrite (RIGHT, $_), length $_, "syswrite to right"); -} - -# stream socket, so our writes will become joined: -my ($buffer, $expect); -$expect = join '', @right; -undef $buffer; -is (read (LEFT, $buffer, length $expect), length $expect, "read on left"); -is ($buffer, $expect, "content what we expected?"); -$expect = join '', @left; -undef $buffer; -is (read (RIGHT, $buffer, length $expect), length $expect, "read on right"); -is ($buffer, $expect, "content what we expected?"); - -ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing"); -# This will hang forever if eof is buggy, and alarm doesn't interrupt system -# Calls. Hence the child process minder. -SKIP: { - skip "SCO Unixware / OSR have a bug with shutdown",2 if $^O =~ /^(?:svr|sco)/; - local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" }; - local $TODO = "Known problems with unix sockets on $^O" - if $^O eq 'hpux' || $^O eq 'super-ux'; - alarm 3; - $! = 0; - ok (eof RIGHT, "right is at EOF"); - local $TODO = "Known problems with unix sockets on $^O" - if $^O eq 'unicos' || $^O eq 'unicosmk'; - is ($!, '', 'and $! should report no error'); - alarm 60; -} - -my $err = $!; -$SIG{PIPE} = 'IGNORE'; -{ - local $SIG{ALRM} - = sub { warn "syswrite to left didn't fail within 3 seconds" }; - alarm 3; - # Split the system call from the is() - is() does IO so - # (say) a flush may do a seek which on a pipe may disturb errno - my $ans = syswrite (LEFT, "void"); - $err = $!; - is ($ans, undef, "syswrite to shutdown left should fail"); - alarm 60; -} -{ - # This may need skipping on some OSes - restoring value saved above - # should help - $! = $err; - ok (($!{EPIPE} or $!{ESHUTDOWN}), '$! should be EPIPE or ESHUTDOWN') - or printf "\$\!=%d(%s)\n", $err, $err; -} - -my @gripping = (chr 255, chr 127); -foreach (@gripping) { - is (syswrite (RIGHT, $_), length $_, "syswrite to right"); -} - -ok (!eof LEFT, "left is not at EOF"); - -$expect = join '', @gripping; -undef $buffer; -is (read (LEFT, $buffer, length $expect), length $expect, "read on left"); -is ($buffer, $expect, "content what we expected?"); - -ok (close LEFT, "close left"); -ok (close RIGHT, "close right"); - - -# And now datagrams -# I suspect we also need a self destruct time-bomb for these, as I don't see any -# guarantee that the stack won't drop a UDP packet, even if it is for localhost. - -SKIP: { - skip "No usable SOCK_DGRAM for socketpair", 24 if ($^O =~ /^(MSWin32|os2)\z/); - local $TODO = "socketpair not supported on $^O" if $^O eq 'nto'; - -ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC), - "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)") - or print "# \$\! = $!\n"; - -if ($has_perlio) { - binmode(LEFT, ":bytes"); - binmode(RIGHT, ":bytes"); -} - -foreach (@left) { - # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left"); - is (syswrite (LEFT, $_), length $_, "syswrite to left"); -} -foreach (@right) { - # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right"); - is (syswrite (RIGHT, $_), length $_, "syswrite to right"); -} - -# stream socket, so our writes will become joined: -my ($total); -$total = join '', @right; -foreach $expect (@right) { - undef $buffer; - is (sysread (LEFT, $buffer, length $total), length $expect, "read on left"); - is ($buffer, $expect, "content what we expected?"); -} -$total = join '', @left; -foreach $expect (@left) { - undef $buffer; - is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right"); - is ($buffer, $expect, "content what we expected?"); -} - -ok (shutdown(LEFT, 1), "shutdown left for writing"); - -# eof uses buffering. eof is indicated by a sysread of zero. -# but for a datagram socket there's no way it can know nothing will ever be -# sent -SKIP: { - skip "$^O does length 0 udp reads", 2 if ($^O eq 'os390'); - - my $alarmed = 0; - local $SIG{ALRM} = sub { $alarmed = 1; }; - print "# Approximate forever as 3 seconds. Wait 'forever'...\n"; - alarm 3; - undef $buffer; - is (sysread (RIGHT, $buffer, 1), undef, - "read on right should be interrupted"); - is ($alarmed, 1, "alarm should have fired"); -} - -alarm 30; - -#ok (eof RIGHT, "right is at EOF"); - -foreach (@gripping) { - is (syswrite (RIGHT, $_), length $_, "syswrite to right"); -} - -$total = join '', @gripping; -foreach $expect (@gripping) { - undef $buffer; - is (sysread (LEFT, $buffer, length $total), length $expect, "read on left"); - is ($buffer, $expect, "content what we expected?"); -} - -ok (close LEFT, "close left"); -ok (close RIGHT, "close right"); - -} # end of DGRAM SKIP - -kill "INT", $child or warn "Failed to kill child process $child: $!"; -exit 0; -- cgit v1.2.1 From 1584259fbedde39de8b5c3750d0a7863dc7a2b0b Mon Sep 17 00:00:00 2001 From: "Paul \\\"LeoNerd\\\" Evans" Date: Tue, 1 Nov 2011 16:16:33 +0000 Subject: More fixups for dual-life to support out-of-core build * Use ExtUtils::CChecker when not in core to replace the Configure-time testing that core does * Don't actually need to dVAR in getaddrinfo/getnameinfo * Expand croak_xs_usage inline rather than rely on xsubpp to write one --- cpan/Socket/Makefile.PL | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ cpan/Socket/Socket.xs | 6 ++-- 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/cpan/Socket/Makefile.PL b/cpan/Socket/Makefile.PL index e5daf93a98..0667b31023 100644 --- a/cpan/Socket/Makefile.PL +++ b/cpan/Socket/Makefile.PL @@ -1,12 +1,90 @@ use ExtUtils::MakeMaker; use ExtUtils::Constant 0.23 'WriteConstants'; use Config; + +my @DEFINES; +unless( $ENV{PERL_CORE} ) { + # Building standalone, not as core. + require ExtUtils::CChecker; + my $cc = ExtUtils::CChecker->new; + + my %defines = ( + # -Dfoo func() $Config{key} + HAS_GETADDRINFO => [ "getaddrinfo", "d_getaddrinfo" ], + HAS_GETNAMEINFO => [ "getnameinfo", "d_getnameinfo" ], + HAS_INET_ATON => [ "inet_aton", "d_inetaton" ], + HAS_INETNTOP => [ "inet_ntop", "d_inetntop" ], + HAS_INETPTON => [ "inet_pton", "d_inetpton" ], + ); + + foreach my $define ( sort keys %defines ) { + my ( $func, $key ) = @{$defines{$define}}; + next if exists $Config{$key}; + + $cc->try_compile_run( + define => $define, + source => <<"EOF" ) +#include +#include +#include +#include +#include +int main(int argc, char *argv[]) { + void *p = &$func; + return 0; +} +EOF + and print "$func() found\n" + or print "$func() not found\n"; + } + + unless( exists $Config{d_sockaddr_sa_len} ) { + $cc->try_compile_run( + define => "HAS_SOCKADDR_SA_LEN", + source => <<'EOF' ) +#include +#include +int main(int argc, char *argv[]) { + struct sockaddr sa; + sa.sa_len = 0; + return 0; +} +EOF + and print "sockaddr has sa_len\n" + or print "sockaddr does not have sa_len\n"; + } + + unless( exists $Config{d_sin6_scope_id} ) { + $cc->try_compile_run( + define => "HAS_SIN6_SCOPE_ID", + source => <<'EOF' ) +#include +#include +#include +int main(int argc, char *argv[]) { + struct sockaddr_in6 sin6; + sin6.sin6_scope_id = 0; + return 0; +} +EOF + and print "sockaddr_in6 has sin6_scope_id\n" + or print "sockaddr_in6 does not have sin6_scope_id\n"; + } + + @DEFINES = @{ $cc->extra_compiler_flags }; +} + WriteMakefile( NAME => 'Socket', VERSION_FROM => 'Socket.pm', ($Config{libs} =~ /(-lsocks\S*)/ ? (LIBS => [ "$1" ] ) : ()), XSPROTOARG => '-noprototypes', # XXX remove later? realclean => {FILES=> 'const-c.inc const-xs.inc'}, + DEFINE => join( " ", @DEFINES ), + CONFIGURE_REQUIRES => { + 'ExtUtils::CChecker' => 0, + 'ExtUtils::Constant' => '0.23', + }, ); my @names = (qw(AF_802 AF_AAL AF_APPLETALK AF_CCITT AF_CHAOS AF_CTF AF_DATAKIT AF_DECnet AF_DLI AF_ECMA AF_GOSIP AF_HYLINK diff --git a/cpan/Socket/Socket.xs b/cpan/Socket/Socket.xs index f571ac7e71..b06cfa6bea 100644 --- a/cpan/Socket/Socket.xs +++ b/cpan/Socket/Socket.xs @@ -241,7 +241,6 @@ static SV *err_to_SV(pTHX_ int err) static void xs_getaddrinfo(pTHX_ CV *cv) { - dVAR; dXSARGS; SV *host; @@ -258,7 +257,7 @@ static void xs_getaddrinfo(pTHX_ CV *cv) int n_res; if(items > 3) - croak_xs_usage(cv, "host, service, hints"); + croak("Usage: Socket::getaddrinfo(host, service, hints)"); SP -= items; @@ -348,7 +347,6 @@ static void xs_getaddrinfo(pTHX_ CV *cv) #ifdef HAS_GETNAMEINFO static void xs_getnameinfo(pTHX_ CV *cv) { - dVAR; dXSARGS; SV *addr; @@ -361,7 +359,7 @@ static void xs_getnameinfo(pTHX_ CV *cv) int err; if(items < 1 || items > 2) - croak_xs_usage(cv, "addr, flags=0"); + croak("Usage: Socket::getnameinfo(addr, flags=0)"); SP -= items; -- cgit v1.2.1 From 0369a5a54628d35a0391b4624eba1887bd5ff71d Mon Sep 17 00:00:00 2001 From: "Paul \\\"LeoNerd\\\" Evans" Date: Tue, 22 Nov 2011 15:11:49 +0000 Subject: Snapshot of P/PE/PEVANS/Socket-1.94_07.tar.gz --- cpan/Socket/Makefile.PL | 13 ++++++++---- cpan/Socket/Socket.pm | 53 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/cpan/Socket/Makefile.PL b/cpan/Socket/Makefile.PL index 0667b31023..5518e33f88 100644 --- a/cpan/Socket/Makefile.PL +++ b/cpan/Socket/Makefile.PL @@ -92,9 +92,12 @@ my @names = (qw(AF_802 AF_AAL AF_APPLETALK AF_CCITT AF_CHAOS AF_CTF AF_LAST AF_LAT AF_LINK AF_MAX AF_NBS AF_NIT AF_NS AF_OSI AF_OSINET AF_PUP AF_ROUTE AF_SNA AF_UNIX AF_UNSPEC AF_USER AF_WAN AF_X25 - AI_CANONNAME AI_NUMERICHOST AI_NUMERICSERV AI_PASSIVE - EAI_ADDRFAMILY EAI_AGAIN EAI_BADFLAGS EAI_FAIL EAI_FAMILY - EAI_NODATA EAI_NONAME EAI_SERVICE EAI_SOCKTYPE + AI_ADDRCONFIG AI_ALL AI_CANONIDN AI_CANONNAME AI_IDN + AI_IDN_ALLOW_UNASSIGNED AI_IDN_USE_STD3_ASCII_RULES + AI_NUMERICHOST AI_NUMERICSERV AI_PASSIVE AI_V4MAPPED + EAI_ADDRFAMILY EAI_AGAIN EAI_BADFLAGS EAI_BADHINTS EAI_FAIL + EAI_FAMILY EAI_NODATA EAI_NONAME EAI_PROTOCOL EAI_SERVICE + EAI_SOCKTYPE EAI_SYSTEM IOV_MAX IP_OPTIONS IP_HDRINCL IP_TOS IP_TTL IP_RECVOPTS IP_RECVRETOPTS IP_RETOPTS IPV6_ADD_MEMBERSHIP IPV6_DROP_MEMBERSHIP IPV6_MTU @@ -104,7 +107,9 @@ my @names = (qw(AF_802 AF_AAL AF_APPLETALK AF_CCITT AF_CHAOS AF_CTF MSG_EOF MSG_EOR MSG_ERRQUEUE MSG_ETAG MSG_FIN MSG_MAXIOVLEN MSG_MCAST MSG_NOSIGNAL MSG_RST MSG_SYN MSG_TRUNC MSG_URG MSG_WAITALL MSG_WIRE - NI_DGRAM NI_NAMEREQD NI_NUMERICHOST NI_NUMERICSERV + NI_DGRAM NI_IDN NI_IDN_ALLOW_UNASSIGNED + NI_IDN_USE_STD3_ASCII_RULES NI_NAMEREQD NI_NOFQDN + NI_NUMERICHOST NI_NUMERICSERV PF_802 PF_AAL PF_APPLETALK PF_CCITT PF_CHAOS PF_CTF PF_DATAKIT PF_DECnet PF_DLI PF_ECMA PF_GOSIP PF_HYLINK PF_IMPLINK PF_INET PF_INET6 PF_ISO PF_KEY diff --git a/cpan/Socket/Socket.pm b/cpan/Socket/Socket.pm index 5fff0d0150..34deb63779 100644 --- a/cpan/Socket/Socket.pm +++ b/cpan/Socket/Socket.pm @@ -3,7 +3,7 @@ package Socket; use strict; our($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); -$VERSION = "1.94_03"; +$VERSION = "1.94_07"; =head1 NAME @@ -506,20 +506,30 @@ require XSLoader; IN6ADDR_ANY IN6ADDR_LOOPBACK + AI_ADDRCONFIG + AI_ALL + AI_CANONIDN AI_CANONNAME + AI_IDN + AI_IDN_ALLOW_UNASSIGNED + AI_IDN_USE_STD3_ASCII_RULES AI_NUMERICHOST AI_NUMERICSERV AI_PASSIVE + AI_V4MAPPED EAI_ADDRFAMILY EAI_AGAIN EAI_BADFLAGS + EAI_BADHINTS EAI_FAIL EAI_FAMILY EAI_NODATA EAI_NONAME + EAI_PROTOCOL EAI_SERVICE EAI_SOCKTYPE + EAI_SYSTEM IPPROTO_IP IPPROTO_IPV6 @@ -539,7 +549,11 @@ require XSLoader; IPV6_V6ONLY NI_DGRAM + NI_IDN + NI_IDN_ALLOW_UNASSIGNED + NI_IDN_USE_STD3_ASCII_RULES NI_NAMEREQD + NI_NOFQDN NI_NUMERICHOST NI_NUMERICSERV @@ -634,6 +648,9 @@ if( defined &getaddrinfo ) { AI_PASSIVE => 1, AI_CANONNAME => 2, AI_NUMERICHOST => 4, + AI_V4MAPPED => 8, + AI_ALL => 16, + AI_ADDRCONFIG => 32, # RFC 2553 doesn't define this but Linux does - lets be nice and # provide it since we can AI_NUMERICSERV => 1024, @@ -646,8 +663,25 @@ if( defined &getaddrinfo ) { NI_NUMERICHOST => 1, NI_NUMERICSERV => 2, + NI_NOFQDN => 4, NI_NAMEREQD => 8, NI_DGRAM => 16, + + # Constants we don't support. Export them, but croak if anyone tries to + # use them + AI_IDN => 64, + AI_CANONIDN => 128, + AI_IDN_ALLOW_UNASSIGNED => 256, + AI_IDN_USE_STD3_ASCII_RULES => 512, + NI_IDN => 32, + NI_IDN_ALLOW_UNASSIGNED => 64, + NI_IDN_USE_STD3_ASCII_RULES => 128, + + # Error constants we'll never return, so it doesn't matter what value + # these have, nor that we don't provide strings for them + EAI_SYSTEM => -11, + EAI_BADHINTS => -1000, + EAI_PROTOCOL => -1001 ); foreach my $name ( keys %constants ) { @@ -706,6 +740,14 @@ sub fake_getaddrinfo my $flag_numerichost = $flags & AI_NUMERICHOST(); $flags &= ~AI_NUMERICHOST(); my $flag_numericserv = $flags & AI_NUMERICSERV(); $flags &= ~AI_NUMERICSERV(); + # These constants don't apply to AF_INET-only lookups, so we might as well + # just ignore them. For AI_ADDRCONFIG we just presume the host has ability + # to talk AF_INET. If not we'd have to return no addresses at all. :) + $flags &= ~(AI_V4MAPPED()|AI_ALL()|AI_ADDRCONFIG()); + + $flags & (AI_IDN()|AI_CANONIDN()|AI_IDN_ALLOW_UNASSIGNED()|AI_IDN_USE_STD3_ASCII_RULES()) and + croak "Socket::getaddrinfo() does not support IDN"; + $flags == 0 or return fake_makeerr( EAI_BADFLAGS() ); $node eq "" and $service eq "" and return fake_makeerr( EAI_NONAME() ); @@ -797,9 +839,13 @@ sub fake_getnameinfo my $flag_numerichost = $flags & NI_NUMERICHOST(); $flags &= ~NI_NUMERICHOST(); my $flag_numericserv = $flags & NI_NUMERICSERV(); $flags &= ~NI_NUMERICSERV(); + my $flag_nofqdn = $flags & NI_NOFQDN(); $flags &= ~NI_NOFQDN(); my $flag_namereqd = $flags & NI_NAMEREQD(); $flags &= ~NI_NAMEREQD(); my $flag_dgram = $flags & NI_DGRAM() ; $flags &= ~NI_DGRAM(); + $flags & (NI_IDN()|NI_IDN_ALLOW_UNASSIGNED()|NI_IDN_USE_STD3_ASCII_RULES()) and + croak "Socket::getnameinfo() does not support IDN"; + $flags == 0 or return fake_makeerr( EAI_BADFLAGS() ); my $node; @@ -812,6 +858,11 @@ sub fake_getnameinfo return fake_makeerr( EAI_NONAME() ) if $flag_namereqd; $node = Socket::inet_ntoa( $inetaddr ); } + elsif( $flag_nofqdn ) { + my ( $shortname ) = split m/\./, $node; + my ( $fqdn ) = gethostbyname $shortname; + $node = $shortname if defined $fqdn and $fqdn eq $node; + } } my $service; -- cgit v1.2.1 From f72af2d244e61d9be2e55bb64a2eeb11fa8968e3 Mon Sep 17 00:00:00 2001 From: "Paul \\\"LeoNerd\\\" Evans" Date: Thu, 1 Dec 2011 14:10:48 +0000 Subject: Bump Socket::VERSION to 1.95; to match CPAN release --- cpan/Socket/Socket.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpan/Socket/Socket.pm b/cpan/Socket/Socket.pm index 34deb63779..2cf8687c76 100644 --- a/cpan/Socket/Socket.pm +++ b/cpan/Socket/Socket.pm @@ -3,7 +3,7 @@ package Socket; use strict; our($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); -$VERSION = "1.94_07"; +$VERSION = "1.95"; =head1 NAME -- cgit v1.2.1