Skip to content

Commit

Permalink
Fix Socket#reuse_port? if SO_REUSEPORT is not supported (#6706)
Browse files Browse the repository at this point in the history
* Fix Socket#reuse_port? if SO_REUSEPORT is not supported

* fixup! Fix Socket#reuse_port? if SO_REUSEPORT is not supported
  • Loading branch information
straight-shoota authored and RX14 committed Sep 12, 2018
1 parent 60c4f4d commit c6ee7bc
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,15 @@ class Socket < IO
end

def reuse_port?
getsockopt_bool LibC::SO_REUSEPORT
ret = getsockopt(LibC::SO_REUSEPORT, 0) do |errno|
# If SO_REUSEPORT is not supported, the return value should be `false`
if errno.errno == Errno::ENOPROTOOPT
return false
else
raise errno
end
end
ret != 0
end

def reuse_port=(val : Bool)
Expand Down Expand Up @@ -445,9 +453,13 @@ class Socket < IO

# Returns the modified *optval*.
def getsockopt(optname, optval, level = LibC::SOL_SOCKET)
getsockopt(optname, optval, level) { |errno| raise errno }
end

protected def getsockopt(optname, optval, level = LibC::SOL_SOCKET)
optsize = LibC::SocklenT.new(sizeof(typeof(optval)))
ret = LibC.getsockopt(fd, level, optname, (pointerof(optval).as(Void*)), pointerof(optsize))
raise Errno.new("getsockopt") if ret == -1
yield Errno.new("getsockopt") if ret == -1
optval
end

Expand Down

0 comments on commit c6ee7bc

Please sign in to comment.