Skip to content

Commit

Permalink
allow to get multiple addresses assign to interface
Browse files Browse the repository at this point in the history
Extend 'address' function so it can be used to return all interface addresses.

closes indutny#70.
  • Loading branch information
MaciejBaj committed Jan 23, 2017
1 parent 5de805a commit ec401f1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
31 changes: 22 additions & 9 deletions lib/ip.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,16 +340,21 @@ ip.loopback = function(family) {
// of the network interface.
// #### @family {ipv4|ipv6} **Optional** IP family of the address (defaults
// to ipv4).
// #### @multiple {Boolean} **Optional** Returns all addresses with for given
// name and family if true
//
// Returns the address for the network interface on the current system with
// the specified `name`:
// Returns the address (or an array of addresses if `multiply` for the network
// interface on the current system with the specified `name`:
// * String: First `family` address of the interface.
// If not found see `undefined`.
// * 'public': the first public ip address of family.
// * 'private': the first private ip address of family.
// * undefined: First address with `ipv4` or loopback address `127.0.0.1`.
//
ip.address = function(name, family) {

ip.address = function(name, family, multiple) {
// if (multiple) {
// return [];
// }
var interfaces = os.networkInterfaces();
var all;

Expand All @@ -360,7 +365,7 @@ ip.address = function(name, family) {

//
// If a specific network interface has been named,
// return the address.
// return the address (or addresses if `multiple`).
//
if (name && name !== 'private' && name !== 'public') {
var res = interfaces[name].filter(function(details) {
Expand All @@ -369,7 +374,9 @@ ip.address = function(name, family) {
});
if (res.length === 0)
return undefined;
return res[0].address;
return multiple ?
res.map(function (a) { return a.address; }) :
res[0].address;
}

var all = Object.keys(interfaces).map(function (nic) {
Expand All @@ -388,11 +395,16 @@ ip.address = function(name, family) {
return name === 'public' ? ip.isPrivate(details.address) :
ip.isPublic(details.address);
});
return multiple ?
addresses.map(function (a) { return a.address; }) :
addresses.length ? addresses[0].address : undefined;
});

return addresses.length ? addresses[0].address : undefined;
}).filter(Boolean);
all = [].concat.apply([], all).filter(Boolean);

return !all.length ? ip.loopback(family) : all[0];
return multiple ?
!all.length ? [ ip.loopback(family) ] : all :
!all.length ? ip.loopback(family) : all[0];
};

ip.toLong = function(ip) {
Expand All @@ -410,3 +422,4 @@ ip.fromLong = function(ipl) {
(ipl >> 8 & 255) + '.' +
(ipl & 255) );
};

39 changes: 31 additions & 8 deletions test/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ describe('IP library for node.js', function() {
assert(/(00){15,15}01/.test(buf.toString('hex', offset, offset + 16)));
assert.equal(ip.toString(buf, offset, 16), '::1');
assert.equal(ip.toString(ip.toBuffer('1::', buf, offset),
offset, 16), '1::');
offset, 16), '1::');
assert.equal(ip.toString(ip.toBuffer('abcd::dcba', buf, offset),
offset, 16), 'abcd::dcba');
offset, 16), 'abcd::dcba');
});

it('should convert to buffer IPv6 mapped IPv4 address', function() {
Expand Down Expand Up @@ -80,11 +80,11 @@ describe('IP library for node.js', function() {
});
it('should or bits in ipv6 addresses', function() {
assert.equal(ip.or('::ff', '::abcd:dcba:abcd:dcba'),
'::abcd:dcba:abcd:dcff');
'::abcd:dcba:abcd:dcff');
});
it('should or bits in mixed addresses', function() {
assert.equal(ip.or('0.0.0.255', '::abcd:dcba:abcd:dcba'),
'::abcd:dcba:abcd:dcff');
'::abcd:dcba:abcd:dcff');
});
});

Expand Down Expand Up @@ -315,14 +315,14 @@ describe('IP library for node.js', function() {
});
});

describe('127.8.8.8', function () {
it('should respond with true', function () {
describe('127.8.8.8', function() {
it('should respond with true', function() {
assert.ok(ip.isLoopback('127.8.8.8'))
});
});

describe('8.8.8.8', function () {
it('should respond with false', function () {
describe('8.8.8.8', function() {
it('should respond with false', function() {
assert.equal(ip.isLoopback('8.8.8.8'), false);
});
});
Expand Down Expand Up @@ -359,6 +359,13 @@ describe('IP library for node.js', function() {
it('should respond with a private ip', function() {
assert.ok(ip.isPrivate(ip.address('private', family)));
});
it('should respond with a private ip addresses', function() {
var addresses = ip.address('private', family, true);
assert.ok(Array.isArray(addresses));
addresses.forEach(function(a) {
assert.ok(ip.isPrivate(a));
});
});
});
});
});
Expand All @@ -373,6 +380,14 @@ describe('IP library for node.js', function() {
var addr = ip.address(nic, family);
assert.ok(!addr || net.isIPv4(addr));
});

it('should respond with multiple ipv4 addresses', function() {
var addresses = ip.address(nic, family, true);
assert.ok(Array.isArray(addresses));
assert.ok(addresses.length === 0 || addresses.every(function(a) {
return net.isIPv4(a);
}));
});
});
});

Expand All @@ -381,6 +396,14 @@ describe('IP library for node.js', function() {
var addr = ip.address(nic, 'ipv6');
assert.ok(!addr || net.isIPv6(addr));
});

it('should respond with multiple ipv6 addresses', function() {
var addresses = ip.address(nic, 'ipv6', true);
assert.ok(Array.isArray(addresses));
assert.ok(addresses.length === 0 || addresses.every(function(a) {
return net.isIPv6(a);
}));
});
})
});
});
Expand Down

0 comments on commit ec401f1

Please sign in to comment.