如何在Javascript中检测给定IP是否在给定子网中?

时间:2021-07-10 07:20:35

I'd like to write a function in Javascript along the lines of:

我想在Javascript中编写一个函数:

in_subnet(ip, network, slash) {
    ...
}

in_subnet('1.2.3.4', '1.2.0.0', 16) # True, since it's in 1.2.0.0/16
in_subnet('1.2.3.4', '1.2.0.0', 24) # False, since it's not in 1.2.0.0/24

Should I write it from scratch, or are there some good libraries I could use? Or is the entire function already written and in the public domain?

我应该从头开始编写,还是可以使用一些好的库?或者整个功能是否已经编写并在公共领域?

3 个解决方案

#1


Pseudocode, building on dustin's answer:

伪代码,基于dustin的答案:

addr_one = ip_addr_to_integer(ip);
addr_two = ip_addr_to_integer(network);
mask = ((1 << (32-slash)) - 1) ^ 0xFFFFFFFF;
return (((addr_one ^ addr_two) & mask) == 0);

If you know that the IP address strings are valid, you can probably use string.split('.') to get the four octets and easily convert them to an integer.

如果您知道IP地址字符串是有效的,您可以使用string.split('。')来获取四个八位字节并轻松将它们转换为整数。

#2


Go ahead and learn it. :)

继续学习吧。 :)

The trick is to parse '1.2.3.4' into a single integer ( (1 << 24 | 2 << 16 | 3 << 8 | 4) == 16909060), and then '1.2.0.0' as an integer (16908288) and then the mask as the number of 1 bits from the msb as an integer (16 bits == 4294901760) and then apply the mask with bitwise and to the original address ((16909060 & 4294901760) == 16908288) if the masked address == the network address, then it's in the network.

诀窍是将'1.2.3.4'解析为单个整数((1 << 24 | 2 << 16 | 3 << 8 | 4)== 16909060),然后将'1.2.0.0'解析为整数(16908288) )然后将掩码作为整数(16位== 4294901760)中msb的1位数,然后按位并应用掩码并将其应用于原始地址((16909060和4294901760)== 16908288)如果掩码地址==网络地址,然后它在网络中。

#3


addr_one = ip_addr_to_integer(ip >> (32 - slash));
addr_two = ip_addr_to_integer(network >> (32 - slash));
return (addr_one == addr_two);

Bitwise operators stop working with 32 bits.

按位运算符停止使用32位。

#1


Pseudocode, building on dustin's answer:

伪代码,基于dustin的答案:

addr_one = ip_addr_to_integer(ip);
addr_two = ip_addr_to_integer(network);
mask = ((1 << (32-slash)) - 1) ^ 0xFFFFFFFF;
return (((addr_one ^ addr_two) & mask) == 0);

If you know that the IP address strings are valid, you can probably use string.split('.') to get the four octets and easily convert them to an integer.

如果您知道IP地址字符串是有效的,您可以使用string.split('。')来获取四个八位字节并轻松将它们转换为整数。

#2


Go ahead and learn it. :)

继续学习吧。 :)

The trick is to parse '1.2.3.4' into a single integer ( (1 << 24 | 2 << 16 | 3 << 8 | 4) == 16909060), and then '1.2.0.0' as an integer (16908288) and then the mask as the number of 1 bits from the msb as an integer (16 bits == 4294901760) and then apply the mask with bitwise and to the original address ((16909060 & 4294901760) == 16908288) if the masked address == the network address, then it's in the network.

诀窍是将'1.2.3.4'解析为单个整数((1 << 24 | 2 << 16 | 3 << 8 | 4)== 16909060),然后将'1.2.0.0'解析为整数(16908288) )然后将掩码作为整数(16位== 4294901760)中msb的1位数,然后按位并应用掩码并将其应用于原始地址((16909060和4294901760)== 16908288)如果掩码地址==网络地址,然后它在网络中。

#3


addr_one = ip_addr_to_integer(ip >> (32 - slash));
addr_two = ip_addr_to_integer(network >> (32 - slash));
return (addr_one == addr_two);

Bitwise operators stop working with 32 bits.

按位运算符停止使用32位。