Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

report: include network interfaces in report #28911

Merged
merged 1 commit into from
Aug 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/api/report.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ is provided below for reference.
"irq": 0
}
],
"networkInterfaces": [
{
"name": "en0",
"internal": false,
"mac": "13:10:de:ad:be:ef",
"address": "10.0.0.37",
"netmask": "255.255.255.0",
"family": "IPv4"
}
],
"host": "test_machine"
},
"javascriptStack": {
Expand Down
53 changes: 53 additions & 0 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ static void PrintLoadedLibraries(JSONWriter* writer);
static void PrintComponentVersions(JSONWriter* writer);
static void PrintRelease(JSONWriter* writer);
static void PrintCpuInfo(JSONWriter* writer);
static void PrintNetworkInterfaceInfo(JSONWriter* writer);

// External function to trigger a report, writing to file.
// The 'name' parameter is in/out: an input filename is used
Expand Down Expand Up @@ -318,6 +319,7 @@ static void PrintVersionInformation(JSONWriter* writer) {
}

PrintCpuInfo(writer);
PrintNetworkInterfaceInfo(writer);

char host[UV_MAXHOSTNAMESIZE];
size_t host_size = sizeof(host);
Expand Down Expand Up @@ -348,6 +350,57 @@ static void PrintCpuInfo(JSONWriter* writer) {
}
}

static void PrintNetworkInterfaceInfo(JSONWriter* writer) {
uv_interface_address_t* interfaces;
char ip[INET6_ADDRSTRLEN];
char netmask[INET6_ADDRSTRLEN];
char mac[18];
int count;

if (uv_interface_addresses(&interfaces, &count) == 0) {
writer->json_arraystart("networkInterfaces");

for (int i = 0; i < count; i++) {
writer->json_start();
writer->json_keyvalue("name", interfaces[i].name);
writer->json_keyvalue("internal", !!interfaces[i].is_internal);
snprintf(mac,
sizeof(mac),
"%02x:%02x:%02x:%02x:%02x:%02x",
static_cast<unsigned char>(interfaces[i].phys_addr[0]),
static_cast<unsigned char>(interfaces[i].phys_addr[1]),
static_cast<unsigned char>(interfaces[i].phys_addr[2]),
static_cast<unsigned char>(interfaces[i].phys_addr[3]),
static_cast<unsigned char>(interfaces[i].phys_addr[4]),
static_cast<unsigned char>(interfaces[i].phys_addr[5]));
writer->json_keyvalue("mac", mac);

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));
writer->json_keyvalue("address", ip);
writer->json_keyvalue("netmask", netmask);
writer->json_keyvalue("family", "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));
writer->json_keyvalue("address", ip);
writer->json_keyvalue("netmask", netmask);
writer->json_keyvalue("family", "IPv6");
writer->json_keyvalue("scopeid",
interfaces[i].address.address6.sin6_scope_id);
} else {
writer->json_keyvalue("family", "unknown");
}

writer->json_end();
}

writer->json_arrayend();
uv_free_interface_addresses(interfaces, count);
}
}

// Report the JavaScript stack.
static void PrintJavaScriptStack(JSONWriter* writer,
Isolate* isolate,
Expand Down
25 changes: 24 additions & 1 deletion test/common/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';
const assert = require('assert');
const fs = require('fs');
const net = require('net');
const os = require('os');
const path = require('path');
const util = require('util');
Expand Down Expand Up @@ -73,7 +74,7 @@ function _validateContent(report) {
'componentVersions', 'release', 'osName', 'osRelease',
'osVersion', 'osMachine', 'cpus', 'host',
'glibcVersionRuntime', 'glibcVersionCompiler', 'cwd',
'reportVersion'];
'reportVersion', 'networkInterfaces'];
checkForUnknownFields(header, headerFields);
assert.strictEqual(header.reportVersion, 1); // Increment as needed.
assert.strictEqual(typeof header.event, 'string');
Expand Down Expand Up @@ -112,6 +113,28 @@ function _validateContent(report) {
return c.model === cpu.model;
}));
});

assert(Array.isArray(header.networkInterfaces));
header.networkInterfaces.forEach((iface) => {
assert.strictEqual(typeof iface.name, 'string');
assert.strictEqual(typeof iface.internal, 'boolean');
assert(/^([0-9A-F][0-9A-F]:){5}[0-9A-F]{2}$/i.test(iface.mac));

if (iface.family === 'IPv4') {
assert.strictEqual(net.isIPv4(iface.address), true);
assert.strictEqual(net.isIPv4(iface.netmask), true);
assert.strictEqual(iface.scopeid, undefined);
} else if (iface.family === 'IPv6') {
assert.strictEqual(net.isIPv6(iface.address), true);
assert.strictEqual(net.isIPv6(iface.netmask), true);
assert(Number.isInteger(iface.scopeid));
} else {
assert.strictEqual(iface.family, 'unknown');
assert.strictEqual(iface.address, undefined);
assert.strictEqual(iface.netmask, undefined);
assert.strictEqual(iface.scopeid, undefined);
}
});
assert.strictEqual(header.host, os.hostname());

// Verify the format of the javascriptStack section.
Expand Down