Skip to content

Commit

Permalink
Merge pull request #2781 from fluent/fix-dual-stack-ipv6-udp-issue
Browse files Browse the repository at this point in the history
server: Fix IPv6 dual stack mode issue for udp socket. fix #2768
  • Loading branch information
repeatedly authored Jan 20, 2020
2 parents 9632daf + ad59cdd commit 2fc79e5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/fluent/plugin_helper/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,10 @@ def server_create_udp_socket(shared, bind, port)
sock = if shared
server_socket_manager_client.listen_udp(bind, port)
else
family = IPAddr.new(IPSocket.getaddress(bind)).ipv4? ? ::Socket::AF_INET : ::Socket::AF_INET6
usock = UDPSocket.new(family)
usock.bind(bind, port)
usock
# UDPSocket.new doesn't set IPV6_V6ONLY flag, so use Addrinfo class instead.
usock = Addrinfo.udp(bind, port).bind
usock.autoclose = false
UDPSocket.for_fd(usock.fileno)
end
# close-on-exec is set by default in Ruby 2.0 or later (, and it's unavailable on Windows)
sock.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK) # nonblock
Expand Down
13 changes: 13 additions & 0 deletions test/plugin_helper/test_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,19 @@ class Dummy < Fluent::Plugin::TestBase
assert_equal 1, errors.size
assert_equal "BUG: this event is disabled for udp: close", errors.first.message
end

test 'can bind IPv4 / IPv6 together' do
omit "IPv6 unavailable here" unless ipv6_enabled?

assert_nothing_raised do
@d.server_create_udp(:s_ipv4_udp, PORT, bind: '0.0.0.0', shared: false, max_bytes: 128) do |data, sock|
# ...
end
@d.server_create_udp(:s_ipv6_udp, PORT, bind: '::', shared: false, max_bytes: 128) do |data, sock|
# ...
end
end
end
end

module CertUtil
Expand Down

0 comments on commit 2fc79e5

Please sign in to comment.