summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-06-07 13:54:18 -0700
committerEric Wong <normalperson@yhbt.net>2011-06-07 14:01:09 -0700
commitc3880bb0cc00821d1715a7dd94b0b76a03a7ace0 (patch)
treedee85ce7f218d0ce4e0232dc9f845f51c59a62ef
parent0dc56fd03ea478ae054e3d0398703f43e017723b (diff)
Enabling this flag for an IPv6 TCP listener allows users to
specify IPv6-only listeners regardless of the OS default.
This should be interest to Rainbows! users.
-rw-r--r--lib/unicorn/configurator.rb18
-rw-r--r--lib/unicorn/http_server.rb3
-rw-r--r--lib/unicorn/socket_helper.rb22
-rw-r--r--test/unit/test_socket_helper.rb8
4 files changed, 44 insertions, 7 deletions
diff --git a/lib/unicorn/configurator.rb b/lib/unicorn/configurator.rb
index b6ad022..0b84d53 100644
--- a/lib/unicorn/configurator.rb
+++ b/lib/unicorn/configurator.rb
@@ -281,6 +281,22 @@ class Unicorn::Configurator
   #
   #   Default: +true+ in \Unicorn 3.4+, +false+ in Rainbows!
   #
+  # [:ipv6only => true or false]
+  #
+  #   This option makes IPv6-capable TCP listeners IPv6-only and unable
+  #   to receive IPv4 queries on dual-stack systems.  A separate IPv4-only
+  #   listener is required if this is true.
+  #
+  #   This option is only available for Ruby 1.9.2 and later.
+  #
+  #   Enabling this option for the IPv6-only listener and having a
+  #   separate IPv4 listener is recommended if you wish to support IPv6
+  #   on the same TCP port.  Otherwise, the value of \env[\"REMOTE_ADDR\"]
+  #   will appear as an ugly IPv4-mapped-IPv6 address for IPv4 clients
+  #   (e.g ":ffff:10.0.0.1" instead of just "10.0.0.1").
+  #
+  #   Default: Operating-system dependent
+  #
   # [:tries => Integer]
   #
   #   Times to retry binding a socket if it is already in use
@@ -358,7 +374,7 @@ class Unicorn::Configurator
         Integer === value or
           raise ArgumentError, "not an integer: #{key}=#{value.inspect}"
       end
-      [ :tcp_nodelay, :tcp_nopush ].each do |key|
+      [ :tcp_nodelay, :tcp_nopush, :ipv6only ].each do |key|
         (value = options[key]).nil? and next
         TrueClass === value || FalseClass === value or
           raise ArgumentError, "not boolean: #{key}=#{value.inspect}"
diff --git a/lib/unicorn/http_server.rb b/lib/unicorn/http_server.rb
index dc29406..604854a 100644
--- a/lib/unicorn/http_server.rb
+++ b/lib/unicorn/http_server.rb
@@ -23,9 +23,6 @@ class Unicorn::HttpServer
   # backwards compatibility with 1.x
   Worker = Unicorn::Worker
 
-  # prevents IO objects in here from being GC-ed
-  IO_PURGATORY = []
-
   # all bound listener sockets
   LISTENERS = []
 
diff --git a/lib/unicorn/socket_helper.rb b/lib/unicorn/socket_helper.rb
index 9f2d55c..8548276 100644
--- a/lib/unicorn/socket_helper.rb
+++ b/lib/unicorn/socket_helper.rb
@@ -4,9 +4,12 @@ require 'socket'
 
 module Unicorn
   module SocketHelper
+    # :stopdoc:
     include Socket::Constants
 
-    # :stopdoc:
+    # prevents IO objects in here from being GC-ed
+    IO_PURGATORY = []
+
     # internal interface, only used by Rainbows!/Zbatery
     DEFAULTS = {
       # The semantics for TCP_DEFER_ACCEPT changed in Linux 2.6.32+
@@ -136,8 +139,9 @@ module Unicorn
         ensure
           File.umask(old_umask)
         end
-      elsif /\A(\d+\.\d+\.\d+\.\d+):(\d+)\z/ =~ address ||
-            /\A\[([a-fA-F0-9:]+)\]:(\d+)\z/ =~ address
+      elsif /\A\[([a-fA-F0-9:]+)\]:(\d+)\z/ =~ address
+        new_ipv6_server($1, $2.to_i, opt)
+      elsif /\A(\d+\.\d+\.\d+\.\d+):(\d+)\z/ =~ address
         Kgio::TCPServer.new($1, $2.to_i)
       else
         raise ArgumentError, "Don't know how to bind: #{address}"
@@ -146,6 +150,18 @@ module Unicorn
       sock
     end
 
+    def new_ipv6_server(addr, port, opt)
+      opt.key?(:ipv6only) or return Kgio::TCPServer.new(addr, port)
+      defined?(IPV6_V6ONLY) or
+        abort "Socket::IPV6_V6ONLY not defined, upgrade Ruby and/or your OS"
+      sock = Socket.new(AF_INET6, SOCK_STREAM, 0)
+      sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, opt[:ipv6only] ? 1 : 0)
+      sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
+      sock.bind(Socket.pack_sockaddr_in(port, addr))
+      IO_PURGATORY << sock
+      Kgio::TCPServer.for_fd(sock.fileno)
+    end
+
     # returns rfc2732-style (e.g. "[::1]:666") addresses for IPv6
     def tcp_name(sock)
       port, addr = Socket.unpack_sockaddr_in(sock.getsockname)
diff --git a/test/unit/test_socket_helper.rb b/test/unit/test_socket_helper.rb
index c6d0d42..8ce1023 100644
--- a/test/unit/test_socket_helper.rb
+++ b/test/unit/test_socket_helper.rb
@@ -12,6 +12,7 @@ class TestSocketHelper < Test::Unit::TestCase
     @log_tmp = Tempfile.new 'logger'
     @logger = Logger.new(@log_tmp.path)
     @test_addr = ENV['UNICORN_TEST_ADDR'] || '127.0.0.1'
+    @test6_addr = ENV['UNICORN_TEST6_ADDR'] || '::1'
     GC.disable
   end
 
@@ -177,4 +178,11 @@ class TestSocketHelper < Test::Unit::TestCase
     assert cur > 1
   end if defined?(TCP_DEFER_ACCEPT)
 
+  def test_ipv6only
+    port = unused_port "#@test6_addr"
+    sock = bind_listen "[#@test6_addr]:#{port}", :ipv6only => true
+    cur = sock.getsockopt(:IPPROTO_IPV6, :IPV6_V6ONLY).unpack('i')[0]
+    assert_equal 1, cur
+    rescue Errno::EAFNOSUPPORT
+  end if RUBY_VERSION >= "1.9.2"
 end