in reply to IO::Socket::INET's sockaddr() returns 16 byte instead of 4, Socket::inet_ntoa() complains

The socket uses IPv6. I'm guessing you're using localhost and that Squeeze defines localhost as ::1. The dumb thing is that sockaddr doesn't tell you if what kind of address it's returning. The module, was written with only IPv4 in mind.

Here's how you can address this in a portable manner:

use Socket qw( sockaddr_family sockaddr_in sockaddr_in6 inet_ntop AF_I +NET ); my $sockaddr = getsockaddr($sock); # Or: $sock->sockname() my $fam = sockaddr_family($sockaddr); my $addr_n = $fam == AF_INET ? (sockaddr_in($sockaddr))[1] : (sockaddr_in6($sockaddr))[1]; my $addr_a = inet_ntop($fam, $addr_n); say $addr_a;
  • Comment on Re: IO::Socket::INET's sockaddr() returns 16 byte instead of 4, Socket::inet_ntoa() complains
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: IO::Socket::INET's sockaddr() returns 16 byte instead of 4, Socket::inet_ntoa() complains
by sedusedan (Pilgrim) on Jan 21, 2012 at 03:06 UTC

    Hm, that's very verbose.

    What I'm not getting is: IO::Socket::INET's doc says, "IO::Socket::INET provides an object interface to creating and using sockets in the AF_INET domain" but why is it still receiving AF_INET6 stuff?

    There's also IO::Socket::INET6 maintained by Shlomi Fish.

    So where should be the appropriate fix/patch placed? In HTTP::Daemon (e.g. creating a separate HTTP::Daemon6)? In IO::Socket::INET (always converts things to IPv4, so sockaddr() never returns 16 bytes)? In still-lower library? This IPv4/IPv6 dichotomy is quite confusing to me.

      You probably have installed package: ii libio-socket-inet6-perl 2.65-1.1 Object interface for AF_INET6 domain sockets uninstall "libio-socket-inet6-perl" package from system and it will work ! :-D
        Thanks! It does solve the problem. I'm linking this page to the RT ticket for reference.