Class: Ronin::Support::Network::IPRange::CIDR
- 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
Constant Summary collapse
- SIZES =
Socket families and IP address sizes
{ Socket::AF_INET => 32, Socket::AF_INET6 => 128 }
- MASKS =
Socket families and IP address masks
{ Socket::AF_INET => IN4MASK, Socket::AF_INET6 => IN6MASK }
Instance Attribute Summary collapse
-
#string ⇒ String
readonly
The CIDR IP range string.
Class Method Summary collapse
-
.each(string) {|ip| ... } ⇒ Object
Enumerates over each IP address that is included in the addresses netmask.
-
.parse(string) ⇒ CIDR
Alias for new.
-
.range(first, last) ⇒ CIDR
Calcualtes the CIDR range between two IP addresses.
Instance Method Summary collapse
-
#each {|ip| ... } ⇒ self
Iterates over each IP address that is included in the addresses netmask.
-
#first ⇒ String
The first IP address of the CIDR Range.
-
#initialize(string, family = Socket::AF_UNSPEC) ⇒ CIDR
constructor
Initializes and parses the CIDR range.
-
#inspect ⇒ String
Inspects the CIDR range.
-
#last ⇒ String
The last IP address of the CIDR range.
-
#to_s ⇒ String
Converts the CIDR range back into a String.
Methods included from Enumerable
Constructor Details
#initialize(string, family = Socket::AF_UNSPEC) ⇒ CIDR
Initializes and parses the CIDR range.
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
#string ⇒ String (readonly)
The CIDR IP range string.
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.
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.
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.
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
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.
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 |
#first ⇒ String
The first IP address of the CIDR Range.
213 214 215 |
# File 'lib/ronin/support/network/ip_range/cidr.rb', line 213 def first _to_string(@addr) end |
#inspect ⇒ String
Inspects the CIDR range.
240 241 242 |
# File 'lib/ronin/support/network/ip_range/cidr.rb', line 240 def inspect "#<#{self.class}: #{@string}>" end |
#last ⇒ String
The last IP address of the CIDR range.
222 223 224 |
# File 'lib/ronin/support/network/ip_range/cidr.rb', line 222 def last _to_string(@addr | ~@mask_addr) end |
#to_s ⇒ String
Converts the CIDR range back into a String.
231 232 233 |
# File 'lib/ronin/support/network/ip_range/cidr.rb', line 231 def to_s @string end |