...
// 驗證IP的正則
var ip_reg = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
// 驗證子網掩碼的正則
var mask_reg = /^(254|252|248|240|224|192|128|0)\.0\.0\.0|255\.(254|252|248|240|224|192|128|0)\.0\.0|255\.255\.(254|252|248|240|224|192|128|0)\.0|255\.255\.255\.(254|252|248|240|224|192|128|0)$/;
// IP轉二進位
function ip_to_binary(ip)
{
if (ip_reg.test(ip)) {
var ip_str = "",
ip_arr = ip.split(".");
for (var i = 0; i < 4; i++) {
curr_num = ip_arr[i];
number_bin = parseInt(curr_num);
number_bin = number_bin.toString(2);
count = 8 - number_bin.length;
for (var j = 0; j < count; j++) {
number_bin = "0" + number_bin;
}
ip_str += number_bin;
}
return ip_str;
}
return '';
}
// 二進位轉IP
function binary_to_ip(binary)
{
if (binary.length == 32) {
a = parseInt(binary.substr(0, 8), 2);
b = parseInt(binary.substr(8, 8), 2);
c = parseInt(binary.substr(16, 8), 2);
d = parseInt(binary.slice(-8), 2);
return a + '.' + b + '.' + c + '.' + d;
}
return '';
}
// 根據子網掩碼和網關計算出IP範圍(網路地址 - 廣播地址)
function get_network_broadcast_addr(mask, ip)
{
network_broadcast = [];
network_addr = "";
mask_arr = mask.split(".");
ip_arr = ip.split(".");
// 計算IP的網路地址 與(&)運算
for (var i = 0; i < 4; i++) {
number1 = parseInt(mask_arr[i]);
number2 = parseInt(ip_arr[i]);
network_addr += number1 & number2;
if( i < 3 ){
network_addr += ".";
}
}
network_broadcast.push(network_addr);
// 計算廣播地址
// 子掩碼後面有幾個0,就去掉IP地址後幾位再補1
mask_binary = ip_to_binary(mask);
gateway_binary = ip_to_binary(ip);
mask_zero = mask_binary.split(0).length - 1;
one_number = new Array(mask_zero + 1).join('1'); // IP地址後位補1
gateway_hou_wei_bu_yi = gateway_binary.slice(0, -mask_zero) + one_number;
network_broadcast.push(binary_to_ip(gateway_hou_wei_bu_yi));
return network_broadcast;
}
// 全排列組合演算法(兩兩遞歸組合)
function doExchange(doubleArrays)
{
var len = doubleArrays.length;
if(len >= 2){
var len1 = doubleArrays[0].length;
var len2 = doubleArrays[1].length;
var newlen = len1 * len2;
var temp = new Array(newlen);
var index = 0;
for(var i = 0; i < len1; i++){
for(var j = 0; j < len2; j++){
temp[index] = doubleArrays[0][i] + '.' + doubleArrays[1][j];
index++;
}
}
var newArray = new Array(len - 1);
for(var i = 2; i < len; i++){
newArray[i - 1] = doubleArrays[i];
}
newArray[0] = temp;
return doExchange(newArray);
} else{
return doubleArrays[0];
}
}
// 返回所有IP
function return_ip(network_addr, broadcast_addr, gateway)
{
range = [];
start = network_addr.split(".");
end = broadcast.split(".");
// range格式為[[192], [168], [0,1,2...254], [0,1,2...254]]
for (var i = 0; i < 4; i++) {
if (start[i] == end[i]) {
range[i] = [start[i]];
} else {
min = Math.min(start[i], end[i]);
max = Math.max(start[i], end[i]);
temp = [];
for (var j = min; j <= max; j++) {
temp.push(j);
}
range[i] = temp;
}
}
ip_list = doExchange(range);
ip_list.shift(); // 去掉網路地址
ip_list.pop(); // 去掉廣播地址
gateway_index = -1;
// 去掉網關
for (var k = 0; k < ip_list.length; k++) {
if (ip_list[k] == gateway) {
gateway_index = k;
break;
}
}
if (gateway_index > -1) {
ip_list.splice(gateway_index, 1);
}
return ip_list;
}