Sha256: 286c4a227bb37f6a2cd0c0e93caedf19aa501148b71a7525ae508fa0322d2e61

Contents?: true

Size: 1022 Bytes

Versions: 9

Compression:

Stored size: 1022 Bytes

Contents

# Copyright (c) 2015 Sqreen. All Rights Reserved.
# Please refer to our terms for more information: https://www.sqreen.com/terms.html

require 'ipaddr'

# TODO: move to Sqreen::IP

module Sqreen
  Prefix = Struct.new(:family, :bitlen, :address, :data) do # addr is integer
    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

    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
  end

  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
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sqreen-1.18.6-java lib/sqreen/prefix.rb
sqreen-1.18.6 lib/sqreen/prefix.rb
sqreen-1.18.5-java lib/sqreen/prefix.rb
sqreen-1.18.5 lib/sqreen/prefix.rb
sqreen-1.18.4-java lib/sqreen/prefix.rb
sqreen-1.18.4 lib/sqreen/prefix.rb
sqreen-1.18.3-java lib/sqreen/prefix.rb
sqreen-1.18.3 lib/sqreen/prefix.rb
sqreen-1.18.3.beta2 lib/sqreen/prefix.rb