From 182943fe6c9daa7bde5b69cab996eb86cfa4504b Mon Sep 17 00:00:00 2001 From: HD Moore Date: Wed, 20 Oct 2010 01:27:00 +0000 Subject: [PATCH] This change allows IPv6 addresses to be ignored by passing false as a second parameter to getaddress() git-svn-id: file:///home/svn/framework3/trunk@10761 4d416f70-5f16-0410-b530-b9f4589650da --- lib/rex/socket.rb | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/rex/socket.rb b/lib/rex/socket.rb index fdc9315d68..397e317416 100644 --- a/lib/rex/socket.rb +++ b/lib/rex/socket.rb @@ -138,9 +138,31 @@ module Socket # These calls can be quite slow. This also fixes an issue with the # Resolv.getaddress() call being non-functional on Ruby 1.9.1 (Win32). # - def self.getaddress(addr) + def self.getaddress(addr, accept_ipv6 = true) begin - dotted_ip?(addr) ? addr : self.addr_ntoa( ::Socket.gethostbyname(addr)[3] ) + + if dotted_ip?(addr) + return addr + end + + res = ::Socket.gethostbyname(addr) + return nil if not res + + # Shift the first three elements out + rname = res.shift + ralias = res.shift + rtype = res.shift + + # Reject IPv6 addresses if we don't accept them + if not accept_ipv6 + res.reject!{|nbo| nbo.length != 4} + end + + # Make sure we have at least one name + return nil if res.length == 0 + + # Return the first address of the result + self.addr_ntoa( res[0] ) rescue ::ArgumentError # Win32 bug nil end