ipaddr and invalid ipv4 addresses

Discussion in 'Ruby' started by Martin DeMello, Dec 3, 2008.

  1. 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
     
    Martin DeMello, Dec 3, 2008
    #1
    1. Advertising

  2. Martin DeMello

    botp Guest

    On Wed, Dec 3, 2008 at 8:03 PM, Martin DeMello <> 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"
     
    botp, Dec 3, 2008
    #2
    1. Advertising

  3. On Wed, Dec 3, 2008 at 5:44 PM, botp <> wrote:
    > On Wed, Dec 3, 2008 at 8:03 PM, Martin DeMello <> 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"


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

    martin
     
    Martin DeMello, Dec 3, 2008
    #3
  4. Martin DeMello

    t3ch.dude Guest

    On Dec 3, 7:03 am, Martin DeMello <> 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"
     
    t3ch.dude, Dec 3, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tweak
    Replies:
    14
    Views:
    2,817
    Eric Sosman
    Jun 11, 2004
  2. raghu

    Processing IPV4 and Ethernet frames

    raghu, Feb 14, 2007, in forum: C Programming
    Replies:
    7
    Views:
    475
    Stephen Sprunk
    Feb 14, 2007
  3. namespace1
    Replies:
    3
    Views:
    945
  4. Neil Spring

    IPAddr as hashtable key?

    Neil Spring, Jan 12, 2004, in forum: Ruby
    Replies:
    0
    Views:
    143
    Neil Spring
    Jan 12, 2004
  5. ninj
    Replies:
    0
    Views:
    116
Loading...

Share This Page