From 42d5de84e1a7ff5b1fba6b2eab751450e0c9a5f4 Mon Sep 17 00:00:00 2001 From: Hiroshi Shirosaki Date: Sat, 6 Apr 2013 15:13:21 +0900 Subject: [PATCH] Fix multicast of rinda Bind a socket to another address before joining a multicast group. Add a socket to @sockets in make_socket() to be closed when shutdown. --- lib/rinda/ring.rb | 12 +++++++++--- test/rinda/test_rinda.rb | 6 +++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/rinda/ring.rb b/lib/rinda/ring.rb index 9b3e273..d0f82dc 100644 --- a/lib/rinda/ring.rb +++ b/lib/rinda/ring.rb @@ -78,7 +78,8 @@ module Rinda @renewer = Renewer.new @ts = ts - @sockets = addresses.map do |address| + @sockets = [] + addresses.each do |address| make_socket(address) end @@ -103,19 +104,24 @@ module Rinda end if addrinfo.ipv4_multicast? then + socket.bind(Addrinfo.udp(Socket::INADDR_ANY, @port)) + mreq = IPAddr.new(addrinfo.ip_address).hton + IPAddr.new('0.0.0.0').hton socket.setsockopt(:IPPROTO_IP, :IP_ADD_MEMBERSHIP, mreq) else + socket.bind(Addrinfo.udp(nil, @port)) + mreq = IPAddr.new(addrinfo.ip_address).hton + [0].pack('I') socket.setsockopt(:IPPROTO_IPV6, :IPV6_JOIN_GROUP, mreq) end + else + socket.bind(addrinfo) end - socket.bind(addrinfo) - + @sockets << socket socket end diff --git a/test/rinda/test_rinda.rb b/test/rinda/test_rinda.rb index 7791c7e..a2cd0f7 100644 --- a/test/rinda/test_rinda.rb +++ b/test/rinda/test_rinda.rb @@ -570,8 +570,8 @@ class TestRingServer < Test::Unit::TestCase assert(v4mc.getsockopt(:SOCKET, :SO_REUSEADDR).bool) end - assert_equal('239.0.0.1', v4mc.local_address.ip_address) - assert_equal(@port, v4mc.local_address.ip_port) + assert_equal('0.0.0.0', v4mc.local_address.ip_address) + assert_equal(@port, v4mc.local_address.ip_port) end def test_make_socket_ipv6_multicast @@ -590,7 +590,7 @@ class TestRingServer < Test::Unit::TestCase assert v6mc.getsockopt(:SOCKET, :SO_REUSEADDR).bool end - assert_equal('ff02::1', v6mc.local_address.ip_address) + assert_equal('::1', v6mc.local_address.ip_address) assert_equal(@port, v6mc.local_address.ip_port) end -- 1.7.10.2 (Apple Git-33)