Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

os: Include netmask in os.networkInterfaces()

re #3765 and #5432
fixes #4743
commit 8a407f58b96ca6c8a5d1542bde39ebb879eb307d 1 parent fbf4641
@wanderview wanderview authored tjfontaine committed
Showing with 29 additions and 13 deletions.
  1. +18 −11 doc/api/os.markdown
  2. +4 −0 src/node_os.cc
  3. +7 −2 test/simple/test-os.js
View
29 doc/api/os.markdown
@@ -127,17 +127,24 @@ Example inspection of os.cpus:
Get a list of network interfaces:
- { lo0:
- [ { address: '::1', family: 'IPv6', internal: true },
- { address: 'fe80::1', family: 'IPv6', internal: true },
- { address: '127.0.0.1', family: 'IPv4', internal: true } ],
- en1:
- [ { address: 'fe80::cabc:c8ff:feef:f996', family: 'IPv6',
- internal: false },
- { address: '10.0.1.123', family: 'IPv4', internal: false } ],
- vmnet1: [ { address: '10.99.99.254', family: 'IPv4', internal: false } ],
- vmnet8: [ { address: '10.88.88.1', family: 'IPv4', internal: false } ],
- ppp0: [ { address: '10.2.0.231', family: 'IPv4', internal: false } ] }
+ { lo0:
+ [ { address: 'fe80::1', netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6', internal: true },
+ { address: '127.0.0.1', netmask: '255.0.0.0',
+ family: 'IPv4', internal: true },
+ { address: '::1', netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
+ family: 'IPv6', internal: true } ],
+ en1:
+ [ { address: 'fe80::226:8ff:fedc:1dd', netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6', internal: false },
+ { address: '10.0.1.6', netmask: '255.255.255.0',
+ family: 'IPv4', internal: false } ],
+ vmnet1:
+ [ { address: '192.168.252.1', netmask: '255.255.255.0',
+ family: 'IPv4', internal: false } ],
+ vmnet8:
+ [ { address: '192.168.207.1', netmask: '255.255.255.0',
+ family: 'IPv4', internal: false } ] }
## os.EOL
View
4 src/node_os.cc
@@ -204,6 +204,7 @@ static Handle<Value> GetInterfaceAddresses(const Arguments& args) {
uv_interface_address_t* interfaces;
int count, i;
char ip[INET6_ADDRSTRLEN];
+ char netmask[INET6_ADDRSTRLEN];
Local<Object> ret, o;
Local<String> name, family;
Local<Array> ifarr;
@@ -226,9 +227,11 @@ static Handle<Value> GetInterfaceAddresses(const Arguments& args) {
if (interfaces[i].address.address4.sin_family == AF_INET) {
uv_ip4_name(&interfaces[i].address.address4,ip, sizeof(ip));
+ uv_ip4_name(&interfaces[i].netmask.netmask4, netmask, sizeof(netmask));
family = String::New("IPv4");
} else if (interfaces[i].address.address4.sin_family == AF_INET6) {
uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip));
+ uv_ip6_name(&interfaces[i].netmask.netmask6, netmask, sizeof(netmask));
family = String::New("IPv6");
} else {
strncpy(ip, "<unknown sa family>", INET6_ADDRSTRLEN);
@@ -237,6 +240,7 @@ static Handle<Value> GetInterfaceAddresses(const Arguments& args) {
o = Object::New();
o->Set(String::New("address"), String::New(ip));
+ o->Set(String::New("netmask"), String::New(netmask));
o->Set(String::New("family"), family);
const bool internal = interfaces[i].is_internal;
View
9 test/simple/test-os.js
@@ -85,13 +85,18 @@ switch (platform) {
case 'linux':
var filter = function(e) { return e.address == '127.0.0.1'; };
var actual = interfaces.lo.filter(filter);
- var expected = [{ address: '127.0.0.1', family: 'IPv4', internal: true }];
+ var expected = [{ address: '127.0.0.1', netmask: '255.0.0.0',
+ family: 'IPv4', internal: true }];
assert.deepEqual(actual, expected);
break;
case 'win32':
var filter = function(e) { return e.address == '127.0.0.1'; };
var actual = interfaces['Loopback Pseudo-Interface 1'].filter(filter);
- var expected = [{ address: '127.0.0.1', family: 'IPv4', internal: true }];
+ // NOTE: Windows does not set a prefix or netmask on 127.0.0.1, so we
+ // default to /32 here. We could put in a special case to force
+ // to /8 if desired.
+ var expected = [{ address: '127.0.0.1', netmask: '255.255.255.255',
+ family: 'IPv4', internal: true }];
assert.deepEqual(actual, expected);
break;
}
Please sign in to comment.
Something went wrong with that request. Please try again.