ipv6网段包含关系校验

function getV6Result (IPv6, compareIp) {
if (IPv6 == compareIp) {
return false
}
const ipAddr = IPv6.split(‘/’)
const compareIpAddr = compareIp.split(‘/’)
const compareMask = compareIpAddr[1]
const mask = ipAddr[1]
let ip = ipAddr[0].split(‘:’)
let compareIP = compareIpAddr[0].split(‘:’)
if (ip[ip.length - 1] == ‘’) {
ip[ip.length - 1] = ‘0000’
}
if (compareIP[compareIP.length - 1] == ‘’) {
compareIP[compareIP.length - 1] = ‘0000’
}
function getV6Mask () {
const maskMin = 128 - Math.min(mask, compareMask)
let maxNum = JSBI.subtract(JSBI.BigInt(Math.pow(2, 128)), JSBI.BigInt(1))
const minNum = JSBI.BigInt(maskMin)
return JSBI.leftShift(maxNum, minNum)
}

function getV6Ip (ips = []) {
// let len = ips.length
let offsetBit = 128
const initValue = 0
return ips.reduce((prev, current) => {
offsetBit -= 16
const a = current
const b = offsetBit
let currentValue = JSBI.BigInt(parseInt(a, 16))
let offsetBitValue = JSBI.BigInt(b)
let currentResult = JSBI.leftShift(currentValue, offsetBitValue)
return JSBI.bitwiseOr(prev, currentResult)
}, JSBI.BigInt(initValue))
}
let results = getV6Ip(ip)
let results2 = getV6Ip(compareIP)
const res1 = JSBI.bitwiseAnd(results, getV6Mask())
const res2 = JSBI.bitwiseAnd(results2, getV6Mask())
return JSBI.equal(res1, res2)
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值