Skip to content

Commit 30701d6

Browse files
mscdexbnoordhuis
authored andcommitted
os: add mac address to networkInterfaces() output
1 parent a622bde commit 30701d6

2 files changed

Lines changed: 34 additions & 18 deletions

File tree

doc/api/os.markdown

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,24 +128,28 @@ Example inspection of os.cpus:
128128

129129
Get a list of network interfaces:
130130

131-
{ lo0:
132-
[ { address: 'fe80::1', netmask: 'ffff:ffff:ffff:ffff::',
133-
family: 'IPv6', internal: true },
134-
{ address: '127.0.0.1', netmask: '255.0.0.0',
135-
family: 'IPv4', internal: true },
136-
{ address: '::1', netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
137-
family: 'IPv6', internal: true } ],
138-
en1:
139-
[ { address: 'fe80::226:8ff:fedc:1dd', netmask: 'ffff:ffff:ffff:ffff::',
140-
family: 'IPv6', internal: false },
141-
{ address: '10.0.1.6', netmask: '255.255.255.0',
142-
family: 'IPv4', internal: false } ],
143-
vmnet1:
144-
[ { address: '192.168.252.1', netmask: '255.255.255.0',
145-
family: 'IPv4', internal: false } ],
146-
vmnet8:
147-
[ { address: '192.168.207.1', netmask: '255.255.255.0',
148-
family: 'IPv4', internal: false } ] }
131+
{ lo:
132+
[ { address: '127.0.0.1',
133+
netmask: '255.0.0.0',
134+
family: 'IPv4',
135+
mac: '00:00:00:00:00:00',
136+
internal: true },
137+
{ address: '::1',
138+
netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
139+
family: 'IPv6',
140+
mac: '00:00:00:00:00:00',
141+
internal: true } ],
142+
eth0:
143+
[ { address: '192.168.1.108',
144+
netmask: '255.255.255.0',
145+
family: 'IPv4',
146+
mac: '01:02:03:0a:0b:0c',
147+
internal: false },
148+
{ address: 'fe80::a00:27ff:fe4e:66a1',
149+
netmask: 'ffff:ffff:ffff:ffff::',
150+
family: 'IPv6',
151+
mac: '01:02:03:0a:0b:0c',
152+
internal: false } ] }
149153

150154
## os.EOL
151155

src/node_os.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
208208
int count, i;
209209
char ip[INET6_ADDRSTRLEN];
210210
char netmask[INET6_ADDRSTRLEN];
211+
char mac[18];
211212
Local<Object> ret, o;
212213
Local<String> name, family;
213214
Local<Array> ifarr;
@@ -228,6 +229,16 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
228229
ret->Set(name, ifarr);
229230
}
230231

232+
snprintf(mac,
233+
18,
234+
"%02x:%02x:%02x:%02x:%02x:%02x",
235+
static_cast<unsigned char>(interfaces[i].phys_addr[0]),
236+
static_cast<unsigned char>(interfaces[i].phys_addr[1]),
237+
static_cast<unsigned char>(interfaces[i].phys_addr[2]),
238+
static_cast<unsigned char>(interfaces[i].phys_addr[3]),
239+
static_cast<unsigned char>(interfaces[i].phys_addr[4]),
240+
static_cast<unsigned char>(interfaces[i].phys_addr[5]));
241+
231242
if (interfaces[i].address.address4.sin_family == AF_INET) {
232243
uv_ip4_name(&interfaces[i].address.address4, ip, sizeof(ip));
233244
uv_ip4_name(&interfaces[i].netmask.netmask4, netmask, sizeof(netmask));
@@ -245,6 +256,7 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
245256
o->Set(String::New("address"), String::New(ip));
246257
o->Set(String::New("netmask"), String::New(netmask));
247258
o->Set(String::New("family"), family);
259+
o->Set(String::New("mac"), String::New(mac));
248260

249261
const bool internal = interfaces[i].is_internal;
250262
o->Set(String::New("internal"),

0 commit comments

Comments
 (0)