Class: Ronin::Support::Network::IPRange::Glob
- Inherits:
-
Object
- Object
- Ronin::Support::Network::IPRange::Glob
- Includes:
- Enumerable
- Defined in:
- lib/ronin/support/network/ip_range/glob.rb
Overview
Constant Summary collapse
- IPV4_REGEX =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Regular expression that matches IPv4-glob ranges.
/\A#{ipv4_addr}\z/
- IPV6_REGEX =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Regular expression that matches IPv6-glob ranges.
/\A(?: (?:#{ipv6_octet_list}:){6}#{ipv4_addr}| (?:#{ipv6_octet_list}:){5}#{ipv6_octet_list}:#{ipv4_addr}| (?:#{ipv6_octet_list}:){5}:#{ipv6_octet_list}:#{ipv4_addr}| (?:#{ipv6_octet_list}:){1,1}(?::#{ipv6_octet_list}){1,4}:#{ipv4_addr}| (?:#{ipv6_octet_list}:){1,2}(?::#{ipv6_octet_list}){1,3}:#{ipv4_addr}| (?:#{ipv6_octet_list}:){1,3}(?::#{ipv6_octet_list}){1,2}:#{ipv4_addr}| (?:#{ipv6_octet_list}:){1,4}(?::#{ipv6_octet_list}){1,1}:#{ipv4_addr}| :(?::#{ipv6_octet_list}){1,5}:#{ipv4_addr}| (?:(?:#{ipv6_octet_list}:){1,5}|:):#{ipv4_addr}| (?:#{ipv6_octet_list}:){1,1}(?::#{ipv6_octet_list}){1,6}| (?:#{ipv6_octet_list}:){1,2}(?::#{ipv6_octet_list}){1,5}| (?:#{ipv6_octet_list}:){1,3}(?::#{ipv6_octet_list}){1,4}| (?:#{ipv6_octet_list}:){1,4}(?::#{ipv6_octet_list}){1,3}| (?:#{ipv6_octet_list}:){1,5}(?::#{ipv6_octet_list}){1,2}| (?:#{ipv6_octet_list}:){1,6}(?::#{ipv6_octet_list}){1,1}| #{ipv6_octet_list}(?::#{ipv6_octet_list}){7}| :(?::#{ipv6_octet_list}){1,7}| (?:(?:#{ipv6_octet_list}:){1,7}|:): )\z/x
- REGEX =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Regular expression to match IP-glob ranges.
/#{IPV4_REGEX}|#{IPV6_REGEX}/
Instance Attribute Summary collapse
-
#string ⇒ String
readonly
The IP glob string.
Class Method Summary collapse
-
.each(string) {|ip| ... } ⇒ self
Enumerates over the IP-glob range.
-
.parse(string) ⇒ Glob
Alias for new.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares the IP glob range to another IP range.
-
#===(other) ⇒ Boolean
Determines if the given IP range is a sub-set of the IP CIDR range.
-
#each {|ip| ... } ⇒ self
Enumerates over the IP-glob range.
-
#first ⇒ String
The first address in the IP glob range.
-
#include?(ip) ⇒ Boolean
Determines whether the IP address exists within the IP range.
-
#initialize(string) ⇒ Glob
constructor
Initializes and parses the IP-glob range.
-
#inspect ⇒ String
Inspects the IP-glob range.
-
#ipv4? ⇒ Boolean
Determines if the IP-glob range is IPv4.
-
#ipv6? ⇒ Boolean
Determines if the IP-glob range is IPv6.
-
#last ⇒ String
The last address in the IP glob range.
-
#size ⇒ Integer
Calculates the size of the IP glob range.
-
#to_s ⇒ String
Converts the IP-glob range back into a String.
Methods included from Enumerable
Constructor Details
#initialize(string) ⇒ Glob
Initializes and parses the IP-glob range.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/ronin/support/network/ip_range/glob.rb', line 121 def initialize(string) @string = string case string when IPV4_REGEX @version = 4 @base = 10 @formatter = method(:format_ipv4_address) separator = '.' octet_range = (0..255) when IPV6_REGEX @version = 6 @base = 16 @formatter = method(:format_ipv6_address) separator = ':' octet_range = (0..0xffff) else raise(ArgumentError,"invalid IP-glob range: #{string.inspect}") end @ranges = string.split(separator).map do |segment| if segment == '*' then octet_range elsif segment.include?(',') then parse_list(segment) elsif segment.include?('-') then parse_range(segment) else [segment] end end end |
Instance Attribute Details
#string ⇒ String (readonly)
The IP glob string.
110 111 112 |
# File 'lib/ronin/support/network/ip_range/glob.rb', line 110 def string @string end |
Class Method Details
.each(string) {|ip| ... } ⇒ self
Enumerates over the IP-glob range.
205 206 207 |
# File 'lib/ronin/support/network/ip_range/glob.rb', line 205 def self.each(string,&block) new(string).each(&block) end |
Instance Method Details
#==(other) ⇒ Boolean
Compares the IP glob range to another IP range.
252 253 254 |
# File 'lib/ronin/support/network/ip_range/glob.rb', line 252 def ==(other) other.kind_of?(self.class) && @string == other.string end |
#===(other) ⇒ Boolean
Determines if the given IP range is a sub-set of the IP CIDR range.
266 267 268 269 270 271 272 273 |
# File 'lib/ronin/support/network/ip_range/glob.rb', line 266 def ===(other) case other when Enumerable other.all? { |ip| include?(ip) } else false end end |
#each {|ip| ... } ⇒ self
Enumerates over the IP-glob range.
312 313 314 315 316 317 318 319 320 321 |
# File 'lib/ronin/support/network/ip_range/glob.rb', line 312 def each return enum_for(__method__) unless block_given? # cycle through the address ranges @ranges.comprehension do |address| yield @formatter.call(address) end return self end |
#first ⇒ String
The first address in the IP glob range.
331 332 333 |
# File 'lib/ronin/support/network/ip_range/glob.rb', line 331 def first @formatter.call(@ranges.map(&:first)) end |
#include?(ip) ⇒ Boolean
Determines whether the IP address exists within the IP range.
238 239 240 |
# File 'lib/ronin/support/network/ip_range/glob.rb', line 238 def include?(ip) super(ip.to_s) end |
#inspect ⇒ String
Inspects the IP-glob range.
374 375 376 |
# File 'lib/ronin/support/network/ip_range/glob.rb', line 374 def inspect "#<#{self.class}: #{@string}>" end |
#ipv4? ⇒ Boolean
Determines if the IP-glob range is IPv4.
214 215 216 |
# File 'lib/ronin/support/network/ip_range/glob.rb', line 214 def ipv4? @version == 4 end |
#ipv6? ⇒ Boolean
Determines if the IP-glob range is IPv6.
223 224 225 |
# File 'lib/ronin/support/network/ip_range/glob.rb', line 223 def ipv6? @version == 6 end |
#last ⇒ String
The last address in the IP glob range.
343 344 345 |
# File 'lib/ronin/support/network/ip_range/glob.rb', line 343 def last @formatter.call(@ranges.map(&:last)) end |
#size ⇒ Integer
Calculates the size of the IP glob range.
354 355 356 357 358 |
# File 'lib/ronin/support/network/ip_range/glob.rb', line 354 def size @ranges.reduce(1) do |total,parts| total * parts.size end end |
#to_s ⇒ String
Converts the IP-glob range back into a String.
365 366 367 |
# File 'lib/ronin/support/network/ip_range/glob.rb', line 365 def to_s @string end |