4 require Config; import Config;
5 if ($Config{'extensions'} !~ /\bSocket\b/ &&
6 !(($^O eq 'VMS') && $Config{d_socket})) {
10 $has_alarm = $Config{d_alarm};
17 $has_echo = $^O ne 'MSWin32';
19 sub arm { $alarmed = 0; alarm(shift) if $has_alarm }
20 sub alarmed { $alarmed = 1 }
21 $SIG{ALRM} = 'alarmed' if $has_alarm;
23 if (socket(T, PF_INET, SOCK_STREAM, IPPROTO_TCP)) {
27 my $host = $^O eq 'MacOS' || ($^O eq 'irix' && $Config{osvers} == 5) ?
28 '127.0.0.1' : 'localhost';
29 my $localhost = inet_aton($host);
31 if ($has_echo && defined $localhost && connect(T,pack_sockaddr_in(7,$localhost))){
36 print "# Connected to " .
37 inet_ntoa((unpack_sockaddr_in(getpeername(T)))[1])."\n";
40 syswrite(T,"hello",5);
44 $read = sysread(T,$buff,10); # Connection may be granted, then closed!
47 while ($read > 0 && length($buff) < 5) {
48 # adjust for fact that TCP doesn't guarantee size of reads/writes
50 $read = sysread(T,$buff,10,length($buff));
53 print(($read == 0 || $buff eq "hello") ? "ok 3\n" : "not ok 3\n");
56 print "# You're allowed to fail tests 2 and 3 if\n";
57 print "# the echo service has been disabled or if your\n";
58 print "# gethostbyname() cannot resolve your localhost.\n";
59 print "# 'Connection refused' indicates disabled echo service.\n";
60 print "# 'Interrupted system call' indicates a hanging echo service.\n";
61 print "# Error: $!\n";
62 print "ok 2 - skipped\n";
63 print "ok 3 - skipped\n";
67 print "# Error: $!\n";
71 if( socket(S, PF_INET,SOCK_STREAM, IPPROTO_TCP) ){
75 if ($has_echo && connect(S,pack_sockaddr_in(7,INADDR_LOOPBACK))){
80 print "# Connected to " .
81 inet_ntoa((unpack_sockaddr_in(getpeername(S)))[1])."\n";
84 syswrite(S,"olleh",5);
88 $read = sysread(S,$buff,10); # Connection may be granted, then closed!
91 while ($read > 0 && length($buff) < 5) {
92 # adjust for fact that TCP doesn't guarantee size of reads/writes
94 $read = sysread(S,$buff,10,length($buff));
97 print(($read == 0 || $buff eq "olleh") ? "ok 6\n" : "not ok 6\n");
100 print "# You're allowed to fail tests 5 and 6 if\n";
101 print "# the echo service has been disabled.\n";
102 print "# 'Interrupted system call' indicates a hanging echo service.\n";
103 print "# Error: $!\n";
104 print "ok 5 - skipped\n";
105 print "ok 6 - skipped\n";
109 print "# Error: $!\n";
114 $SIG{__WARN__} = sub {
115 ++ $w if $_[0] =~ /^6-ARG sockaddr_in call is deprecated/ ;
118 sockaddr_in(1,2,3,4,5,6) ;
119 print ($w == 1 ? "not ok 7\n" : "ok 7\n") ;
120 use warnings 'Socket' ;
121 sockaddr_in(1,2,3,4,5,6) ;
122 print ($w == 1 ? "ok 8\n" : "not ok 8\n") ;
124 # Thest that whatever we give into pack/unpack_sockaddr retains
125 # the value thru the entire chain.
126 if((inet_ntoa((unpack_sockaddr_in(pack_sockaddr_in(100,inet_aton("10.250.230.10"))))[1])) eq '10.250.230.10') {
131 print ((inet_ntoa(inet_aton("10.20.30.40")) eq "10.20.30.40") ? "ok 10\n" : "not ok 10\n");
132 print ((inet_ntoa(v10.20.30.40) eq "10.20.30.40") ? "ok 11\n" : "not ok 11\n");
134 my ($port,$addr) = unpack_sockaddr_in(pack_sockaddr_in(100,v10.10.10.10));
135 print (($port == 100) ? "ok 12\n" : "not ok 12\n");
136 print ((inet_ntoa($addr) eq "10.10.10.10") ? "ok 13\n" : "not ok 13\n");
139 eval { inet_ntoa(v10.20.30.400) };
140 print (($@ =~ /^Wide character in Socket::inet_ntoa at/) ? "ok 14\n" : "not ok 14\n");
142 if (sockaddr_family(pack_sockaddr_in(100,inet_aton("10.250.230.10"))) == AF_INET) {
148 eval { sockaddr_family("") };
149 print (($@ =~ /^Bad arg length for Socket::sockaddr_family, length is 0, should be at least \d+/) ? "ok 16\n" : "not ok 16\n");
151 if ($^O eq 'linux') {
152 # see if we can handle abstract sockets
153 my $test_abstract_socket = chr(0) . '/org/perl/hello'. chr(0) . 'world';
154 my $addr = sockaddr_un ($test_abstract_socket);
155 my ($path) = sockaddr_un ($addr);
156 if ($test_abstract_socket eq $path) {
161 print "# got <$path>\n";
165 # see if we calculate the address structure length correctly
166 if (length ($test_abstract_socket) + 2 == length $addr) {
169 print "# got ".(length $addr)."\n";
174 # doesn't have abstract socket support
175 print "ok 17 - skipped on this platform\n";
176 print "ok 18 - skipped on this platform\n";
179 if($Config{d_inetntop} && $Config{d_inetaton}){
180 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");
181 print ((inet_ntop(AF_INET, inet_aton("10.20.30.40")) eq "10.20.30.40") ? "ok 20\n" : "not ok 20\n");
182 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");
185 print "ok $_ - skipped on this platform\n" for 19 .. 21;