Skip to content

Commit

Permalink
Merge pull request #102 from particle-iot/export-more-fields-from-iface
Browse files Browse the repository at this point in the history
Export more fields for network interface
  • Loading branch information
keeramis authored May 14, 2024
2 parents 51e9d8b + a537e4e commit fe83f42
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
52 changes: 50 additions & 2 deletions src/network-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ const InterfaceConfigurationSource = fromProtobufEnum(proto.InterfaceConfigurati
DHCPV6: 'DHCPV6'
});

const InterfaceFlag = fromProtobufEnum(proto.InterfaceFlag, {
NONE: 'IFF_NONE',
UP: 'IFF_UP',
BROADCAST: 'IFF_BROADCAST',
DEBUG: 'IFF_DEBUG',
LOOPBACK: 'IFF_LOOPBACK',
POINTOPOINT: 'IFF_POINTTOPOINT',
RUNNING : 'IFF_RUNNING',
LOWER_UP : 'IFF_LOWER_UP',
NOARP : 'IFF_NOARP',
PROMISC : 'IFF_PROMISC',
ALLMULTI : 'IFF_ALLMULTI',
MULTICAST : 'IFF_MULTICAST',
NOND6 : 'IFF_NOND6'
});

/**
* Converts a given interface IP address into a string
*
Expand Down Expand Up @@ -155,6 +171,17 @@ const NetworkDevice = base => class extends base {
}));
}

// Helper function to get active flags of a network interface
_getActiveFlags(flags, flagDefinitions) {
const activeFlags = [];
for (const value of Object.values(flagDefinitions)) {
if (value !== 0 && (flags & value)) {
activeFlags.push(value);
}
}
return activeFlags;
}

/**
* Gets the network interface and its fields
*
Expand All @@ -173,13 +200,34 @@ const NetworkDevice = base => class extends base {
throw new NotFoundError();
}

const { index: ifaceIndex, name, type, ipv4Config, ipv6Config, hwAddress } = reply.interface;
const {
index: ifaceIndex,
name,
type,
flags,
extFlags,
ipv4Config,
ipv6Config,
hwAddress,
mtu,
metric,
profile
} = reply.interface;

const activeFlags = this._getActiveFlags(flags, proto.InterfaceFlag);
const flagsStrings = activeFlags.map(flag => InterfaceFlag.fromProtobuf(flag));

const result = {
index: ifaceIndex,
name,
type: InterfaceType.fromProtobuf(type),
hwAddress: convertBufferToMacAddress(hwAddress)
hwAddress: convertBufferToMacAddress(hwAddress),
mtu,
flagsVal: flags,
extFlags,
flagsStrings,
metric,
profile
};

if (ipv4Config) {
Expand Down
15 changes: 13 additions & 2 deletions src/network-device.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ describe('NetworkDevice', () => {
'index': 4,
'name': 'wl3',
'type': 8,
'flags': 98307,
'extFlags': 1048576,
'ipv4Config': {
'dns': [
{
Expand Down Expand Up @@ -80,7 +82,10 @@ describe('NetworkDevice', () => {
}
]
},
'hwAddress': Buffer.from([48,174,164,229,83,16])
'hwAddress': Buffer.from([48,174,164,229,83,16]),
'mtu': 1500,
'metric': 0,
'profile': Buffer.from([])
};

const expectedOutput = {
Expand All @@ -100,7 +105,13 @@ describe('NetworkDevice', () => {
'gateway': undefined,
'dns': ['0002:0104:0306:0508:070a:090c:0b0e:0d0f'],
'source': 'UNKNOWN'
}
},
'mtu': 1500,
'flagsVal': 98307,
'extFlags': 1048576,
'flagsStrings': ['UP', 'BROADCAST', 'MULTICAST', 'NOND6'],
'metric': 0,
'profile': Buffer.from([])
};
sinon.stub(dev, 'sendRequest').resolves({ interface: input });

Expand Down

0 comments on commit fe83f42

Please sign in to comment.