Filename | /usr/lib/perl/5.10/IO/Socket/INET.pm |
Statements | Executed 30 statements in 3.21ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 48µs | 60µs | BEGIN@9 | IO::Socket::INET::
1 | 1 | 1 | 37µs | 1.57ms | BEGIN@12 | IO::Socket::INET::
1 | 1 | 1 | 25µs | 120µs | BEGIN@13 | IO::Socket::INET::
1 | 1 | 1 | 22µs | 64µs | BEGIN@14 | IO::Socket::INET::
1 | 1 | 1 | 22µs | 2.00ms | BEGIN@11 | IO::Socket::INET::
1 | 1 | 1 | 21µs | 59µs | BEGIN@15 | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | _cache_proto | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | _error | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | _get_addr | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | _get_proto_name | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | _get_proto_number | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | _sock_info | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | bind | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | configure | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | connect | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | new | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | peeraddr | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | peerhost | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | peerport | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | sockaddr | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | sockhost | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | sockport | IO::Socket::INET::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # IO::Socket::INET.pm | ||||
2 | # | ||||
3 | # Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. 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 | 3 | 90µs | 2 | 71µs | # spent 60µs (48+12) within IO::Socket::INET::BEGIN@9 which was called:
# once (48µs+12µs) by Net::LDAP::BEGIN@8 at line 9 # spent 60µs making 1 call to IO::Socket::INET::BEGIN@9
# spent 12µs making 1 call to strict::import |
10 | 1 | 2µs | our(@ISA, $VERSION); | ||
11 | 3 | 60µs | 2 | 3.98ms | # spent 2.00ms (22µs+1.98) within IO::Socket::INET::BEGIN@11 which was called:
# once (22µs+1.98ms) by Net::LDAP::BEGIN@8 at line 11 # spent 2.00ms making 1 call to IO::Socket::INET::BEGIN@11
# spent 1.98ms making 1 call to IO::Socket::import |
12 | 3 | 74µs | 2 | 3.09ms | # spent 1.57ms (37µs+1.53) within IO::Socket::INET::BEGIN@12 which was called:
# once (37µs+1.53ms) by Net::LDAP::BEGIN@8 at line 12 # spent 1.57ms making 1 call to IO::Socket::INET::BEGIN@12
# spent 1.53ms making 1 call to Exporter::import |
13 | 3 | 61µs | 2 | 216µs | # spent 120µs (25+95) within IO::Socket::INET::BEGIN@13 which was called:
# once (25µs+95µs) by Net::LDAP::BEGIN@8 at line 13 # spent 120µs making 1 call to IO::Socket::INET::BEGIN@13
# spent 95µs making 1 call to Exporter::import |
14 | 3 | 60µs | 2 | 106µs | # spent 64µs (22+42) within IO::Socket::INET::BEGIN@14 which was called:
# once (22µs+42µs) by Net::LDAP::BEGIN@8 at line 14 # spent 64µs making 1 call to IO::Socket::INET::BEGIN@14
# spent 42µs making 1 call to Exporter::import |
15 | 3 | 2.81ms | 2 | 96µs | # spent 59µs (21+37) within IO::Socket::INET::BEGIN@15 which was called:
# once (21µs+37µs) by Net::LDAP::BEGIN@8 at line 15 # spent 59µs making 1 call to IO::Socket::INET::BEGIN@15
# spent 38µs making 1 call to Exporter::import |
16 | |||||
17 | 1 | 16µs | @ISA = qw(IO::Socket); | ||
18 | 1 | 500ns | $VERSION = "1.31"; | ||
19 | |||||
20 | 1 | 1µs | my $EINVAL = exists(&Errno::EINVAL) ? Errno::EINVAL() : 1; | ||
21 | |||||
22 | 1 | 9µs | 1 | 6µs | IO::Socket::INET->register_domain( AF_INET ); # spent 6µs making 1 call to IO::Socket::register_domain |
23 | |||||
24 | 1 | 3µs | my %socket_type = ( tcp => SOCK_STREAM, | ||
25 | udp => SOCK_DGRAM, | ||||
26 | icmp => SOCK_RAW | ||||
27 | ); | ||||
28 | 1 | 300ns | my %proto_number; | ||
29 | 1 | 1µs | $proto_number{tcp} = Socket::IPPROTO_TCP() if defined &Socket::IPPROTO_TCP; | ||
30 | 1 | 700ns | $proto_number{udp} = Socket::IPPROTO_UDP() if defined &Socket::IPPROTO_UDP; | ||
31 | 1 | 500ns | $proto_number{icmp} = Socket::IPPROTO_ICMP() if defined &Socket::IPPROTO_ICMP; | ||
32 | 1 | 6µs | my %proto_name = reverse %proto_number; | ||
33 | |||||
34 | sub new { | ||||
35 | my $class = shift; | ||||
36 | unshift(@_, "PeerAddr") if @_ == 1; | ||||
37 | return $class->SUPER::new(@_); | ||||
38 | } | ||||
39 | |||||
40 | sub _cache_proto { | ||||
41 | my @proto = @_; | ||||
42 | for (map lc($_), $proto[0], split(' ', $proto[1])) { | ||||
43 | $proto_number{$_} = $proto[2]; | ||||
44 | } | ||||
45 | $proto_name{$proto[2]} = $proto[0]; | ||||
46 | } | ||||
47 | |||||
48 | sub _get_proto_number { | ||||
49 | my $name = lc(shift); | ||||
50 | return undef unless defined $name; | ||||
51 | return $proto_number{$name} if exists $proto_number{$name}; | ||||
52 | |||||
53 | my @proto = getprotobyname($name); | ||||
54 | return undef unless @proto; | ||||
55 | _cache_proto(@proto); | ||||
56 | |||||
57 | return $proto[2]; | ||||
58 | } | ||||
59 | |||||
60 | sub _get_proto_name { | ||||
61 | my $num = shift; | ||||
62 | return undef unless defined $num; | ||||
63 | return $proto_name{$num} if exists $proto_name{$num}; | ||||
64 | |||||
65 | my @proto = getprotobynumber($num); | ||||
66 | return undef unless @proto; | ||||
67 | _cache_proto(@proto); | ||||
68 | |||||
69 | return $proto[0]; | ||||
70 | } | ||||
71 | |||||
72 | sub _sock_info { | ||||
73 | my($addr,$port,$proto) = @_; | ||||
74 | my $origport = $port; | ||||
75 | my @serv = (); | ||||
76 | |||||
77 | $port = $1 | ||||
78 | if(defined $addr && $addr =~ s,:([\w\(\)/]+)$,,); | ||||
79 | |||||
80 | if(defined $proto && $proto =~ /\D/) { | ||||
81 | my $num = _get_proto_number($proto); | ||||
82 | unless (defined $num) { | ||||
83 | $@ = "Bad protocol '$proto'"; | ||||
84 | return; | ||||
85 | } | ||||
86 | $proto = $num; | ||||
87 | } | ||||
88 | |||||
89 | if(defined $port) { | ||||
90 | my $defport = ($port =~ s,\((\d+)\)$,,) ? $1 : undef; | ||||
91 | my $pnum = ($port =~ m,^(\d+)$,)[0]; | ||||
92 | |||||
93 | @serv = getservbyname($port, _get_proto_name($proto) || "") | ||||
94 | if ($port =~ m,\D,); | ||||
95 | |||||
96 | $port = $serv[2] || $defport || $pnum; | ||||
97 | unless (defined $port) { | ||||
98 | $@ = "Bad service '$origport'"; | ||||
99 | return; | ||||
100 | } | ||||
101 | |||||
102 | $proto = _get_proto_number($serv[3]) if @serv && !$proto; | ||||
103 | } | ||||
104 | |||||
105 | return ($addr || undef, | ||||
106 | $port || undef, | ||||
107 | $proto || undef | ||||
108 | ); | ||||
109 | } | ||||
110 | |||||
111 | sub _error { | ||||
112 | my $sock = shift; | ||||
113 | my $err = shift; | ||||
114 | { | ||||
115 | local($!); | ||||
116 | my $title = ref($sock).": "; | ||||
117 | $@ = join("", $_[0] =~ /^$title/ ? "" : $title, @_); | ||||
118 | $sock->close() | ||||
119 | if(defined fileno($sock)); | ||||
120 | } | ||||
121 | $! = $err; | ||||
122 | return undef; | ||||
123 | } | ||||
124 | |||||
125 | sub _get_addr { | ||||
126 | my($sock,$addr_str, $multi) = @_; | ||||
127 | my @addr; | ||||
128 | if ($multi && $addr_str !~ /^\d+(?:\.\d+){3}$/) { | ||||
129 | (undef, undef, undef, undef, @addr) = gethostbyname($addr_str); | ||||
130 | } else { | ||||
131 | my $h = inet_aton($addr_str); | ||||
132 | push(@addr, $h) if defined $h; | ||||
133 | } | ||||
134 | @addr; | ||||
135 | } | ||||
136 | |||||
137 | sub configure { | ||||
138 | my($sock,$arg) = @_; | ||||
139 | my($lport,$rport,$laddr,$raddr,$proto,$type); | ||||
140 | |||||
141 | $arg->{LocalAddr} = $arg->{LocalHost} | ||||
142 | if exists $arg->{LocalHost} && !exists $arg->{LocalAddr}; | ||||
143 | |||||
144 | ($laddr,$lport,$proto) = _sock_info($arg->{LocalAddr}, | ||||
145 | $arg->{LocalPort}, | ||||
146 | $arg->{Proto}) | ||||
147 | or return _error($sock, $!, $@); | ||||
148 | |||||
149 | $laddr = defined $laddr ? inet_aton($laddr) | ||||
150 | : INADDR_ANY; | ||||
151 | |||||
152 | return _error($sock, $EINVAL, "Bad hostname '",$arg->{LocalAddr},"'") | ||||
153 | unless(defined $laddr); | ||||
154 | |||||
155 | $arg->{PeerAddr} = $arg->{PeerHost} | ||||
156 | if exists $arg->{PeerHost} && !exists $arg->{PeerAddr}; | ||||
157 | |||||
158 | unless(exists $arg->{Listen}) { | ||||
159 | ($raddr,$rport,$proto) = _sock_info($arg->{PeerAddr}, | ||||
160 | $arg->{PeerPort}, | ||||
161 | $proto) | ||||
162 | or return _error($sock, $!, $@); | ||||
163 | } | ||||
164 | |||||
165 | $proto ||= _get_proto_number('tcp'); | ||||
166 | |||||
167 | $type = $arg->{Type} || $socket_type{lc _get_proto_name($proto)}; | ||||
168 | |||||
169 | my @raddr = (); | ||||
170 | |||||
171 | if(defined $raddr) { | ||||
172 | @raddr = $sock->_get_addr($raddr, $arg->{MultiHomed}); | ||||
173 | return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'") | ||||
174 | unless @raddr; | ||||
175 | } | ||||
176 | |||||
177 | while(1) { | ||||
178 | |||||
179 | $sock->socket(AF_INET, $type, $proto) or | ||||
180 | return _error($sock, $!, "$!"); | ||||
181 | |||||
182 | if (defined $arg->{Blocking}) { | ||||
183 | defined $sock->blocking($arg->{Blocking}) | ||||
184 | or return _error($sock, $!, "$!"); | ||||
185 | } | ||||
186 | |||||
187 | if ($arg->{Reuse} || $arg->{ReuseAddr}) { | ||||
188 | $sock->sockopt(SO_REUSEADDR,1) or | ||||
189 | return _error($sock, $!, "$!"); | ||||
190 | } | ||||
191 | |||||
192 | if ($arg->{ReusePort}) { | ||||
193 | $sock->sockopt(SO_REUSEPORT,1) or | ||||
194 | return _error($sock, $!, "$!"); | ||||
195 | } | ||||
196 | |||||
197 | if ($arg->{Broadcast}) { | ||||
198 | $sock->sockopt(SO_BROADCAST,1) or | ||||
199 | return _error($sock, $!, "$!"); | ||||
200 | } | ||||
201 | |||||
202 | if($lport || ($laddr ne INADDR_ANY) || exists $arg->{Listen}) { | ||||
203 | $sock->bind($lport || 0, $laddr) or | ||||
204 | return _error($sock, $!, "$!"); | ||||
205 | } | ||||
206 | |||||
207 | if(exists $arg->{Listen}) { | ||||
208 | $sock->listen($arg->{Listen} || 5) or | ||||
209 | return _error($sock, $!, "$!"); | ||||
210 | last; | ||||
211 | } | ||||
212 | |||||
213 | # don't try to connect unless we're given a PeerAddr | ||||
214 | last unless exists($arg->{PeerAddr}); | ||||
215 | |||||
216 | $raddr = shift @raddr; | ||||
217 | |||||
218 | return _error($sock, $EINVAL, 'Cannot determine remote port') | ||||
219 | unless($rport || $type == SOCK_DGRAM || $type == SOCK_RAW); | ||||
220 | |||||
221 | last | ||||
222 | unless($type == SOCK_STREAM || defined $raddr); | ||||
223 | |||||
224 | 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 | undef $@; | ||||
231 | if ($sock->connect(pack_sockaddr_in($rport, $raddr))) { | ||||
232 | # ${*$sock}{'io_socket_timeout'} = $timeout; | ||||
233 | return $sock; | ||||
234 | } | ||||
235 | |||||
236 | 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 | $sock; | ||||
250 | } | ||||
251 | |||||
252 | sub connect { | ||||
253 | @_ == 2 || @_ == 3 or | ||||
254 | croak 'usage: $sock->connect(NAME) or $sock->connect(PORT, ADDR)'; | ||||
255 | my $sock = shift; | ||||
256 | return $sock->SUPER::connect(@_ == 1 ? shift : pack_sockaddr_in(@_)); | ||||
257 | } | ||||
258 | |||||
259 | sub bind { | ||||
260 | @_ == 2 || @_ == 3 or | ||||
261 | croak 'usage: $sock->bind(NAME) or $sock->bind(PORT, ADDR)'; | ||||
262 | my $sock = shift; | ||||
263 | return $sock->SUPER::bind(@_ == 1 ? shift : pack_sockaddr_in(@_)) | ||||
264 | } | ||||
265 | |||||
266 | sub sockaddr { | ||||
267 | @_ == 1 or croak 'usage: $sock->sockaddr()'; | ||||
268 | my($sock) = @_; | ||||
269 | my $name = $sock->sockname; | ||||
270 | $name ? (sockaddr_in($name))[1] : undef; | ||||
271 | } | ||||
272 | |||||
273 | sub sockport { | ||||
274 | @_ == 1 or croak 'usage: $sock->sockport()'; | ||||
275 | my($sock) = @_; | ||||
276 | my $name = $sock->sockname; | ||||
277 | $name ? (sockaddr_in($name))[0] : undef; | ||||
278 | } | ||||
279 | |||||
280 | sub sockhost { | ||||
281 | @_ == 1 or croak 'usage: $sock->sockhost()'; | ||||
282 | my($sock) = @_; | ||||
283 | my $addr = $sock->sockaddr; | ||||
284 | $addr ? inet_ntoa($addr) : undef; | ||||
285 | } | ||||
286 | |||||
287 | sub peeraddr { | ||||
288 | @_ == 1 or croak 'usage: $sock->peeraddr()'; | ||||
289 | my($sock) = @_; | ||||
290 | my $name = $sock->peername; | ||||
291 | $name ? (sockaddr_in($name))[1] : undef; | ||||
292 | } | ||||
293 | |||||
294 | sub peerport { | ||||
295 | @_ == 1 or croak 'usage: $sock->peerport()'; | ||||
296 | my($sock) = @_; | ||||
297 | my $name = $sock->peername; | ||||
298 | $name ? (sockaddr_in($name))[0] : undef; | ||||
299 | } | ||||
300 | |||||
301 | sub peerhost { | ||||
302 | @_ == 1 or croak 'usage: $sock->peerhost()'; | ||||
303 | my($sock) = @_; | ||||
304 | my $addr = $sock->peeraddr; | ||||
305 | $addr ? inet_ntoa($addr) : undef; | ||||
306 | } | ||||
307 | |||||
308 | 1 | 12µs | 1; | ||
309 | |||||
310 | __END__ |