Skip to content

Commit

Permalink
sockets: add SO_REUSEPORT option
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbaddaden authored and asterite committed Dec 23, 2016
1 parent afb5c53 commit d1159e5
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 3 deletions.
15 changes: 12 additions & 3 deletions spec/std/socket_spec.cr
Expand Up @@ -310,10 +310,19 @@ end

describe TCPServer do
it "fails when port is in use" do
port = free_udp_socket_port

expect_raises Errno, /(already|Address) in use/ do
TCPServer.open("::", 0) do |server|
TCPServer.open("::", server.local_address.port) { }
end
sock = Socket.tcp(Socket::Family::INET6)
sock.bind(Socket::IPAddress.new("::1", port))

TCPServer.open("::1", port) {}
end
end

it "allows to share the same port (SO_REUSEPORT)" do
TCPServer.open("::", 0) do |server|
TCPServer.open("::", server.local_address.port) {}
end
end
end
Expand Down
1 change: 1 addition & 0 deletions src/lib_c/aarch64-linux-gnu/c/sys/socket.cr
Expand Up @@ -11,6 +11,7 @@ lib LibC
SO_LINGER = 13
SO_RCVBUF = 8
SO_REUSEADDR = 2
SO_REUSEPORT = 15
SO_SNDBUF = 7
PF_INET = 2
PF_INET6 = 10
Expand Down
1 change: 1 addition & 0 deletions src/lib_c/amd64-unknown-openbsd/c/sys/socket.cr
Expand Up @@ -11,6 +11,7 @@ lib LibC
SO_LINGER = 0x0080
SO_RCVBUF = 0x1002
SO_REUSEADDR = 0x0004
SO_REUSEPORT = 0x0200
SO_SNDBUF = 0x1001
PF_INET = LibC::AF_INET
PF_INET6 = LibC::AF_INET6
Expand Down
1 change: 1 addition & 0 deletions src/lib_c/arm-linux-gnueabihf/c/sys/socket.cr
Expand Up @@ -11,6 +11,7 @@ lib LibC
SO_LINGER = 13
SO_RCVBUF = 8
SO_REUSEADDR = 2
SO_REUSEPORT = 15
SO_SNDBUF = 7
PF_INET = 2
PF_INET6 = 10
Expand Down
1 change: 1 addition & 0 deletions src/lib_c/i686-linux-gnu/c/sys/socket.cr
Expand Up @@ -11,6 +11,7 @@ lib LibC
SO_LINGER = 13
SO_RCVBUF = 8
SO_REUSEADDR = 2
SO_REUSEPORT = 15
SO_SNDBUF = 7
PF_INET = 2
PF_INET6 = 10
Expand Down
1 change: 1 addition & 0 deletions src/lib_c/i686-linux-musl/c/sys/socket.cr
Expand Up @@ -11,6 +11,7 @@ lib LibC
SO_LINGER = 13
SO_RCVBUF = 8
SO_REUSEADDR = 2
SO_REUSEPORT = 15
SO_SNDBUF = 7
PF_INET = 2
PF_INET6 = 10
Expand Down
1 change: 1 addition & 0 deletions src/lib_c/x86_64-linux-gnu/c/sys/socket.cr
Expand Up @@ -11,6 +11,7 @@ lib LibC
SO_LINGER = 13
SO_RCVBUF = 8
SO_REUSEADDR = 2
SO_REUSEPORT = 15
SO_SNDBUF = 7
PF_INET = 2
PF_INET6 = 10
Expand Down
1 change: 1 addition & 0 deletions src/lib_c/x86_64-linux-musl/c/sys/socket.cr
Expand Up @@ -11,6 +11,7 @@ lib LibC
SO_LINGER = 13
SO_RCVBUF = 8
SO_REUSEADDR = 2
SO_REUSEPORT = 15
SO_SNDBUF = 7
PF_INET = 2
PF_INET6 = 10
Expand Down
1 change: 1 addition & 0 deletions src/lib_c/x86_64-macosx-darwin/c/sys/socket.cr
Expand Up @@ -11,6 +11,7 @@ lib LibC
SO_LINGER = 0x0080
SO_RCVBUF = 0x1002
SO_REUSEADDR = 0x0004
SO_REUSEPORT = 0x0200
SO_SNDBUF = 0x1001
PF_INET = LibC::AF_INET
PF_INET6 = LibC::AF_INET6
Expand Down
1 change: 1 addition & 0 deletions src/lib_c/x86_64-portbld-freebsd/c/sys/socket.cr
Expand Up @@ -11,6 +11,7 @@ lib LibC
SO_LINGER = 0x0080
SO_RCVBUF = 0x1002
SO_REUSEADDR = 0x0004
SO_REUSEPORT = 0x0200
SO_SNDBUF = 0x1001
PF_INET = LibC::AF_INET
PF_INET6 = LibC::AF_INET6
Expand Down
8 changes: 8 additions & 0 deletions src/socket.cr
Expand Up @@ -387,6 +387,14 @@ class Socket < IO::FileDescriptor
setsockopt_bool LibC::SO_REUSEADDR, val
end

def reuse_port?
getsockopt_bool LibC::SO_REUSEPORT
end

def reuse_port=(val : Bool)
setsockopt_bool LibC::SO_REUSEPORT, val
end

def broadcast?
getsockopt_bool LibC::SO_BROADCAST
end
Expand Down
1 change: 1 addition & 0 deletions src/socket/tcp_server.cr
Expand Up @@ -22,6 +22,7 @@ class TCPServer < TCPSocket
super(addrinfo.family, addrinfo.type, addrinfo.protocol)

self.reuse_address = true
self.reuse_port = true

if errno = bind(addrinfo) { |errno| errno }
close
Expand Down

0 comments on commit d1159e5

Please sign in to comment.