No accept Ipv6 in IPAddr

C

Cyril Mougel

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
 
B

Brian Candler

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'
=> # said:
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)

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.
 
C

Cyril Mougel

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'


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top