Skip to content

Socket.getifaddrs returns incorrect interface list #2137

@gfowley

Description

@gfowley

With ethernet and wifi interfaces disconnected, I expect to see 5 interfaces from Socket.getifaddrs.
Running ruby 2.2.0 head all is well:

$ ruby -v
ruby 2.2.0dev (2014-11-07 trunk 48311) [x86_64-linux]

5 interfaces...

head :042 > Socket.getifaddrs.count
 => 5

Interfaces listed: 1 each for physcial interfaces eth0, wlan0, and 3 for lo...

head :043 > Socket.getifaddrs.each { |i| puts i.inspect }
#<Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 PACKET[protocol=0 lo hatype=772 HOST hwaddr=00:00:00:00:00:00]>
#<Socket::Ifaddr eth0 UP,BROADCAST,MULTICAST PACKET[protocol=0 eth0 hatype=1 HOST hwaddr=78:24:af:18:87:93] broadcast=PACKET[protocol=0 eth0 hatype=1 HOST hwaddr=ff:ff:ff:ff:ff:ff]>
#<Socket::Ifaddr wlan0 BROADCAST,MULTICAST PACKET[protocol=0 wlan0 hatype=1 HOST hwaddr=a0:a8:cd:12:09:7b] broadcast=PACKET[protocol=0 wlan0 hatype=1 HOST hwaddr=ff:ff:ff:ff:ff:ff]>
#<Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 127.0.0.1 netmask=255.0.0.0>
#<Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 ::1 netmask=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>

As expected only one (lo) has an IPv4 address...

head :044 > Socket.getifaddrs.each { |i| puts i.addr.ipv4? }                                                                                                                                  
false
false
false
true
false

Which can be retrieved...

head :045 > Socket.getifaddrs[3].addr.ip_address
 => "127.0.0.1" 

Running jruby-head, Socket.getifaddrs returns incorrect results:

$ jruby -v
jruby 9000.dev-SNAPSHOT (2.1.2p142) 2014-11-06 9af87db OpenJDK 64-Bit Server VM 24.65-b04 on 1.7.0_65-b32 +jit [linux-amd64]

Incorrect number of interfaces...

jruby-head :045 >   Socket.getifaddrs.count
 => 4 

Interfaces listed: 4 for lo, none for wlan0 and eth0...

jruby-head :046 > Socket.getifaddrs.each { |i| puts i.inspect }
#<Socket::Ifaddr: lo UP,LOOPBACK,MTU=65536 LINK[lo]>
#<Socket::Ifaddr: lo UP,LOOPBACK,MTU=65536 0:0:0:0:0:0:0:1 netmask=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
#<Socket::Ifaddr: lo UP,LOOPBACK,MTU=65536 LINK[lo]>
#<Socket::Ifaddr: lo UP,LOOPBACK,MTU=65536 127.0.0.1>

It seems to think 2 interfaces have IPv4 addresses...

jruby-head :047 > Socket.getifaddrs.each { |i| puts i.addr.ipv4? }
false
false
true
true

One is 127.0.0.1 as expected...

jruby-head :049 > Socket.getifaddrs[3].addr.ip_address
 => "127.0.0.1" 

The other isn't actually...

jruby-head :050 > Socket.getifaddrs[2].addr.ip_address
SocketError: need IPv4 or IPv6 address
        from org/jruby/ext/socket/Addrinfo.java:291:in `ip_address'
        from (irb):50:in `evaluate'
        from org/jruby/RubyKernel.java:962:in `eval'
        from org/jruby/RubyKernel.java:1282:in `loop'
        from org/jruby/RubyKernel.java:1092:in `catch'
        from org/jruby/RubyKernel.java:1092:in `catch'
        from /home/gerard/.rvm/rubies/jruby-head/bin/irb:13:in `__script__'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions