Class: Sqreen::Prefix
- Inherits:
-
Struct
- Object
- Struct
- Sqreen::Prefix
- Defined in:
- lib/sqreen/prefix.rb
Overview
addr is integer
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
-
#bitlen ⇒ Object
Returns the value of attribute bitlen.
-
#data ⇒ Object
Returns the value of attribute data.
-
#family ⇒ Object
Returns the value of attribute family.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(*args) ⇒ Prefix
constructor
A new instance of Prefix.
- #matches?(address, family) ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ Prefix
Returns a new instance of Prefix.
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
#address ⇒ Object
Returns the value of attribute address
11 12 13 |
# File 'lib/sqreen/prefix.rb', line 11 def address @address end |
#bitlen ⇒ Object
Returns the value of attribute bitlen
11 12 13 |
# File 'lib/sqreen/prefix.rb', line 11 def bitlen @bitlen end |
#data ⇒ Object
Returns the value of attribute data
11 12 13 |
# File 'lib/sqreen/prefix.rb', line 11 def data @data end |
#family ⇒ Object
Returns the value of attribute 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
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 |