diff options
author | Nobuyoshi Nakada <[email protected]> | 2025-01-21 16:44:46 +0900 |
---|---|---|
committer | git <[email protected]> | 2025-01-21 07:58:34 +0000 |
commit | d3bd8df2b8fac48e7fa614c41519ab9449b55d89 (patch) | |
tree | 990e284563509b5ef3ea4ada3ae7eeaad15da244 | |
parent | e5f81e511f566edaf2ca871aa72dbb0260c242bf (diff) |
[ruby/resolv] Use port number 0 for ephemeral port if save
On platforms where ephemeral port randomization is implemented, the
same randomization is not needed in the ruby library layer.
Fixes https://github.com/ruby/resolv/pull/63.
https://github.com/ruby/resolv/commit/45e1b563c0
-rw-r--r-- | lib/resolv.rb | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/lib/resolv.rb b/lib/resolv.rb index 83f2fbf0e8..e46b4cef92 100644 --- a/lib/resolv.rb +++ b/lib/resolv.rb @@ -659,8 +659,20 @@ class Resolv } end - def self.bind_random_port(udpsock, bind_host="0.0.0.0") # :nodoc: - begin + case RUBY_PLATFORM + when *[ + # https://www.rfc-editor.org/rfc/rfc6056.txt + # Appendix A. Survey of the Algorithms in Use by Some Popular Implementations + /freebsd/, /linux/, /netbsd/, /openbsd/, /solaris/, + /darwin/, # the same as FreeBSD + ] then + def self.bind_random_port(udpsock, bind_host="0.0.0.0") # :nodoc: + udpsock.bind(bind_host, 0) + end + else + # Sequential port assignment + def self.bind_random_port(udpsock, bind_host="0.0.0.0") # :nodoc: + # Ephemeral port number range recommended by RFC 6056 port = random(1024..65535) udpsock.bind(bind_host, port) rescue Errno::EADDRINUSE, # POSIX |