ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Socket.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Socket.pm (file contents):
Revision 1.116 by root, Sat Nov 28 13:43:47 2009 UTC vs.
Revision 1.117 by root, Sat Dec 5 02:50:48 2009 UTC

430*ntoa = \&format_address; 430*ntoa = \&format_address;
431 431
432=item inet_aton $name_or_address, $cb->(@addresses) 432=item inet_aton $name_or_address, $cb->(@addresses)
433 433
434Works similarly to its Socket counterpart, except that it uses a 434Works similarly to its Socket counterpart, except that it uses a
435callback. Also, if a host has only an IPv6 address, this might be passed 435callback. Use the length to distinguish between ipv4 and ipv6 (4 octets
436to the callback instead (use the length to detect this - 4 for IPv4, 16 436for IPv4, 16 for IPv6), or use C<format_address> to convert it to a more
437for IPv6). 437readable format.
438 438
439Unlike the L<Socket> function of the same name, you can get multiple IPv4 439Note that C<resolve_sockaddr>, while initially a more complex interface,
440and IPv6 addresses as result (and maybe even other adrdess types). 440resolves host addresses, service names and SRV records and gives you an
441ordered list of socket addresses to try and should be preferred over
442C<inet_aton>.
441 443
442Example. 444Example.
443 445
444 inet_aton "www.google.com", my $cv = AE::cv; 446 inet_aton "www.google.com", my $cv = AE::cv;
445 say unpack "H*", $_ 447 say unpack "H*", $_
446 for $cv->recv; 448 for $cv->recv;
447 # => d155e363 449 # => d155e363
448 # => d155e367 etc. 450 # => d155e367 etc.
451
452 inet_aton "ipv6.google.com", my $cv = AE::cv;
453 say unpack "H*", $_
454 for $cv->recv;
455 # => 20014860a00300000000000000000068
449 456
450=cut 457=cut
451 458
452sub inet_aton { 459sub inet_aton {
453 my ($name, $cb) = @_; 460 my ($name, $cb) = @_;
459 } elsif ($name eq "localhost") { # rfc2606 et al. 466 } elsif ($name eq "localhost") { # rfc2606 et al.
460 $cb->(v127.0.0.1, v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1); 467 $cb->(v127.0.0.1, v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1);
461 } else { 468 } else {
462 require AnyEvent::DNS; 469 require AnyEvent::DNS;
463 470
464 # simple, bad suboptimal algorithm 471 my $ipv4 = $AnyEvent::PROTOCOL{ipv4};
472 my $ipv6 = $AnyEvent::PROTOCOL{ipv6};
473
474 my @res;
475
476 my $cv = AE::cv {
477 $cb->(map @$_, reverse @res);
478 };
479
480 $cv->begin;
481
482 if ($ipv4) {
483 $cv->begin;
465 AnyEvent::DNS::a ($name, sub { 484 AnyEvent::DNS::a ($name, sub {
466 if (@_) { 485 $res[$ipv4] = [map &parse_ipv4, @_];
467 $cb->(map +(parse_ipv4 $_), @_);
468 } else {
469 $cb->(); 486 $cv->end;
470 #AnyEvent::DNS::aaaa ($name, $cb); need inet_pton
471 } 487 });
472 }); 488 };
489
490 if ($ipv6) {
491 $cv->begin;
492 AnyEvent::DNS::aaaa ($name, sub {
493 $res[$ipv6] = [map &parse_ipv6, @_];
494 $cv->end;
495 });
496 };
497
498 $cv->end;
473 } 499 }
474} 500}
475 501
476BEGIN { 502BEGIN {
477 *sockaddr_family = $Socket::VERSION >= 1.75 503 *sockaddr_family = $Socket::VERSION >= 1.75
481 | eval { Socket::pack_sockaddr_un "U" }) =~ /^\x00/ 507 | eval { Socket::pack_sockaddr_un "U" }) =~ /^\x00/
482 ? sub { unpack "xC", $_[0] } 508 ? sub { unpack "xC", $_[0] }
483 : sub { unpack "S" , $_[0] }; 509 : sub { unpack "S" , $_[0] };
484} 510}
485 511
486# check for broken platforms with extra field in sockaddr structure 512# check for broken platforms with an extra field in sockaddr structure
487# kind of a rfc vs. bsd issue, as usual (ok, normally it's a 513# kind of a rfc vs. bsd issue, as usual (ok, normally it's a
488# unix vs. bsd issue, a iso C vs. bsd issue or simply a 514# unix vs. bsd issue, a iso C vs. bsd issue or simply a
489# correctness vs. bsd issue.) 515# correctness vs. bsd issue.)
490my $pack_family = 0x55 == sockaddr_family ("\x55\x55") 516my $pack_family = 0x55 == sockaddr_family ("\x55\x55")
491 ? "xC" : "S"; 517 ? "xC" : "S";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines