Class: IPLogic::CIDR

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/iplogic/cidr.rb

Constant Summary collapse

SHORTENED_IP_REGEX =
/^(\d{1,3}(\.\d{1,3}){1,3})\/(\d+)$/
ALL =
self.new(0,0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip, bits) ⇒ CIDR

Returns a new instance of CIDR.



75
76
77
78
# File 'lib/iplogic/cidr.rb', line 75

def initialize(ip, bits)
  @bits = bits.to_i
  @ip = IP.wrap(ip)
end

Instance Attribute Details

#bitsObject (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

#ipObject (readonly)

Returns the value of attribute ip.



72
73
74
# File 'lib/iplogic/cidr.rb', line 72

def ip
  @ip
end

Class Method Details

.allObject



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

Returns:

  • (Boolean)


103
104
105
# File 'lib/iplogic/cidr.rb', line 103

def include?(ip)
  IP.wrap(ip).min(bits) == min
end

#inspectObject



89
90
91
# File 'lib/iplogic/cidr.rb', line 89

def inspect
  "#<CIDR [ #{self} ]>"
end

#inv_bitsObject



85
86
87
# File 'lib/iplogic/cidr.rb', line 85

def inv_bits
  32 - bits
end

#maxObject Also known as: end, last



116
117
118
# File 'lib/iplogic/cidr.rb', line 116

def max
  @max ||= min + (size - 1)
end

#minObject 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

#netmaskObject



93
94
95
96
97
# File 'lib/iplogic/cidr.rb', line 93

def netmask
  @netmask ||= IP.wrap(
    ((1 << bits) - 1) << (32 - bits)
  )
end

#rest_fieldObject Also known as: rest



122
123
124
# File 'lib/iplogic/cidr.rb', line 122

def rest_field
  @rest_field ||= ip - min
end

#significant_octetsObject



132
133
134
# File 'lib/iplogic/cidr.rb', line 132

def significant_octets
  4 - (bits / 8)
end

#sizeObject



99
100
101
# File 'lib/iplogic/cidr.rb', line 99

def size
  @size ||= (1 << inv_bits)
end

#to_sObject Also known as: to_str



127
128
129
# File 'lib/iplogic/cidr.rb', line 127

def to_s
  "#{ip}/#{bits}"
end

#zoneObject



136
137
138
# File 'lib/iplogic/cidr.rb', line 136

def zone
  ip.parts[0..-(1+significant_octets)].reverse.join('.')
end