Skip to content

Commit

Permalink
lib: add back support for Node.js 6
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed May 10, 2022
1 parent af82ef4 commit 1a4ca35
Showing 1 changed file with 57 additions and 52 deletions.
109 changes: 57 additions & 52 deletions lib/ip.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
const ip = exports;
const { Buffer } = require('buffer');
const os = require('os');
var ip = exports;
var { Buffer } = require('buffer');
var os = require('os');

ip.toBuffer = function (ip, buff, offset) {
offset = ~~offset;

let result;
var result;

if (this.isV4Format(ip)) {
result = buff || new Buffer(offset + 4);
ip.split(/\./g).map((byte) => {
result[offset++] = parseInt(byte, 10) & 0xff;
});
} else if (this.isV6Format(ip)) {
const sections = ip.split(':', 8);
var sections = ip.split(':', 8);

let i;
var i;
for (i = 0; i < sections.length; i++) {
const isv4 = this.isV4Format(sections[i]);
let v4Buffer;
var isv4 = this.isV4Format(sections[i]);
var v4Buffer;

if (isv4) {
v4Buffer = this.toBuffer(sections[i]);
Expand All @@ -36,16 +36,16 @@ ip.toBuffer = function (ip, buff, offset) {
while (sections.length < 8) sections.push('0');
} else if (sections.length < 8) {
for (i = 0; i < sections.length && sections[i] !== ''; i++);
const argv = [i, 1];
var argv = [i, 1];
for (i = 9 - sections.length; i > 0; i--) {
argv.push('0');
}
sections.splice(...argv);
sections.splice.apply(sections, argv);
}

result = buff || new Buffer(offset + 16);
for (i = 0; i < sections.length; i++) {
const word = parseInt(sections[i], 16);
var word = parseInt(sections[i], 16);
result[offset++] = (word >> 8) & 0xff;
result[offset++] = word & 0xff;
}
Expand All @@ -62,16 +62,17 @@ ip.toString = function (buff, offset, length) {
offset = ~~offset;
length = length || (buff.length - offset);

let result = [];
var result = [];
var i;
if (length === 4) {
// IPv4
for (let i = 0; i < length; i++) {
for (i = 0; i < length; i++) {
result.push(buff[offset + i]);
}
result = result.join('.');
} else if (length === 16) {
// IPv6
for (let i = 0; i < length; i += 2) {
for (i = 0; i < length; i += 2) {
result.push(buff.readUInt16BE(offset + i).toString(16));
}
result = result.join(':');
Expand All @@ -82,8 +83,8 @@ ip.toString = function (buff, offset, length) {
return result;
};

const ipv4Regex = /^(\d{1,3}\.){3,3}\d{1,3}$/;
const ipv6Regex = /^(::)?(((\d{1,3}\.){3}(\d{1,3}){1})?([0-9a-f]){0,4}:{0,2}){1,8}(::)?$/i;
var ipv4Regex = /^(\d{1,3}\.){3,3}\d{1,3}$/;
var ipv6Regex = /^(::)?(((\d{1,3}\.){3}(\d{1,3}){1})?([0-9a-f]){0,4}:{0,2}){1,8}(::)?$/i;

ip.isV4Format = function (ip) {
return ipv4Regex.test(ip);
Expand All @@ -110,14 +111,14 @@ ip.fromPrefixLen = function (prefixlen, family) {
family = _normalizeFamily(family);
}

let len = 4;
var len = 4;
if (family === 'ipv6') {
len = 16;
}
const buff = new Buffer(len);
var buff = new Buffer(len);

for (let i = 0, n = buff.length; i < n; ++i) {
let bits = 8;
for (var i = 0, n = buff.length; i < n; ++i) {
var bits = 8;
if (prefixlen < 8) {
bits = prefixlen;
}
Expand All @@ -133,10 +134,10 @@ ip.mask = function (addr, mask) {
addr = ip.toBuffer(addr);
mask = ip.toBuffer(mask);

const result = new Buffer(Math.max(addr.length, mask.length));
var result = new Buffer(Math.max(addr.length, mask.length));

// Same protocol - do bitwise and
let i;
var i;
if (addr.length === mask.length) {
for (i = 0; i < addr.length; i++) {
result[i] = addr[i] & mask[i];
Expand Down Expand Up @@ -169,38 +170,38 @@ ip.mask = function (addr, mask) {
};

ip.cidr = function (cidrString) {
const cidrParts = cidrString.split('/');
var cidrParts = cidrString.split('/');

const addr = cidrParts[0];
var addr = cidrParts[0];
if (cidrParts.length !== 2) {
throw new Error(`invalid CIDR subnet: ${addr}`);
}

const mask = ip.fromPrefixLen(parseInt(cidrParts[1], 10));
var mask = ip.fromPrefixLen(parseInt(cidrParts[1], 10));

return ip.mask(addr, mask);
};

ip.subnet = function (addr, mask) {
const networkAddress = ip.toLong(ip.mask(addr, mask));
var networkAddress = ip.toLong(ip.mask(addr, mask));

// Calculate the mask's length.
const maskBuffer = ip.toBuffer(mask);
let maskLength = 0;
var maskBuffer = ip.toBuffer(mask);
var maskLength = 0;

for (let i = 0; i < maskBuffer.length; i++) {
for (var i = 0; i < maskBuffer.length; i++) {
if (maskBuffer[i] === 0xff) {
maskLength += 8;
} else {
let octet = maskBuffer[i] & 0xff;
var octet = maskBuffer[i] & 0xff;
while (octet) {
octet = (octet << 1) & 0xff;
maskLength++;
}
}
}

const numberOfAddresses = 2 ** (32 - maskLength);
var numberOfAddresses = 2 ** (32 - maskLength);

return {
networkAddress: ip.fromLong(networkAddress),
Expand All @@ -223,82 +224,86 @@ ip.subnet = function (addr, mask) {
};

ip.cidrSubnet = function (cidrString) {
const cidrParts = cidrString.split('/');
var cidrParts = cidrString.split('/');

const addr = cidrParts[0];
var addr = cidrParts[0];
if (cidrParts.length !== 2) {
throw new Error(`invalid CIDR subnet: ${addr}`);
}

const mask = ip.fromPrefixLen(parseInt(cidrParts[1], 10));
var mask = ip.fromPrefixLen(parseInt(cidrParts[1], 10));

return ip.subnet(addr, mask);
};

ip.not = function (addr) {
const buff = ip.toBuffer(addr);
for (let i = 0; i < buff.length; i++) {
var buff = ip.toBuffer(addr);
for (var i = 0; i < buff.length; i++) {
buff[i] = 0xff ^ buff[i];
}
return ip.toString(buff);
};

ip.or = function (a, b) {
var i;

a = ip.toBuffer(a);
b = ip.toBuffer(b);

// same protocol
if (a.length === b.length) {
for (let i = 0; i < a.length; ++i) {
for (i = 0; i < a.length; ++i) {
a[i] |= b[i];
}
return ip.toString(a);

// mixed protocols
}
let buff = a;
let other = b;
var buff = a;
var other = b;
if (b.length > a.length) {
buff = b;
other = a;
}

const offset = buff.length - other.length;
for (let i = offset; i < buff.length; ++i) {
var offset = buff.length - other.length;
for (i = offset; i < buff.length; ++i) {
buff[i] |= other[i - offset];
}

return ip.toString(buff);
};

ip.isEqual = function (a, b) {
var i;

a = ip.toBuffer(a);
b = ip.toBuffer(b);

// Same protocol
if (a.length === b.length) {
for (let i = 0; i < a.length; i++) {
for (i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}

// Swap
if (b.length === 4) {
const t = b;
var t = b;
b = a;
a = t;
}

// a - IPv4, b - IPv6
for (let i = 0; i < 10; i++) {
for (i = 0; i < 10; i++) {
if (b[i] !== 0) return false;
}

const word = b.readUInt16BE(10);
var word = b.readUInt16BE(10);
if (word !== 0 && word !== 0xffff) return false;

for (let i = 0; i < 4; i++) {
for (i = 0; i < 4; i++) {
if (a[i] !== b[i + 12]) return false;
}

Expand Down Expand Up @@ -360,7 +365,7 @@ ip.loopback = function (family) {
// * undefined: First address with `ipv4` or loopback address `127.0.0.1`.
//
ip.address = function (name, family) {
const interfaces = os.networkInterfaces();
var interfaces = os.networkInterfaces();

//
// Default to `ipv4`
Expand All @@ -372,8 +377,8 @@ ip.address = function (name, family) {
// return the address.
//
if (name && name !== 'private' && name !== 'public') {
const res = interfaces[name].filter((details) => {
const itemFamily = _normalizeFamily(details.family);
var res = interfaces[name].filter((details) => {
var itemFamily = _normalizeFamily(details.family);
return itemFamily === family;
});
if (res.length === 0) {
Expand All @@ -382,12 +387,12 @@ ip.address = function (name, family) {
return res[0].address;
}

const all = Object.keys(interfaces).map((nic) => {
var all = Object.keys(interfaces).map((nic) => {
//
// Note: name will only be `public` or `private`
// when this is called.
//
const addresses = interfaces[nic].filter((details) => {
var addresses = interfaces[nic].filter((details) => {
details.family = _normalizeFamily(details.family);
if (details.family !== family || ip.isLoopback(details.address)) {
return false;
Expand All @@ -406,7 +411,7 @@ ip.address = function (name, family) {
};

ip.toLong = function (ip) {
let ipl = 0;
var ipl = 0;
ip.split('.').forEach((octet) => {
ipl <<= 8;
ipl += parseInt(octet);
Expand Down

0 comments on commit 1a4ca35

Please sign in to comment.