IPアドレスを計算する
7159 ワード
/*
*
* :
* :[email protected]
* :V1.0
* : JavaScript
* QQ: 929168233
* :2012-8-17
*
*/
var index = 0;
function MyUtil()
{
//
this.getNetWorkAddress = GetNetWorkAddress;
//
this.getBroadCaseAddress = GetBroadCaseAddress;
//
this.getAllValidateHosts = GetAllValidateHosts;
//
this.desc = desc;
}
//
function GetNetWorkAddress(ip, netmask)
{
// IP , , : var ipArray = ["192", "128", "1", "1"];
var ipArray = ip.toString().split(".");
// netmask , , : var netmaskArray = ["255", "255", "255", "0"];
var netmaskArray = netmask.toString().split(".");
// ,
var networkAddress = "";
// , : =IP &netmask
for (var i=0; i<ipArray.length; i++)
{
//if : xxx.xxx.xxx.xxx
// “.” 。 , :xxx.xxx.xxx.xxx.
// 。
if ( i == (ipArray.length-1) )
{
// IP netmask , networkAddress 。
networkAddress += ( (ipArray[i]) & parseInt(netmaskArray[i]) );
}
else
{
// IP netmask , networkAddress 。
networkAddress += ( (ipArray[i]) & parseInt(netmaskArray[i]) ) + ".";
}
}
//
return ( networkAddress );
}
function GetBroadCaseAddress(ip, netmask)
{
// netmask , , : var netmaskArray = ["255", "255", "255", "0"];
var netmaskArray = netmask.toString().split(".") //255, 255, 255, 0
// netmask , :
// ["11111111", "11111111", "11111111", "00000000"]
var netmaskBinArray = new Array();
// network , , : var networkArray = ["192", "168", "1", "0"];
var networkArray = GetNetWorkAddress(ip, netmask).toString().split(".");
// network 。 :
// ["11111111", "11111111", "11111111", "00000000"]
var networkBinArray = new Array();
// ,
var tmp_string = "";
// ,
var tmp_array = [0,0,0,0,0,0,0,0];
//netmask
var netmaskString = "";
//network
var networkString = "";
//
var broadcaseArray = new Array();
// ,
var broadcaseString = "";
// , 32 4 , 8
var counter = 0;
//
netmaskString = "";
var tmptmp = "";
tmp_array = [0,0,0,0,0,0,0,0];
for (var i=0; i<netmaskArray.length; i++)
{
// i netmask
// netmask
// : ["1", "1", "1", "1", "1", "1", "1", "1"]
tmp_string = parseInt(netmaskArray[i]).toString(2).split("");
// , 0 , ,
while ( tmp_string.length != 8 )
{
for (var qq=8-tmp_string.length, j=0; qq<8 && j< tmp_string.length; qq++, j++)
{
tmp_array[qq] = tmp_string[j];
}
tmp_string = tmp_array;
}
if (i == netmaskArray.length -1)
{
netmaskString += tmp_string;
}
else
{
netmaskString += tmp_string + ",";
}
}
// netmaskString netmask ,
netmaskBinArray = netmaskString.split(",");
// 0
var indexOfZeroInnetmaskBinArray = netmaskBinArray.indexOf("0", 0);
index = (32 - indexOfZeroInnetmaskBinArray);
// ,
tmp_array = [0,0,0,0,0,0,0,0];
for (var i=0; i<networkArray.length; i++)
{
// network i
tmp_string = parseInt(networkArray[i]).toString(2).split("");
// , 0 , ,
while ( tmp_string.length != 8 )
{
for (var qq=8-tmp_string.length, j=0; qq<8 && j< tmp_string.length; qq++, j++)
{
tmp_array[qq] = tmp_string[j];
}
tmp_string = tmp_array;
}
if (i == networkArray.length -1)
{
networkString += tmp_string
}
else
{
networkString += tmp_string + ",";
}
}
// network network
networkBinArray = networkString.split(",");
// : , 1, 。
// 1
for (var n=indexOfZeroInnetmaskBinArray; n<networkBinArray.length; n++)
{
networkBinArray[n] = "1";
}
// network (32 ) 4 , 8 , broadcaseArray 。
counter = 0;
tmp_string = "";
for (var kk=0; kk<networkBinArray.length; kk++)
{
counter++;
tmp_string += networkBinArray[kk];
if ( counter % 8 == 0 )
{
broadcaseArray[(counter / 8) - 1] = tmp_string;
tmp_string = "";
}
}
// broadcaseString
broadcaseString = "";
// broadcaseArray 4 , broadcaseString
broadcaseString += BinaryToTen(broadcaseArray[0]) + ".";
broadcaseString += BinaryToTen(broadcaseArray[1]) + ".";
broadcaseString += BinaryToTen(broadcaseArray[2]) + ".";
broadcaseString += BinaryToTen(broadcaseArray[3]);
//
return (broadcaseString);
}
// ,
function BinaryToTen(bin)
{
var orignaly = bin.toString().split("");
var result = new Number();
if ( bin != "" )
{
for (var i=orignaly.length-1, j=0; i>=0 && j<orignaly.length; i--, j++)
{
result += parseInt(orignaly[j]) * Math.pow(2, i) ;
}
return result;
}
}
// IP
// : +1 , -1
function GetAllValidateHosts(ip, netmask)
{
var networkAddress = GetNetWorkAddress(ip, netmask).toString().split(".");
var broadcaseAddress = GetBroadCaseAddress(ip, netmask).toString().split(".");
//alert("net = " + networkAddress + "<br />" + " broad = " + broadcaseAddress);
var startHost = ( parseInt(networkAddress[3]) + 1 );
var endHost = ( parseInt(broadcaseAddress[3]) -1);
var startString = "";
var endString = "";
startString += networkAddress[0] + ".";
startString += networkAddress[1] + ".";
startString += networkAddress[2] + ".";
startString += startHost;
endString += broadcaseAddress[0] + ".";
endString += broadcaseAddress[1] + ".";
endString += broadcaseAddress[2] + ".";
endString += endHost;
return (startString + "--" + endString);
}
//
// : =2 -2
function desc(ip, netmask)
{
var string = "IP :" + ip + "<br />";
string += " :" + netmask + "<br />";
string += " :" + ( GetNetWorkAddress(ip, netmask) ) + "<br />";
string += " :" + GetBroadCaseAddress(ip, netmask) + "<br />";
string += " :" + GetAllValidateHosts(ip, netmask) + "<br />";
string += " :" + ( Math.pow(2, index) - 2 ) + " <br />";
return string;
}