Skip to content

Commit c4bfa07

Browse files
authored
Merge pull request #80 from ruby/random_port
Use port number 0 for ephemeral port if save
2 parents 320a179 + 45e1b56 commit c4bfa07

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

lib/resolv.rb

+14-2
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,20 @@ def self.free_request_id(host, port, id) # :nodoc:
659659
}
660660
end
661661

662-
def self.bind_random_port(udpsock, bind_host="0.0.0.0") # :nodoc:
663-
begin
662+
case RUBY_PLATFORM
663+
when *[
664+
# https://www.rfc-editor.org/rfc/rfc6056.txt
665+
# Appendix A. Survey of the Algorithms in Use by Some Popular Implementations
666+
/freebsd/, /linux/, /netbsd/, /openbsd/, /solaris/,
667+
/darwin/, # the same as FreeBSD
668+
] then
669+
def self.bind_random_port(udpsock, bind_host="0.0.0.0") # :nodoc:
670+
udpsock.bind(bind_host, 0)
671+
end
672+
else
673+
# Sequential port assignment
674+
def self.bind_random_port(udpsock, bind_host="0.0.0.0") # :nodoc:
675+
# Ephemeral port number range recommended by RFC 6056
664676
port = random(1024..65535)
665677
udpsock.bind(bind_host, port)
666678
rescue Errno::EADDRINUSE, # POSIX

0 commit comments

Comments
 (0)