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