mirror of https://github.com/nodejs/node.git
os.getNetworkInterfaces()
parent
6111c17a0e
commit
19e53512b8
|
@ -99,4 +99,15 @@ Example inspection of os.cpus:
|
|||
nice: 1480,
|
||||
sys: 34920,
|
||||
idle: 1072572010,
|
||||
irq: 30 } } ]
|
||||
irq: 30 } } ]
|
||||
|
||||
### os.getNetworkInterfaces()
|
||||
|
||||
Get a list of network interfaces:
|
||||
|
||||
{ lo: { ip: '127.0.0.1', internal: true, ip6: '::1' },
|
||||
eth0: { ip6: 'fe80::f2de:f1ff:fe19:ae7', internal: false },
|
||||
wlan0: { ip: '10.0.1.118', internal: false, ip6: 'fe80::226:c7ff:fe7d:1602' },
|
||||
vboxnet0: {} }
|
||||
|
||||
|
||||
|
|
|
@ -29,3 +29,4 @@ exports.totalmem = binding.getTotalMem;
|
|||
exports.cpus = binding.getCPUs;
|
||||
exports.type = binding.getOSType;
|
||||
exports.release = binding.getOSRelease;
|
||||
exports.getNetworkInterfaces = binding.getInterfaceAddresses;
|
||||
|
|
|
@ -161,6 +161,12 @@ static Handle<Value> GetLoadAvg(const Arguments& args) {
|
|||
return scope.Close(loads);
|
||||
}
|
||||
|
||||
|
||||
static Handle<Value> GetInterfaceAddresses(const Arguments& args) {
|
||||
return Platform::GetInterfaceAddresses();
|
||||
}
|
||||
|
||||
|
||||
#ifdef __MINGW32__
|
||||
static Handle<Value> OpenOSHandle(const Arguments& args) {
|
||||
HandleScope scope;
|
||||
|
@ -186,6 +192,7 @@ void OS::Initialize(v8::Handle<v8::Object> target) {
|
|||
NODE_SET_METHOD(target, "getCPUs", GetCPUInfo);
|
||||
NODE_SET_METHOD(target, "getOSType", GetOSType);
|
||||
NODE_SET_METHOD(target, "getOSRelease", GetOSRelease);
|
||||
NODE_SET_METHOD(target, "getInterfaceAddresses", GetInterfaceAddresses);
|
||||
|
||||
#ifdef __MINGW32__
|
||||
NODE_SET_METHOD(target, "openOSHandle", OpenOSHandle);
|
||||
|
|
|
@ -42,6 +42,7 @@ class Platform {
|
|||
return adjusted ? GetUptimeImpl() - prog_start_time : GetUptimeImpl();
|
||||
}
|
||||
static int GetLoadAvg(v8::Local<v8::Array> *loads);
|
||||
static v8::Handle<v8::Value> GetInterfaceAddresses();
|
||||
private:
|
||||
static double GetUptimeImpl();
|
||||
static double prog_start_time;
|
||||
|
|
|
@ -380,4 +380,10 @@ int Platform::GetLoadAvg(Local<Array> *loads) {
|
|||
}
|
||||
|
||||
|
||||
Handle<Value> Platform::GetInterfaceAddresses() {
|
||||
HandleScope scope;
|
||||
return scope.Close(Object::New());
|
||||
}
|
||||
|
||||
|
||||
} // namespace node
|
||||
|
|
|
@ -209,4 +209,10 @@ int Platform::GetLoadAvg(Local<Array> *loads) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Handle<Value> Platform::GetInterfaceAddresses() {
|
||||
HandleScope scope;
|
||||
return scope.Close(Object::New());
|
||||
}
|
||||
|
||||
} // namespace node
|
||||
|
|
|
@ -229,4 +229,10 @@ int Platform::GetLoadAvg(Local<Array> *loads) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Handle<Value> Platform::GetInterfaceAddresses() {
|
||||
HandleScope scope;
|
||||
return scope.Close(Object::New());
|
||||
}
|
||||
|
||||
} // namespace node
|
||||
|
|
|
@ -36,6 +36,14 @@
|
|||
#include <stdlib.h> // free
|
||||
#include <string.h> // strdup
|
||||
|
||||
/* GetInterfaceAddresses */
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/types.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <errno.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <net/if.h>
|
||||
|
||||
#if HAVE_MONOTONIC_CLOCK
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
@ -295,4 +303,77 @@ int Platform::GetLoadAvg(Local<Array> *loads) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
bool IsInternal(struct ifaddrs* addr) {
|
||||
return addr->ifa_flags & IFF_UP &&
|
||||
addr->ifa_flags & IFF_RUNNING &&
|
||||
addr->ifa_flags & IFF_LOOPBACK;
|
||||
}
|
||||
|
||||
|
||||
Handle<Value> Platform::GetInterfaceAddresses() {
|
||||
HandleScope scope;
|
||||
|
||||
struct ::ifaddrs *addrs;
|
||||
|
||||
int r = getifaddrs(&addrs);
|
||||
|
||||
if (r != 0) {
|
||||
return ThrowException(ErrnoException(errno, "getifaddrs"));
|
||||
}
|
||||
|
||||
struct ::ifaddrs *addr;
|
||||
|
||||
Local<Object> a = Object::New();
|
||||
|
||||
for (addr = addrs;
|
||||
addr;
|
||||
addr = addr->ifa_next) {
|
||||
Local<String> name = String::New(addr->ifa_name);
|
||||
Local<Object> info;
|
||||
|
||||
if (a->Has(name)) {
|
||||
info = a->Get(name)->ToObject();
|
||||
} else {
|
||||
info = Object::New();
|
||||
a->Set(name, info);
|
||||
}
|
||||
|
||||
struct sockaddr *address = addr->ifa_addr;
|
||||
char ip[INET6_ADDRSTRLEN];
|
||||
|
||||
switch (address->sa_family) {
|
||||
case AF_INET6: {
|
||||
struct sockaddr_in6 *a6 = (struct sockaddr_in6*)address;
|
||||
inet_ntop(AF_INET6, &(a6->sin6_addr), ip, INET6_ADDRSTRLEN);
|
||||
info->Set(String::New("ip6"), String::New(ip));
|
||||
if (addr->ifa_flags) {
|
||||
info->Set(String::New("internal"),
|
||||
IsInternal(addr) ? True() : False());
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case AF_INET: {
|
||||
struct sockaddr_in *a4 = (struct sockaddr_in*)address;
|
||||
inet_ntop(AF_INET, &(a4->sin_addr), ip, INET6_ADDRSTRLEN);
|
||||
info->Set(String::New("ip"), String::New(ip));
|
||||
if (addr->ifa_flags) {
|
||||
info->Set(String::New("internal"),
|
||||
IsInternal(addr) ? True() : False());
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
freeifaddrs(addrs);
|
||||
|
||||
return scope.Close(a);
|
||||
}
|
||||
|
||||
|
||||
} // namespace node
|
||||
|
|
|
@ -220,4 +220,11 @@ int Platform::GetLoadAvg(Local<Array> *loads) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Handle<Value> Platform::GetInterfaceAddresses() {
|
||||
HandleScope scope;
|
||||
return scope.Close(Object::New());
|
||||
}
|
||||
|
||||
|
||||
} // namespace node
|
||||
|
|
|
@ -140,5 +140,11 @@ int Platform::GetLoadAvg(Local<Array> *loads) {
|
|||
}
|
||||
|
||||
|
||||
Handle<Value> Platform::GetInterfaceAddresses() {
|
||||
HandleScope scope;
|
||||
return scope.Close(Object::New());
|
||||
}
|
||||
|
||||
|
||||
} // namespace node
|
||||
|
||||
|
|
|
@ -250,4 +250,11 @@ int Platform::GetLoadAvg(Local<Array> *loads) {
|
|||
return -1;
|
||||
}
|
||||
|
||||
|
||||
Handle<Value> Platform::GetInterfaceAddresses() {
|
||||
HandleScope scope;
|
||||
return scope.Close(Object::New());
|
||||
}
|
||||
|
||||
|
||||
} // namespace node
|
||||
|
|
|
@ -30,4 +30,13 @@ assert.ok(os.freemem() > 0);
|
|||
assert.ok(os.totalmem() > 0);
|
||||
assert.ok(os.cpus().length > 0);
|
||||
assert.ok(os.type().length > 0);
|
||||
assert.ok(os.release().length > 0);
|
||||
assert.ok(os.release().length > 0);
|
||||
|
||||
|
||||
var interfaces = os.getNetworkInterfaces();
|
||||
console.error(interfaces);
|
||||
switch (process.platform) {
|
||||
case 'linux':
|
||||
assert.equal('127.0.0.1', interfaces.lo.ip);
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue