UDPSocket#bind(ip, port) fails under IPv6!

Hi, the following script shows that UDPSocket#bind fails if the given
IP is IPv6 (however TCPServer does not fail). It occurs with Ruby 1.8
and 1.9.X.

My computer has IPv4 and IPv6 (using Miredo/Teredo).

The script:


#!/usr/bin/ruby

require “socket”

IPv4 = “192.168.1.12”
IPv6 = “2001:0:53aa:64c:187c:3ecb:3419:d1a6”
PORT = 9999

begin
puts “1) binding in IPv4 TCP…”
s4 = TCPServer.open IPv4, PORT
puts “=> OK”
rescue => e
$stderr.puts “#{e.class}: #{e}”
end

begin
puts “2) binding in IPv6 TCP…”
s4 = TCPServer.open IPv6, PORT
puts “=> OK”
rescue => e
$stderr.puts “#{e.class}: #{e}”
end

begin
puts “3) binding in IPv4 UDP…”
s4 = UDPSocket.new
s4.bind IPv4, PORT
puts “=> OK”
rescue => e
$stderr.puts “#{e.class}: #{e}”
end

begin
puts “4) binding in IPv6 UDP…”
s4 = UDPSocket.new
s4.bind IPv6, PORT
puts “=> OK”
rescue => e
$stderr.puts “#{e.class}: #{e}”
end

Result of the script:


  1. binding in IPv4 TCP…
    => OK

  2. binding in IPv6 TCP…
    => OK

  3. binding in IPv4 UDP…
    => OK

  4. binding in IPv6 UDP…
    Errno::EAFNOSUPPORT: Address family not supported by protocol - bind(2)

On Oct 31, 2011, at 7:16 AM, Iaki Baz C. wrote:

Is it a known issue? If not I will report it.

You need to create an AF_INET6 UDPSocket to bind an IPv6 address

$ ruby -vr ‘socket’ -e ‘u = UDPSocket.new Socket::AF_INET6; u.bind
“::1”, 9999’
ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]

Repeated from Bug #5525: UDPSocket#bind(ip, port) fails under IPv6 => Errno::EAFNOSUPPORT - Ruby master - Ruby Issue Tracking System

2011/10/31 Eric H. [email protected]:

You need to create an AF_INET6 UDPSocket to bind an IPv6 address

$ ruby -vr ‘socket’ -e ‘u = UDPSocket.new Socket::AF_INET6; u.bind “::1”, 9999’
ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]

Repeated from Bug #5525: UDPSocket#bind(ip, port) fails under IPv6 => Errno::EAFNOSUPPORT - Ruby master - Ruby Issue Tracking System

Thanks a lot. It works. Anyhow I’ve commented in the bug report. IMHO
it should work out-of-the-box as in the case of TCPServer.