Class: IPLogic::CIDR
Constant Summary collapse
- SHORTENED_IP_REGEX =
/^(\d{1,3}(\.\d{1,3}){1,3})\/(\d+)$/
- ALL =
self.new(0,0)
Instance Attribute Summary collapse
-
#bits ⇒ Object
(also: #prefix_length)
readonly
Returns the value of attribute bits.
-
#ip ⇒ Object
readonly
Returns the value of attribute ip.
Class Method Summary collapse
Instance Method Summary collapse
- #each(&blk) ⇒ Object
- #include?(ip) ⇒ Boolean
-
#initialize(ip, bits) ⇒ CIDR
constructor
A new instance of CIDR.
- #inspect ⇒ Object
- #inv_bits ⇒ Object
- #max ⇒ Object (also: #end, #last)
- #min ⇒ Object (also: #begin, #first, #prefix)
- #netmask ⇒ Object
- #rest_field ⇒ Object (also: #rest)
- #significant_octets ⇒ Object
- #size ⇒ Object
- #to_s ⇒ Object (also: #to_str)
- #zone ⇒ Object
Constructor Details
Instance Attribute Details
#bits ⇒ Object (readonly) Also known as: prefix_length
Returns the value of attribute bits.
72 73 74 |
# File 'lib/iplogic/cidr.rb', line 72 def bits @bits end |
#ip ⇒ Object (readonly)
Returns the value of attribute ip.
72 73 74 |
# File 'lib/iplogic/cidr.rb', line 72 def ip @ip end |
Class Method Details
.all ⇒ Object
81 82 83 |
# File 'lib/iplogic/cidr.rb', line 81 def self.all ALL end |
.wrap(*args) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/iplogic/cidr.rb', line 7 def wrap(*args) return args.first if args.first.is_a? CIDR if args.size == 2 ip, bits = args bits = case bits when Integer bits when String, IP netmask_to_bits(IP.wrap(bits)) else if bits.respond_to? :to_i bits.to_i else format_error(args, bits) end end ip = parse_shortened_ip(ip) if ip.is_a? String return new(ip, bits) elsif args.size == 1 arg = args.first return arg if arg.is_a? CIDR # one argument means it's gotta be a string to parse format_error(arg) unless arg.is_a? String if arg =~ SHORTENED_IP_REGEX new(parse_shortened_ip($1), $3) elsif (parts = arg.split('/')).size == 2 ip = parse_shortened_ip(parts[0]) mask = IP.wrap(parts[1]) return new(ip, netmask_to_bits(mask)) else format_error(arg) end end end |
Instance Method Details
#each(&blk) ⇒ Object
140 141 142 |
# File 'lib/iplogic/cidr.rb', line 140 def each(&blk) (min..max).each(&blk) end |
#include?(ip) ⇒ Boolean
103 104 105 |
# File 'lib/iplogic/cidr.rb', line 103 def include?(ip) IP.wrap(ip).min(bits) == min end |
#inspect ⇒ Object
89 90 91 |
# File 'lib/iplogic/cidr.rb', line 89 def inspect "#<CIDR [ #{self} ]>" end |
#inv_bits ⇒ Object
85 86 87 |
# File 'lib/iplogic/cidr.rb', line 85 def inv_bits 32 - bits end |
#max ⇒ Object Also known as: end, last
116 117 118 |
# File 'lib/iplogic/cidr.rb', line 116 def max @max ||= min + (size - 1) end |
#min ⇒ Object Also known as: begin, first, prefix
107 108 109 110 111 |
# File 'lib/iplogic/cidr.rb', line 107 def min @min ||= IP.wrap( (ip.to_i >> inv_bits) << inv_bits ) end |
#netmask ⇒ Object
93 94 95 96 97 |
# File 'lib/iplogic/cidr.rb', line 93 def netmask @netmask ||= IP.wrap( ((1 << bits) - 1) << (32 - bits) ) end |
#rest_field ⇒ Object Also known as: rest
122 123 124 |
# File 'lib/iplogic/cidr.rb', line 122 def rest_field @rest_field ||= ip - min end |
#significant_octets ⇒ Object
132 133 134 |
# File 'lib/iplogic/cidr.rb', line 132 def significant_octets 4 - (bits / 8) end |
#size ⇒ Object
99 100 101 |
# File 'lib/iplogic/cidr.rb', line 99 def size @size ||= (1 << inv_bits) end |
#to_s ⇒ Object Also known as: to_str
127 128 129 |
# File 'lib/iplogic/cidr.rb', line 127 def to_s "#{ip}/#{bits}" end |
#zone ⇒ Object
136 137 138 |
# File 'lib/iplogic/cidr.rb', line 136 def zone ip.parts[0..-(1+significant_octets)].reverse.join('.') end |