Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/net-util/export/InterfaceAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ export class InterfaceAddress extends IntfDeconstructable {
let needCanonicalization;
if (typeof fullAddress === 'string') {
fullAddress = InterfaceAddress.parseInterface(fullAddress);
// `parseInterface()` expects `port` not `portNumber`. TODO: Fix it to be
// consistent with this class (not the other way around).
fullAddress.portNumber = fullAddress.port;
delete fullAddress.port;
needCanonicalization = false;
} else {
needCanonicalization = true;
Expand Down Expand Up @@ -395,13 +391,14 @@ export class InterfaceAddress extends IntfDeconstructable {
* port, users of this system might want to provide it more directly.
*
* @param {string} iface Interface spec to parse.
* @returns {{address: ?string, fd: ?number, port: ?number}} The parsed form.
* @returns {{address: ?string, fd: ?number, portNumber: ?number}} The parsed
* form.
*/
static parseInterface(iface) {
MustBe.string(iface);

const portStr = iface.match(/:(?<port>[0-9]{1,5})$/)?.groups.port ?? null;
const port = portStr ? this.#mustBePortNumber(portStr, true) : null;
const portStr = iface.match(/:(?<port>[0-9]{1,5})$/)?.groups.port ?? null;
const portNumber = portStr ? this.#mustBePortNumber(portStr, true) : null;

const addressStr = portStr
? iface.match(/^(?<address>.*):[^:]+$/).groups.address
Expand All @@ -417,7 +414,7 @@ export class InterfaceAddress extends IntfDeconstructable {
if (addressOrFd.fd) {
const fd = MustBe.number(parseInt(addressOrFd.fd),
{ safeInteger: true, minInclusive: 0, maxInclusive: 65535 });
return (port === null) ? { fd } : { fd, port };
return (portNumber === null) ? { fd } : { fd, portNumber };
}

const address = InterfaceAddress.canonicalizeAddress(addressOrFd.address);
Expand All @@ -426,12 +423,12 @@ export class InterfaceAddress extends IntfDeconstructable {
// If we managed to parse and made it here, then we are necessarily
// looking at an IPv6 address without brackets.
throw new Error(`Invalid network interface (missing brackets): ${iface}`);
} else if (port === null) {
} else if (portNumber === null) {
// Must specify port at this point. (It's optional with the FD form).
throw new Error(`Invalid network interface (missing port): ${iface}`);
}

return { address, port };
return { address, portNumber };
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/net-util/tests/InterfaceAddress.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,17 +558,17 @@ describe('parseInterface()', () => {

test('parses an interface with IPv4 address as expected', () => {
const got = InterfaceAddress.parseInterface('12.34.56.78:123');
expect(got).toStrictEqual({ address: '12.34.56.78', port: 123 });
expect(got).toStrictEqual({ address: '12.34.56.78', portNumber: 123 });
});

test('parses an interface with IPv6 address as expected', () => {
const got = InterfaceAddress.parseInterface('[abc::123:4567]:999');
expect(got).toStrictEqual({ address: 'abc::123:4567', port: 999 });
expect(got).toStrictEqual({ address: 'abc::123:4567', portNumber: 999 });
});

test('parses an interface with wildcard address as expected', () => {
const got = InterfaceAddress.parseInterface('*:17777');
expect(got).toStrictEqual({ address: '*', port: 17777 });
expect(got).toStrictEqual({ address: '*', portNumber: 17777 });
});

test('parses an FD interface with no port as expected', () => {
Expand All @@ -578,7 +578,7 @@ describe('parseInterface()', () => {

test('parses an FD interface with port as expected', () => {
const got = InterfaceAddress.parseInterface('/dev/fd/109:914');
expect(got).toStrictEqual({ fd: 109, port: 914 });
expect(got).toStrictEqual({ fd: 109, portNumber: 914 });
});

test('accepts the minimum and maximum allowed FD numbers', () => {
Expand Down
Loading