Class: Ronin::Support::Network::IPRange::Range

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ronin/support/network/ip_range/range.rb

Overview

Represents an arbitrary range of IP addresses.

Examples

range = Network::IPRange::Range.new('128.2.3.4','200.10.1.255')
range.each { |ip| puts ip }
# 128.2.3.4
# 128.2.3.5
# ...
# 200.10.1.254
# 200.10.1.255

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#map_hash

Constructor Details

#initialize(first, last) ⇒ Range

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the ASN IP range.

Parameters:

  • first (String)

    The first IP address in the ASN IP range.

  • last (String)

    The last IP address in the ASN IP range.

Raises:

  • (ArgumentError)

    Both IP addresses must be either IPv4 addresses or IPv6 addresses.

Since:

  • 1.0.0



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ronin/support/network/ip_range/range.rb', line 85

def initialize(first,last)
  @begin  = IP.new(first)
  @end    = IP.new(last)

  if (@begin.ipv4? && !@end.ipv4?) ||
     (@begin.ipv6? && !@end.ipv6?)
    raise(ArgumentError,"must specify two IPv4 or IPv6 addresses: #{first.inspect} #{last.inspect}")
  end

  @prefix = self.class.prefix(first,last)
  @family = @begin.family

  @begin_uint = @begin.to_i
  @end_uint   = @end.to_i
end

Instance Attribute Details

#beginIP (readonly) Also known as: first

The first IP address of the ASN IP range.

Returns:

Since:

  • 1.0.0



51
52
53
# File 'lib/ronin/support/network/ip_range/range.rb', line 51

def begin
  @begin
end

#endIP (readonly) Also known as: last

The last IP address of the ASN IP range.

Returns:

Since:

  • 1.0.0



57
58
59
# File 'lib/ronin/support/network/ip_range/range.rb', line 57

def end
  @end
end

#familySocket::AF_INET, Socket::AF_INET6 (readonly)

The address family of the ASN IP range.

Returns:

  • (Socket::AF_INET, Socket::AF_INET6)

Since:

  • 1.0.0



69
70
71
# File 'lib/ronin/support/network/ip_range/range.rb', line 69

def family
  @family
end

#prefixString (readonly)

The common prefix shared by the first and last IP addresses in the ASN IP range.

Returns:

Since:

  • 1.0.0



64
65
66
# File 'lib/ronin/support/network/ip_range/range.rb', line 64

def prefix
  @prefix
end

Class Method Details

.prefix(first, last) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds the common prefix of two IP addresses.

Parameters:

  • first (String)

    The first IP address of the ASN IP range.

  • last (String)

    The last IP address of the ASN IP range.

Returns:

  • (String)

    The common prefix shared by both IPs.

Since:

  • 1.0.0



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ronin/support/network/ip_range/range.rb', line 115

def self.prefix(first,last)
  min_length = [first.length, last.length].min

  min_length.times do |index|
    if first[index] != last[index]
      return first[0,index]
    end
  end

  return first
end

Instance Method Details

#==(other) ⇒ Boolean

Compares the IP range to another object.

Parameters:

  • other (Object)

    The other object to compare to.

Returns:

  • (Boolean)

Since:

  • 1.0.0



221
222
223
224
225
# File 'lib/ronin/support/network/ip_range/range.rb', line 221

def ==(other)
  self.class == other.class &&
    self.begin == other.begin &&
    self.end   == other.end
end

#each {|ip| ... } ⇒ Enumerator

Note:

This method will skip IPv4 addresses ending in .0 or .255.

Enumerates over every IP address in the ASN IP range.

Examples:

range = IPRange::Range.new('1.1.1.0','1.1.1.255')
range.each { |ip| puts ip }
# 1.1.1.1
# 1.1.1.2
# ...
# 1.1.1.253
# 1.1.1.254
range IPRange::Range.new('1.1.1.100','1.1.3.200')
range.each { |ip| puts ip }
# 1.1.1.100
# 1.1.1.101
# ...
# 1.1.3.199
# 1.1.3.200

Yields:

  • (ip)

    If a block is given, it will be passed every IP within the ASN IP range.

Yield Parameters:

  • ip (IP)

    An IP within the ASN IP range.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.

Since:

  • 1.0.0



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/ronin/support/network/ip_range/range.rb', line 195

def each
  return enum_for(__method__) unless block_given?

  ipaddr = @begin.clone

  (@begin_uint..@end_uint).each do |ip_uint|
    # skip IPv4 addresses ending in .0 or .255
    if (ipv4? && ((ip_uint & 0xff) == 0 || (ip_uint & 0xff) == 0xff))
      next
    end

    ipaddr.send(:set,ip_uint)
    yield ipaddr.to_s
  end

  return self
end

#include?(ip) ⇒ Boolean

Determines if the given IP belongs to the ASN IP range.

Parameters:

Returns:

  • (Boolean)

    Specifies whether the IP is or is not within the ASN IP range.

Since:

  • 1.0.0



154
155
156
157
158
159
# File 'lib/ronin/support/network/ip_range/range.rb', line 154

def include?(ip)
  ip      = IPAddr.new(ip) unless ip.kind_of?(IPAddr)
  ip_uint = ip.to_i

  return (ip_uint >= @begin_uint) && (ip_uint <= @end_uint)
end

#inspectString

Inspects the IP range.

Returns:

Since:

  • 1.0.0



241
242
243
# File 'lib/ronin/support/network/ip_range/range.rb', line 241

def inspect
  "#<#{self.class}: #{self}>"
end

#ipv4?Boolean

Determines whether the IP range is an IPv4 range.

Returns:

  • (Boolean)

Since:

  • 1.0.0



132
133
134
# File 'lib/ronin/support/network/ip_range/range.rb', line 132

def ipv4?
  @begin.ipv4?
end

#ipv6?Boolean

Determines whether the IP range is an IPv6 range.

Returns:

  • (Boolean)

Since:

  • 1.0.0



141
142
143
# File 'lib/ronin/support/network/ip_range/range.rb', line 141

def ipv6?
  @end.ipv6?
end

#to_sString

Converts the IP range to a String.

Returns:

Since:

  • 1.0.0



232
233
234
# File 'lib/ronin/support/network/ip_range/range.rb', line 232

def to_s
  "#{@begin} - #{@end}"
end