Sha256: 786334c73754ac527a6bce23563852a45fb45fb4a177c1aa5e29fe4ef79a0be1

Contents?: true

Size: 1.01 KB

Versions: 29

Compression:

Stored size: 1.01 KB

Contents

# typed: false

# 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

29 entries across 29 versions & 1 rubygems

Version Path
sqreen-1.22.0-java lib/sqreen/prefix.rb
sqreen-1.22.0 lib/sqreen/prefix.rb
sqreen-1.21.1-java lib/sqreen/prefix.rb
sqreen-1.21.1 lib/sqreen/prefix.rb
sqreen-1.21.0-java lib/sqreen/prefix.rb
sqreen-1.21.0 lib/sqreen/prefix.rb
sqreen-1.20.4-java lib/sqreen/prefix.rb
sqreen-1.20.4 lib/sqreen/prefix.rb
sqreen-1.21.0.beta3-java lib/sqreen/prefix.rb
sqreen-1.21.0.beta3 lib/sqreen/prefix.rb
sqreen-1.20.4.beta1 lib/sqreen/prefix.rb
sqreen-1.19.4 lib/sqreen/prefix.rb
sqreen-1.20.3 lib/sqreen/prefix.rb
sqreen-1.20.2 lib/sqreen/prefix.rb
sqreen-1.21.0.beta2 lib/sqreen/prefix.rb
sqreen-1.21.0.beta1 lib/sqreen/prefix.rb
sqreen-1.20.1-java lib/sqreen/prefix.rb
sqreen-1.20.1 lib/sqreen/prefix.rb
sqreen-1.20.0-java lib/sqreen/prefix.rb
sqreen-1.20.0 lib/sqreen/prefix.rb