|
24 | 24 | const { pushValToArrayMax, safeGetenv } = process.binding('util');
|
25 | 25 | const constants = process.binding('constants').os;
|
26 | 26 | const { deprecate } = require('internal/util');
|
27 |
| -const { getCIDRSuffix } = require('internal/os'); |
28 | 27 | const isWindows = process.platform === 'win32';
|
29 | 28 |
|
30 | 29 | const { ERR_SYSTEM_ERROR } = require('internal/errors');
|
@@ -144,19 +143,67 @@ function endianness() {
|
144 | 143 | }
|
145 | 144 | endianness[Symbol.toPrimitive] = () => kEndianness;
|
146 | 145 |
|
| 146 | +// Returns the number of ones in the binary representation of the decimal |
| 147 | +// number. |
| 148 | +function countBinaryOnes(n) { |
| 149 | + let count = 0; |
| 150 | + // Remove one "1" bit from n until n is the power of 2. This iterates k times |
| 151 | + // while k is the number of "1" in the binary representation. |
| 152 | + // For more check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators |
| 153 | + while (n !== 0) { |
| 154 | + n = n & (n - 1); |
| 155 | + count++; |
| 156 | + } |
| 157 | + return count; |
| 158 | +} |
| 159 | + |
| 160 | +function getCIDR({ address, netmask, family }) { |
| 161 | + let ones = 0; |
| 162 | + let split = '.'; |
| 163 | + let range = 10; |
| 164 | + let groupLength = 8; |
| 165 | + let hasZeros = false; |
| 166 | + |
| 167 | + if (family === 'IPv6') { |
| 168 | + split = ':'; |
| 169 | + range = 16; |
| 170 | + groupLength = 16; |
| 171 | + } |
| 172 | + |
| 173 | + const parts = netmask.split(split); |
| 174 | + for (var i = 0; i < parts.length; i++) { |
| 175 | + if (parts[i] !== '') { |
| 176 | + const binary = parseInt(parts[i], range); |
| 177 | + const tmp = countBinaryOnes(binary); |
| 178 | + ones += tmp; |
| 179 | + if (hasZeros) { |
| 180 | + if (tmp !== 0) { |
| 181 | + return null; |
| 182 | + } |
| 183 | + } else if (tmp !== groupLength) { |
| 184 | + if ((binary & 1) !== 0) { |
| 185 | + return null; |
| 186 | + } |
| 187 | + hasZeros = true; |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + return `${address}/${ones}`; |
| 193 | +} |
| 194 | + |
147 | 195 | function networkInterfaces() {
|
148 | 196 | const interfaceAddresses = getInterfaceAddresses();
|
149 | 197 |
|
150 |
| - return Object.entries(interfaceAddresses).reduce((acc, [key, val]) => { |
151 |
| - acc[key] = val.map((v) => { |
152 |
| - const protocol = v.family.toLowerCase(); |
153 |
| - const suffix = getCIDRSuffix(v.netmask, protocol); |
154 |
| - const cidr = suffix ? `${v.address}/${suffix}` : null; |
| 198 | + const keys = Object.keys(interfaceAddresses); |
| 199 | + for (var i = 0; i < keys.length; i++) { |
| 200 | + const arr = interfaceAddresses[keys[i]]; |
| 201 | + for (var j = 0; j < arr.length; j++) { |
| 202 | + arr[j].cidr = getCIDR(arr[j]); |
| 203 | + } |
| 204 | + } |
155 | 205 |
|
156 |
| - return Object.assign({}, v, { cidr }); |
157 |
| - }); |
158 |
| - return acc; |
159 |
| - }, {}); |
| 206 | + return interfaceAddresses; |
160 | 207 | }
|
161 | 208 |
|
162 | 209 | module.exports = {
|
|
0 commit comments