In my earlier blog I put a set of tools together for IPv4 address manipulation for sqlite,oracle and even Excel. How can I forget the simple tools needed in Javascript. Changing IP address to long integer, subnet mask to IP address range, have all become a necessity to daily security operations people. So here is what I have cooked up.
// first convert your IP address to long integer
// first convert your IP address to long integer
function ip2long(ip) {
var ipa=ip.split('.').reverse();
var long=0;
for(var i=0;i<ipa.length;i++) long=long+ipa[i]*Math.pow(2,i*8)
return long;
}
//Coveert it back
function long2ip(long) {
if(long < 1) return '';
return [long >>> 24, long >>> 16 & 0xFF, long >>> 8 & 0xFF, long & 0xFF].join('.');
}
// convert a subnet like 192.168.11.24/16 to a IP integer address range or 192.168.11.0/255.255.255.0
function subnet2range(subnet) {
var arr=subnet.split("/");
var ip=arr[0]
var sub=32;
if(arr.length == 2) {
var match = arr[1].match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/);
if((match) && (match.length == 5))
sub=32-Math.log(ip2long('255.255.255.255')-ip2long(arr[1])+1)/Math.LN2
else if (parseInt(arr[1]))
sub=parseInt(arr[1])
}
sub=32-sub;
return [((ip2long(ip) >>> (sub))*Math.pow(2,sub)),((ip2long(ip) >>> (sub))*Math.pow(2,sub)+Math.pow(2,sub)-1)]
}
// convert a range to subnet back
function range2subnet(range) {
var diff=range[1]-range[0]+1;
var sub=32-Math.log(diff)/Math.LN2
return long2ip(range[0])+"/"+sub
}
Let the browser do the work!
Harrah's Philadelphia Casino and Racetrack - Mapyro
ReplyDeleteHarrah's 인천광역 출장마사지 Philadelphia Casino and Racetrack · Casino · Race 포항 출장샵 & 의정부 출장안마 Sportsbook · Casino at Harrah's 보령 출장안마 Philadelphia in Chester · Casino & Racetrack · 안양 출장샵 Harrah's Philadelphia