Skip to content

Commit

Permalink
simplify condition logic, more consistently return true/false from ch…
Browse files Browse the repository at this point in the history
…ecks
  • Loading branch information
Brent Cook committed Jun 25, 2017
1 parent bdbdd27 commit 91e44eb
Showing 1 changed file with 14 additions and 27 deletions.
41 changes: 14 additions & 27 deletions lib/rex/socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ def self.create_ip(opts = {})
#
##


# Cache our IPv6 support flag
@@support_ipv6 = nil

Expand Down Expand Up @@ -124,54 +123,44 @@ def self.support_ipv6?
# Determine whether this is an IPv4 address
#
def self.is_ipv4?(addr)
( addr =~ MATCH_IPV4 ) ? true : false
addr =~ MATCH_IPV4 ? true : false
end

#
# Determine whether this is an IPv6 address
#
def self.is_ipv6?(addr)
( addr =~ MATCH_IPV6 ) ? true : false
addr =~ MATCH_IPV6 ? true : false
end

#
# Determine whether this is a MAC address
#
def self.is_mac_addr?(addr)
( addr =~ MATCH_MAC_ADDR) ? true : false
addr =~ MATCH_MAC_ADDR ? true : false
end

#
# Determine whether this is an IP address at all
# Check for v4 (less expensive), v6, else false
#
def self.is_ip_addr?(addr)
self.is_ipv4?(addr) ? true : self.is_ipv6?(addr) ? true : false
self.is_ipv4?(addr) || self.is_ipv6?(addr)
end

#
# Checks to see if the supplied address is in "dotted" form
#
def self.dotted_ip?(addr)
# Match IPv6
return true if (support_ipv6? and addr =~ MATCH_IPV6)

# Match IPv4
return true if (addr =~ MATCH_IPV4)

false
(support_ipv6? && addr =~ MATCH_IPV6) || (addr =~ MATCH_IPV4)
end

#
# Return true if +addr+ is within the ranges specified in RFC1918, or
# RFC5735/RFC3927
#
def self.is_internal?(addr)
if self.dotted_ip?(addr)
addr =~ MATCH_IPV4_PRIVATE
else
false
end
self.dotted_ip?(addr) && addr =~ MATCH_IPV4_PRIVATE
end

# Get the first address returned by a DNS lookup for +hostname+.
Expand All @@ -193,7 +182,7 @@ def self.getaddress(hostname, accept_ipv6 = true)
# @param hostname [String] A hostname or ASCII IP address
# @return [Array<String>]
def self.getaddresses(hostname, accept_ipv6 = true)
if hostname =~ MATCH_IPV4 or (accept_ipv6 and hostname =~ MATCH_IPV6)
if hostname =~ MATCH_IPV4 || (accept_ipv6 && hostname =~ MATCH_IPV6)
return [hostname]
end

Expand Down Expand Up @@ -229,7 +218,7 @@ def self.getaddresses(hostname, accept_ipv6 = true)
# on Windows.
#
def self.gethostbyname(host)
if (is_ipv4?(host))
if is_ipv4?(host)
return [ host, [], 2, host.split('.').map{ |c| c.to_i }.pack("C4") ]
end

Expand All @@ -247,8 +236,7 @@ def self.gethostbyname(host)
# address family
#
def self.to_sockaddr(ip, port)

if (ip == '::ffff:0.0.0.0')
if ip == '::ffff:0.0.0.0'
ip = support_ipv6?() ? '::' : '0.0.0.0'
end

Expand All @@ -262,7 +250,7 @@ def self.to_sockaddr(ip, port)
def self.from_sockaddr(saddr)
port, host = ::Socket::unpack_sockaddr_in(saddr)
af = ::Socket::AF_INET
if (support_ipv6?() and is_ipv6?(host))
if support_ipv6?() && is_ipv6?(host)
af = ::Socket::AF_INET6
end
return [ af, host, port ]
Expand Down Expand Up @@ -488,7 +476,6 @@ def self.cidr_crack(cidr, v6=false)
# lame kid way of doing it.
#
def self.net2bitmask(netmask)

nmask = resolv_nbo(netmask)
imask = addr_ntoi(nmask)
bits = 32
Expand Down Expand Up @@ -768,17 +755,17 @@ def getpeername_as_array
def peerinfo
if (pi = getpeername_as_array)
return pi[1] + ':' + pi[2].to_s
end
end
end
end

#
# Returns local information (host + port) in host:port format.
#
def localinfo
if (pi = getlocalname)
return pi[1] + ':' + pi[2].to_s
end
end
end
end

#
# Returns a string that indicates the type of the socket, such as 'tcp'.
Expand Down

0 comments on commit 91e44eb

Please sign in to comment.