Feature #8368

Socket.getifaddrs

Added by Akira Tanaka 12 months ago. Updated 12 months ago.

[ruby-core:54777]
Status:Closed
Priority:Normal
Assignee:-
Category:-
Target version:2.1.0

Description

I'd like to add a method: Socket.getifaddrs.

This method is a wrapper to getifaddrs() function.
The result is an array of instances of Socket::Ifaddr class as follows.

% ./ruby -rpp -rsocket -e 'pp Socket.getifaddrs'
[#,
#,
#,
#,
#,
#,
#]

This method can be used to choose multicast-enabled interfaces.
Multicast applications needs to identify a such interface to sending a multicast datagram.

% ./ruby -rpp -rsocket -e '
pp Socket.getifaddrs.reject {|ifaddr|
!ifaddr.addr.ip? || (ifaddr.flags & Socket::IFF_MULTICAST == 0)
}.map {|ifaddr| [ifaddr.name, ifaddr.ifindex, ifaddr.addr] }'
[["eth0", 2, #],
["eth0", 2, #]]

This method can be used to obtain broadcast addresses to send a broadcast
messages. (use case: )

Also, the broadcast addresses can be used to receive broadcast messages.
(Programming UNIX Sockets in C: 4.12. How can I write a multi-homed server?
http://www.faqs.org/faqs/unix-faq/socket/ )

getifaddrs() is not standardized but many platforms have.
BSDI, FreeBSD, NetBSD, OpenBSD, DragonFly, MirOS, GNU/Linux, MacOS X, SunOS 5.11 (OpenIndiana), Cygwin 1.7.15

From Gnulib document, getifaddrs() is not exist on
AIX 5.1, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x, mingw, MSVC 9, Interix 3.5, BeOS.
http://www.gnu.org/software/gnulib/manual/html_node/getifaddrs.html

I implemented new Socket::Ifaddr class for return value of Socket.getifaddrs,
instead of constructing the result from currentn data structures such as Array.
This choice make us possible to implement Socket::Ifaddr#inspect to show flags as its names (UP,...).
Also, struct getifaddrs contains Address-specific data, ifadata which may be extended by platforms.
When we find a way to extract some data from ifa
data, we can add a method for that.

Socket::Ifaddr is different from Socket::Interface by
http://bugs.ruby-lang.org/issues/8075 .
They have no one-to-one mapping.
For example, Socket.getifaddrs returns two or more elements for one interface.

getifaddrs.patch Magnifier (31.6 KB) Akira Tanaka, 05/04/2013 04:57 PM

getifaddrs2.patch Magnifier (32.7 KB) Akira Tanaka, 05/06/2013 08:33 PM

Associated revisions

Revision 40639
Added by Akira Tanaka 12 months ago

  • ext/socket: New method, Socket.getifaddrs, implemented. [Feature #8368]

History

#1 Updated by Yui NARUSE 12 months ago

  • Target version set to 2.1.0

I'm for this method.

#2 Updated by Magnus Holm 12 months ago

Shouldn't this be moved to CommonRuby?

#3 Updated by Yui NARUSE 12 months ago

judofyr (Magnus Holm) wrote:

Shouldn't this be moved to CommonRuby?

There's no such consensus yet, especially bundled libraries.

#4 Updated by Akira Tanaka 12 months ago

I updated the patch to fix compile error on environments which has no getifaddrs().
Also I refined the result of Socket::Ifaddrs#inspect.

% ./ruby -rsocket -rpp -e 'pp Socket.getifaddrs'
[#,
#,
#,
#,
#,
#,
#]

#5 Updated by Akira Tanaka 12 months ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r40639.
Akira, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • ext/socket: New method, Socket.getifaddrs, implemented. [Feature #8368]

#6 Updated by Akira Tanaka 12 months ago

It seems no one against for this method and naruse-san (the release manager of Ruby 2.1.0) supported.
I added Socket.getifaddrs.

Also available in: Atom PDF