Class: Sqreen::Prefix

Inherits:
Struct
  • Object
show all
Defined in:
lib/sqreen/prefix.rb

Overview

addr is integer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Prefix

Returns a new instance of Prefix.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
# File 'lib/sqreen/prefix.rb', line 12

def initialize(*args)
  super
  raise ArgumentError, 'no family given' unless family
  raise ArgumentError, 'no bitlen given' unless bitlen
  raise ArgumentError, 'no address given' unless address
end

Instance Attribute Details

#addressObject

Returns the value of attribute address

Returns:

  • (Object)

    the current value of address



11
12
13
# File 'lib/sqreen/prefix.rb', line 11

def address
  @address
end

#bitlenObject

Returns the value of attribute bitlen

Returns:

  • (Object)

    the current value of bitlen



11
12
13
# File 'lib/sqreen/prefix.rb', line 11

def bitlen
  @bitlen
end

#dataObject

Returns the value of attribute data

Returns:

  • (Object)

    the current value of data



11
12
13
# File 'lib/sqreen/prefix.rb', line 11

def data
  @data
end

#familyObject

Returns the value of attribute family

Returns:

  • (Object)

    the current value of family



11
12
13
# File 'lib/sqreen/prefix.rb', line 11

def family
  @family
end

Class Method Details

.from_str(str, data = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/sqreen/prefix.rb', line 26

def Prefix.from_str(str, data = nil)
  ip_addr = IPAddr.new(str)
  bitlen = if str =~ /\/(\d+)$/
             $~[1].to_i
           else
             ip_addr.family == Socket::AF_INET6 ? 128 : 32
           end
  Prefix.new(ip_addr.family, bitlen, ip_addr.to_i, data)
end

Instance Method Details

#matches?(address, family) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
# File 'lib/sqreen/prefix.rb', line 19

def matches?(address, family)
  raise 'family mismatch' unless family == self.family
  shift_amount = (family == Socket::AF_INET ? 32 : 128) - bitlen
  (address ^ self.address) >> shift_amount == 0
end