Last active
August 29, 2015 14:02
-
-
Save aa65535/909ed5431d9be2070cb0 to your computer and use it in GitHub Desktop.
IP计算
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ip2long(IP) { | |
var i = 0; | |
IP = IP.match(/^([1-9]\d*|0[0-7]*|0x[\da-f]+)(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?$/i); | |
if (!IP) { | |
return false; | |
} | |
IP[0] = 0; | |
for (i = 1; i < 5; i += 1) { | |
IP[0] += !!((IP[i] || '').length); | |
IP[i] = parseInt(IP[i]) || 0; | |
} | |
IP.push(256, 256, 256, 256); | |
IP[4 + IP[0]] *= Math.pow(256, 4 - IP[0]); | |
if (IP[1] >= IP[5] || IP[2] >= IP[6] || IP[3] >= IP[7] || IP[4] >= IP[8]) { | |
return false; | |
} | |
return IP[1] * (IP[0] === 1 || 16777216) + IP[2] * (IP[0] <= 2 || 65536) + IP[3] * (IP[0] <= 3 || 256) + IP[4] * 1; | |
} | |
function calcMask(range) { | |
var a, b, c; | |
a = range.split('-'); | |
b = ip2long(a[1]) - ip2long(a[0]); | |
c = { | |
255: 24, | |
511: 23, | |
1023: 22, | |
2047: 21, | |
4095: 20, | |
8191: 19, | |
16383: 18, | |
32767: 17, | |
65535: 16, | |
131071: 15, | |
262143: 14, | |
524287: 13, | |
1048575: 12, | |
2097151: 11, | |
4194303: 10, | |
8388607: 9, | |
16777215: 8 | |
}; | |
return a[0] + '/' + c[b]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calcRange(s) { | |
var o, a, m, t; | |
function str(o) { | |
var s = ''; | |
if (o.n === 0) { | |
return 'Error'; | |
} | |
if (o.n === 1) { | |
return 'IP: ' + o.b.join('.'); | |
} | |
return 'IP: ' + o.b.join('.') + ' ~ ' + o.e.join('.') + '; Count: ' + o.n; | |
} | |
o = {}; | |
a = s.split('.'); | |
t = a[3].split('/'); | |
a[0] = parseInt(a[0], 10); | |
a[1] = parseInt(a[1], 10); | |
a[2] = parseInt(a[2], 10); | |
a[3] = parseInt(t[0], 10); | |
t = parseInt(t[1], 10); | |
o.n = 0; | |
if (isNaN(t) || t > 32 || t < 0) { | |
return str(o); | |
} | |
m = (function(n) { | |
var o; | |
o = [0, 0, 0, 0]; | |
function left(n) { | |
if (n >= 8) { | |
return 255; | |
} | |
b = 0xff00; | |
while (n > 0) { | |
b = b >> 1; | |
n--; | |
} | |
return (b & 0xff); | |
}; | |
if (n >= 8) { | |
o[0] = 255; | |
n -= 8; | |
} else { | |
o[0] = left(n); | |
return o; | |
} | |
if (n >= 8) { | |
o[1] = 255; | |
n -= 8; | |
} else { | |
o[1] = left(n); | |
return o; | |
} | |
if (n >= 8) { | |
o[2] = 255; | |
n -= 8; | |
} else { | |
o[2] = left(n); | |
return o; | |
} | |
o[3] = left(n); | |
return o; | |
} (t)); | |
o.b = []; | |
if (t === 32) { | |
o.n = 1; | |
o.b[0] = a[0]; | |
o.b[1] = a[1]; | |
o.b[2] = a[2]; | |
o.b[3] = a[3]; | |
return str(o); | |
} | |
o.e = []; | |
if (t === 31) { | |
o.n = 2; | |
o.b[0] = a[0] & m[0]; | |
o.b[1] = a[1] & m[1]; | |
o.b[2] = a[2] & m[2]; | |
o.b[3] = a[3] & m[3]; | |
o.e[0] = a[0] | (~m[0] & 0xff); | |
o.e[1] = a[1] | (~m[1] & 0xff); | |
o.e[2] = a[2] | (~m[2] & 0xff); | |
o.e[3] = a[3] | (~m[3] & 0xff); | |
return str(o); | |
} | |
o.n = Math.pow(2, 32 - t) - 2; | |
o.b[0] = a[0] & m[0]; | |
o.b[1] = a[1] & m[1]; | |
o.b[2] = a[2] & m[2]; | |
o.b[3] = parseInt(a[3] & m[3]) + 1; | |
o.e[0] = a[0] | (~m[0] & 0xff); | |
o.e[1] = a[1] | (~m[1] & 0xff); | |
o.e[2] = a[2] | (~m[2] & 0xff); | |
o.e[3] = parseInt(a[3] | (~m[3] & 0xff)) - 1; | |
return str(o); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment