Forum: Ruby ipaddr and invalid ipv4 addresses

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.
Martin DeMello (Guest)
on 2008-12-03 14:09
(Received via mailing list)
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
botp (Guest)
on 2008-12-03 14:20
(Received via mailing list)
On Wed, Dec 3, 2008 at 8:03 PM, Martin DeMello 
<removed_email_address@domain.invalid>
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:in `new'
  from (irb):2
> RUBY_VERSION
=> "1.8.7"
Martin DeMello (Guest)
on 2008-12-03 14:34
(Received via mailing list)
On Wed, Dec 3, 2008 at 5:44 PM, botp <removed_email_address@domain.invalid> 
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
t3ch.dude (Guest)
on 2008-12-04 00:15
(Received via mailing list)
On Dec 3, 7:03 am, Martin DeMello <removed_email_address@domain.invalid> 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 topic is locked and can not be replied to.