2018-09-09 18:35:27 +02:00
|
|
|
|
|
|
|
import bigInt from 'big-integer'
|
|
|
|
|
|
|
|
|
|
|
|
export function IPv6ToNumeric(addr) {
|
|
|
|
const parts = addr.split(":"); // let's se what we can do about the :: expansion
|
|
|
|
let expanded = [];
|
|
|
|
|
2018-09-22 12:11:05 +02:00
|
|
|
for (const p of parts) {
|
|
|
|
if (p === "") { continue; }
|
2018-09-09 18:35:27 +02:00
|
|
|
let binary = parseInt(p, 16).toString(2); // Convert to binary
|
|
|
|
while (binary.length < 16) {
|
2018-09-22 12:11:05 +02:00
|
|
|
binary = "0" + binary; // leftpad
|
2018-09-09 18:35:27 +02:00
|
|
|
}
|
|
|
|
expanded.push(binary);
|
|
|
|
}
|
|
|
|
return bigInt(expanded.join(""), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function IPv4ToNumeric(addr) {
|
|
|
|
const octets = addr.split('.');
|
|
|
|
return parseInt(octets[0], 10) * 16777216 + // 256^3
|
|
|
|
parseInt(octets[1], 10) * 65536 + // 256^2
|
|
|
|
parseInt(octets[2], 10) * 256 + // 256^1
|
|
|
|
parseInt(octets[3], 10); // 256^0
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ipToNumeric(addr) {
|
|
|
|
if (addr.includes(":")) {
|
|
|
|
return IPv6ToNumeric(addr);
|
|
|
|
}
|
|
|
|
return IPv4ToNumeric(addr);
|
|
|
|
}
|
|
|
|
|