export function isCidrOverlap (val1, val2) {
if (!val1 || !val2) {
return false
}
let ip1 = val1.split(‘/’)[0]
let mask1 = val1.split(‘/’)[1]
let ip2 = val2.split(‘/’)[0]
let mask2 = val2.split(‘/’)[1]
// 根据小的掩码获取高位为1的二进制数
let mask = 0xFFFFFFFF << (32 - Math.min(mask1, mask2))
let ip1Arr = ip1.split(‘.’)
let ip2Arr = ip2.split(‘.’)
// ip地址转化为二进制数
let ip1Addr = (ip1Arr[0] << 24) | (ip1Arr[1] << 16) | (ip1Arr[2] << 8) | ip1Arr[3]
let ip2Addr = (ip2Arr[0] << 24) | (ip2Arr[1] << 16) | (ip2Arr[2] << 8) | ip2Arr[3]
// 将ip地址的二进制数和最小掩码做与运算,如果相同,表示ip有重合
return (ip1Addr & mask) === (ip2Addr & mask)
}
ipv4网段包含关系校验
最新推荐文章于 2024-08-31 15:53:48 发布