2009-10-01 134 views
3

使用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)?驗證IP地址不是0.0.0.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; 
} 

看着你SPEC,即可以簡化爲(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

您在使用'*'而不是按位運算符意味着你的數字不應該在任何時候轉換爲'Int32' - 聰明! – 2009-10-01 13:48:41

+4

小心這樣的,這是有效的:70.16.18.015。您的parseInt調用可能不會將所有內容都視爲十進制。 – 2009-10-01 14:00:32

+1

使用0.0.0.015進行快速測試證實了該錯誤。我修復了你的代碼。 – 2009-10-01 14:06:09

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; 
} 
+0

<< 8相當於* 256,再看看我的for循環。 – 2009-10-01 13:46:47

+0

哦,是的,我明白了,我沒有發現你正在轉移基地而不是價值本身。 – AnthonyWJones 2009-10-01 13:50:01

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! 
} 
+0

我喜歡我們中的3人發佈了幾乎完全相同的東西。我會投你們倆:) – 2009-10-01 13:52:37

2

多播IP地址被定義爲那些具有1110作爲其高序4位(見http://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtmlhttp://tools.ietf.org/html/rfc1112) 。在十六進制中,1110表示爲0xE(十進制值爲14,記住每個十六進制數字值爲4個二進制位),所以如果此值位於32位整數(大端)的高位4位,則十六進制將爲0xE0000000。如果首先將IP地址轉換爲32位數,然後使用&將其與0xF0000000相結合,如果結果值等於0xE00000001110位於高位4位),則它是一個多播IPv4地址(與Java中使用的邏輯相同Inet4Address.isMulticastAddress())。

var ipNum = IPToNumber(sTestIP); 
var isMulticast = ((ipNum & 0xF0000000) == 0xE0000000); 

這裏有一個表來證明這一點(注意十六進制格式有效mutlicast IP地址都開始0xE,也整型值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] 
+0

到目前爲止最好的答案。以最簡單的方式直接將規範轉換爲代碼。 – 2013-06-06 20:56:44