rama133101 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to create an application that support Dual stack IP. I am using the IO::Socket::IP which allows both IPv4 and IPv6. Below is the piece of code I took from Socket::IP cpan page.

use Net::SSH2; use IO::Socket::IP; use Socket qw(:addrinfo SOCK_RAW); use Socket6; my $ipv6 = "2620:0:170:2531:60:165d:4f14:1068"; my $ipv4 = "10.243.106.8"; my $sock = IO::Socket::IP->new( PeerHost => $ipv6, PeerPort => "22", Timeout => 3 ) or die "Cannot construct socket - $@";

The above code is giving "Cannot construct socket - nodename nor servname provided" error. Initially I was using perl 5.20.2 and everything was working fine. Now,for some reason I had to move to 5.16.1 and this same piece of code is giving error.

I went through couple of posts discussing similar issues in ipv6 support. One suggestion I see is the usage of getaddrinfo. But, that doesn't help me in any way. Another post says, Socket doesn't support Ipv6. Another post suggests using socket6. I have tried all options and nothing helps

kindly provide suggestions. I believe I am missing out something in Perl 5.16.

Replies are listed 'Best First'.
Re: Activeperl 5.16.1 Ipv6 support
by Apero (Scribe) on Nov 30, 2015 at 17:07 UTC

    I'd suggest you be a bit more explicit about the type of socket you want to create. It's possible the version of Perl you're using can't figure out some of the details that you're expecting to be assumed properly. Of course, it's also possible the version of Perl or some library was built without IPv6 support, in which case there's not much you can do to fix it besides recompile.

    For starters, you should always define the Proto and Type of socket you create. In this case it appears you want a TCP socket of a STREAM type (TCP sockets are always streams; other types of sockets, like Unix Domain, can be one of multiple types.)

    You can also enforce a desired address Family, such as AF_INET for IPv4 or AF_INET6 for IPv6. These constants are defined in the Socket Perl class (and ultimately defined in the C library sys/socket.h where Perl gets them.) If you do not declare an address family, the kernel will determine an appropriate type through a gethostbyname(3) system call. For dual-stacked hosts, this may not be what you intend when using a DNS name, like google.com instead of a raw IP. We don't usually hard-code IPs in code for IPv6, and it's even bad practice in IPv4 in most cases.

    Also, you probably want to use the $! variable in errors describing system calls, such as socket creation. The $@ variable holds the text of the last eval failure (this is how you "catch" exceptions in Perl) while the $! variable holds the C errno value, which will be translated to its human-readable error text when used in a string. Update: $@ may contain higher-level details on the failure, so sometimes "Socket error: $@ ($!)" can be helpful. See perlvar docs for details.

    I've given you a bit more of a complete example below that creates a TCP socket in STREAM mode to an IPv6 endpoint for google on port 80. Then it'll send a request for the homepage and print results to STDOUT. This should work anywhere provided you have a sufficiently new-enough Perl (5.20 or better) that includes IO::Socket::IP, or have obtained that module from CPAN or similar.

    Note that using a socket like this is a fairly bad way to get a webpage, but this is simply a demonstration of how you normally use a client-socket.

      Thanks for the insight. I was able to reckon that the Socket library must have no support for the Ipv6. In other words, the Socket must be only Ipv4 compatible. Then I came across this bug reported months back.

      https://rt.cpan.org/Public/Bug/Display.html?id=79110#txn-1160212

      As suggested in the above bug discussion, I force installed the Socket library and now the library supports Ipv6. I dont see the error anymore.

        Good find, and I'm glad it's working for you.

        If you're interested in reporting this as a bug against Activeperl (provided the version you're using is actively supported) it might be worth your time to let them know that defaults builds of built-in classes lack IPv6 support, and presumably a brief description of the issue and a link to that bug should be all they need to fix it in later releases.

        from the looks of the CPAN bug you linked, this is an issue in the Socket class which provides some system-call integration involving address resolution, and that is what lacks IPv6 support, even when IO::Socket::IP does support it.In this day there's no good reason for out-of-the-box software not to at least support it.