1 |
root |
1.7 |
=head1 NAME |
2 |
elmex |
1.1 |
|
3 |
root |
1.7 |
AnyEvent::Socket - useful IPv4 and IPv6 stuff. |
4 |
elmex |
1.1 |
|
5 |
root |
1.7 |
=head1 SYNOPSIS |
6 |
elmex |
1.1 |
|
7 |
root |
1.45 |
use AnyEvent::Socket; |
8 |
|
|
|
9 |
|
|
tcp_connect "gameserver.deliantra.net", 13327, sub { |
10 |
|
|
my ($fh) = @_ |
11 |
|
|
or die "gameserver.deliantra.net connect failed: $!"; |
12 |
|
|
|
13 |
|
|
# enjoy your filehandle |
14 |
|
|
}; |
15 |
|
|
|
16 |
|
|
# a simple tcp server |
17 |
|
|
tcp_server undef, 8888, sub { |
18 |
|
|
my ($fh, $host, $port) = @_; |
19 |
|
|
|
20 |
|
|
syswrite $fh, "The internet is full, $host:$port. Go away!\015\012"; |
21 |
|
|
}; |
22 |
root |
1.19 |
|
23 |
root |
1.7 |
=head1 DESCRIPTION |
24 |
elmex |
1.1 |
|
25 |
root |
1.7 |
This module implements various utility functions for handling internet |
26 |
|
|
protocol addresses and sockets, in an as transparent and simple way as |
27 |
|
|
possible. |
28 |
elmex |
1.1 |
|
29 |
root |
1.7 |
All functions documented without C<AnyEvent::Socket::> prefix are exported |
30 |
|
|
by default. |
31 |
elmex |
1.1 |
|
32 |
root |
1.7 |
=over 4 |
33 |
elmex |
1.1 |
|
34 |
root |
1.7 |
=cut |
35 |
elmex |
1.1 |
|
36 |
root |
1.7 |
package AnyEvent::Socket; |
37 |
elmex |
1.1 |
|
38 |
root |
1.7 |
use Carp (); |
39 |
|
|
use Errno (); |
40 |
root |
1.34 |
use Socket qw(AF_INET AF_UNIX SOCK_STREAM SOCK_DGRAM SOL_SOCKET SO_REUSEADDR); |
41 |
elmex |
1.1 |
|
42 |
root |
1.96 |
use AnyEvent (); BEGIN { AnyEvent::common_sense } |
43 |
root |
1.21 |
use AnyEvent::Util qw(guard fh_nonblocking AF_INET6); |
44 |
root |
1.17 |
use AnyEvent::DNS (); |
45 |
elmex |
1.1 |
|
46 |
root |
1.7 |
use base 'Exporter'; |
47 |
elmex |
1.2 |
|
48 |
root |
1.34 |
our @EXPORT = qw( |
49 |
root |
1.93 |
getprotobyname |
50 |
root |
1.103 |
parse_hostport format_hostport |
51 |
root |
1.34 |
parse_ipv4 parse_ipv6 |
52 |
|
|
parse_ip parse_address |
53 |
root |
1.85 |
format_ipv4 format_ipv6 |
54 |
root |
1.34 |
format_ip format_address |
55 |
|
|
address_family |
56 |
|
|
inet_aton |
57 |
|
|
tcp_server |
58 |
|
|
tcp_connect |
59 |
|
|
); |
60 |
elmex |
1.2 |
|
61 |
root |
1.108 |
our $VERSION = 4.91; |
62 |
|
|
|
63 |
|
|
# used in cases where we may return immediately but want the |
64 |
|
|
# caller to do stuff first |
65 |
|
|
sub _postpone { |
66 |
|
|
my ($cb, @args) = @_; |
67 |
|
|
|
68 |
|
|
my $w; $w = AE::timer 0, 0, sub { |
69 |
|
|
undef $w; |
70 |
|
|
$cb->(@args); |
71 |
|
|
}; |
72 |
|
|
} |
73 |
elmex |
1.1 |
|
74 |
root |
1.9 |
=item $ipn = parse_ipv4 $dotted_quad |
75 |
|
|
|
76 |
|
|
Tries to parse the given dotted quad IPv4 address and return it in |
77 |
|
|
octet form (or undef when it isn't in a parsable format). Supports all |
78 |
|
|
forms specified by POSIX (e.g. C<10.0.0.1>, C<10.1>, C<10.0x020304>, |
79 |
|
|
C<0x12345678> or C<0377.0377.0377.0377>). |
80 |
|
|
|
81 |
|
|
=cut |
82 |
|
|
|
83 |
|
|
sub parse_ipv4($) { |
84 |
|
|
$_[0] =~ /^ (?: 0x[0-9a-fA-F]+ | 0[0-7]* | [1-9][0-9]* ) |
85 |
|
|
(?:\. (?: 0x[0-9a-fA-F]+ | 0[0-7]* | [1-9][0-9]* ) ){0,3}$/x |
86 |
|
|
or return undef; |
87 |
|
|
|
88 |
|
|
@_ = map /^0/ ? oct : $_, split /\./, $_[0]; |
89 |
|
|
|
90 |
|
|
# check leading parts against range |
91 |
|
|
return undef if grep $_ >= 256, @_[0 .. @_ - 2]; |
92 |
|
|
|
93 |
|
|
# check trailing part against range |
94 |
root |
1.51 |
return undef if $_[-1] >= 2 ** (8 * (4 - $#_)); |
95 |
root |
1.9 |
|
96 |
|
|
pack "N", (pop) |
97 |
|
|
+ ($_[0] << 24) |
98 |
|
|
+ ($_[1] << 16) |
99 |
|
|
+ ($_[2] << 8); |
100 |
|
|
} |
101 |
|
|
|
102 |
root |
1.14 |
=item $ipn = parse_ipv6 $textual_ipv6_address |
103 |
root |
1.9 |
|
104 |
|
|
Tries to parse the given IPv6 address and return it in |
105 |
|
|
octet form (or undef when it isn't in a parsable format). |
106 |
|
|
|
107 |
|
|
Should support all forms specified by RFC 2373 (and additionally all IPv4 |
108 |
root |
1.26 |
forms supported by parse_ipv4). Note that scope-id's are not supported |
109 |
|
|
(and will not parse). |
110 |
root |
1.12 |
|
111 |
|
|
This function works similarly to C<inet_pton AF_INET6, ...>. |
112 |
root |
1.9 |
|
113 |
|
|
=cut |
114 |
|
|
|
115 |
|
|
sub parse_ipv6($) { |
116 |
|
|
# quick test to avoid longer processing |
117 |
|
|
my $n = $_[0] =~ y/://; |
118 |
|
|
return undef if $n < 2 || $n > 8; |
119 |
|
|
|
120 |
|
|
my ($h, $t) = split /::/, $_[0], 2; |
121 |
|
|
|
122 |
root |
1.11 |
unless (defined $t) { |
123 |
root |
1.9 |
($h, $t) = (undef, $h); |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
my @h = split /:/, $h; |
127 |
|
|
my @t = split /:/, $t; |
128 |
|
|
|
129 |
root |
1.14 |
# check for ipv4 tail |
130 |
root |
1.9 |
if (@t && $t[-1]=~ /\./) { |
131 |
|
|
return undef if $n > 6; |
132 |
|
|
|
133 |
|
|
my $ipn = parse_ipv4 pop @t |
134 |
|
|
or return undef; |
135 |
|
|
|
136 |
|
|
push @t, map +(sprintf "%x", $_), unpack "nn", $ipn; |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
# no :: then we need to have exactly 8 components |
140 |
root |
1.11 |
return undef unless @h + @t == 8 || $_[0] =~ /::/; |
141 |
root |
1.9 |
|
142 |
|
|
# now check all parts for validity |
143 |
|
|
return undef if grep !/^[0-9a-fA-F]{1,4}$/, @h, @t; |
144 |
|
|
|
145 |
|
|
# now pad... |
146 |
|
|
push @h, 0 while @h + @t < 8; |
147 |
|
|
|
148 |
|
|
# and done |
149 |
|
|
pack "n*", map hex, @h, @t |
150 |
root |
1.7 |
} |
151 |
elmex |
1.1 |
|
152 |
root |
1.34 |
sub parse_unix($) { |
153 |
|
|
$_[0] eq "unix/" |
154 |
|
|
? pack "S", AF_UNIX |
155 |
|
|
: undef |
156 |
root |
1.11 |
|
157 |
root |
1.34 |
} |
158 |
|
|
|
159 |
root |
1.81 |
=item $ipn = parse_address $ip |
160 |
root |
1.34 |
|
161 |
|
|
Combines C<parse_ipv4> and C<parse_ipv6> in one function. The address |
162 |
|
|
here refers to the host address (not socket address) in network form |
163 |
|
|
(binary). |
164 |
|
|
|
165 |
|
|
If the C<$text> is C<unix/>, then this function returns a special token |
166 |
|
|
recognised by the other functions in this module to mean "UNIX domain |
167 |
|
|
socket". |
168 |
root |
1.11 |
|
169 |
root |
1.81 |
If the C<$text> to parse is a mapped IPv4 in IPv6 address (:ffff::<ipv4>), |
170 |
|
|
then it will be treated as an IPv4 address. If you don't want that, you |
171 |
|
|
have to call C<parse_ipv4> and/or C<parse_ipv6> manually. |
172 |
|
|
|
173 |
|
|
=item $ipn = AnyEvent::Socket::aton $ip |
174 |
root |
1.58 |
|
175 |
|
|
Same as C<parse_address>, but not exported (think C<Socket::inet_aton> but |
176 |
|
|
I<without> name resolution). |
177 |
|
|
|
178 |
root |
1.11 |
=cut |
179 |
|
|
|
180 |
root |
1.34 |
sub parse_address($) { |
181 |
root |
1.81 |
for (&parse_ipv6) { |
182 |
|
|
if ($_) { |
183 |
|
|
s/^\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff//; |
184 |
|
|
return $_; |
185 |
|
|
} else { |
186 |
|
|
return &parse_ipv4 || &parse_unix |
187 |
|
|
} |
188 |
|
|
} |
189 |
root |
1.11 |
} |
190 |
|
|
|
191 |
root |
1.58 |
*aton = \&parse_address; |
192 |
root |
1.34 |
|
193 |
root |
1.93 |
=item ($name, $aliases, $proto) = getprotobyname $name |
194 |
|
|
|
195 |
|
|
Works like the builtin function of the same name, except it tries hard to |
196 |
|
|
work even on broken platforms (well, that's windows), where getprotobyname |
197 |
|
|
is traditionally very unreliable. |
198 |
|
|
|
199 |
|
|
=cut |
200 |
|
|
|
201 |
|
|
# microsoft can't even get getprotobyname working (the etc/protocols file |
202 |
|
|
# gets lost fairly often on windows), so we have to hardcode some common |
203 |
|
|
# protocol numbers ourselves. |
204 |
|
|
our %PROTO_BYNAME; |
205 |
|
|
|
206 |
|
|
$PROTO_BYNAME{tcp} = Socket::IPPROTO_TCP () if defined &Socket::IPPROTO_TCP; |
207 |
|
|
$PROTO_BYNAME{udp} = Socket::IPPROTO_UDP () if defined &Socket::IPPROTO_UDP; |
208 |
|
|
$PROTO_BYNAME{icmp} = Socket::IPPROTO_ICMP() if defined &Socket::IPPROTO_ICMP; |
209 |
|
|
|
210 |
|
|
sub getprotobyname($) { |
211 |
|
|
my $name = lc shift; |
212 |
|
|
|
213 |
|
|
defined (my $proton = $PROTO_BYNAME{$name} || (getprotobyname $name)[2]) |
214 |
|
|
or return; |
215 |
|
|
|
216 |
|
|
($name, uc $name, $proton) |
217 |
|
|
} |
218 |
|
|
|
219 |
root |
1.54 |
=item ($host, $service) = parse_hostport $string[, $default_service] |
220 |
|
|
|
221 |
|
|
Splitting a string of the form C<hostname:port> is a common |
222 |
|
|
problem. Unfortunately, just splitting on the colon makes it hard to |
223 |
|
|
specify IPv6 addresses and doesn't support the less common but well |
224 |
|
|
standardised C<[ip literal]> syntax. |
225 |
|
|
|
226 |
|
|
This function tries to do this job in a better way, it supports the |
227 |
|
|
following formats, where C<port> can be a numerical port number of a |
228 |
|
|
service name, or a C<name=port> string, and the C< port> and C<:port> |
229 |
|
|
parts are optional. Also, everywhere where an IP address is supported |
230 |
|
|
a hostname or unix domain socket address is also supported (see |
231 |
|
|
C<parse_unix>). |
232 |
|
|
|
233 |
|
|
hostname:port e.g. "www.linux.org", "www.x.de:443", "www.x.de:https=443" |
234 |
|
|
ipv4:port e.g. "198.182.196.56", "127.1:22" |
235 |
|
|
ipv6 e.g. "::1", "affe::1" |
236 |
|
|
[ipv4or6]:port e.g. "[::1]", "[10.0.1]:80" |
237 |
|
|
[ipv4or6] port e.g. "[127.0.0.1]", "[www.x.org] 17" |
238 |
|
|
ipv4or6 port e.g. "::1 443", "10.0.0.1 smtp" |
239 |
|
|
|
240 |
|
|
It also supports defaulting the service name in a simple way by using |
241 |
|
|
C<$default_service> if no service was detected. If neither a service was |
242 |
|
|
detected nor a default was specified, then this function returns the |
243 |
root |
1.102 |
empty list. The same happens when a parse error was detected, such as a |
244 |
root |
1.54 |
hostname with a colon in it (the function is rather conservative, though). |
245 |
|
|
|
246 |
|
|
Example: |
247 |
|
|
|
248 |
|
|
print join ",", parse_hostport "localhost:443"; |
249 |
|
|
# => "localhost,443" |
250 |
|
|
|
251 |
|
|
print join ",", parse_hostport "localhost", "https"; |
252 |
|
|
# => "localhost,https" |
253 |
|
|
|
254 |
|
|
print join ",", parse_hostport "[::1]"; |
255 |
|
|
# => "," (empty list) |
256 |
|
|
|
257 |
|
|
=cut |
258 |
|
|
|
259 |
|
|
sub parse_hostport($;$) { |
260 |
|
|
my ($host, $port); |
261 |
|
|
|
262 |
root |
1.56 |
for ("$_[0]") { # work on a copy, just in case, and also reset pos |
263 |
root |
1.55 |
|
264 |
root |
1.54 |
# parse host, special cases: "ipv6" or "ipv6 port" |
265 |
|
|
unless ( |
266 |
root |
1.55 |
($host) = /^\s* ([0-9a-fA-F:]*:[0-9a-fA-F:]*:[0-9a-fA-F\.:]*)/xgc |
267 |
root |
1.54 |
and parse_ipv6 $host |
268 |
|
|
) { |
269 |
|
|
/^\s*/xgc; |
270 |
|
|
|
271 |
|
|
if (/^ \[ ([^\[\]]+) \]/xgc) { |
272 |
|
|
$host = $1; |
273 |
|
|
} elsif (/^ ([^\[\]:\ ]+) /xgc) { |
274 |
|
|
$host = $1; |
275 |
|
|
} else { |
276 |
|
|
return; |
277 |
|
|
} |
278 |
|
|
} |
279 |
|
|
|
280 |
|
|
# parse port |
281 |
|
|
if (/\G (?:\s+|:) ([^:[:space:]]+) \s*$/xgc) { |
282 |
|
|
$port = $1; |
283 |
|
|
} elsif (/\G\s*$/gc && length $_[1]) { |
284 |
|
|
$port = $_[1]; |
285 |
|
|
} else { |
286 |
|
|
return; |
287 |
|
|
} |
288 |
|
|
} |
289 |
|
|
|
290 |
|
|
# hostnames must not contain :'s |
291 |
|
|
return if $host =~ /:/ && !parse_ipv6 $host; |
292 |
|
|
|
293 |
|
|
($host, $port) |
294 |
|
|
} |
295 |
|
|
|
296 |
root |
1.103 |
=item $string = format_hostport $host, $port |
297 |
|
|
|
298 |
|
|
Takes a host (in textual form) and a port and formats in unambigiously in |
299 |
|
|
a way that C<parse_hostport> can parse it again. C<$port> can be C<undef>. |
300 |
|
|
|
301 |
|
|
=cut |
302 |
|
|
|
303 |
|
|
sub format_hostport($;$) { |
304 |
|
|
my ($host, $port) = @_; |
305 |
|
|
|
306 |
|
|
$port = ":$port" if length $port; |
307 |
|
|
$host = "[$host]" if $host =~ /:/; |
308 |
|
|
|
309 |
|
|
"$host$port" |
310 |
|
|
} |
311 |
|
|
|
312 |
root |
1.34 |
=item $sa_family = address_family $ipn |
313 |
|
|
|
314 |
|
|
Returns the address family/protocol-family (AF_xxx/PF_xxx, in one value :) |
315 |
|
|
of the given host address in network format. |
316 |
|
|
|
317 |
|
|
=cut |
318 |
|
|
|
319 |
|
|
sub address_family($) { |
320 |
|
|
4 == length $_[0] |
321 |
|
|
? AF_INET |
322 |
|
|
: 16 == length $_[0] |
323 |
|
|
? AF_INET6 |
324 |
|
|
: unpack "S", $_[0] |
325 |
|
|
} |
326 |
root |
1.11 |
|
327 |
root |
1.81 |
=item $text = format_ipv4 $ipn |
328 |
|
|
|
329 |
|
|
Expects a four octet string representing a binary IPv4 address and returns |
330 |
|
|
its textual format. Rarely used, see C<format_address> for a nicer |
331 |
|
|
interface. |
332 |
|
|
|
333 |
|
|
=item $text = format_ipv6 $ipn |
334 |
|
|
|
335 |
|
|
Expects a sixteen octet string representing a binary IPv6 address and |
336 |
|
|
returns its textual format. Rarely used, see C<format_address> for a |
337 |
|
|
nicer interface. |
338 |
|
|
|
339 |
root |
1.34 |
=item $text = format_address $ipn |
340 |
|
|
|
341 |
|
|
Covnvert a host address in network format (e.g. 4 octets for IPv4 or 16 |
342 |
|
|
octets for IPv6) and convert it into textual form. |
343 |
|
|
|
344 |
|
|
Returns C<unix/> for UNIX domain sockets. |
345 |
root |
1.11 |
|
346 |
root |
1.12 |
This function works similarly to C<inet_ntop AF_INET || AF_INET6, ...>, |
347 |
|
|
except it automatically detects the address type. |
348 |
|
|
|
349 |
root |
1.34 |
Returns C<undef> if it cannot detect the type. |
350 |
|
|
|
351 |
root |
1.81 |
If the C<$ipn> is a mapped IPv4 in IPv6 address (:ffff::<ipv4>), then just |
352 |
|
|
the contained IPv4 address will be returned. If you do not want that, you |
353 |
|
|
have to call C<format_ipv6> manually. |
354 |
|
|
|
355 |
root |
1.58 |
=item $text = AnyEvent::Socket::ntoa $ipn |
356 |
|
|
|
357 |
|
|
Same as format_address, but not exported (think C<inet_ntoa>). |
358 |
|
|
|
359 |
root |
1.11 |
=cut |
360 |
|
|
|
361 |
root |
1.81 |
sub format_ipv4($) { |
362 |
|
|
join ".", unpack "C4", $_[0] |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
sub format_ipv6($) { |
366 |
|
|
if (v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 eq $_[0]) { |
367 |
|
|
return "::"; |
368 |
|
|
} elsif (v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1 eq $_[0]) { |
369 |
|
|
return "::1"; |
370 |
|
|
} elsif (v0.0.0.0.0.0.0.0.0.0.0.0 eq substr $_[0], 0, 12) { |
371 |
|
|
# v4compatible |
372 |
|
|
return "::" . format_ipv4 substr $_[0], 12; |
373 |
|
|
} elsif (v0.0.0.0.0.0.0.0.0.0.255.255 eq substr $_[0], 0, 12) { |
374 |
|
|
# v4mapped |
375 |
|
|
return "::ffff:" . format_ipv4 substr $_[0], 12; |
376 |
|
|
} elsif (v0.0.0.0.0.0.0.0.255.255.0.0 eq substr $_[0], 0, 12) { |
377 |
|
|
# v4translated |
378 |
|
|
return "::ffff:0:" . format_ipv4 substr $_[0], 12; |
379 |
|
|
} else { |
380 |
|
|
my $ip = sprintf "%x:%x:%x:%x:%x:%x:%x:%x", unpack "n8", $_[0]; |
381 |
|
|
|
382 |
|
|
# this is rather sucky, I admit |
383 |
|
|
$ip =~ s/^0:(?:0:)*(0$)?/::/ |
384 |
|
|
or $ip =~ s/(:0){7}$/::/ or $ip =~ s/(:0){7}/:/ |
385 |
|
|
or $ip =~ s/(:0){6}$/::/ or $ip =~ s/(:0){6}/:/ |
386 |
|
|
or $ip =~ s/(:0){5}$/::/ or $ip =~ s/(:0){5}/:/ |
387 |
|
|
or $ip =~ s/(:0){4}$/::/ or $ip =~ s/(:0){4}/:/ |
388 |
|
|
or $ip =~ s/(:0){3}$/::/ or $ip =~ s/(:0){3}/:/ |
389 |
|
|
or $ip =~ s/(:0){2}$/::/ or $ip =~ s/(:0){2}/:/ |
390 |
|
|
or $ip =~ s/(:0){1}$/::/ or $ip =~ s/(:0){1}/:/; |
391 |
|
|
return $ip |
392 |
|
|
} |
393 |
|
|
} |
394 |
|
|
|
395 |
root |
1.34 |
sub format_address($) { |
396 |
|
|
my $af = address_family $_[0]; |
397 |
|
|
if ($af == AF_INET) { |
398 |
root |
1.81 |
return &format_ipv4; |
399 |
root |
1.34 |
} elsif ($af == AF_INET6) { |
400 |
root |
1.81 |
return (v0.0.0.0.0.0.0.0.0.0.255.255 eq substr $_[0], 0, 12) |
401 |
|
|
? format_ipv4 substr $_[0], 12 |
402 |
|
|
: &format_ipv6; |
403 |
root |
1.34 |
} elsif ($af == AF_UNIX) { |
404 |
|
|
return "unix/" |
405 |
root |
1.11 |
} else { |
406 |
|
|
return undef |
407 |
|
|
} |
408 |
|
|
} |
409 |
|
|
|
410 |
root |
1.58 |
*ntoa = \&format_address; |
411 |
root |
1.34 |
|
412 |
root |
1.7 |
=item inet_aton $name_or_address, $cb->(@addresses) |
413 |
elmex |
1.1 |
|
414 |
root |
1.7 |
Works similarly to its Socket counterpart, except that it uses a |
415 |
|
|
callback. Also, if a host has only an IPv6 address, this might be passed |
416 |
|
|
to the callback instead (use the length to detect this - 4 for IPv4, 16 |
417 |
|
|
for IPv6). |
418 |
elmex |
1.2 |
|
419 |
root |
1.7 |
Unlike the L<Socket> function of the same name, you can get multiple IPv4 |
420 |
root |
1.34 |
and IPv6 addresses as result (and maybe even other adrdess types). |
421 |
elmex |
1.2 |
|
422 |
root |
1.7 |
=cut |
423 |
elmex |
1.2 |
|
424 |
root |
1.7 |
sub inet_aton { |
425 |
|
|
my ($name, $cb) = @_; |
426 |
elmex |
1.2 |
|
427 |
root |
1.9 |
if (my $ipn = &parse_ipv4) { |
428 |
|
|
$cb->($ipn); |
429 |
|
|
} elsif (my $ipn = &parse_ipv6) { |
430 |
|
|
$cb->($ipn); |
431 |
root |
1.7 |
} elsif ($name eq "localhost") { # rfc2606 et al. |
432 |
root |
1.9 |
$cb->(v127.0.0.1, v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1); |
433 |
root |
1.7 |
} else { |
434 |
|
|
require AnyEvent::DNS; |
435 |
elmex |
1.2 |
|
436 |
root |
1.7 |
# simple, bad suboptimal algorithm |
437 |
|
|
AnyEvent::DNS::a ($name, sub { |
438 |
|
|
if (@_) { |
439 |
root |
1.9 |
$cb->(map +(parse_ipv4 $_), @_); |
440 |
root |
1.7 |
} else { |
441 |
root |
1.8 |
$cb->(); |
442 |
|
|
#AnyEvent::DNS::aaaa ($name, $cb); need inet_pton |
443 |
root |
1.7 |
} |
444 |
|
|
}); |
445 |
|
|
} |
446 |
|
|
} |
447 |
elmex |
1.2 |
|
448 |
root |
1.95 |
BEGIN { |
449 |
|
|
*sockaddr_family = $Socket::VERSION >= 1.75 |
450 |
|
|
? \&Socket::sockaddr_family |
451 |
|
|
: # for 5.6.x, we need to do something much more horrible |
452 |
|
|
(Socket::pack_sockaddr_in 0x5555, "\x55\x55\x55\x55" |
453 |
|
|
| eval { Socket::pack_sockaddr_un "U" }) =~ /^\x00/ |
454 |
|
|
? sub { unpack "xC", $_[0] } |
455 |
|
|
: sub { unpack "S" , $_[0] }; |
456 |
|
|
} |
457 |
|
|
|
458 |
root |
1.32 |
# check for broken platforms with extra field in sockaddr structure |
459 |
|
|
# kind of a rfc vs. bsd issue, as usual (ok, normally it's a |
460 |
|
|
# unix vs. bsd issue, a iso C vs. bsd issue or simply a |
461 |
root |
1.95 |
# correctness vs. bsd issue.) |
462 |
|
|
my $pack_family = 0x55 == sockaddr_family ("\x55\x55") |
463 |
root |
1.32 |
? "xC" : "S"; |
464 |
|
|
|
465 |
root |
1.34 |
=item $sa = AnyEvent::Socket::pack_sockaddr $service, $host |
466 |
root |
1.15 |
|
467 |
root |
1.34 |
Pack the given port/host combination into a binary sockaddr |
468 |
|
|
structure. Handles both IPv4 and IPv6 host addresses, as well as UNIX |
469 |
|
|
domain sockets (C<$host> == C<unix/> and C<$service> == absolute |
470 |
|
|
pathname). |
471 |
root |
1.15 |
|
472 |
|
|
=cut |
473 |
|
|
|
474 |
|
|
sub pack_sockaddr($$) { |
475 |
root |
1.34 |
my $af = address_family $_[1]; |
476 |
|
|
|
477 |
|
|
if ($af == AF_INET) { |
478 |
root |
1.15 |
Socket::pack_sockaddr_in $_[0], $_[1] |
479 |
root |
1.34 |
} elsif ($af == AF_INET6) { |
480 |
root |
1.32 |
pack "$pack_family nL a16 L", |
481 |
root |
1.21 |
AF_INET6, |
482 |
root |
1.15 |
$_[0], # port |
483 |
|
|
0, # flowinfo |
484 |
|
|
$_[1], # addr |
485 |
|
|
0 # scope id |
486 |
root |
1.34 |
} elsif ($af == AF_UNIX) { |
487 |
|
|
Socket::pack_sockaddr_un $_[0] |
488 |
root |
1.15 |
} else { |
489 |
|
|
Carp::croak "pack_sockaddr: invalid host"; |
490 |
|
|
} |
491 |
|
|
} |
492 |
|
|
|
493 |
root |
1.34 |
=item ($service, $host) = AnyEvent::Socket::unpack_sockaddr $sa |
494 |
root |
1.15 |
|
495 |
|
|
Unpack the given binary sockaddr structure (as used by bind, getpeername |
496 |
root |
1.34 |
etc.) into a C<$service, $host> combination. |
497 |
|
|
|
498 |
|
|
For IPv4 and IPv6, C<$service> is the port number and C<$host> the host |
499 |
|
|
address in network format (binary). |
500 |
root |
1.15 |
|
501 |
root |
1.34 |
For UNIX domain sockets, C<$service> is the absolute pathname and C<$host> |
502 |
|
|
is a special token that is understood by the other functions in this |
503 |
|
|
module (C<format_address> converts it to C<unix/>). |
504 |
root |
1.15 |
|
505 |
|
|
=cut |
506 |
|
|
|
507 |
|
|
sub unpack_sockaddr($) { |
508 |
root |
1.95 |
my $af = sockaddr_family $_[0]; |
509 |
root |
1.15 |
|
510 |
root |
1.23 |
if ($af == AF_INET) { |
511 |
root |
1.15 |
Socket::unpack_sockaddr_in $_[0] |
512 |
root |
1.21 |
} elsif ($af == AF_INET6) { |
513 |
|
|
unpack "x2 n x4 a16", $_[0] |
514 |
root |
1.34 |
} elsif ($af == AF_UNIX) { |
515 |
root |
1.35 |
((Socket::unpack_sockaddr_un $_[0]), pack "S", AF_UNIX) |
516 |
root |
1.15 |
} else { |
517 |
|
|
Carp::croak "unpack_sockaddr: unsupported protocol family $af"; |
518 |
|
|
} |
519 |
|
|
} |
520 |
|
|
|
521 |
root |
1.34 |
=item resolve_sockaddr $node, $service, $proto, $family, $type, $cb->([$family, $type, $proto, $sockaddr], ...) |
522 |
|
|
|
523 |
|
|
Tries to resolve the given nodename and service name into protocol families |
524 |
|
|
and sockaddr structures usable to connect to this node and service in a |
525 |
|
|
protocol-independent way. It works remotely similar to the getaddrinfo |
526 |
|
|
posix function. |
527 |
|
|
|
528 |
|
|
For internet addresses, C<$node> is either an IPv4 or IPv6 address or an |
529 |
|
|
internet hostname, and C<$service> is either a service name (port name |
530 |
|
|
from F</etc/services>) or a numerical port number. If both C<$node> and |
531 |
|
|
C<$service> are names, then SRV records will be consulted to find the real |
532 |
|
|
service, otherwise they will be used as-is. If you know that the service |
533 |
|
|
name is not in your services database, then you can specify the service in |
534 |
|
|
the format C<name=port> (e.g. C<http=80>). |
535 |
|
|
|
536 |
|
|
For UNIX domain sockets, C<$node> must be the string C<unix/> and |
537 |
|
|
C<$service> must be the absolute pathname of the socket. In this case, |
538 |
|
|
C<$proto> will be ignored. |
539 |
|
|
|
540 |
|
|
C<$proto> must be a protocol name, currently C<tcp>, C<udp> or |
541 |
|
|
C<sctp>. The default is currently C<tcp>, but in the future, this function |
542 |
|
|
might try to use other protocols such as C<sctp>, depending on the socket |
543 |
|
|
type and any SRV records it might find. |
544 |
|
|
|
545 |
|
|
C<$family> must be either C<0> (meaning any protocol is OK), C<4> (use |
546 |
root |
1.67 |
only IPv4) or C<6> (use only IPv6). The default is influenced by |
547 |
root |
1.34 |
C<$ENV{PERL_ANYEVENT_PROTOCOLS}>. |
548 |
|
|
|
549 |
|
|
C<$type> must be C<SOCK_STREAM>, C<SOCK_DGRAM> or C<SOCK_SEQPACKET> (or |
550 |
root |
1.67 |
C<undef> in which case it gets automatically chosen to be C<SOCK_STREAM> |
551 |
|
|
unless C<$proto> is C<udp>). |
552 |
root |
1.34 |
|
553 |
|
|
The callback will receive zero or more array references that contain |
554 |
|
|
C<$family, $type, $proto> for use in C<socket> and a binary |
555 |
|
|
C<$sockaddr> for use in C<connect> (or C<bind>). |
556 |
|
|
|
557 |
|
|
The application should try these in the order given. |
558 |
|
|
|
559 |
|
|
Example: |
560 |
|
|
|
561 |
|
|
resolve_sockaddr "google.com", "http", 0, undef, undef, sub { ... }; |
562 |
|
|
|
563 |
|
|
=cut |
564 |
|
|
|
565 |
|
|
sub resolve_sockaddr($$$$$$) { |
566 |
|
|
my ($node, $service, $proto, $family, $type, $cb) = @_; |
567 |
|
|
|
568 |
|
|
if ($node eq "unix/") { |
569 |
root |
1.67 |
return $cb->() if $family || $service !~ /^\//; # no can do |
570 |
root |
1.34 |
|
571 |
root |
1.67 |
return $cb->([AF_UNIX, defined $type ? $type : SOCK_STREAM, 0, Socket::pack_sockaddr_un $service]); |
572 |
root |
1.34 |
} |
573 |
|
|
|
574 |
|
|
unless (AF_INET6) { |
575 |
|
|
$family != 6 |
576 |
|
|
or return $cb->(); |
577 |
|
|
|
578 |
|
|
$family = 4; |
579 |
|
|
} |
580 |
|
|
|
581 |
|
|
$cb->() if $family == 4 && !$AnyEvent::PROTOCOL{ipv4}; |
582 |
|
|
$cb->() if $family == 6 && !$AnyEvent::PROTOCOL{ipv6}; |
583 |
|
|
|
584 |
|
|
$family ||= 4 unless $AnyEvent::PROTOCOL{ipv6}; |
585 |
|
|
$family ||= 6 unless $AnyEvent::PROTOCOL{ipv4}; |
586 |
|
|
|
587 |
|
|
$proto ||= "tcp"; |
588 |
|
|
$type ||= $proto eq "udp" ? SOCK_DGRAM : SOCK_STREAM; |
589 |
|
|
|
590 |
root |
1.93 |
my $proton = getprotobyname $proto |
591 |
root |
1.34 |
or Carp::croak "$proto: protocol unknown"; |
592 |
|
|
|
593 |
|
|
my $port; |
594 |
|
|
|
595 |
|
|
if ($service =~ /^(\S+)=(\d+)$/) { |
596 |
|
|
($service, $port) = ($1, $2); |
597 |
|
|
} elsif ($service =~ /^\d+$/) { |
598 |
|
|
($service, $port) = (undef, $service); |
599 |
|
|
} else { |
600 |
|
|
$port = (getservbyname $service, $proto)[2] |
601 |
root |
1.35 |
or Carp::croak "$service/$proto: service unknown"; |
602 |
root |
1.34 |
} |
603 |
|
|
|
604 |
|
|
my @target = [$node, $port]; |
605 |
|
|
|
606 |
|
|
# resolve a records / provide sockaddr structures |
607 |
|
|
my $resolve = sub { |
608 |
|
|
my @res; |
609 |
root |
1.107 |
my $cv = AE::cv { |
610 |
root |
1.34 |
$cb->( |
611 |
|
|
map $_->[2], |
612 |
|
|
sort { |
613 |
|
|
$AnyEvent::PROTOCOL{$b->[1]} <=> $AnyEvent::PROTOCOL{$a->[1]} |
614 |
|
|
or $a->[0] <=> $b->[0] |
615 |
|
|
} |
616 |
|
|
@res |
617 |
|
|
) |
618 |
root |
1.107 |
}; |
619 |
root |
1.34 |
|
620 |
|
|
$cv->begin; |
621 |
|
|
for my $idx (0 .. $#target) { |
622 |
|
|
my ($node, $port) = @{ $target[$idx] }; |
623 |
|
|
|
624 |
|
|
if (my $noden = parse_address $node) { |
625 |
root |
1.40 |
my $af = address_family $noden; |
626 |
|
|
|
627 |
|
|
if ($af == AF_INET && $family != 6) { |
628 |
root |
1.34 |
push @res, [$idx, "ipv4", [AF_INET, $type, $proton, |
629 |
|
|
pack_sockaddr $port, $noden]] |
630 |
|
|
} |
631 |
|
|
|
632 |
root |
1.40 |
if ($af == AF_INET6 && $family != 4) { |
633 |
root |
1.34 |
push @res, [$idx, "ipv6", [AF_INET6, $type, $proton, |
634 |
|
|
pack_sockaddr $port, $noden]] |
635 |
|
|
} |
636 |
|
|
} else { |
637 |
|
|
# ipv4 |
638 |
|
|
if ($family != 6) { |
639 |
|
|
$cv->begin; |
640 |
root |
1.39 |
AnyEvent::DNS::a $node, sub { |
641 |
root |
1.34 |
push @res, [$idx, "ipv4", [AF_INET, $type, $proton, |
642 |
|
|
pack_sockaddr $port, parse_ipv4 $_]] |
643 |
|
|
for @_; |
644 |
|
|
$cv->end; |
645 |
|
|
}; |
646 |
|
|
} |
647 |
|
|
|
648 |
|
|
# ipv6 |
649 |
|
|
if ($family != 4) { |
650 |
|
|
$cv->begin; |
651 |
root |
1.39 |
AnyEvent::DNS::aaaa $node, sub { |
652 |
root |
1.34 |
push @res, [$idx, "ipv6", [AF_INET6, $type, $proton, |
653 |
|
|
pack_sockaddr $port, parse_ipv6 $_]] |
654 |
|
|
for @_; |
655 |
|
|
$cv->end; |
656 |
|
|
}; |
657 |
|
|
} |
658 |
|
|
} |
659 |
|
|
} |
660 |
|
|
$cv->end; |
661 |
|
|
}; |
662 |
|
|
|
663 |
|
|
# try srv records, if applicable |
664 |
|
|
if ($node eq "localhost") { |
665 |
|
|
@target = (["127.0.0.1", $port], ["::1", $port]); |
666 |
|
|
&$resolve; |
667 |
|
|
} elsif (defined $service && !parse_address $node) { |
668 |
root |
1.39 |
AnyEvent::DNS::srv $service, $proto, $node, sub { |
669 |
root |
1.34 |
my (@srv) = @_; |
670 |
|
|
|
671 |
|
|
# no srv records, continue traditionally |
672 |
|
|
@srv |
673 |
|
|
or return &$resolve; |
674 |
|
|
|
675 |
root |
1.43 |
# the only srv record has "." ("" here) => abort |
676 |
root |
1.42 |
$srv[0][2] ne "" || $#srv |
677 |
root |
1.34 |
or return $cb->(); |
678 |
|
|
|
679 |
|
|
# use srv records then |
680 |
|
|
@target = map ["$_->[3].", $_->[2]], |
681 |
|
|
grep $_->[3] ne ".", |
682 |
|
|
@srv; |
683 |
|
|
|
684 |
|
|
&$resolve; |
685 |
|
|
}; |
686 |
|
|
} else { |
687 |
|
|
&$resolve; |
688 |
|
|
} |
689 |
|
|
} |
690 |
|
|
|
691 |
root |
1.15 |
=item $guard = tcp_connect $host, $service, $connect_cb[, $prepare_cb] |
692 |
elmex |
1.1 |
|
693 |
root |
1.15 |
This is a convenience function that creates a TCP socket and makes a 100% |
694 |
root |
1.34 |
non-blocking connect to the given C<$host> (which can be a hostname or |
695 |
|
|
a textual IP address, or the string C<unix/> for UNIX domain sockets) |
696 |
|
|
and C<$service> (which can be a numeric port number or a service name, |
697 |
|
|
or a C<servicename=portnumber> string, or the pathname to a UNIX domain |
698 |
|
|
socket). |
699 |
root |
1.7 |
|
700 |
root |
1.8 |
If both C<$host> and C<$port> are names, then this function will use SRV |
701 |
root |
1.15 |
records to locate the real target(s). |
702 |
root |
1.8 |
|
703 |
root |
1.15 |
In either case, it will create a list of target hosts (e.g. for multihomed |
704 |
root |
1.17 |
hosts or hosts with both IPv4 and IPv6 addresses) and try to connect to |
705 |
root |
1.15 |
each in turn. |
706 |
root |
1.7 |
|
707 |
root |
1.108 |
After the connection is established, then the C<$connect_cb> will be |
708 |
|
|
invoked with the socket file handle (in non-blocking mode) as first and |
709 |
|
|
the peer host (as a textual IP address) and peer port as second and third |
710 |
|
|
arguments, respectively. The fourth argument is a code reference that you |
711 |
|
|
can call if, for some reason, you don't like this connection, which will |
712 |
|
|
cause C<tcp_connect> to try the next one (or call your callback without |
713 |
|
|
any arguments if there are no more connections). In most cases, you can |
714 |
|
|
simply ignore this argument. |
715 |
root |
1.15 |
|
716 |
|
|
$cb->($filehandle, $host, $port, $retry) |
717 |
root |
1.7 |
|
718 |
|
|
If the connect is unsuccessful, then the C<$connect_cb> will be invoked |
719 |
|
|
without any arguments and C<$!> will be set appropriately (with C<ENXIO> |
720 |
root |
1.17 |
indicating a DNS resolution failure). |
721 |
root |
1.7 |
|
722 |
root |
1.108 |
The callback will I<never> be invoked before C<tcp_connect> returns, even |
723 |
|
|
if C<tcp_connect> was able to connect immediately (e.g. on unix domain |
724 |
|
|
sockets). |
725 |
|
|
|
726 |
root |
1.17 |
The file handle is perfect for being plugged into L<AnyEvent::Handle>, but |
727 |
|
|
can be used as a normal perl file handle as well. |
728 |
root |
1.7 |
|
729 |
root |
1.15 |
Unless called in void context, C<tcp_connect> returns a guard object that |
730 |
|
|
will automatically abort connecting when it gets destroyed (it does not do |
731 |
|
|
anything to the socket after the connect was successful). |
732 |
|
|
|
733 |
root |
1.7 |
Sometimes you need to "prepare" the socket before connecting, for example, |
734 |
|
|
to C<bind> it to some port, or you want a specific connect timeout that |
735 |
|
|
is lower than your kernel's default timeout. In this case you can specify |
736 |
|
|
a second callback, C<$prepare_cb>. It will be called with the file handle |
737 |
|
|
in not-yet-connected state as only argument and must return the connection |
738 |
|
|
timeout value (or C<0>, C<undef> or the empty list to indicate the default |
739 |
|
|
timeout is to be used). |
740 |
|
|
|
741 |
root |
1.17 |
Note that the socket could be either a IPv4 TCP socket or an IPv6 TCP |
742 |
root |
1.7 |
socket (although only IPv4 is currently supported by this module). |
743 |
|
|
|
744 |
root |
1.28 |
Note to the poor Microsoft Windows users: Windows (of course) doesn't |
745 |
|
|
correctly signal connection errors, so unless your event library works |
746 |
|
|
around this, failed connections will simply hang. The only event libraries |
747 |
|
|
that handle this condition correctly are L<EV> and L<Glib>. Additionally, |
748 |
|
|
AnyEvent works around this bug with L<Event> and in its pure-perl |
749 |
|
|
backend. All other libraries cannot correctly handle this condition. To |
750 |
|
|
lessen the impact of this windows bug, a default timeout of 30 seconds |
751 |
|
|
will be imposed on windows. Cygwin is not affected. |
752 |
root |
1.27 |
|
753 |
root |
1.7 |
Simple Example: connect to localhost on port 22. |
754 |
|
|
|
755 |
root |
1.45 |
tcp_connect localhost => 22, sub { |
756 |
|
|
my $fh = shift |
757 |
|
|
or die "unable to connect: $!"; |
758 |
|
|
# do something |
759 |
|
|
}; |
760 |
root |
1.7 |
|
761 |
|
|
Complex Example: connect to www.google.com on port 80 and make a simple |
762 |
|
|
GET request without much error handling. Also limit the connection timeout |
763 |
|
|
to 15 seconds. |
764 |
|
|
|
765 |
|
|
tcp_connect "www.google.com", "http", |
766 |
|
|
sub { |
767 |
|
|
my ($fh) = @_ |
768 |
|
|
or die "unable to connect: $!"; |
769 |
|
|
|
770 |
|
|
my $handle; # avoid direct assignment so on_eof has it in scope. |
771 |
|
|
$handle = new AnyEvent::Handle |
772 |
|
|
fh => $fh, |
773 |
root |
1.90 |
on_error => sub { |
774 |
|
|
warn "error $_[2]\n"; |
775 |
root |
1.91 |
$_[0]->destroy; |
776 |
root |
1.90 |
}, |
777 |
root |
1.7 |
on_eof => sub { |
778 |
root |
1.90 |
$handle->destroy; # destroy handle |
779 |
root |
1.7 |
warn "done.\n"; |
780 |
|
|
}; |
781 |
|
|
|
782 |
|
|
$handle->push_write ("GET / HTTP/1.0\015\012\015\012"); |
783 |
|
|
|
784 |
|
|
$handle->push_read_line ("\015\012\015\012", sub { |
785 |
|
|
my ($handle, $line) = @_; |
786 |
|
|
|
787 |
|
|
# print response header |
788 |
|
|
print "HEADER\n$line\n\nBODY\n"; |
789 |
|
|
|
790 |
|
|
$handle->on_read (sub { |
791 |
|
|
# print response body |
792 |
|
|
print $_[0]->rbuf; |
793 |
|
|
$_[0]->rbuf = ""; |
794 |
|
|
}); |
795 |
|
|
}); |
796 |
|
|
}, sub { |
797 |
|
|
my ($fh) = @_; |
798 |
|
|
# could call $fh->bind etc. here |
799 |
elmex |
1.2 |
|
800 |
root |
1.7 |
15 |
801 |
|
|
}; |
802 |
elmex |
1.2 |
|
803 |
root |
1.34 |
Example: connect to a UNIX domain socket. |
804 |
|
|
|
805 |
|
|
tcp_connect "unix/", "/tmp/.X11-unix/X0", sub { |
806 |
|
|
... |
807 |
|
|
} |
808 |
|
|
|
809 |
root |
1.7 |
=cut |
810 |
elmex |
1.2 |
|
811 |
root |
1.7 |
sub tcp_connect($$$;$) { |
812 |
|
|
my ($host, $port, $connect, $prepare) = @_; |
813 |
elmex |
1.2 |
|
814 |
root |
1.7 |
# see http://cr.yp.to/docs/connect.html for some background |
815 |
root |
1.33 |
# also http://advogato.org/article/672.html |
816 |
elmex |
1.2 |
|
817 |
root |
1.7 |
my %state = ( fh => undef ); |
818 |
elmex |
1.2 |
|
819 |
root |
1.33 |
# name/service to type/sockaddr resolution |
820 |
root |
1.67 |
resolve_sockaddr $host, $port, 0, 0, undef, sub { |
821 |
root |
1.15 |
my @target = @_; |
822 |
root |
1.7 |
|
823 |
root |
1.15 |
$state{next} = sub { |
824 |
|
|
return unless exists $state{fh}; |
825 |
root |
1.7 |
|
826 |
root |
1.15 |
my $target = shift @target |
827 |
root |
1.108 |
or return (%state = (), _postpone $connect); |
828 |
root |
1.7 |
|
829 |
root |
1.15 |
my ($domain, $type, $proto, $sockaddr) = @$target; |
830 |
root |
1.7 |
|
831 |
root |
1.15 |
# socket creation |
832 |
|
|
socket $state{fh}, $domain, $type, $proto |
833 |
|
|
or return $state{next}(); |
834 |
|
|
|
835 |
|
|
fh_nonblocking $state{fh}, 1; |
836 |
|
|
|
837 |
root |
1.27 |
my $timeout = $prepare && $prepare->($state{fh}); |
838 |
|
|
|
839 |
root |
1.30 |
$timeout ||= 30 if AnyEvent::WIN32; |
840 |
root |
1.15 |
|
841 |
root |
1.107 |
$state{to} = AE::timer $timeout, 0, sub { |
842 |
root |
1.90 |
$! = Errno::ETIMEDOUT; |
843 |
root |
1.27 |
$state{next}(); |
844 |
root |
1.107 |
} if $timeout; |
845 |
root |
1.7 |
|
846 |
root |
1.107 |
# now connect |
847 |
|
|
if ( |
848 |
|
|
(connect $state{fh}, $sockaddr) |
849 |
|
|
|| ($! == Errno::EINPROGRESS # POSIX |
850 |
|
|
|| $! == Errno::EWOULDBLOCK |
851 |
|
|
# WSAEINPROGRESS intentionally not checked - it means something else entirely |
852 |
|
|
|| $! == AnyEvent::Util::WSAEINVAL # not convinced, but doesn't hurt |
853 |
|
|
|| $! == AnyEvent::Util::WSAEWOULDBLOCK) |
854 |
|
|
) { |
855 |
|
|
$state{ww} = AE::io $state{fh}, 1, sub { |
856 |
|
|
# we are connected, or maybe there was an error |
857 |
|
|
if (my $sin = getpeername $state{fh}) { |
858 |
|
|
my ($port, $host) = unpack_sockaddr $sin; |
859 |
|
|
|
860 |
|
|
delete $state{ww}; delete $state{to}; |
861 |
|
|
|
862 |
|
|
my $guard = guard { %state = () }; |
863 |
|
|
|
864 |
|
|
$connect->(delete $state{fh}, format_address $host, $port, sub { |
865 |
|
|
$guard->cancel; |
866 |
|
|
$state{next}(); |
867 |
|
|
}); |
868 |
|
|
} else { |
869 |
|
|
# dummy read to fetch real error code |
870 |
|
|
sysread $state{fh}, my $buf, 1 if $! == Errno::ENOTCONN; |
871 |
root |
1.15 |
|
872 |
root |
1.107 |
return if $! == Errno::EAGAIN; # skip spurious wake-ups |
873 |
root |
1.89 |
|
874 |
root |
1.107 |
delete $state{ww}; delete $state{to}; |
875 |
root |
1.15 |
|
876 |
|
|
$state{next}(); |
877 |
root |
1.107 |
} |
878 |
|
|
}; |
879 |
root |
1.7 |
} else { |
880 |
root |
1.29 |
$state{next}(); |
881 |
root |
1.7 |
} |
882 |
|
|
}; |
883 |
elmex |
1.1 |
|
884 |
root |
1.90 |
$! = Errno::ENXIO; |
885 |
root |
1.15 |
$state{next}(); |
886 |
root |
1.7 |
}; |
887 |
elmex |
1.1 |
|
888 |
root |
1.15 |
defined wantarray && guard { %state = () } |
889 |
elmex |
1.1 |
} |
890 |
|
|
|
891 |
root |
1.35 |
=item $guard = tcp_server $host, $service, $accept_cb[, $prepare_cb] |
892 |
elmex |
1.1 |
|
893 |
root |
1.35 |
Create and bind a stream socket to the given host, and port, set the |
894 |
|
|
SO_REUSEADDR flag (if applicable) and call C<listen>. Unlike the name |
895 |
|
|
implies, this function can also bind on UNIX domain sockets. |
896 |
|
|
|
897 |
|
|
For internet sockets, C<$host> must be an IPv4 or IPv6 address (or |
898 |
root |
1.38 |
C<undef>, in which case it binds either to C<0> or to C<::>, depending |
899 |
|
|
on whether IPv4 or IPv6 is the preferred protocol, and maybe to both in |
900 |
|
|
future versions, as applicable). |
901 |
root |
1.21 |
|
902 |
|
|
To bind to the IPv4 wildcard address, use C<0>, to bind to the IPv6 |
903 |
|
|
wildcard address, use C<::>. |
904 |
|
|
|
905 |
root |
1.35 |
The port is specified by C<$service>, which must be either a service name or |
906 |
root |
1.21 |
a numeric port number (or C<0> or C<undef>, in which case an ephemeral |
907 |
|
|
port will be used). |
908 |
|
|
|
909 |
root |
1.35 |
For UNIX domain sockets, C<$host> must be C<unix/> and C<$service> must be |
910 |
|
|
the absolute pathname of the socket. This function will try to C<unlink> |
911 |
|
|
the socket before it tries to bind to it. See SECURITY CONSIDERATIONS, |
912 |
|
|
below. |
913 |
|
|
|
914 |
root |
1.21 |
For each new connection that could be C<accept>ed, call the C<< |
915 |
|
|
$accept_cb->($fh, $host, $port) >> with the file handle (in non-blocking |
916 |
|
|
mode) as first and the peer host and port as second and third arguments |
917 |
|
|
(see C<tcp_connect> for details). |
918 |
|
|
|
919 |
|
|
Croaks on any errors it can detect before the listen. |
920 |
elmex |
1.1 |
|
921 |
root |
1.7 |
If called in non-void context, then this function returns a guard object |
922 |
root |
1.17 |
whose lifetime it tied to the TCP server: If the object gets destroyed, |
923 |
root |
1.7 |
the server will be stopped (but existing accepted connections will |
924 |
|
|
continue). |
925 |
elmex |
1.1 |
|
926 |
root |
1.7 |
If you need more control over the listening socket, you can provide a |
927 |
root |
1.21 |
C<< $prepare_cb->($fh, $host, $port) >>, which is called just before the |
928 |
|
|
C<listen ()> call, with the listen file handle as first argument, and IP |
929 |
|
|
address and port number of the local socket endpoint as second and third |
930 |
|
|
arguments. |
931 |
elmex |
1.2 |
|
932 |
root |
1.7 |
It should return the length of the listen queue (or C<0> for the default). |
933 |
elmex |
1.2 |
|
934 |
root |
1.38 |
Note to IPv6 users: RFC-compliant behaviour for IPv6 sockets listening on |
935 |
|
|
C<::> is to bind to both IPv6 and IPv4 addresses by default on dual-stack |
936 |
|
|
hosts. Unfortunately, only GNU/Linux seems to implement this properly, so |
937 |
|
|
if you want both IPv4 and IPv6 listening sockets you should create the |
938 |
|
|
IPv6 socket first and then attempt to bind on the IPv4 socket, but ignore |
939 |
|
|
any C<EADDRINUSE> errors. |
940 |
|
|
|
941 |
root |
1.24 |
Example: bind on some TCP port on the local machine and tell each client |
942 |
root |
1.7 |
to go away. |
943 |
elmex |
1.2 |
|
944 |
root |
1.24 |
tcp_server undef, undef, sub { |
945 |
root |
1.7 |
my ($fh, $host, $port) = @_; |
946 |
elmex |
1.1 |
|
947 |
root |
1.7 |
syswrite $fh, "The internet is full, $host:$port. Go away!\015\012"; |
948 |
root |
1.24 |
}, sub { |
949 |
|
|
my ($fh, $thishost, $thisport) = @_; |
950 |
|
|
warn "bound to $thishost, port $thisport\n"; |
951 |
root |
1.7 |
}; |
952 |
elmex |
1.1 |
|
953 |
root |
1.67 |
Example: bind a server on a unix domain socket. |
954 |
|
|
|
955 |
|
|
tcp_server "unix/", "/tmp/mydir/mysocket", sub { |
956 |
|
|
my ($fh) = @_; |
957 |
|
|
}; |
958 |
|
|
|
959 |
root |
1.7 |
=cut |
960 |
elmex |
1.1 |
|
961 |
root |
1.7 |
sub tcp_server($$$;$) { |
962 |
root |
1.35 |
my ($host, $service, $accept, $prepare) = @_; |
963 |
elmex |
1.1 |
|
964 |
root |
1.25 |
$host = $AnyEvent::PROTOCOL{ipv4} < $AnyEvent::PROTOCOL{ipv6} && AF_INET6 |
965 |
root |
1.22 |
? "::" : "0" |
966 |
root |
1.21 |
unless defined $host; |
967 |
|
|
|
968 |
root |
1.34 |
my $ipn = parse_address $host |
969 |
|
|
or Carp::croak "AnyEvent::Socket::tcp_server: cannot parse '$host' as host address"; |
970 |
root |
1.21 |
|
971 |
root |
1.35 |
my $af = address_family $ipn; |
972 |
root |
1.21 |
|
973 |
root |
1.7 |
my %state; |
974 |
elmex |
1.1 |
|
975 |
root |
1.36 |
# win32 perl is too stupid to get this right :/ |
976 |
|
|
Carp::croak "tcp_server/socket: address family not supported" |
977 |
|
|
if AnyEvent::WIN32 && $af == AF_UNIX; |
978 |
|
|
|
979 |
root |
1.35 |
socket $state{fh}, $af, SOCK_STREAM, 0 |
980 |
root |
1.36 |
or Carp::croak "tcp_server/socket: $!"; |
981 |
elmex |
1.1 |
|
982 |
root |
1.35 |
if ($af == AF_INET || $af == AF_INET6) { |
983 |
|
|
setsockopt $state{fh}, SOL_SOCKET, SO_REUSEADDR, 1 |
984 |
root |
1.36 |
or Carp::croak "tcp_server/so_reuseaddr: $!" |
985 |
root |
1.37 |
unless AnyEvent::WIN32; # work around windows bug |
986 |
root |
1.35 |
|
987 |
|
|
unless ($service =~ /^\d*$/) { |
988 |
|
|
$service = (getservbyname $service, "tcp")[2] |
989 |
|
|
or Carp::croak "$service: service unknown" |
990 |
|
|
} |
991 |
|
|
} elsif ($af == AF_UNIX) { |
992 |
|
|
unlink $service; |
993 |
|
|
} |
994 |
elmex |
1.1 |
|
995 |
root |
1.35 |
bind $state{fh}, pack_sockaddr $service, $ipn |
996 |
root |
1.7 |
or Carp::croak "bind: $!"; |
997 |
elmex |
1.1 |
|
998 |
root |
1.7 |
fh_nonblocking $state{fh}, 1; |
999 |
elmex |
1.1 |
|
1000 |
root |
1.21 |
my $len; |
1001 |
|
|
|
1002 |
|
|
if ($prepare) { |
1003 |
root |
1.35 |
my ($service, $host) = unpack_sockaddr getsockname $state{fh}; |
1004 |
|
|
$len = $prepare && $prepare->($state{fh}, format_address $host, $service); |
1005 |
root |
1.21 |
} |
1006 |
|
|
|
1007 |
|
|
$len ||= 128; |
1008 |
elmex |
1.1 |
|
1009 |
root |
1.7 |
listen $state{fh}, $len |
1010 |
|
|
or Carp::croak "listen: $!"; |
1011 |
elmex |
1.1 |
|
1012 |
root |
1.107 |
$state{aw} = AE::io $state{fh}, 0, sub { |
1013 |
root |
1.7 |
# this closure keeps $state alive |
1014 |
|
|
while (my $peer = accept my $fh, $state{fh}) { |
1015 |
|
|
fh_nonblocking $fh, 1; # POSIX requires inheritance, the outside world does not |
1016 |
root |
1.37 |
|
1017 |
root |
1.35 |
my ($service, $host) = unpack_sockaddr $peer; |
1018 |
|
|
$accept->($fh, format_address $host, $service); |
1019 |
root |
1.7 |
} |
1020 |
root |
1.107 |
}; |
1021 |
elmex |
1.1 |
|
1022 |
root |
1.7 |
defined wantarray |
1023 |
|
|
? guard { %state = () } # clear fh and watcher, which breaks the circular dependency |
1024 |
|
|
: () |
1025 |
elmex |
1.1 |
} |
1026 |
|
|
|
1027 |
root |
1.7 |
1; |
1028 |
|
|
|
1029 |
elmex |
1.1 |
=back |
1030 |
|
|
|
1031 |
root |
1.38 |
=head1 SECURITY CONSIDERATIONS |
1032 |
|
|
|
1033 |
|
|
This module is quite powerful, with with power comes the ability to abuse |
1034 |
|
|
as well: If you accept "hostnames" and ports from untrusted sources, |
1035 |
|
|
then note that this can be abused to delete files (host=C<unix/>). This |
1036 |
|
|
is not really a problem with this module, however, as blindly accepting |
1037 |
|
|
any address and protocol and trying to bind a server or connect to it is |
1038 |
|
|
harmful in general. |
1039 |
|
|
|
1040 |
elmex |
1.1 |
=head1 AUTHOR |
1041 |
|
|
|
1042 |
root |
1.7 |
Marc Lehmann <schmorp@schmorp.de> |
1043 |
|
|
http://home.schmorp.de/ |
1044 |
elmex |
1.1 |
|
1045 |
|
|
=cut |
1046 |
|
|
|