From c3880bb0cc00821d1715a7dd94b0b76a03a7ace0 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 7 Jun 2011 13:54:18 -0700 Subject: configurator: add :ipv6only directive 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. --- lib/unicorn/configurator.rb | 18 +++++++++++++++++- lib/unicorn/http_server.rb | 3 --- lib/unicorn/socket_helper.rb | 22 +++++++++++++++++++--- 3 files changed, 36 insertions(+), 7 deletions(-) (limited to 'lib') 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) -- cgit v1.2.3-24-ge0c7