Browse Source

os.getNetworkInterfaces()

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
19e53512b8
  1. 11
      doc/api/os.markdown
  2. 1
      lib/os.js
  3. 7
      src/node_os.cc
  4. 1
      src/platform.h
  5. 6
      src/platform_cygwin.cc
  6. 6
      src/platform_darwin.cc
  7. 6
      src/platform_freebsd.cc
  8. 81
      src/platform_linux.cc
  9. 7
      src/platform_openbsd.cc
  10. 6
      src/platform_sunos.cc
  11. 7
      src/platform_win32.cc
  12. 9
      test/simple/test-os.js

11
doc/api/os.markdown

@ -100,3 +100,14 @@ Example inspection of os.cpus:
sys: 34920,
idle: 1072572010,
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: {} }

1
lib/os.js

@ -29,3 +29,4 @@ exports.totalmem = binding.getTotalMem;
exports.cpus = binding.getCPUs;
exports.type = binding.getOSType;
exports.release = binding.getOSRelease;
exports.getNetworkInterfaces = binding.getInterfaceAddresses;

7
src/node_os.cc

@ -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);

1
src/platform.h

@ -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;

6
src/platform_cygwin.cc

@ -380,4 +380,10 @@ int Platform::GetLoadAvg(Local<Array> *loads) {
}
Handle<Value> Platform::GetInterfaceAddresses() {
HandleScope scope;
return scope.Close(Object::New());
}
} // namespace node

6
src/platform_darwin.cc

@ -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

6
src/platform_freebsd.cc

@ -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

81
src/platform_linux.cc

@ -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

7
src/platform_openbsd.cc

@ -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

6
src/platform_sunos.cc

@ -140,5 +140,11 @@ int Platform::GetLoadAvg(Local<Array> *loads) {
}
Handle<Value> Platform::GetInterfaceAddresses() {
HandleScope scope;
return scope.Close(Object::New());
}
} // namespace node

7
src/platform_win32.cc

@ -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

9
test/simple/test-os.js

@ -31,3 +31,12 @@ assert.ok(os.totalmem() > 0);
assert.ok(os.cpus().length > 0);
assert.ok(os.type().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…
Cancel
Save