1) Basic Idea: Determine whether two IP addresses are in the same CIDR block, and separate the IP addresses with the subnet mask. The result is the network number. If the network number is the same, it is in the same subnet. Otherwise, it is not in the same subnet. 2) Implementation: *** [isEqualIPAddres 1) Basic Idea: Determine whether two IP addresses are in the same CIDR block, and perform and calculate the IP addresses and subnet masks respectively, the result is the network number. If the network number is the same, it is in the same subnet. Otherwise, it is not in the same subnet. 2) specific implementation:
/*** [IsEqualIPAddress determines whether two IP addresses are in the same CIDR block] * @ param {[String]} addr1 [address 1] * @ param {[String]} addr2 [address 2] * @ param {[String]} mask [subnet mask] * @ return {Boolean} [true or false] */function isEqualIPAddress (addr1, addr2, mask) {if (! Addr1 |! Addr2 |! Mask) {console. log ("parameters cannot be blank"); return false;} var res1 = [], res2 = []; addr1 = addr1.split (". "); addr2 = addr2.split (". "); mask = mask. split (". "); for (var I = 0, ilen = addr1.length; I <ilen; I + = 1) {res1.push (parseInt (addr1 [I]) & parseInt (mask [I]); res2.push (parseInt (addr2 [I]) & parseInt (mask [I]);} if (res1.join (". ") = res2.join (". ") {console. log ("in the same network segment"); return true;} else {console. log ("not in the same network segment"); return false ;}}