mirror of
https://github.com/nodejs/node.git
synced 2024-11-29 23:16:30 +01:00
4a6b678070
This patch adds support for CIDR notation to the output of the `networkInterfaces()` method PR-URL: https://github.com/nodejs/node/pull/14307 Fixes: https://github.com/nodejs/node/issues/14006 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
42 lines
975 B
JavaScript
42 lines
975 B
JavaScript
'use strict';
|
|
|
|
function getCIDRSuffix(mask, protocol = 'ipv4') {
|
|
const isV6 = protocol === 'ipv6';
|
|
const bitsString = mask
|
|
.split(isV6 ? ':' : '.')
|
|
.filter((v) => !!v)
|
|
.map((v) => pad(parseInt(v, isV6 ? 16 : 10).toString(2), isV6))
|
|
.join('');
|
|
|
|
if (isValidMask(bitsString)) {
|
|
return countOnes(bitsString);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function pad(binaryString, isV6) {
|
|
const groupLength = isV6 ? 16 : 8;
|
|
const binLen = binaryString.length;
|
|
|
|
return binLen < groupLength ?
|
|
`${'0'.repeat(groupLength - binLen)}${binaryString}` : binaryString;
|
|
}
|
|
|
|
function isValidMask(bitsString) {
|
|
const firstIndexOfZero = bitsString.indexOf(0);
|
|
const lastIndexOfOne = bitsString.lastIndexOf(1);
|
|
|
|
return firstIndexOfZero < 0 || firstIndexOfZero > lastIndexOfOne;
|
|
}
|
|
|
|
function countOnes(bitsString) {
|
|
return bitsString
|
|
.split('')
|
|
.reduce((acc, bit) => acc += parseInt(bit, 10), 0);
|
|
}
|
|
|
|
module.exports = {
|
|
getCIDRSuffix
|
|
};
|