line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# IO::Socket::INET.pm |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Copyright (c) 1997-8 Graham Barr . All rights reserved. |
4
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or |
5
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package IO::Socket::INET; |
8
|
|
|
|
|
|
|
|
9
|
17
|
|
|
17
|
|
568
|
use strict; |
|
17
|
|
|
|
|
47
|
|
|
17
|
|
|
|
|
613
|
|
10
|
17
|
|
|
17
|
|
515
|
use IO::Socket; |
|
17
|
|
|
|
|
26
|
|
|
17
|
|
|
|
|
74
|
|
11
|
17
|
|
|
17
|
|
110
|
use Socket; |
|
17
|
|
|
|
|
20
|
|
|
17
|
|
|
|
|
10998
|
|
12
|
17
|
|
|
17
|
|
98
|
use Carp; |
|
17
|
|
|
|
|
28
|
|
|
17
|
|
|
|
|
909
|
|
13
|
17
|
|
|
17
|
|
94
|
use Exporter; |
|
17
|
|
|
|
|
31
|
|
|
17
|
|
|
|
|
425
|
|
14
|
17
|
|
|
17
|
|
64
|
use Errno; |
|
17
|
|
|
|
|
25
|
|
|
17
|
|
|
|
|
32608
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our @ISA = qw(IO::Socket); |
17
|
|
|
|
|
|
|
our $VERSION = "1.51"; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $EINVAL = exists(&Errno::EINVAL) ? Errno::EINVAL() : 1; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
IO::Socket::INET->register_domain( AF_INET ); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my %socket_type = ( tcp => SOCK_STREAM, |
24
|
|
|
|
|
|
|
udp => SOCK_DGRAM, |
25
|
|
|
|
|
|
|
icmp => SOCK_RAW |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
my %proto_number; |
28
|
|
|
|
|
|
|
$proto_number{tcp} = Socket::IPPROTO_TCP() if defined &Socket::IPPROTO_TCP; |
29
|
|
|
|
|
|
|
$proto_number{udp} = Socket::IPPROTO_UDP() if defined &Socket::IPPROTO_UDP; |
30
|
|
|
|
|
|
|
$proto_number{icmp} = Socket::IPPROTO_ICMP() if defined &Socket::IPPROTO_ICMP; |
31
|
|
|
|
|
|
|
my %proto_name = reverse %proto_number; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub new { |
34
|
49
|
|
|
49
|
1
|
5267506
|
my $class = shift; |
35
|
49
|
100
|
|
|
|
439
|
unshift(@_, "PeerAddr") if @_ == 1; |
36
|
49
|
|
|
|
|
710
|
return $class->SUPER::new(@_); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub _cache_proto { |
40
|
0
|
|
|
0
|
|
0
|
my @proto = @_; |
41
|
0
|
|
|
|
|
0
|
for (map lc($_), $proto[0], split(' ', $proto[1])) { |
42
|
0
|
|
|
|
|
0
|
$proto_number{$_} = $proto[2]; |
43
|
|
|
|
|
|
|
} |
44
|
0
|
|
|
|
|
0
|
$proto_name{$proto[2]} = $proto[0]; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _get_proto_number { |
48
|
34
|
|
|
34
|
|
98
|
my $name = lc(shift); |
49
|
34
|
50
|
|
|
|
108
|
return undef unless defined $name; |
50
|
34
|
50
|
|
|
|
212
|
return $proto_number{$name} if exists $proto_number{$name}; |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
0
|
my @proto = eval { getprotobyname($name) }; |
|
0
|
|
|
|
|
0
|
|
53
|
0
|
0
|
|
|
|
0
|
return undef unless @proto; |
54
|
0
|
|
|
|
|
0
|
_cache_proto(@proto); |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
0
|
return $proto[2]; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub _get_proto_name { |
60
|
34
|
|
|
34
|
|
71
|
my $num = shift; |
61
|
34
|
50
|
|
|
|
159
|
return undef unless defined $num; |
62
|
34
|
50
|
|
|
|
302
|
return $proto_name{$num} if exists $proto_name{$num}; |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
0
|
my @proto = eval { getprotobynumber($num) }; |
|
0
|
|
|
|
|
0
|
|
65
|
0
|
0
|
|
|
|
0
|
return undef unless @proto; |
66
|
0
|
|
|
|
|
0
|
_cache_proto(@proto); |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
0
|
return $proto[0]; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub _sock_info { |
72
|
52
|
|
|
52
|
|
292
|
my($addr,$port,$proto) = @_; |
73
|
52
|
|
|
|
|
100
|
my $origport = $port; |
74
|
52
|
|
|
|
|
150
|
my @serv = (); |
75
|
|
|
|
|
|
|
|
76
|
52
|
100
|
100
|
|
|
638
|
$port = $1 |
77
|
|
|
|
|
|
|
if(defined $addr && $addr =~ s,:([\w\(\)/]+)$,,); |
78
|
|
|
|
|
|
|
|
79
|
52
|
100
|
100
|
|
|
505
|
if(defined $proto && $proto =~ /\D/) { |
80
|
22
|
|
|
|
|
115
|
my $num = _get_proto_number($proto); |
81
|
22
|
50
|
|
|
|
89
|
unless (defined $num) { |
82
|
0
|
|
|
|
|
0
|
$IO::Socket::errstr = $@ = "Bad protocol '$proto'"; |
83
|
0
|
|
|
|
|
0
|
return; |
84
|
|
|
|
|
|
|
} |
85
|
22
|
|
|
|
|
48
|
$proto = $num; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
52
|
100
|
|
|
|
152
|
if(defined $port) { |
89
|
10
|
50
|
|
|
|
367
|
my $defport = ($port =~ s,\((\d+)\)$,,) ? $1 : undef; |
90
|
10
|
|
|
|
|
152
|
my $pnum = ($port =~ m,^(\d+)$,)[0]; |
91
|
|
|
|
|
|
|
|
92
|
10
|
50
|
0
|
|
|
401
|
@serv = getservbyname($port, _get_proto_name($proto) || "") |
93
|
|
|
|
|
|
|
if ($port =~ m,\D,); |
94
|
|
|
|
|
|
|
|
95
|
10
|
|
33
|
|
|
184
|
$port = $serv[2] || $defport || $pnum; |
96
|
10
|
50
|
|
|
|
84
|
unless (defined $port) { |
97
|
0
|
|
|
|
|
0
|
$IO::Socket::errstr = $@ = "Bad service '$origport'"; |
98
|
0
|
|
|
|
|
0
|
return; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
10
|
50
|
33
|
|
|
70
|
$proto = _get_proto_number($serv[3]) if @serv && !$proto; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
52
|
|
100
|
|
|
932
|
return ($addr || undef, |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
105
|
|
|
|
|
|
|
$port || undef, |
106
|
|
|
|
|
|
|
$proto || undef |
107
|
|
|
|
|
|
|
); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub _error { |
111
|
0
|
|
|
0
|
|
0
|
my $sock = shift; |
112
|
0
|
|
|
|
|
0
|
my $err = shift; |
113
|
|
|
|
|
|
|
{ |
114
|
0
|
|
|
|
|
0
|
local($!); |
|
0
|
|
|
|
|
0
|
|
115
|
0
|
|
|
|
|
0
|
my $title = ref($sock).": "; |
116
|
0
|
0
|
|
|
|
0
|
$IO::Socket::errstr = $@ = join("", $_[0] =~ /^$title/ ? "" : $title, @_); |
117
|
0
|
0
|
|
|
|
0
|
$sock->close() |
118
|
|
|
|
|
|
|
if(defined fileno($sock)); |
119
|
|
|
|
|
|
|
} |
120
|
0
|
|
|
|
|
0
|
$! = $err; |
121
|
0
|
|
|
|
|
0
|
return undef; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub _get_addr { |
125
|
9
|
|
|
9
|
|
51
|
my($sock,$addr_str, $multi) = @_; |
126
|
9
|
|
|
|
|
30
|
my @addr; |
127
|
9
|
50
|
33
|
|
|
97
|
if ($multi && $addr_str !~ /^\d+(?:\.\d+){3}$/) { |
128
|
0
|
|
|
|
|
0
|
(undef, undef, undef, undef, @addr) = gethostbyname($addr_str); |
129
|
|
|
|
|
|
|
} else { |
130
|
9
|
|
|
|
|
1199
|
my $h = inet_aton($addr_str); |
131
|
9
|
50
|
|
|
|
69
|
push(@addr, $h) if defined $h; |
132
|
|
|
|
|
|
|
} |
133
|
9
|
|
|
|
|
49
|
@addr; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub configure { |
137
|
34
|
|
|
34
|
0
|
110
|
my($sock,$arg) = @_; |
138
|
34
|
|
|
|
|
84
|
my($lport,$rport,$laddr,$raddr,$proto,$type); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
$arg->{LocalAddr} = $arg->{LocalHost} |
142
|
34
|
50
|
33
|
|
|
142
|
if exists $arg->{LocalHost} && !exists $arg->{LocalAddr}; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
($laddr,$lport,$proto) = _sock_info($arg->{LocalAddr}, |
145
|
|
|
|
|
|
|
$arg->{LocalPort}, |
146
|
|
|
|
|
|
|
$arg->{Proto}) |
147
|
34
|
50
|
|
|
|
314
|
or return _error($sock, $!, $@); |
148
|
|
|
|
|
|
|
|
149
|
34
|
100
|
|
|
|
4712
|
$laddr = defined $laddr ? inet_aton($laddr) |
150
|
|
|
|
|
|
|
: INADDR_ANY; |
151
|
|
|
|
|
|
|
|
152
|
34
|
50
|
|
|
|
140
|
return _error($sock, $EINVAL, "Bad hostname '",$arg->{LocalAddr},"'") |
153
|
|
|
|
|
|
|
unless(defined $laddr); |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
$arg->{PeerAddr} = $arg->{PeerHost} |
156
|
34
|
50
|
33
|
|
|
156
|
if exists $arg->{PeerHost} && !exists $arg->{PeerAddr}; |
157
|
|
|
|
|
|
|
|
158
|
34
|
100
|
|
|
|
126
|
unless(exists $arg->{Listen}) { |
159
|
|
|
|
|
|
|
($raddr,$rport,$proto) = _sock_info($arg->{PeerAddr}, |
160
|
|
|
|
|
|
|
$arg->{PeerPort}, |
161
|
18
|
50
|
|
|
|
172
|
$proto) |
162
|
|
|
|
|
|
|
or return _error($sock, $!, $@); |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
34
|
|
66
|
|
|
161
|
$proto ||= _get_proto_number('tcp'); |
166
|
|
|
|
|
|
|
|
167
|
34
|
|
33
|
|
|
325
|
$type = $arg->{Type} || $socket_type{lc _get_proto_name($proto)}; |
168
|
|
|
|
|
|
|
|
169
|
34
|
|
|
|
|
90
|
my @raddr = (); |
170
|
|
|
|
|
|
|
|
171
|
34
|
100
|
|
|
|
145
|
if(defined $raddr) { |
172
|
10
|
|
|
|
|
124
|
@raddr = $sock->_get_addr($raddr, $arg->{MultiHomed}); |
173
|
10
|
50
|
|
|
|
505
|
return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'") |
174
|
|
|
|
|
|
|
unless @raddr; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
34
|
|
|
|
|
201
|
while(1) { |
178
|
|
|
|
|
|
|
|
179
|
36
|
50
|
|
|
|
656
|
$sock->socket(AF_INET, $type, $proto) or |
180
|
|
|
|
|
|
|
return _error($sock, $!, "$!"); |
181
|
|
|
|
|
|
|
|
182
|
36
|
100
|
|
|
|
105
|
if (defined $arg->{Blocking}) { |
183
|
|
|
|
|
|
|
defined $sock->blocking($arg->{Blocking}) |
184
|
1
|
50
|
|
|
|
4
|
or return _error($sock, $!, "$!"); |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
36
|
50
|
33
|
|
|
261
|
if ($arg->{Reuse} || $arg->{ReuseAddr}) { |
188
|
0
|
0
|
|
|
|
0
|
$sock->sockopt(SO_REUSEADDR,1) or |
189
|
|
|
|
|
|
|
return _error($sock, $!, "$!"); |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
36
|
50
|
|
|
|
83
|
if ($arg->{ReusePort}) { |
193
|
0
|
0
|
|
|
|
0
|
$sock->sockopt(SO_REUSEPORT,1) or |
194
|
|
|
|
|
|
|
return _error($sock, $!, "$!"); |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
36
|
50
|
|
|
|
86
|
if ($arg->{Broadcast}) { |
198
|
0
|
0
|
|
|
|
0
|
$sock->sockopt(SO_BROADCAST,1) or |
199
|
|
|
|
|
|
|
return _error($sock, $!, "$!"); |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
36
|
100
|
66
|
|
|
314
|
if($lport || ($laddr ne INADDR_ANY) || exists $arg->{Listen}) { |
|
|
|
100
|
|
|
|
|
203
|
23
|
50
|
50
|
|
|
322
|
$sock->bind($lport || 0, $laddr) or |
204
|
|
|
|
|
|
|
return _error($sock, $!, "$!"); |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
36
|
100
|
|
|
|
122
|
if(exists $arg->{Listen}) { |
208
|
16
|
50
|
100
|
|
|
207
|
$sock->listen($arg->{Listen} || 5) or |
209
|
|
|
|
|
|
|
return _error($sock, $!, "$!"); |
210
|
16
|
|
|
|
|
46
|
last; |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
# don't try to connect unless we're given a PeerAddr |
214
|
20
|
100
|
|
|
|
75
|
last unless exists($arg->{PeerAddr}); |
215
|
|
|
|
|
|
|
|
216
|
12
|
|
|
|
|
27
|
$raddr = shift @raddr; |
217
|
|
|
|
|
|
|
|
218
|
12
|
0
|
33
|
|
|
68
|
return _error($sock, $EINVAL, 'Cannot determine remote port') |
|
|
|
33
|
|
|
|
|
219
|
|
|
|
|
|
|
unless($rport || $type == SOCK_DGRAM || $type == SOCK_RAW); |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
last |
222
|
12
|
50
|
66
|
|
|
71
|
unless($type == SOCK_STREAM || defined $raddr); |
223
|
|
|
|
|
|
|
|
224
|
12
|
50
|
|
|
|
70
|
return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'") |
225
|
|
|
|
|
|
|
unless defined $raddr; |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
# my $timeout = ${*$sock}{'io_socket_timeout'}; |
228
|
|
|
|
|
|
|
# my $before = time() if $timeout; |
229
|
|
|
|
|
|
|
|
230
|
12
|
|
|
|
|
55
|
undef $@; |
231
|
12
|
100
|
|
|
|
113
|
if ($sock->connect(pack_sockaddr_in($rport, $raddr))) { |
232
|
|
|
|
|
|
|
# ${*$sock}{'io_socket_timeout'} = $timeout; |
233
|
10
|
|
|
|
|
118
|
return $sock; |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
|
236
|
2
|
50
|
0
|
|
|
335
|
return _error($sock, $!, $@ || "Timeout") |
237
|
|
|
|
|
|
|
unless @raddr; |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
# if ($timeout) { |
240
|
|
|
|
|
|
|
# my $new_timeout = $timeout - (time() - $before); |
241
|
|
|
|
|
|
|
# return _error($sock, |
242
|
|
|
|
|
|
|
# (exists(&Errno::ETIMEDOUT) ? Errno::ETIMEDOUT() : $EINVAL), |
243
|
|
|
|
|
|
|
# "Timeout") if $new_timeout <= 0; |
244
|
|
|
|
|
|
|
# ${*$sock}{'io_socket_timeout'} = $new_timeout; |
245
|
|
|
|
|
|
|
# } |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
|
249
|
24
|
|
|
|
|
245
|
$sock; |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
sub connect { |
253
|
11
|
50
|
33
|
11
|
0
|
155
|
@_ == 2 || @_ == 3 or |
254
|
|
|
|
|
|
|
croak 'usage: $sock->connect(NAME) or $sock->connect(PORT, ADDR)'; |
255
|
11
|
|
|
|
|
31
|
my $sock = shift; |
256
|
11
|
50
|
|
|
|
426
|
return $sock->SUPER::connect(@_ == 1 ? shift : pack_sockaddr_in(@_)); |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
sub bind { |
260
|
23
|
50
|
33
|
23
|
1
|
141
|
@_ == 2 || @_ == 3 or |
261
|
|
|
|
|
|
|
croak 'usage: $sock->bind(NAME) or $sock->bind(PORT, ADDR)'; |
262
|
23
|
|
|
|
|
129
|
my $sock = shift; |
263
|
23
|
50
|
|
|
|
258
|
return $sock->SUPER::bind(@_ == 1 ? shift : pack_sockaddr_in(@_)) |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
sub sockaddr { |
267
|
0
|
0
|
|
0
|
1
|
0
|
@_ == 1 or croak 'usage: $sock->sockaddr()'; |
268
|
0
|
|
|
|
|
0
|
my($sock) = @_; |
269
|
0
|
|
|
|
|
0
|
my $name = $sock->sockname; |
270
|
0
|
0
|
|
|
|
0
|
$name ? (sockaddr_in($name))[1] : undef; |
271
|
|
|
|
|
|
|
} |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
sub sockport { |
274
|
18
|
50
|
|
18
|
1
|
1469
|
@_ == 1 or croak 'usage: $sock->sockport()'; |
275
|
18
|
|
|
|
|
40
|
my($sock) = @_; |
276
|
18
|
|
|
|
|
103
|
my $name = $sock->sockname; |
277
|
18
|
50
|
|
|
|
193
|
$name ? (sockaddr_in($name))[0] : undef; |
278
|
|
|
|
|
|
|
} |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
sub sockhost { |
281
|
0
|
0
|
|
0
|
1
|
|
@_ == 1 or croak 'usage: $sock->sockhost()'; |
282
|
0
|
|
|
|
|
|
my($sock) = @_; |
283
|
0
|
|
|
|
|
|
my $addr = $sock->sockaddr; |
284
|
0
|
0
|
|
|
|
|
$addr ? inet_ntoa($addr) : undef; |
285
|
|
|
|
|
|
|
} |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
sub peeraddr { |
288
|
0
|
0
|
|
0
|
1
|
|
@_ == 1 or croak 'usage: $sock->peeraddr()'; |
289
|
0
|
|
|
|
|
|
my($sock) = @_; |
290
|
0
|
|
|
|
|
|
my $name = $sock->peername; |
291
|
0
|
0
|
|
|
|
|
$name ? (sockaddr_in($name))[1] : undef; |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
sub peerport { |
295
|
0
|
0
|
|
0
|
1
|
|
@_ == 1 or croak 'usage: $sock->peerport()'; |
296
|
0
|
|
|
|
|
|
my($sock) = @_; |
297
|
0
|
|
|
|
|
|
my $name = $sock->peername; |
298
|
0
|
0
|
|
|
|
|
$name ? (sockaddr_in($name))[0] : undef; |
299
|
|
|
|
|
|
|
} |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
sub peerhost { |
302
|
0
|
0
|
|
0
|
1
|
|
@_ == 1 or croak 'usage: $sock->peerhost()'; |
303
|
0
|
|
|
|
|
|
my($sock) = @_; |
304
|
0
|
|
|
|
|
|
my $addr = $sock->peeraddr; |
305
|
0
|
0
|
|
|
|
|
$addr ? inet_ntoa($addr) : undef; |
306
|
|
|
|
|
|
|
} |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
1; |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
__END__ |