2012-12-03 11 views
5

Vorrei ottenere gli IP massimi e minimi da un blocco CIDR. Il mio codice corrente funziona correttamente, ad eccezione di alcuni IP che restituiscono numeri negativi sul numero minimo. Di seguito è riportato il mio codice e l'output esistenti.Recupera gli IP max/min dall'intervallo CIDR

function long2ip (proper_address) { 
// Converts an (IPv4) Internet network address into a string in Internet standard dotted format 
// 
// version: 1109.2015 
// discuss at: http://phpjs.org/functions/long2ip 
// + original by: Waldo Malqui Silva 
// *  example 1: long2ip(3221234342); 
// *  returns 1: '192.0.34.166' 
var output = false; 
if (!isNaN(proper_address) && (proper_address >= 0 || proper_address <= 4294967295)) { 
    output = Math.floor(proper_address/Math.pow(256, 3)) + '.' + 
    Math.floor((proper_address % Math.pow(256, 3))/Math.pow(256, 2)) + '.' + 
    Math.floor(((proper_address % Math.pow(256, 3)) % Math.pow(256, 2))/Math.pow(256, 1)) + '.' + 
    Math.floor((((proper_address % Math.pow(256, 3)) % Math.pow(256, 2)) % Math.pow(256, 1))/Math.pow(256, 0)); 
} 
return output; 
} 

function cidrToRange(cidr) { 
var range = [2]; 
cidr = cidr.split('/'); 
var longIp = ip2long(cidr[0]); 
var mask = ((-1 << (32 - cidr[1]))); 
var longIp = ip2long(cidr[0]); 
range[0] = long2ip(longIp & ((-1 << (32 - cidr[1])))); 
range[1] = long2ip(longIp + Math.pow(2, (32 - cidr[1])) - 1); 
return range; 
} 



console.log(cidrToRange('157.60.0.0/16')); // returns [ '-99.-196.0.0', '157.60.255.255' ] 
console.log(cidrToRange('157.56.0.0/14')); // returns [ '-99.-200.0.0', '157.59.255.255' ] 
console.log(cidrToRange('127.0.0.1/8')); // returns [ '127.0.0.0', '128.0.0.0' ] 

Sto testando in node.js ma non riesco a ottenere gli intervalli sopra indicati per funzionare come vorrei. Qualsiasi aiuto è apprezzato.

Mark

+0

hai mai immaginato questo? –

risposta

2

Rispondendo a "Carter Cole" Ho risolto il problema cambiando la funzione cidrToRange ad avere un diverso punto di partenza.

function cidrToRange(cidr) { 
    var range = [2]; 
    cidr = cidr.split('/'); 
    var start = ip2long(cidr[0]); 
    range[0] = long2ip(start); 
    range[1] = long2ip(Math.pow(2, 32 - cidr[1]) + start - 1); 
    return range; 
} 

che ora ritorna:

[ '157.60.0.0', '157.60.255.255' ] 
[ '157.56.0.0', '157.59.255.255' ] 
[ '127.0.0.1', '128.0.0.0' ] 
4

Proprio per il riferimento di questo è corretto codice cidrToRange in javascript:

function cidrToRange(cidr) { 
    var range = [2]; 
    cidr = cidr.split('/'); 
    var cidr_1 = parseInt(cidr[1]) 
    range[0] = long2ip((ip2long(cidr[0])) & ((-1 << (32 - cidr_1)))); 
    start = ip2long(range[0]) 
    range[1] = long2ip(start + Math.pow(2, (32 - cidr_1)) - 1); 
    return range; 
} 

Per esempio

cidrToRange ('192.168.62.156/ 27 ') dà ["192.168.62.128", "192.168.62.159"] e questo è esattamente ciò che bisogno.

+0

Nota: questa è la versione corretta. Quello di @ mark-willis fallisce per 10.1.0.0/12. Restituisce ["10.1.0.0", "10.16.255.255"], mentre dovrebbe restituire ["10.0.0.0", "10.15.255.255"] – Andy

Problemi correlati