Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dual stack mode issue for tcp socket with IPv6 #101

Merged
merged 2 commits into from
Nov 17, 2019

Conversation

repeatedly
Copy link
Contributor

@repeatedly repeatedly commented Nov 14, 2019

See fluentd issue for the detail: fluent/fluentd#2682
TCPServer.new doesn't set IPV6_V6ONLY and no parameter for IPV6_V6ONLY.
Socket.tcp_server_sockets set IPV6_V6ONLY internally, so use it instead.

Test echo server sciprt for checking the behaviour.

  • TCPServer
require "socket"

ths = []
['0.0.0.0', '::'].each { |host|
  ths << Thread.new {
    gs = TCPServer.new(host, 2300)

    while true
      Thread.start(gs.accept) do |s|
        p s
        print(s, " is accepted in #{host}\n")
        while s.gets
          s.write($_)
        end
        print(s, " is gone\n")
        s.close
      end
    end
  }
}

ths.each { |th| th.join }
  • Socket.tcp_server_sockets
require "socket"

ths = []
['0.0.0.0', '::'].each { |host|
  ths << Thread.new {
    gs = Socket.tcp_server_sockets(host, 2300).first
    gs.autoclose = false
    gs = TCPServer.for_fd(gs.fileno)

    while true
      Thread.start(gs.accept) do |s|
        p s
        print(s, " is accepted in #{host}\n")
        while s.gets
          s.write($_)
        end
        print(s, " is gone\n")
        s.close
      end
    end
  }
}

ths.each { |th| th.join }
  • tcp client
require 'socket'

['0.0.0.0', '::'].each { |host|
  socket = TCPSocket.open(host, 2300)
  socket.print("hello\n")
  socket.flush
  puts response = socket.gets
  socket.close
}

@repeatedly
Copy link
Contributor Author

repeatedly commented Nov 15, 2019

Tanaka-san suggests using Addrinfo class is more better. I modified the patch to use it.

https://twitter.com/tanaka_akr/status/1194870142305329153 (in japanese)

  • Test script with Addrinfo.
require "socket"

ths = []
['0.0.0.0', '::'].each { |host|
  ths << Thread.new {
    gs = Addrinfo.tcp(host, 2300).listen(1024)
    gs.autoclose = false
    gs = TCPServer.for_fd(gs.fileno)
    printf("server is on %s\n", [host, '2300'].join(":"))

    while true
      Thread.start(gs.accept) do |s|
        p s
        print(s, " is accepted in #{host}\n")
        while s.gets
          s.write($_)
        end
        print(s, " is gone\n")
        s.close
      end
    end
  }
}

ths.each { |th| th.join }

@repeatedly
Copy link
Contributor Author

@nurse @ganmacs If no problem, I will merge and release new version.

@repeatedly repeatedly merged commit b36621c into master Nov 17, 2019
@repeatedly repeatedly deleted the fix-dual-stack-ipv6-issue branch November 17, 2019 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants