Class: Ronin::Support::Network::IPRange::CIDR

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

Overview

Represents CIDR notation IP ranges.

Examples

cidr = Network::IP::CIDR.new('10.0.0.1/24')
cidr.each { |ip puts }
# 10.0.0.0
# 10.0.0.1
# 10.0.0.2
# ...
# 10.0.0.254
# 10.0.0.255

Since:

  • 1.0.0

Constant Summary collapse

SIZES =

Socket families and IP address sizes

Since:

  • 1.0.0

{
  Socket::AF_INET  => 32,
  Socket::AF_INET6 => 128
}
MASKS =

Socket families and IP address masks

Since:

  • 1.0.0

{
  Socket::AF_INET  => IN4MASK,
  Socket::AF_INET6 => IN6MASK
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#map_hash

Constructor Details

#initialize(string, family = Socket::AF_UNSPEC) ⇒ CIDR

Initializes and parses the CIDR range.

Parameters:

  • string (String)

    The CIDR range string to parse.

  • family (Integer) (defaults to: Socket::AF_UNSPEC)

    The address family for the CIDR range. This is mainly for backwards compatibility with IPAddr#initialize.

Since:

  • 1.0.0



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

def initialize(string,family=Socket::AF_UNSPEC)
  super(string,family)

  @string = string
end

Instance Attribute Details

#stringString (readonly)

The CIDR IP range string.

Returns:

Since:

  • 1.0.0



50
51
52
# File 'lib/ronin/support/network/ip_range/cidr.rb', line 50

def string
  @string
end

Class Method Details

.each(string) {|ip| ... } ⇒ Object

Enumerates over each IP address that is included in the addresses netmask. Supports both IPv4 and IPv6 addresses.

Examples:

IPRange::CIDR.each('10.0.0.1/24') { |ip| puts ip }
# 10.0.0.1
# 10.0.0.2
# ...
# 10.0.0.254
# 10.0.0.255

Parameters:

  • string (String)

    The CIDR range string to parse and enumerate over.

Yields:

  • (ip)

    The block which will be passed every IP address covered be the netmask of the IPAddr object.

Yield Parameters:

  • ip (String)

    An IP address.

Since:

  • 1.0.0



160
161
162
# File 'lib/ronin/support/network/ip_range/cidr.rb', line 160

def self.each(string,&block)
  new(string).each(&block)
end

.parse(string) ⇒ CIDR

Alias for new.

Parameters:

  • string (String)

    The CIDR range string to parse.

Returns:

  • (CIDR)

    The parsed CIDR range.

See Also:

Since:

  • 1.0.0



79
80
81
# File 'lib/ronin/support/network/ip_range/cidr.rb', line 79

def self.parse(string)
  new(string)
end

.range(first, last) ⇒ CIDR

Calcualtes the CIDR range between two IP addresses.

Examples:

IPRange::CIDR.range("1.1.1.1","1.1.1.255")
# => #<Ronin::Support::Network::IPRange::CIDR: 1.1.1.1/24>

Parameters:

  • first (String, IPAddr)

    The first IP address in the CIDR range.

  • last (String, IPAddr)

    The last IP Address in the CIDR range.

Returns:

  • (CIDR)

    The CIDR range between the two IP addresses.

Since:

  • 1.0.0



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ronin/support/network/ip_range/cidr.rb', line 111

def self.range(first,last)
  first_ip = case first
             when IPAddr then first
             else             IPAddr.new(first)
             end

  last_ip = case last
            when IPAddr then last
            else             IPAddr.new(last)
            end

  unless (first_ip.family == last_ip.family)
    raise(ArgumentError,"must specify two IPv4 or IPv6 addresses: #{first.inspect} #{last.inspect}")
  end

  num_bits  = SIZES.fetch(first_ip.family)
  diff_bits = first_ip.to_i ^ last_ip.to_i

  if diff_bits > 0
    prefix_length = num_bits - Math.log2(diff_bits).ceil

    return new("#{first_ip}/#{prefix_length}")
  else
    return new(first_ip.to_s)
  end
end

Instance Method Details

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

Note:

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

Iterates over each IP address that is included in the addresses netmask. Supports both IPv4 and IPv6 addresses.

Examples:

cidr = IPAddr.new('10.1.1.1/24')
cidr.each { |ip| puts ip }
# 10.0.0.1
# 10.0.0.2
# ...
# 10.0.0.253
# 10.0.0.254

Yields:

  • (ip)

    The block which will be passed every IP address covered be the netmask of the IPAddr object.

Yield Parameters:

  • ip (String)

    An IP address.

Returns:

  • (self)

Since:

  • 1.0.0



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/ronin/support/network/ip_range/cidr.rb', line 189

def each
  return enum_for(__method__) unless block_given?

  family_mask = MASKS[@family]

  (0..((~@mask_addr) & family_mask)).each do |i|
    ip_uint = (@addr | i)

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

    yield _to_string(ip_uint)
  end

  return self
end

#firstString

The first IP address of the CIDR Range.

Returns:

Since:

  • 1.0.0



213
214
215
# File 'lib/ronin/support/network/ip_range/cidr.rb', line 213

def first
  _to_string(@addr)
end

#inspectString

Inspects the CIDR range.

Returns:

Since:

  • 1.0.0



240
241
242
# File 'lib/ronin/support/network/ip_range/cidr.rb', line 240

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

#lastString

The last IP address of the CIDR range.

Returns:

Since:

  • 1.0.0



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

def last
  _to_string(@addr | ~@mask_addr)
end

#to_sString

Converts the CIDR range back into a String.

Returns:

Since:

  • 1.0.0



231
232
233
# File 'lib/ronin/support/network/ip_range/cidr.rb', line 231

def to_s
  @string
end