… | |
… | |
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 | |
434 | Works similarly to its Socket counterpart, except that it uses a |
434 | Works similarly to its Socket counterpart, except that it uses a |
435 | callback. Also, if a host has only an IPv6 address, this might be passed |
435 | callback. Use the length to distinguish between ipv4 and ipv6 (4 octets |
436 | to the callback instead (use the length to detect this - 4 for IPv4, 16 |
436 | for IPv4, 16 for IPv6), or use C<format_address> to convert it to a more |
437 | for IPv6). |
437 | readable format. |
438 | |
438 | |
439 | Unlike the L<Socket> function of the same name, you can get multiple IPv4 |
439 | Note that C<resolve_sockaddr>, while initially a more complex interface, |
440 | and IPv6 addresses as result (and maybe even other adrdess types). |
440 | resolves host addresses, service names and SRV records and gives you an |
|
|
441 | ordered list of socket addresses to try and should be preferred over |
|
|
442 | C<inet_aton>. |
441 | |
443 | |
442 | Example. |
444 | Example. |
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 | |
452 | sub inet_aton { |
459 | sub 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 | |
476 | BEGIN { |
502 | BEGIN { |
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.) |
490 | my $pack_family = 0x55 == sockaddr_family ("\x55\x55") |
516 | my $pack_family = 0x55 == sockaddr_family ("\x55\x55") |
491 | ? "xC" : "S"; |
517 | ? "xC" : "S"; |