Feature #3711
closed
IPAddr must not make DNS lookups via IPSocket.getaddress
Added by pk (Philipp Kempgen) over 14 years ago.
Updated about 7 years ago.
Description
=begin
class MustNotCallError < NotImplementedError ; end
class IpAddrNoResolvTestCase < Test::Unit::TestCase
def setup()
class << IPSocket
alias original_getaddress getaddress
def getaddress( host )
# DNS lookups are slow and totally unnecessary for IP addresses
raise MustNotCallError.new( 'IPAddr.new() must not call IPSocket.getaddress()!' )
end
end
end
def test_ipaddr_must_not_resolve_valid_ipv4_address
assert_nothing_raised { IPAddr.new( '1.2.3.4', Socket::AF_INET ) }
end
def test_ipaddr_must_not_resolve_invalid_ipv4_style_address
assert_raise( ArgumentError ) { IPAddr.new( '1.2.3.400', Socket::AF_INET ) }
end
def test_ipaddr_must_not_resolve_valid_ipv6_address
assert_nothing_raised { IPAddr.new( '::', Socket::AF_INET6 ) }
end
def test_ipaddr_must_not_resolve_invalid_ipv6_style_address
assert_raise( ArgumentError ) { IPAddr.new( '::1::', Socket::AF_INET6 ) }
end
end
=end
=begin
Hi,
At Wed, 18 Aug 2010 22:07:42 +0900,
Philipp Kempgen wrote in [ruby-core:31756]:
DNS lookups are slow and totally unnecessary for IP addresses¶
raise MustNotCallError.new( 'IPAddr.new() must not call IPSocket.getaddress()!' )
IPAddr.new already does not call getaddress for valid IPv4
addresses if IPv6 is not available. Seems it should be enabled
even if IPv6 is available, since there is valid_v6? method
already.
diff --git a/lib/ipaddr.rb b/lib/ipaddr.rb
index 226a996..8d3ee00 100644
--- a/lib/ipaddr.rb
+++ b/lib/ipaddr.rb
@@ -17,45 +17,71 @@
require 'socket'
-unless Socket.const_defined? "AF_INET6"
+have_inet4 = Socket.const_defined? "AF_INET"
+have_inet6 = Socket.const_defined? "AF_INET6"
+
+unless have_inet6
class Socket < BasicSocket
AF_INET6 = Object.new
end
+end
+if have_inet4
class << IPSocket
def valid_v4?(addr)
if /\A(\d{1,3}).(\d{1,3}).(\d{1,3}).(\d{1,3})\Z/ =~ addr
+if have_inet6
- class << IPSocket
def valid_v6?(addr)
-
s = nil
# IPv6 (normal)
return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*\Z/ =~ addr
return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr
return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr
# IPv6 (IPv4 compat)
-
return s if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:/ =~ addr && (s = valid_v4?($')) != false
return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_v4?($')
+if have_inet6
- class << IPSocket
def valid?(addr)
+class << IPSocket
--
Nobu Nakada
=end
- Status changed from Open to Feedback
- Description updated (diff)
- Assignee set to knu (Akinori MUSHA)
- Target version set to 2.6
A few years later IPAddr was fixed so it does not call IPSocket.getaddress.
- Status changed from Feedback to Closed
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0