Class: IPLogic::IP

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/iplogic/ip.rb

Constant Summary collapse

FormatError =
Class.new(ArgumentError)
MAXIP =
IP.new(0xFFFFFFFF)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(int, extra = {}) ⇒ IP

Returns a new instance of IP.



86
87
88
# File 'lib/iplogic/ip.rb', line 86

def initialize(int, extra={})
  @int = int
end

Instance Attribute Details

#intInteger (readonly) Also known as: to_i, to_int

Get the integer representation of this IP address

Returns:

  • (Integer)


80
81
82
# File 'lib/iplogic/ip.rb', line 80

def int
  @int
end

Class Method Details

.maxIP

Returns The maximum IP address: 255.255.255.255.

Returns:

  • (IP)

    The maximum IP address: 255.255.255.255



92
93
94
# File 'lib/iplogic/ip.rb', line 92

def self.max
  MAXIP
end

.rand(cidr = CIDR.all) ⇒ IP

Returns a random IP address. Useful for mocks / tests.

Parameters:

  • cidr (CIDR) (defaults to: CIDR.all)

    an optional CIDR-like object to limit the IP to

Returns:

  • (IP)

    a random IP address. Useful for mocks / tests



61
62
63
64
# File 'lib/iplogic/ip.rb', line 61

def rand(cidr=CIDR.all)
  cidr = CIDR.wrap(cidr)
  cidr.min + Kernel.rand(cidr.size)
end

.wrap(arg) ⇒ IP Also known as: []

Basic idempotent wrapper for creating addresses.

Examples:

>> IP['11.22.33.44']
=> #<IP [ 11.22.33.44 ]>
>> IP[0xFFFFFFFF]
=> #<IP [ 255.255.255.255 ]>
>> IP[nil]
=>  #<IP [ 0.0.0.0 ]>
>> IP(['11', 22, 33, '44'])
=> #<IP [ 11.22.33.44 ]>
>> ip = IP['11.22.33.44']
=> #<IP [ 11.22.33.44 ]>
>> IP(ip)
=> #<IP [ 11.22.33.44 ]>
>> ip.object_id == IP[ip].object_id
=> true

Parameters:

  • (String, Fixnum, Array, nil)

Returns:

  • (IP)

    the wrapped IP address



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/iplogic/ip.rb', line 32

def wrap(arg)
  return arg if arg.is_a? IP

  int = case arg
  when Array
    octets_to_int(arg)
  when String
    octets_to_int(arg.split('.'))
  when Fixnum
    arg
  when nil
    0
  else
    raise FormatError, "IP: Unable to parse #{arg.inspect}"
  end

  unless (0..0xFFFFFFFF).include? int
    raise FormatError, "IP: Address #{arg.inspect} out of range"
  end

  return new(int)
end

Instance Method Details

#+(int_ish) ⇒ IP

Returns the result of adding int_ish to the underlying integer.

Parameters:

  • int_ish (#to_i)

    the amount to add

Returns:

  • (IP)

    the result of adding int_ish to the underlying integer



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

def +(int_ish)
  IP.wrap(int + int_ish.to_i)
end

#-(int_ish) ⇒ IP

Returns the result of subtracting int_ish from the underlying integer.

Parameters:

  • int_ish (#to_i)

    the amount to subtract

Returns:

  • (IP)

    the result of subtracting int_ish from the underlying integer



142
143
144
# File 'lib/iplogic/ip.rb', line 142

def -(int_ish)
  self + (-int_ish.to_i)
end

#<=>(other) ⇒ Object

IP addresses are ordered in the usual way



130
131
132
# File 'lib/iplogic/ip.rb', line 130

def <=>(other)
  self.int <=> other.int
end

#assert_netmask!Object

raises an error unless this IP address is a valid netmask.

Raises:

  • (ArgumentError)


188
189
190
191
192
# File 'lib/iplogic/ip.rb', line 188

def assert_netmask!
  raise ArgumentError, <<-msg.strip unless netmask?
    #{self.inspect} is not a valid netmask.
  msg
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


123
124
125
# File 'lib/iplogic/ip.rb', line 123

def eql?(other)
  self.int == IP.wrap(other).int
end

#hashString

an IP uses its string representation as its hash

Returns:

  • (String)


119
120
121
# File 'lib/iplogic/ip.rb', line 119

def hash
  self.to_s.hash
end

#inspectObject



113
114
115
# File 'lib/iplogic/ip.rb', line 113

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

#max(netmask) ⇒ Object



163
164
165
# File 'lib/iplogic/ip.rb', line 163

def max(netmask)
  CIDR.wrap(self, netmask).max
end

#netmask?Boolean

test if this IP address is a valid netmask.

Returns:

  • (Boolean)


180
181
182
183
184
185
# File 'lib/iplogic/ip.rb', line 180

def netmask?
  maxint32 = 0xFFFFFFFF
  (0..32).any? do |i|
    (int-1) + (1 << i) == maxint32
  end
end

#octetsArray Also known as: parts

Returns an array of the four octets.

Examples:

IP['1.2.3.4'].octets # => [1, 2, 3, 4]

Returns:

  • (Array)

    an array of the four octets.



100
101
102
103
104
105
# File 'lib/iplogic/ip.rb', line 100

def octets
  @octets ||= begin
    rad = int.radix(256)
    [0]*([4-rad.size,0].max) + rad
  end
end

#prefix(netmask) ⇒ IP Also known as: min

given a netmask, returns the network prefix.

Examples:

IP['1.2.3.4'].prefix('255.255.0.0') # => #<IP [ 1.2.0.0 ]>

Returns:

  • (IP)

    the network prefix



158
159
160
# File 'lib/iplogic/ip.rb', line 158

def prefix(netmask)
  CIDR.wrap(self, netmask).min
end

#rest_field(netmask) ⇒ IP Also known as: rest

given a netmask, returns the “rest field” - the bits not covered by the netmask. See CIDR#rest_field.

IP['1.2.3.4'].rest_field # => #<IP [ 0.0.3.4 ]>

Returns:

  • (IP)

    the rest field

See Also:



174
175
176
# File 'lib/iplogic/ip.rb', line 174

def rest_field(netmask)
  CIDR.wrap(self, netmask).rest_field
end

#succIP

This allows IP “ranges” with (ip1..ip2)

Returns:



148
149
150
# File 'lib/iplogic/ip.rb', line 148

def succ
  self + 1
end

#to_sString

Returns the usual string representation of the ip address.

Returns:

  • (String)

    the usual string representation of the ip address.



109
110
111
# File 'lib/iplogic/ip.rb', line 109

def to_s
  octets.join('.')
end