File | /usr/local/lib/perl5/5.10.1/darwin-2level/IO/Socket/INET.pm |
Statements Executed | 664 |
Statement Execution Time | 108ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 2.39ms | 6.47ms | BEGIN@11 | IO::Socket::INET::
41 | 1 | 1 | 562µs | 1.38ms | peeraddr | IO::Socket::INET::
41 | 1 | 1 | 503µs | 2.07ms | peerhost | IO::Socket::INET::
41 | 1 | 1 | 445µs | 762µs | peerport | IO::Socket::INET::
2 | 1 | 1 | 201µs | 181ms | configure | IO::Socket::INET::
4 | 2 | 1 | 69µs | 91µs | _sock_info | IO::Socket::INET::
2 | 1 | 1 | 66µs | 105ms | _get_addr | IO::Socket::INET::
2 | 1 | 1 | 37µs | 75.6ms | connect | IO::Socket::INET::
2 | 1 | 1 | 31µs | 181ms | new | IO::Socket::INET::
1 | 1 | 1 | 28µs | 802µs | BEGIN@12 | IO::Socket::INET::
1 | 1 | 1 | 15µs | 18µs | BEGIN@9 | IO::Socket::INET::
8 | 3 | 2 | 11µs | 11µs | CORE:match (opcode) | IO::Socket::INET::
2 | 1 | 1 | 9µs | 9µs | _get_proto_number | IO::Socket::INET::
1 | 1 | 1 | 8µs | 37µs | BEGIN@13 | IO::Socket::INET::
2 | 1 | 1 | 8µs | 8µs | _get_proto_name | IO::Socket::INET::
1 | 1 | 1 | 7µs | 20µs | BEGIN@14 | IO::Socket::INET::
1 | 1 | 1 | 7µs | 19µs | BEGIN@15 | IO::Socket::INET::
4 | 2 | 2 | 2µs | 2µs | CORE:subst (opcode) | 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 | bind | 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 | 31µs | 2 | 21µs | # spent 18µs (15+3) within IO::Socket::INET::BEGIN@9 which was called
# once (15µs+3µs) by LWP::Protocol::implementor at line 9 # spent 18µs making 1 call to IO::Socket::INET::BEGIN@9
# spent 3µs making 1 call to strict::import |
10 | 1 | 400ns | our(@ISA, $VERSION); | ||
11 | 3 | 121µs | 2 | 7.13ms | # spent 6.47ms (2.39+4.08) within IO::Socket::INET::BEGIN@11 which was called
# once (2.39ms+4.08ms) by LWP::Protocol::implementor at line 11 # spent 6.47ms making 1 call to IO::Socket::INET::BEGIN@11
# spent 659µs making 1 call to IO::Socket::import |
12 | 3 | 25µs | 2 | 1.58ms | # spent 802µs (28+774) within IO::Socket::INET::BEGIN@12 which was called
# once (28µs+774µs) by LWP::Protocol::implementor at line 12 # spent 802µs making 1 call to IO::Socket::INET::BEGIN@12
# spent 774µs making 1 call to Exporter::import |
13 | 3 | 25µs | 2 | 67µs | # spent 37µs (8+29) within IO::Socket::INET::BEGIN@13 which was called
# once (8µs+29µs) by LWP::Protocol::implementor at line 13 # spent 37µs making 1 call to IO::Socket::INET::BEGIN@13
# spent 29µs making 1 call to Exporter::import |
14 | 3 | 20µs | 2 | 33µs | # spent 20µs (7+13) within IO::Socket::INET::BEGIN@14 which was called
# once (7µs+13µs) by LWP::Protocol::implementor at line 14 # spent 20µs making 1 call to IO::Socket::INET::BEGIN@14
# spent 13µs making 1 call to Exporter::import |
15 | 3 | 1.24ms | 2 | 30µs | # spent 19µs (7+12) within IO::Socket::INET::BEGIN@15 which was called
# once (7µs+12µs) by LWP::Protocol::implementor at line 15 # spent 19µs making 1 call to IO::Socket::INET::BEGIN@15
# spent 12µs making 1 call to Exporter::import |
16 | |||||
17 | 1 | 8µs | @ISA = qw(IO::Socket); | ||
18 | 1 | 400ns | $VERSION = "1.31"; | ||
19 | |||||
20 | 1 | 700ns | my $EINVAL = exists(&Errno::EINVAL) ? Errno::EINVAL() : 1; | ||
21 | |||||
22 | 1 | 9µs | 1 | 3µs | IO::Socket::INET->register_domain( AF_INET ); # spent 3µs making 1 call to IO::Socket::register_domain |
23 | |||||
24 | 1 | 1µs | my %socket_type = ( tcp => SOCK_STREAM, | ||
25 | udp => SOCK_DGRAM, | ||||
26 | icmp => SOCK_RAW | ||||
27 | ); | ||||
28 | 1 | 100ns | my %proto_number; | ||
29 | 1 | 700ns | $proto_number{tcp} = Socket::IPPROTO_TCP() if defined &Socket::IPPROTO_TCP; | ||
30 | 1 | 200ns | $proto_number{udp} = Socket::IPPROTO_UDP() if defined &Socket::IPPROTO_UDP; | ||
31 | 1 | 200ns | $proto_number{icmp} = Socket::IPPROTO_ICMP() if defined &Socket::IPPROTO_ICMP; | ||
32 | 1 | 4µs | my %proto_name = reverse %proto_number; | ||
33 | |||||
34 | # spent 181ms (31µs+181) within IO::Socket::INET::new which was called 2 times, avg 90.7ms/call:
# 2 times (31µs+181ms) by Net::HTTP::new at line 19 of Net/HTTP.pm, avg 90.7ms/call | ||||
35 | 2 | 1µs | my $class = shift; | ||
36 | 2 | 1µs | unshift(@_, "PeerAddr") if @_ == 1; | ||
37 | 2 | 32µs | 2 | 181ms | return $class->SUPER::new(@_); # spent 181ms making 2 calls to IO::Socket::new, avg 90.7ms/call |
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 | # spent 9µs within IO::Socket::INET::_get_proto_number which was called 2 times, avg 5µs/call:
# 2 times (9µs+0s) by IO::Socket::INET::_sock_info at line 81, avg 5µs/call | ||||
49 | 2 | 2µs | my $name = lc(shift); | ||
50 | 2 | 500ns | return undef unless defined $name; | ||
51 | 2 | 8µs | 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 | # spent 8µs within IO::Socket::INET::_get_proto_name which was called 2 times, avg 4µs/call:
# 2 times (8µs+0s) by IO::Socket::INET::configure at line 168, avg 4µs/call | ||||
61 | 2 | 1µs | my $num = shift; | ||
62 | 2 | 500ns | return undef unless defined $num; | ||
63 | 2 | 8µs | 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 | 4 | 5µs | my($addr,$port,$proto) = @_; | ||
74 | 4 | 1µs | my $origport = $port; | ||
75 | 4 | 1µs | my @serv = (); | ||
76 | |||||
77 | 4 | 8µs | 2 | 1µs | $port = $1 # spent 1µs making 2 calls to IO::Socket::INET::CORE:subst, avg 550ns/call |
78 | if(defined $addr && $addr =~ s,:([\w\(\)/]+)$,,); | ||||
79 | |||||
80 | 4 | 17µs | 4 | 5µs | if(defined $proto && $proto =~ /\D/) { # spent 5µs making 4 calls to IO::Socket::INET::CORE:match, avg 1µs/call |
81 | 2 | 5µs | 2 | 9µs | my $num = _get_proto_number($proto); # spent 9µs making 2 calls to IO::Socket::INET::_get_proto_number, avg 5µs/call |
82 | 2 | 500ns | unless (defined $num) { | ||
83 | $@ = "Bad protocol '$proto'"; | ||||
84 | return; | ||||
85 | } | ||||
86 | 2 | 900ns | $proto = $num; | ||
87 | } | ||||
88 | |||||
89 | 4 | 1µs | if(defined $port) { | ||
90 | 2 | 6µs | 2 | 400ns | my $defport = ($port =~ s,\((\d+)\)$,,) ? $1 : undef; # spent 400ns making 2 calls to IO::Socket::INET::CORE:subst, avg 200ns/call |
91 | 2 | 12µs | 2 | 6µs | my $pnum = ($port =~ m,^(\d+)$,)[0]; # spent 6µs making 2 calls to IO::Socket::INET::CORE:match, avg 3µs/call |
92 | |||||
93 | 2 | 5µs | 2 | 700ns | @serv = getservbyname($port, _get_proto_name($proto) || "") # spent 700ns making 2 calls to IO::Socket::INET::CORE:match, avg 350ns/call |
94 | if ($port =~ m,\D,); | ||||
95 | |||||
96 | 2 | 1µs | $port = $serv[2] || $defport || $pnum; | ||
97 | 2 | 500ns | unless (defined $port) { | ||
98 | $@ = "Bad service '$origport'"; | ||||
99 | return; | ||||
100 | } | ||||
101 | |||||
102 | 2 | 2µs | $proto = _get_proto_number($serv[3]) if @serv && !$proto; | ||
103 | } | ||||
104 | |||||
105 | 4 | 14µs | 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 | # spent 105ms (66µs+105) within IO::Socket::INET::_get_addr which was called 2 times, avg 52.4ms/call:
# 2 times (66µs+105ms) by IO::Socket::INET::configure at line 173, avg 52.4ms/call | ||||
126 | 2 | 3µs | my($sock,$addr_str, $multi) = @_; | ||
127 | 2 | 300ns | my @addr; | ||
128 | 2 | 2µs | if ($multi && $addr_str !~ /^\d+(?:\.\d+){3}$/) { | ||
129 | (undef, undef, undef, undef, @addr) = gethostbyname($addr_str); | ||||
130 | } else { | ||||
131 | 2 | 105ms | 2 | 105ms | my $h = inet_aton($addr_str); # spent 105ms making 2 calls to Socket::inet_aton, avg 52.3ms/call |
132 | 2 | 9µs | push(@addr, $h) if defined $h; | ||
133 | } | ||||
134 | 2 | 16µs | @addr; | ||
135 | } | ||||
136 | |||||
137 | # spent 181ms (201µs+181) within IO::Socket::INET::configure which was called 2 times, avg 90.4ms/call:
# 2 times (201µs+181ms) by Net::HTTP::http_connect at line 29 of Net/HTTP.pm, avg 90.4ms/call | ||||
138 | 2 | 1µs | my($sock,$arg) = @_; | ||
139 | 2 | 1µs | my($lport,$rport,$laddr,$raddr,$proto,$type); | ||
140 | |||||
141 | |||||
142 | 2 | 2µs | $arg->{LocalAddr} = $arg->{LocalHost} | ||
143 | if exists $arg->{LocalHost} && !exists $arg->{LocalAddr}; | ||||
144 | |||||
145 | 2 | 11µs | 2 | 44µs | ($laddr,$lport,$proto) = _sock_info($arg->{LocalAddr}, # spent 44µs making 2 calls to IO::Socket::INET::_sock_info, avg 22µs/call |
146 | $arg->{LocalPort}, | ||||
147 | $arg->{Proto}) | ||||
148 | or return _error($sock, $!, $@); | ||||
149 | |||||
150 | 2 | 1µs | $laddr = defined $laddr ? inet_aton($laddr) | ||
151 | : INADDR_ANY; | ||||
152 | |||||
153 | 2 | 400ns | return _error($sock, $EINVAL, "Bad hostname '",$arg->{LocalAddr},"'") | ||
154 | unless(defined $laddr); | ||||
155 | |||||
156 | 2 | 1µs | $arg->{PeerAddr} = $arg->{PeerHost} | ||
157 | if exists $arg->{PeerHost} && !exists $arg->{PeerAddr}; | ||||
158 | |||||
159 | 2 | 3µs | unless(exists $arg->{Listen}) { | ||
160 | 2 | 7µs | 2 | 47µs | ($raddr,$rport,$proto) = _sock_info($arg->{PeerAddr}, # spent 47µs making 2 calls to IO::Socket::INET::_sock_info, avg 23µs/call |
161 | $arg->{PeerPort}, | ||||
162 | $proto) | ||||
163 | or return _error($sock, $!, $@); | ||||
164 | } | ||||
165 | |||||
166 | 2 | 500ns | $proto ||= _get_proto_number('tcp'); | ||
167 | |||||
168 | 2 | 7µs | 2 | 8µs | $type = $arg->{Type} || $socket_type{lc _get_proto_name($proto)}; # spent 8µs making 2 calls to IO::Socket::INET::_get_proto_name, avg 4µs/call |
169 | |||||
170 | 2 | 700ns | my @raddr = (); | ||
171 | |||||
172 | 2 | 1µs | if(defined $raddr) { | ||
173 | 2 | 17µs | 2 | 105ms | @raddr = $sock->_get_addr($raddr, $arg->{MultiHomed}); # spent 105ms making 2 calls to IO::Socket::INET::_get_addr, avg 52.4ms/call |
174 | 2 | 1µs | return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'") | ||
175 | unless @raddr; | ||||
176 | } | ||||
177 | |||||
178 | 2 | 2µs | while(1) { | ||
179 | |||||
180 | 2 | 36µs | 2 | 121µs | $sock->socket(AF_INET, $type, $proto) or # spent 121µs making 2 calls to IO::Socket::socket, avg 61µs/call |
181 | return _error($sock, $!, "$!"); | ||||
182 | |||||
183 | 2 | 3µs | if (defined $arg->{Blocking}) { | ||
184 | defined $sock->blocking($arg->{Blocking}) | ||||
185 | or return _error($sock, $!, "$!"); | ||||
186 | } | ||||
187 | |||||
188 | 2 | 3µs | if ($arg->{Reuse} || $arg->{ReuseAddr}) { | ||
189 | $sock->sockopt(SO_REUSEADDR,1) or | ||||
190 | return _error($sock, $!, "$!"); | ||||
191 | } | ||||
192 | |||||
193 | 2 | 1µs | if ($arg->{ReusePort}) { | ||
194 | $sock->sockopt(SO_REUSEPORT,1) or | ||||
195 | return _error($sock, $!, "$!"); | ||||
196 | } | ||||
197 | |||||
198 | 2 | 2µs | if ($arg->{Broadcast}) { | ||
199 | $sock->sockopt(SO_BROADCAST,1) or | ||||
200 | return _error($sock, $!, "$!"); | ||||
201 | } | ||||
202 | |||||
203 | 2 | 5µs | if($lport || ($laddr ne INADDR_ANY) || exists $arg->{Listen}) { | ||
204 | $sock->bind($lport || 0, $laddr) or | ||||
205 | return _error($sock, $!, "$!"); | ||||
206 | } | ||||
207 | |||||
208 | 2 | 2µs | 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 | 2 | 2µs | last unless exists($arg->{PeerAddr}); | ||
216 | |||||
217 | 2 | 3µs | $raddr = shift @raddr; | ||
218 | |||||
219 | 2 | 1µs | return _error($sock, $EINVAL, 'Cannot determine remote port') | ||
220 | unless($rport || $type == SOCK_DGRAM || $type == SOCK_RAW); | ||||
221 | |||||
222 | last | ||||
223 | 2 | 1µs | unless($type == SOCK_STREAM || defined $raddr); | ||
224 | |||||
225 | 2 | 500ns | 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 | 2 | 3µs | undef $@; | ||
232 | 2 | 74µs | 4 | 75.6ms | if ($sock->connect(pack_sockaddr_in($rport, $raddr))) { # spent 75.6ms making 2 calls to IO::Socket::INET::connect, avg 37.8ms/call
# spent 12µs making 2 calls to Socket::pack_sockaddr_in, avg 6µs/call |
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 | # spent 75.6ms (37µs+75.6) within IO::Socket::INET::connect which was called 2 times, avg 37.8ms/call:
# 2 times (37µs+75.6ms) by IO::Socket::INET::configure at line 232, avg 37.8ms/call | ||||
254 | 2 | 2µs | @_ == 2 || @_ == 3 or | ||
255 | croak 'usage: $sock->connect(NAME) or $sock->connect(PORT, ADDR)'; | ||||
256 | 2 | 1µs | my $sock = shift; | ||
257 | 2 | 33µs | 2 | 75.6ms | return $sock->SUPER::connect(@_ == 1 ? shift : pack_sockaddr_in(@_)); # spent 75.6ms making 2 calls to IO::Socket::connect, avg 37.8ms/call |
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 | # spent 1.38ms (562µs+821µs) within IO::Socket::INET::peeraddr which was called 41 times, avg 34µs/call:
# 41 times (562µs+821µs) by IO::Socket::INET::peerhost at line 305, avg 34µs/call | ||||
289 | 41 | 23µs | @_ == 1 or croak 'usage: $sock->peeraddr()'; | ||
290 | 41 | 28µs | my($sock) = @_; | ||
291 | 41 | 178µs | 41 | 290µs | my $name = $sock->peername; # spent 290µs making 41 calls to IO::Socket::peername, avg 7µs/call |
292 | 41 | 324µs | 41 | 532µs | $name ? (sockaddr_in($name))[1] : undef; # spent 532µs making 41 calls to Socket::sockaddr_in, avg 13µs/call |
293 | } | ||||
294 | |||||
295 | # spent 762µs (445+316) within IO::Socket::INET::peerport which was called 41 times, avg 19µs/call:
# 41 times (445µs+316µs) by LWP::Protocol::http::_get_sock_info at line 77 of LWP/Protocol/http.pm, avg 19µs/call | ||||
296 | 41 | 25µs | @_ == 1 or croak 'usage: $sock->peerport()'; | ||
297 | 41 | 26µs | my($sock) = @_; | ||
298 | 41 | 89µs | 41 | 91µs | my $name = $sock->peername; # spent 91µs making 41 calls to IO::Socket::peername, avg 2µs/call |
299 | 41 | 200µs | 41 | 225µs | $name ? (sockaddr_in($name))[0] : undef; # spent 225µs making 41 calls to Socket::sockaddr_in, avg 6µs/call |
300 | } | ||||
301 | |||||
302 | # spent 2.07ms (503µs+1.57) within IO::Socket::INET::peerhost which was called 41 times, avg 51µs/call:
# 41 times (503µs+1.57ms) by LWP::Protocol::http::_get_sock_info at line 77 of LWP/Protocol/http.pm, avg 51µs/call | ||||
303 | 41 | 37µs | @_ == 1 or croak 'usage: $sock->peerhost()'; | ||
304 | 41 | 26µs | my($sock) = @_; | ||
305 | 41 | 144µs | 41 | 1.38ms | my $addr = $sock->peeraddr; # spent 1.38ms making 41 calls to IO::Socket::INET::peeraddr, avg 34µs/call |
306 | 41 | 446µs | 41 | 187µs | $addr ? inet_ntoa($addr) : undef; # spent 187µs making 41 calls to Socket::inet_ntoa, avg 5µs/call |
307 | } | ||||
308 | |||||
309 | 1 | 17µs | 1; | ||
310 | |||||
311 | __END__ | ||||
312 | |||||
313 | =head1 NAME | ||||
314 | |||||
315 | IO::Socket::INET - Object interface for AF_INET domain sockets | ||||
316 | |||||
317 | =head1 SYNOPSIS | ||||
318 | |||||
319 | use IO::Socket::INET; | ||||
320 | |||||
321 | =head1 DESCRIPTION | ||||
322 | |||||
323 | C<IO::Socket::INET> provides an object interface to creating and using sockets | ||||
324 | in the AF_INET domain. It is built upon the L<IO::Socket> interface and | ||||
325 | inherits all the methods defined by L<IO::Socket>. | ||||
326 | |||||
327 | =head1 CONSTRUCTOR | ||||
328 | |||||
329 | =over 4 | ||||
330 | |||||
331 | =item new ( [ARGS] ) | ||||
332 | |||||
333 | Creates an C<IO::Socket::INET> object, which is a reference to a | ||||
334 | newly created symbol (see the C<Symbol> package). C<new> | ||||
335 | optionally takes arguments, these arguments are in key-value pairs. | ||||
336 | |||||
337 | In addition to the key-value pairs accepted by L<IO::Socket>, | ||||
338 | C<IO::Socket::INET> provides. | ||||
339 | |||||
340 | |||||
341 | PeerAddr Remote host address <hostname>[:<port>] | ||||
342 | PeerHost Synonym for PeerAddr | ||||
343 | PeerPort Remote port or service <service>[(<no>)] | <no> | ||||
344 | LocalAddr Local host bind address hostname[:port] | ||||
345 | LocalHost Synonym for LocalAddr | ||||
346 | LocalPort Local host bind port <service>[(<no>)] | <no> | ||||
347 | Proto Protocol name (or number) "tcp" | "udp" | ... | ||||
348 | Type Socket type SOCK_STREAM | SOCK_DGRAM | ... | ||||
349 | Listen Queue size for listen | ||||
350 | ReuseAddr Set SO_REUSEADDR before binding | ||||
351 | Reuse Set SO_REUSEADDR before binding (deprecated, prefer ReuseAddr) | ||||
352 | ReusePort Set SO_REUSEPORT before binding | ||||
353 | Broadcast Set SO_BROADCAST before binding | ||||
354 | Timeout Timeout value for various operations | ||||
355 | MultiHomed Try all addresses for multi-homed hosts | ||||
356 | Blocking Determine if connection will be blocking mode | ||||
357 | |||||
358 | If C<Listen> is defined then a listen socket is created, else if the | ||||
359 | socket type, which is derived from the protocol, is SOCK_STREAM then | ||||
360 | connect() is called. | ||||
361 | |||||
362 | Although it is not illegal, the use of C<MultiHomed> on a socket | ||||
363 | which is in non-blocking mode is of little use. This is because the | ||||
364 | first connect will never fail with a timeout as the connect call | ||||
365 | will not block. | ||||
366 | |||||
367 | The C<PeerAddr> can be a hostname or the IP-address on the | ||||
368 | "xx.xx.xx.xx" form. The C<PeerPort> can be a number or a symbolic | ||||
369 | service name. The service name might be followed by a number in | ||||
370 | parenthesis which is used if the service is not known by the system. | ||||
371 | The C<PeerPort> specification can also be embedded in the C<PeerAddr> | ||||
372 | by preceding it with a ":". | ||||
373 | |||||
374 | If C<Proto> is not given and you specify a symbolic C<PeerPort> port, | ||||
375 | then the constructor will try to derive C<Proto> from the service | ||||
376 | name. As a last resort C<Proto> "tcp" is assumed. The C<Type> | ||||
377 | parameter will be deduced from C<Proto> if not specified. | ||||
378 | |||||
379 | If the constructor is only passed a single argument, it is assumed to | ||||
380 | be a C<PeerAddr> specification. | ||||
381 | |||||
382 | If C<Blocking> is set to 0, the connection will be in nonblocking mode. | ||||
383 | If not specified it defaults to 1 (blocking mode). | ||||
384 | |||||
385 | Examples: | ||||
386 | |||||
387 | $sock = IO::Socket::INET->new(PeerAddr => 'www.perl.org', | ||||
388 | PeerPort => 'http(80)', | ||||
389 | Proto => 'tcp'); | ||||
390 | |||||
391 | $sock = IO::Socket::INET->new(PeerAddr => 'localhost:smtp(25)'); | ||||
392 | |||||
393 | $sock = IO::Socket::INET->new(Listen => 5, | ||||
394 | LocalAddr => 'localhost', | ||||
395 | LocalPort => 9000, | ||||
396 | Proto => 'tcp'); | ||||
397 | |||||
398 | $sock = IO::Socket::INET->new('127.0.0.1:25'); | ||||
399 | |||||
400 | $sock = IO::Socket::INET->new(PeerPort => 9999, | ||||
401 | PeerAddr => inet_ntoa(INADDR_BROADCAST), | ||||
402 | Proto => udp, | ||||
403 | LocalAddr => 'localhost', | ||||
404 | Broadcast => 1 ) | ||||
405 | or die "Can't bind : $@\n"; | ||||
406 | |||||
407 | NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE | ||||
408 | |||||
409 | As of VERSION 1.18 all IO::Socket objects have autoflush turned on | ||||
410 | by default. This was not the case with earlier releases. | ||||
411 | |||||
412 | NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE | ||||
413 | |||||
414 | =back | ||||
415 | |||||
416 | =head2 METHODS | ||||
417 | |||||
418 | =over 4 | ||||
419 | |||||
420 | =item sockaddr () | ||||
421 | |||||
422 | Return the address part of the sockaddr structure for the socket | ||||
423 | |||||
424 | =item sockport () | ||||
425 | |||||
426 | Return the port number that the socket is using on the local host | ||||
427 | |||||
428 | =item sockhost () | ||||
429 | |||||
430 | Return the address part of the sockaddr structure for the socket in a | ||||
431 | text form xx.xx.xx.xx | ||||
432 | |||||
433 | =item peeraddr () | ||||
434 | |||||
435 | Return the address part of the sockaddr structure for the socket on | ||||
436 | the peer host | ||||
437 | |||||
438 | =item peerport () | ||||
439 | |||||
440 | Return the port number for the socket on the peer host. | ||||
441 | |||||
442 | =item peerhost () | ||||
443 | |||||
444 | Return the address part of the sockaddr structure for the socket on the | ||||
445 | peer host in a text form xx.xx.xx.xx | ||||
446 | |||||
447 | =back | ||||
448 | |||||
449 | =head1 SEE ALSO | ||||
450 | |||||
451 | L<Socket>, L<IO::Socket> | ||||
452 | |||||
453 | =head1 AUTHOR | ||||
454 | |||||
455 | Graham Barr. Currently maintained by the Perl Porters. Please report all | ||||
456 | bugs to <perl5-porters@perl.org>. | ||||
457 | |||||
458 | =head1 COPYRIGHT | ||||
459 | |||||
460 | Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved. | ||||
461 | This program is free software; you can redistribute it and/or | ||||
462 | modify it under the same terms as Perl itself. | ||||
463 | |||||
464 | =cut | ||||
# spent 11µs within IO::Socket::INET::CORE:match which was called 8 times, avg 1µs/call:
# 4 times (5µs+0s) by IO::Socket::INET::_sock_info at line 80 of IO/Socket/INET.pm, avg 1µs/call
# 2 times (6µs+0s) by IO::Socket::INET::_sock_info at line 91 of IO/Socket/INET.pm, avg 3µs/call
# 2 times (700ns+0s) by IO::Socket::INET::_sock_info at line 93 of IO/Socket/INET.pm, avg 350ns/call | |||||
# spent 2µs within IO::Socket::INET::CORE:subst which was called 4 times, avg 375ns/call:
# 2 times (1µs+0s) by IO::Socket::INET::_sock_info at line 77 of IO/Socket/INET.pm, avg 550ns/call
# 2 times (400ns+0s) by IO::Socket::INET::_sock_info at line 90 of IO/Socket/INET.pm, avg 200ns/call |