ipaddr and invalid ipv4 addresses

M

Martin DeMello

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
 
M

Martin DeMello

ArgumentError: invalid address
from /usr/local/lib/ruby/1.8/ipaddr.rb:467:in `initialize'
from (irb):2:in `new'
from (irb):2
=> "1.8.7"

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

martin
 
T

t3ch.dude

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"
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top