Skip to content

Commit

Permalink
Simplify the IP conversion code
Browse files Browse the repository at this point in the history
  • Loading branch information
monkbroc committed Jan 26, 2024
1 parent 14c2cd6 commit affcf2e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
71 changes: 39 additions & 32 deletions src/network-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,46 @@ const InterfaceConfigurationSource = fromProtobufEnum(proto.InterfaceConfigurati
});

/**
* Converts a given interface IP address object with version into a dotted-decimal format.
* Converts a given interface IP address into a string
*
* @param {object} ifaceAddr
* @param {string} version 'v4' or 'v6'
* @returns {string} address in dotted-decimal format
* @param {object} ifaceAddr Object with address and prefixLength keys
* @returns {string} address in ${ip}/${prefixLength} format
*/
function convertInterfaceAddress(ifaceAddr, version) {
let res = null;
let prefixLength = null;
if (ifaceAddr && ifaceAddr.address && ifaceAddr.address[version]) {
const addrObj = ifaceAddr.address[version];
res = convertIPAddress(addrObj, version);
function convertInterfaceAddress(ifaceAddr) {
if (!ifaceAddr || !ifaceAddr.address) {
return ifaceAddr;
}
if (ifaceAddr && ifaceAddr.prefixLength) {
prefixLength = ifaceAddr.prefixLength;

const ip = convertIpv4Address(ifaceAddr.address.v4) || convertIpv6Address(ifaceAddr.address.v6);
return `${ip}/${ifaceAddr.prefixLength}`;
}

/**
* Converts an IPv4 to a string
*
* @param {object} addr Object with the IP encoded as int32 in the address key
* @returns {string} address in dotted-decimal format
*/
function convertIpv4Address(addr) {
if (!addr) {
return addr;
}
return res ? `${res}/${prefixLength}` : null;

return Address4.fromInteger(addr.address).address;
}

/**
* Converts a given IP address object with version into a dotted-decimal address string.
*
* @param {object} addr addr.address sent as int32 for 'v4' and as a buffer for 'v6'
* @param {string} version 'v4' or 'v6'
* @returns {string} address in dotted-decimal format
*/
function convertIPAddress(addr, version) {
if (addr && addr.address) {
const val = addr.address;
const res = version === 'v4' ? Address4.fromInteger(val) : Address6.fromByteArray(val);
return res.address;
* Converts an IPv6 to a string
*
* @param {object} addr Object with the IP encoded as a buffer in the address key
* @returns {string} address in colon-separated format
*/
function convertIpv6Address(addr) {
if (!addr) {
return addr;
}
return null;

return Address6.fromByteArray(addr.address).address;
}

/**
Expand Down Expand Up @@ -177,19 +184,19 @@ const NetworkDevice = base => class extends base {

if (ipv4Config) {
result.ipv4Config = {
addresses: ipv4Config.addresses.map((addr) => convertInterfaceAddress(addr, 'v4')),
gateway: convertIPAddress(ipv4Config.gateway, 'v4'),
peer: convertIPAddress(ipv4Config.peer, 'v4'),
dns: ipv4Config.dns.map((addr) => convertIPAddress(addr, 'v4')),
addresses: ipv4Config.addresses.map(convertInterfaceAddress),
gateway: convertIpv4Address(ipv4Config.gateway),
peer: convertIpv4Address(ipv4Config.peer),
dns: ipv4Config.dns.map(convertIpv4Address),
source: InterfaceConfigurationSource.fromProtobuf(ipv4Config.source)
};
}

if (ipv6Config) {
result.ipv6Config = {
addresses: ipv6Config.addresses.map((addr) => convertInterfaceAddress(addr, 'v6')),
gateway: convertIPAddress(ipv6Config.gateway, 'v6'),
dns: ipv6Config.dns.map((addr) => convertIPAddress(addr, 'v6')),
addresses: ipv6Config.addresses.map(convertInterfaceAddress),
gateway: convertIpv6Address(ipv6Config.gateway),
dns: ipv6Config.dns.map(convertIpv6Address),
source: InterfaceConfigurationSource.fromProtobuf(ipv6Config.source)
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/network-device.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ describe('NetworkDevice', () => {
'hwAddress': '30:ae:a4:e5:53:10',
'ipv4Config': {
'addresses': ['192.168.86.31/24'],
'gateway': null,
'peer': null,
'gateway': undefined,
'peer': undefined,
'dns': ['192.168.86.31'],
'source': 'UNKNOWN'
},
'ipv6Config': {
'addresses': ['0001:0203:0405:0607:0809:0a0b:0c0d:0e0f/24'],
'gateway': null,
'gateway': undefined,
'dns': ['0002:0104:0306:0508:070a:090c:0b0e:0d0f'],
'source': 'UNKNOWN'
}
Expand Down

0 comments on commit affcf2e

Please sign in to comment.