使用JavaScript如何验证IP地址“x.x.x.x”是有效的IPV4单播地址,例如不是0.0.0.0或多播(224.0.0.0到224.0.0.255,224.0.1.0到238.255.255.255,239.0.0.0到239.255.255.255)?
答案 0 :(得分:6)
首先你需要把它变成一个数字,使用这个函数: -
function IPToNumber(s)
{
var arr = s.split(".");
var n = 0
for (var i = 0; i < 4; i++)
{
n = n * 256
n += parseInt(arr[i],10)
}
return n;
}
看着你的规格,虽然你似乎列出了一系列范围,但这些范围似乎与我相邻,可以简化为(224.0.0.0到239.255.255.255)。因此,您可以测试: -
var min = IPToNumber("224.0.0.0");
var max = IPToNumber("239.255.255.255");
var ipNum = IPToNumber(sTestIP);
var isValid = (ipNum != 0 && (ipNum < min || ipNum > max))
当然注意,如果不了解目的地子网,则无法判断该地址是该子网的网络地址还是广播地址。
答案 1 :(得分:3)
您所看到的虚线四边形符号就是这样:符号使人眼更容易。 IP地址实际上是32位整数。我建议你将你的地址转换为那个整数,然后检查它是否在有效范围内(即第一个要求为> 0)。
为此目的:
function dottedQuadToInt(ip)
{
var parts = ip.split('.', 4);
if(parts.length < 4) return -1;
var result = 0, base = 1;
for (var i = 3;i>=0;i--)
{
//validation
if (parts[i].length == 0 || parts[i].length > 3) return -1;
var segment = parseInt(parts[i],10);
if (isNaN(segment) || segment<0 || segment > 255) return -1;
//compute next segment
result += base * segment;
base = base << 8;
}
return result;
}
然后:
function isValidIP(ip)
{
ip = dottedQuadToInt(ip);
if (ip <= 0) return false;
//mulitcast range:
if (ip >= 3758096384 && ip <= 4026531839) return false;
//alternate way to check multicast (slower, but more readable):
if (ip >= dottedQuadToInt('224.0.0.0') && ip <= dottedQuadToInt('239.255.255.255')) return false;
return true;
}
答案 2 :(得分:2)
/**
* Converts an IPv4 address to a (signed) 32-bit integer.
*/
function parse_ipv4_address(str) {
var arr = str.split('.');
for (var i = 0, val = 0; i < 4; i++) {
val += parseInt(arr[i], 10) << (8 * (3 - i));
}
return val;
}
var min = parse_ipv4_address('224.0.0.0');
var max = parse_ipv4_address('239.255.255.255');
var myIp = parse_ipv4_address(myIpStr);
// because the values are signed ints, min and max will be
// negative and we need to reverse the comparison operators :(
if (myIp == 0 || (myIp <= min && myIp >= max)) {
// multicast!
}
答案 3 :(得分:2)
多播IP地址定义为1110
高位四位的地址(请参阅http://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtml和http://tools.ietf.org/html/rfc1112)。在十六进制中,1110
表示为0xE
(十进制值为14,记住每个十六进制数字值4个二进制位),所以如果该值是高位4位32位整数(大端) )十六进制将是0xE0000000
。如果您首先将IP地址转换为32位数字,然后&
将其转换为0xF0000000
,如果结果值等于0xE0000000
(1110
高位然后它是一个多播IPv4地址(在Java Inet4Address.isMulticastAddress()中使用相同的逻辑)。
var ipNum = IPToNumber(sTestIP);
var isMulticast = ((ipNum & 0xF0000000) == 0xE0000000);
这是一个证明这一点的表(注意十六进制格式的有效mutlicast IP地址都以0xE
开头,int值也是32位数字的符号):
// not a multicast, starts with 0xD
ip [223.000.000.000] hex [0xDF000000] int [-553648128] unsigned [3741319168]
ip [223.255.255.255] hex [0xDFFFFFFF] int [-536870913] unsigned [3758096383]
// is a multicast, starts with 0xE
ip [224.000.000.000] hex [0xE0000000] int [-536870912] unsigned [3758096384]
ip [224.255.255.255] hex [0xE0FFFFFF] int [-520093697] unsigned [3774873599]
ip [225.000.000.000] hex [0xE1000000] int [-520093696] unsigned [3774873600]
ip [225.255.255.255] hex [0xE1FFFFFF] int [-503316481] unsigned [3791650815]
ip [226.000.000.000] hex [0xE2000000] int [-503316480] unsigned [3791650816]
ip [226.255.255.255] hex [0xE2FFFFFF] int [-486539265] unsigned [3808428031]
ip [227.000.000.000] hex [0xE3000000] int [-486539264] unsigned [3808428032]
ip [227.255.255.255] hex [0xE3FFFFFF] int [-469762049] unsigned [3825205247]
ip [228.000.000.000] hex [0xE4000000] int [-469762048] unsigned [3825205248]
ip [228.255.255.255] hex [0xE4FFFFFF] int [-452984833] unsigned [3841982463]
ip [229.000.000.000] hex [0xE5000000] int [-452984832] unsigned [3841982464]
ip [229.255.255.255] hex [0xE5FFFFFF] int [-436207617] unsigned [3858759679]
ip [230.000.000.000] hex [0xE6000000] int [-436207616] unsigned [3858759680]
ip [230.255.255.255] hex [0xE6FFFFFF] int [-419430401] unsigned [3875536895]
ip [231.000.000.000] hex [0xE7000000] int [-419430400] unsigned [3875536896]
ip [231.255.255.255] hex [0xE7FFFFFF] int [-402653185] unsigned [3892314111]
ip [232.000.000.000] hex [0xE8000000] int [-402653184] unsigned [3892314112]
ip [232.255.255.255] hex [0xE8FFFFFF] int [-385875969] unsigned [3909091327]
ip [233.000.000.000] hex [0xE9000000] int [-385875968] unsigned [3909091328]
ip [233.255.255.255] hex [0xE9FFFFFF] int [-369098753] unsigned [3925868543]
ip [234.000.000.000] hex [0xEA000000] int [-369098752] unsigned [3925868544]
ip [234.255.255.255] hex [0xEAFFFFFF] int [-352321537] unsigned [3942645759]
ip [235.000.000.000] hex [0xEB000000] int [-352321536] unsigned [3942645760]
ip [235.255.255.255] hex [0xEBFFFFFF] int [-335544321] unsigned [3959422975]
ip [236.000.000.000] hex [0xEC000000] int [-335544320] unsigned [3959422976]
ip [236.255.255.255] hex [0xECFFFFFF] int [-318767105] unsigned [3976200191]
ip [237.000.000.000] hex [0xED000000] int [-318767104] unsigned [3976200192]
ip [237.255.255.255] hex [0xEDFFFFFF] int [-301989889] unsigned [3992977407]
ip [238.000.000.000] hex [0xEE000000] int [-301989888] unsigned [3992977408]
ip [238.255.255.255] hex [0xEEFFFFFF] int [-285212673] unsigned [4009754623]
ip [239.000.000.000] hex [0xEF000000] int [-285212672] unsigned [4009754624]
ip [239.255.255.255] hex [0xEFFFFFFF] int [-268435457] unsigned [4026531839]
// not a multicast, starts with 0xF
ip [240.000.000.000] hex [0xF0000000] int [-268435456] unsigned [4026531840]
ip [240.255.255.255] hex [0xF0FFFFFF] int [-251658241] unsigned [4043309055]