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.
-
#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.
-
#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.
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
This method will skip IPv4 addresses ending in .0
or .255
.
Enumerates over every IP address in the ASN IP range.
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.
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.
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.
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 |
#to_s ⇒ String
Converts the IP range to a String.
232 233 234 |
# File 'lib/ronin/support/network/ip_range/range.rb', line 232 def to_s "#{@begin} - #{@end}" end |