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