Filename | /usr/local/lib/perl5/5.24/mach/IO/Socket/INET.pm |
Statements | Executed 23 statements in 4.01ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 54µs | 5.52ms | BEGIN@12 | IO::Socket::INET::
1 | 1 | 1 | 47µs | 66µs | BEGIN@9 | IO::Socket::INET::
1 | 1 | 1 | 31µs | 5.28ms | BEGIN@11 | IO::Socket::INET::
1 | 1 | 1 | 27µs | 94µs | BEGIN@14 | IO::Socket::INET::
1 | 1 | 1 | 26µs | 231µs | BEGIN@13 | IO::Socket::INET::
1 | 1 | 1 | 25µs | 98µ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 | 2 | 95µs | 2 | 84µs | # spent 66µs (47+18) within IO::Socket::INET::BEGIN@9 which was called:
# once (47µs+18µs) by Mail::SpamAssassin::PerMsgStatus::BEGIN@35 at line 9 # spent 66µs making 1 call to IO::Socket::INET::BEGIN@9
# spent 18µs making 1 call to strict::import |
10 | our(@ISA, $VERSION); | ||||
11 | 2 | 78µs | 2 | 10.5ms | # spent 5.28ms (31µs+5.25) within IO::Socket::INET::BEGIN@11 which was called:
# once (31µs+5.25ms) by Mail::SpamAssassin::PerMsgStatus::BEGIN@35 at line 11 # spent 5.28ms making 1 call to IO::Socket::INET::BEGIN@11
# spent 5.25ms making 1 call to IO::Socket::import |
12 | 2 | 87µs | 2 | 11.0ms | # spent 5.52ms (54µs+5.47) within IO::Socket::INET::BEGIN@12 which was called:
# once (54µs+5.47ms) by Mail::SpamAssassin::PerMsgStatus::BEGIN@35 at line 12 # spent 5.52ms making 1 call to IO::Socket::INET::BEGIN@12
# spent 5.47ms making 1 call to Exporter::import |
13 | 2 | 83µs | 2 | 437µs | # spent 231µs (26+206) within IO::Socket::INET::BEGIN@13 which was called:
# once (26µs+206µs) by Mail::SpamAssassin::PerMsgStatus::BEGIN@35 at line 13 # spent 231µs making 1 call to IO::Socket::INET::BEGIN@13
# spent 206µs making 1 call to Exporter::import |
14 | 2 | 65µs | 2 | 160µs | # spent 94µs (27+67) within IO::Socket::INET::BEGIN@14 which was called:
# once (27µs+67µs) by Mail::SpamAssassin::PerMsgStatus::BEGIN@35 at line 14 # spent 94µs making 1 call to IO::Socket::INET::BEGIN@14
# spent 67µs making 1 call to Exporter::import |
15 | 2 | 3.49ms | 2 | 172µs | # spent 98µs (25+74) within IO::Socket::INET::BEGIN@15 which was called:
# once (25µs+74µs) by Mail::SpamAssassin::PerMsgStatus::BEGIN@35 at line 15 # spent 98µs making 1 call to IO::Socket::INET::BEGIN@15
# spent 74µs making 1 call to Exporter::import |
16 | |||||
17 | 1 | 18µs | @ISA = qw(IO::Socket); | ||
18 | 1 | 2µs | $VERSION = "1.35"; | ||
19 | |||||
20 | 1 | 9µs | my $EINVAL = exists(&Errno::EINVAL) ? Errno::EINVAL() : 1; | ||
21 | |||||
22 | 1 | 17µs | 1 | 10µs | IO::Socket::INET->register_domain( AF_INET ); # spent 10µs making 1 call to IO::Socket::register_domain |
23 | |||||
24 | 1 | 11µs | my %socket_type = ( tcp => SOCK_STREAM, | ||
25 | udp => SOCK_DGRAM, | ||||
26 | icmp => SOCK_RAW | ||||
27 | ); | ||||
28 | 1 | 2µs | my %proto_number; | ||
29 | 1 | 9µs | $proto_number{tcp} = Socket::IPPROTO_TCP() if defined &Socket::IPPROTO_TCP; | ||
30 | 1 | 2µs | $proto_number{udp} = Socket::IPPROTO_UDP() if defined &Socket::IPPROTO_UDP; | ||
31 | 1 | 2µs | $proto_number{icmp} = Socket::IPPROTO_ICMP() if defined &Socket::IPPROTO_ICMP; | ||
32 | 1 | 12µ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 = eval { 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 = eval { 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 | |||||
142 | $arg->{LocalAddr} = $arg->{LocalHost} | ||||
143 | if exists $arg->{LocalHost} && !exists $arg->{LocalAddr}; | ||||
144 | |||||
145 | ($laddr,$lport,$proto) = _sock_info($arg->{LocalAddr}, | ||||
146 | $arg->{LocalPort}, | ||||
147 | $arg->{Proto}) | ||||
148 | or return _error($sock, $!, $@); | ||||
149 | |||||
150 | $laddr = defined $laddr ? inet_aton($laddr) | ||||
151 | : INADDR_ANY; | ||||
152 | |||||
153 | return _error($sock, $EINVAL, "Bad hostname '",$arg->{LocalAddr},"'") | ||||
154 | unless(defined $laddr); | ||||
155 | |||||
156 | $arg->{PeerAddr} = $arg->{PeerHost} | ||||
157 | if exists $arg->{PeerHost} && !exists $arg->{PeerAddr}; | ||||
158 | |||||
159 | unless(exists $arg->{Listen}) { | ||||
160 | ($raddr,$rport,$proto) = _sock_info($arg->{PeerAddr}, | ||||
161 | $arg->{PeerPort}, | ||||
162 | $proto) | ||||
163 | or return _error($sock, $!, $@); | ||||
164 | } | ||||
165 | |||||
166 | $proto ||= _get_proto_number('tcp'); | ||||
167 | |||||
168 | $type = $arg->{Type} || $socket_type{lc _get_proto_name($proto)}; | ||||
169 | |||||
170 | my @raddr = (); | ||||
171 | |||||
172 | if(defined $raddr) { | ||||
173 | @raddr = $sock->_get_addr($raddr, $arg->{MultiHomed}); | ||||
174 | return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'") | ||||
175 | unless @raddr; | ||||
176 | } | ||||
177 | |||||
178 | while(1) { | ||||
179 | |||||
180 | $sock->socket(AF_INET, $type, $proto) or | ||||
181 | return _error($sock, $!, "$!"); | ||||
182 | |||||
183 | if (defined $arg->{Blocking}) { | ||||
184 | defined $sock->blocking($arg->{Blocking}) | ||||
185 | or return _error($sock, $!, "$!"); | ||||
186 | } | ||||
187 | |||||
188 | if ($arg->{Reuse} || $arg->{ReuseAddr}) { | ||||
189 | $sock->sockopt(SO_REUSEADDR,1) or | ||||
190 | return _error($sock, $!, "$!"); | ||||
191 | } | ||||
192 | |||||
193 | if ($arg->{ReusePort}) { | ||||
194 | $sock->sockopt(SO_REUSEPORT,1) or | ||||
195 | return _error($sock, $!, "$!"); | ||||
196 | } | ||||
197 | |||||
198 | if ($arg->{Broadcast}) { | ||||
199 | $sock->sockopt(SO_BROADCAST,1) or | ||||
200 | return _error($sock, $!, "$!"); | ||||
201 | } | ||||
202 | |||||
203 | if($lport || ($laddr ne INADDR_ANY) || exists $arg->{Listen}) { | ||||
204 | $sock->bind($lport || 0, $laddr) or | ||||
205 | return _error($sock, $!, "$!"); | ||||
206 | } | ||||
207 | |||||
208 | if(exists $arg->{Listen}) { | ||||
209 | $sock->listen($arg->{Listen} || 5) or | ||||
210 | return _error($sock, $!, "$!"); | ||||
211 | last; | ||||
212 | } | ||||
213 | |||||
214 | # don't try to connect unless we're given a PeerAddr | ||||
215 | last unless exists($arg->{PeerAddr}); | ||||
216 | |||||
217 | $raddr = shift @raddr; | ||||
218 | |||||
219 | return _error($sock, $EINVAL, 'Cannot determine remote port') | ||||
220 | unless($rport || $type == SOCK_DGRAM || $type == SOCK_RAW); | ||||
221 | |||||
222 | last | ||||
223 | unless($type == SOCK_STREAM || defined $raddr); | ||||
224 | |||||
225 | return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'") | ||||
226 | unless defined $raddr; | ||||
227 | |||||
228 | # my $timeout = ${*$sock}{'io_socket_timeout'}; | ||||
229 | # my $before = time() if $timeout; | ||||
230 | |||||
231 | undef $@; | ||||
232 | if ($sock->connect(pack_sockaddr_in($rport, $raddr))) { | ||||
233 | # ${*$sock}{'io_socket_timeout'} = $timeout; | ||||
234 | return $sock; | ||||
235 | } | ||||
236 | |||||
237 | return _error($sock, $!, $@ || "Timeout") | ||||
238 | unless @raddr; | ||||
239 | |||||
240 | # if ($timeout) { | ||||
241 | # my $new_timeout = $timeout - (time() - $before); | ||||
242 | # return _error($sock, | ||||
243 | # (exists(&Errno::ETIMEDOUT) ? Errno::ETIMEDOUT() : $EINVAL), | ||||
244 | # "Timeout") if $new_timeout <= 0; | ||||
245 | # ${*$sock}{'io_socket_timeout'} = $new_timeout; | ||||
246 | # } | ||||
247 | |||||
248 | } | ||||
249 | |||||
250 | $sock; | ||||
251 | } | ||||
252 | |||||
253 | sub connect { | ||||
254 | @_ == 2 || @_ == 3 or | ||||
255 | croak 'usage: $sock->connect(NAME) or $sock->connect(PORT, ADDR)'; | ||||
256 | my $sock = shift; | ||||
257 | return $sock->SUPER::connect(@_ == 1 ? shift : pack_sockaddr_in(@_)); | ||||
258 | } | ||||
259 | |||||
260 | sub bind { | ||||
261 | @_ == 2 || @_ == 3 or | ||||
262 | croak 'usage: $sock->bind(NAME) or $sock->bind(PORT, ADDR)'; | ||||
263 | my $sock = shift; | ||||
264 | return $sock->SUPER::bind(@_ == 1 ? shift : pack_sockaddr_in(@_)) | ||||
265 | } | ||||
266 | |||||
267 | sub sockaddr { | ||||
268 | @_ == 1 or croak 'usage: $sock->sockaddr()'; | ||||
269 | my($sock) = @_; | ||||
270 | my $name = $sock->sockname; | ||||
271 | $name ? (sockaddr_in($name))[1] : undef; | ||||
272 | } | ||||
273 | |||||
274 | sub sockport { | ||||
275 | @_ == 1 or croak 'usage: $sock->sockport()'; | ||||
276 | my($sock) = @_; | ||||
277 | my $name = $sock->sockname; | ||||
278 | $name ? (sockaddr_in($name))[0] : undef; | ||||
279 | } | ||||
280 | |||||
281 | sub sockhost { | ||||
282 | @_ == 1 or croak 'usage: $sock->sockhost()'; | ||||
283 | my($sock) = @_; | ||||
284 | my $addr = $sock->sockaddr; | ||||
285 | $addr ? inet_ntoa($addr) : undef; | ||||
286 | } | ||||
287 | |||||
288 | sub peeraddr { | ||||
289 | @_ == 1 or croak 'usage: $sock->peeraddr()'; | ||||
290 | my($sock) = @_; | ||||
291 | my $name = $sock->peername; | ||||
292 | $name ? (sockaddr_in($name))[1] : undef; | ||||
293 | } | ||||
294 | |||||
295 | sub peerport { | ||||
296 | @_ == 1 or croak 'usage: $sock->peerport()'; | ||||
297 | my($sock) = @_; | ||||
298 | my $name = $sock->peername; | ||||
299 | $name ? (sockaddr_in($name))[0] : undef; | ||||
300 | } | ||||
301 | |||||
302 | sub peerhost { | ||||
303 | @_ == 1 or croak 'usage: $sock->peerhost()'; | ||||
304 | my($sock) = @_; | ||||
305 | my $addr = $sock->peeraddr; | ||||
306 | $addr ? inet_ntoa($addr) : undef; | ||||
307 | } | ||||
308 | |||||
309 | 1 | 28µs | 1; | ||
310 | |||||
311 | __END__ |