Ipaddr and invalid ipv4 addresses

What’s going on here?

irb(main):002:0> require ‘ipaddr’
=> true

irb(main):003:0> ip = IPAddr.new(‘192.168.1.1024’)
=> #<IPAddr: IPv4:192.168.5.0/255.255.255.255>

irb(main):005:0> IPSocket.getaddress(‘192.168.1.1024’)
=> “68.142.212.71”

irb(main):003:0> IPSocket.getaddress(‘asd’)
=> “68.142.212.71”

martin

On Wed, Dec 3, 2008 at 8:03 PM, Martin DeMello [email protected]
wrote:

What’s going on here?

require ‘ipaddr’
=> true
ip = IPAddr.new(‘192.168.1.1024’)
ArgumentError: invalid address
from /usr/local/lib/ruby/1.8/ipaddr.rb:467:in initialize' from (irb):2:innew’
from (irb):2
RUBY_VERSION
=> “1.8.7”

On Wed, Dec 3, 2008 at 5:44 PM, botp [email protected] wrote:

RUBY_VERSION
=> “1.8.7”

Ah - my bad, I’m still running 1.8.6 and didn’t think to check the
latest version.

martin

On Dec 3, 7:03 am, Martin DeMello [email protected] wrote:

What’s going on here?

irb(main):002:0> require ‘ipaddr’
=> true

irb(main):003:0> ip = IPAddr.new(‘192.168.1.1024’)
=> #<IPAddr: IPv4:192.168.5.0/255.255.255.255>

The snippet above is pretty easy to understand if you convert the ip
to a single integer, then convert back to hex…

irb(main):003:0> ip1=((192*256+168)*256+1)*256+1024
=> 3232236800
irb(main):004:0> ip1.to_s(16).scan(/…/).map{|v| v.to_i(16)}.join
( “.”)
=> “192.168.5.0”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs