Class: Ronin::Support::Network::IPRange::Range
- Inherits:
-
Object
- Object
- Ronin::Support::Network::IPRange::Range
- Includes:
- Enumerable
- Defined in:
- lib/ronin/support/network/ip_range/range.rb
Overview
Instance Attribute Summary collapse
-
#begin ⇒ IP
(also: #first)
readonly
The first IP address of the ASN IP range.
-
#end ⇒ IP
(also: #last)
readonly
The last IP address of the ASN IP range.
-
#family ⇒ Socket::AF_INET, Socket::AF_INET6
readonly
The address family of the ASN IP range.
-
#prefix ⇒ String
readonly
The common prefix shared by the first and last IP addresses in the ASN IP range.
Class Method Summary collapse
-
.prefix(first, last) ⇒ String
private
Finds the common prefix of two IP addresses.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares the IP range to another object.
-
#===(other) ⇒ Boolean
Determines if the given IP range is a sub-set of the IP range.
-
#each {|ip| ... } ⇒ Enumerator
Enumerates over every IP address in the ASN IP range.
-
#include?(ip) ⇒ Boolean
Determines if the given IP belongs to the ASN IP range.
-
#initialize(first, last) ⇒ Range
constructor
private
Initializes the ASN IP range.
-
#inspect ⇒ String
Inspects the IP range.
-
#ipv4? ⇒ Boolean
Determines whether the IP range is an IPv4 range.
-
#ipv6? ⇒ Boolean
Determines whether the IP range is an IPv6 range.
-
#size ⇒ Integer
Calculates the size of the IP range.
-
#to_s ⇒ String
Converts the IP range to a String.
Methods included from Enumerable
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.
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
#begin ⇒ IP (readonly) Also known as: first
The first IP address of the ASN IP range.
51 52 53 |
# File 'lib/ronin/support/network/ip_range/range.rb', line 51 def begin @begin end |
#end ⇒ IP (readonly) Also known as: last
The last IP address of the ASN IP range.
57 58 59 |
# File 'lib/ronin/support/network/ip_range/range.rb', line 57 def end @end end |
#family ⇒ Socket::AF_INET, Socket::AF_INET6 (readonly)
The address family of the ASN IP range.
69 70 71 |
# File 'lib/ronin/support/network/ip_range/range.rb', line 69 def family @family end |
#prefix ⇒ String (readonly)
The common prefix shared by the first and last IP addresses in the ASN IP range.
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.
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.
204 205 206 207 208 |
# File 'lib/ronin/support/network/ip_range/range.rb', line 204 def ==(other) self.class == other.class && self.begin == other.begin && self.end == other.end end |
#===(other) ⇒ Boolean
Determines if the given IP range is a sub-set of the IP range.
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/ronin/support/network/ip_range/range.rb', line 220 def ===(other) case other when IPRange::Range self.begin <= other.begin && self.end >= other.end when Enumerable other.all? { |ip| include?(ip) } else false end end |
#each {|ip| ... } ⇒ Enumerator
Enumerates over every IP address in the ASN IP range.
183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/ronin/support/network/ip_range/range.rb', line 183 def each return enum_for(__method__) unless block_given? ipaddr = @begin.clone (@begin_uint..@end_uint).each do |ip_uint| 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.
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 |
#inspect ⇒ String
Inspects the IP range.
256 257 258 |
# File 'lib/ronin/support/network/ip_range/range.rb', line 256 def inspect "#<#{self.class}: #{self}>" end |
#ipv4? ⇒ Boolean
Determines whether the IP range is an IPv4 range.
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.
141 142 143 |
# File 'lib/ronin/support/network/ip_range/range.rb', line 141 def ipv6? @end.ipv6? end |
#size ⇒ Integer
Calculates the size of the IP range.
238 239 240 |
# File 'lib/ronin/support/network/ip_range/range.rb', line 238 def size (@end_uint - @begin_uint) + 1 end |
#to_s ⇒ String
Converts the IP range to a String.
247 248 249 |
# File 'lib/ronin/support/network/ip_range/range.rb', line 247 def to_s "#{@begin} - #{@end}" end |