From 87fd86ef22b6b80fa75dd8e50f53a4e62e8339f7 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 2 Feb 2011 15:22:02 -0800 Subject: allow binding on IPv6 sockets with listen "[#{addr}]:#{port}" This is much like how nginx does it, except we always require a port when explicitly binding to IPv6 using the "listen" directive. This also adds support to listen with an address-only, which can be useful to Rainbows! users. --- lib/unicorn/socket_helper.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'lib/unicorn/socket_helper.rb') diff --git a/lib/unicorn/socket_helper.rb b/lib/unicorn/socket_helper.rb index 0cab3cb..e5b0735 100644 --- a/lib/unicorn/socket_helper.rb +++ b/lib/unicorn/socket_helper.rb @@ -136,7 +136,9 @@ module Unicorn ensure File.umask(old_umask) end - elsif address =~ /^(\d+\.\d+\.\d+\.\d+):(\d+)$/ + elsif /\A(\d+\.\d+\.\d+\.\d+):(\d+)\z/ =~ address || + /\A\[([a-fA-F0-9:]+)\]:(\d+)\z/ =~ address + p [ $1, $2 ] Kgio::TCPServer.new($1, $2.to_i) else raise ArgumentError, "Don't know how to bind: #{address}" @@ -145,6 +147,12 @@ module Unicorn sock end + # returns rfc2732-style (e.g. "[::1]:666") addresses for IPv6 + def tcp_name(sock) + port, addr = Socket.unpack_sockaddr_in(sock.getsockname) + /:/ =~ addr ? "[#{addr}]:#{port}" : "#{addr}:#{port}" + end + # Returns the configuration name of a socket as a string. sock may # be a string value, in which case it is returned as-is # Warning: TCP sockets may not always return the name given to it. @@ -154,10 +162,10 @@ module Unicorn when UNIXServer Socket.unpack_sockaddr_un(sock.getsockname) when TCPServer - Socket.unpack_sockaddr_in(sock.getsockname).reverse!.join(':') + tcp_name(sock) when Socket begin - Socket.unpack_sockaddr_in(sock.getsockname).reverse!.join(':') + tcp_name(sock) rescue ArgumentError Socket.unpack_sockaddr_un(sock.getsockname) end -- cgit v1.2.3-24-ge0c7