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