Forum: Ruby No accept Ipv6 in IPAddr

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2fd0206c71a1b22a9cc6293f38537461?d=identicon&s=25 Cyril Mougel (Guest)
on 2007-05-13 21:42
(Received via mailing list)
Hi,

I ask me a question since few day and I don't understand why this code
doesn't run :

   require 'ipaddr'
   IPAddr.new '2002:0000:1234:4561'

In fact this code raise an Exception :

   /usr/lib64/ruby/1.8/ipaddr.rb:422:in `initialize': invalid address
(ArgumentError)

But this adress is syntactically valid like an Ipv6. My search lead me
to this conclusion :

IPAddr think my IP like an invalid adress because it hadn't a name
resolution. The line who raise the exception is :

   IPSocket.getaddress(prefix)

For me, the prefix is '2002:0000:1234:4561', because it's never modify
before in intialize of IPAddr (Verify with the debuggeur). The command
follow prouve it :

   hello% host 2002:0000:1234:4561
   Host 2002:0000:1234:4561 not found: 3(NXDOMAIN)

or with irb :

   hello% irb
   irb(main):001:0> require 'socket'
   => true
   irb(main):002:0> IPSocket.getaddress('2002:0000:1234:4561')
   SocketError: getaddrinfo: Name or service not known
       from (irb):2:in `getaddress'
       from (irb):2

But , it's not specify anywhere that an IPAddr must be "valid" for be
use. Likewise, if we see the code of this file ipaddr.rb, We remark
that in first step, there are :

   unless Socket.const_defined? "AF_INET6"

But if you have compile Ruby with Ipv6 support like some binary linux
distribution, this variable is define. So the
override of the method getaddress made after this unless is never
execute. In this new method, we can see that :

  return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*\Z/ =~ addr

With this my Ipv6 is valid.

The question that I ask me is the follow :

It's a bug or not ?

thank for your answer
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-05-13 22:11
(Received via mailing list)
On Mon, May 14, 2007 at 04:41:13AM +0900, Cyril Mougel wrote:
> I ask me a question since few day and I don't understand why this code
> doesn't run :
>
>   require 'ipaddr'
>   IPAddr.new '2002:0000:1234:4561'
>
> In fact this code raise an Exception :
>
>   /usr/lib64/ruby/1.8/ipaddr.rb:422:in `initialize': invalid address
> (ArgumentError)

Correct - that's not a valid IPv6 address. IPv6 addresses have 128 bits;
your address has only 64 bits.

irb(main):001:0> require 'ipaddr'
=> true
irb(main):002:0> IPAddr.new '2002:0000:1234:4561'
ArgumentError: invalid address
        from /usr/lib/ruby/1.8/ipaddr.rb:423:in `initialize'
        from (irb):2
irb(main):003:0> IPAddr.new '2002:0000:1234:4561/64'
ArgumentError: invalid address
        from /usr/lib/ruby/1.8/ipaddr.rb:423:in `initialize'
        from (irb):3
irb(main):004:0> IPAddr.new '2002:0000:1234:4561::0/64'
=> #<IPAddr:
IPv6:2002:0000:1234:4561:0000:0000:0000:0000/ffff:ffff:ffff:ffff:0000:0000:0000:0000>

The "::" means "add as many :0000:'s as necessary to make up to 128
bits".
But you could also use a valid 128 bit address explicitly:

irb(main):005:0> IPAddr.new '2002:0000:1234:4561:0000:0000:0000:0000'
=> #<IPAddr:
IPv6:2002:0000:1234:4561:0000:0000:0000:0000/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>

>   Host 2002:0000:1234:4561 not found: 3(NXDOMAIN)
If you give an address which is not syntactially valid as an IPv6
numeric
address, I guess it's reasonable for Ruby to try it as a hostname
instead.

Brian.
2fd0206c71a1b22a9cc6293f38537461?d=identicon&s=25 Cyril Mougel (Guest)
on 2007-05-14 00:20
(Received via mailing list)
On 5/13/07, Brian Candler <B.Candler@pobox.com> wrote:
> > (ArgumentError)
> irb(main):003:0> IPAddr.new '2002:0000:1234:4561/64'
> => #<IPAddr: 
IPv6:2002:0000:1234:4561:0000:0000:0000:0000/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
> >   hello% host 2002:0000:1234:4561
> >   Host 2002:0000:1234:4561 not found: 3(NXDOMAIN)
>
> If you give an address which is not syntactially valid as an IPv6 numeric
> address, I guess it's reasonable for Ruby to try it as a hostname instead.
>

Ok, thanks for your answer. I don't know enough the specification of
Ipv6. I thought that this adress was valid.
This topic is locked and can not be replied to.