Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for the comment: code is explicit enough.

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