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