diff --git a/src/node_report.cc b/src/node_report.cc index cffb9dd1b655af..2a8fbf354b2472 100644 --- a/src/node_report.cc +++ b/src/node_report.cc @@ -202,7 +202,7 @@ static void WriteNodeReport(Isolate* isolate, writer.json_arraystart("libuv"); if (env != nullptr) { - uv_walk(env->event_loop(), WalkHandle, static_cast(&writer)); + uv_walk(env->event_loop(), exclude_network ? WalkHandleNoNetwork : WalkHandleNetwork, static_cast(&writer)); writer.json_start(); writer.json_keyvalue("type", "loop"); diff --git a/src/node_report.h b/src/node_report.h index 7a2e817ac82f6b..98be339ae90d8f 100644 --- a/src/node_report.h +++ b/src/node_report.h @@ -19,7 +19,8 @@ namespace node { namespace report { // Function declarations - utility functions in src/node_report_utils.cc -void WalkHandle(uv_handle_t* h, void* arg); +void WalkHandleNetwork(uv_handle_t* h, void* arg); +void WalkHandleNoNetwork(uv_handle_t* h, void* arg); template std::string ValueToHexString(T value) { diff --git a/src/node_report_utils.cc b/src/node_report_utils.cc index 516eac22dc63a2..700e501c95169d 100644 --- a/src/node_report_utils.cc +++ b/src/node_report_utils.cc @@ -12,7 +12,8 @@ static constexpr auto null = JSONWriter::Null{}; static void ReportEndpoint(uv_handle_t* h, struct sockaddr* addr, const char* name, - JSONWriter* writer) { + JSONWriter* writer, + bool exclude_network) { if (addr == nullptr) { writer->json_keyvalue(name, null); return; @@ -26,7 +27,7 @@ static void ReportEndpoint(uv_handle_t* h, reinterpret_cast(addr)->sin_port : reinterpret_cast(addr)->sin6_port); - if (uv_getnameinfo(h->loop, &endpoint, nullptr, addr, NI_NUMERICSERV) == 0) { + if (!exclude_network && uv_getnameinfo(h->loop, &endpoint, nullptr, addr, NI_NUMERICSERV) == 0) { host = endpoint.host; DCHECK_EQ(port, std::stoi(endpoint.service)); } else { @@ -48,7 +49,7 @@ static void ReportEndpoint(uv_handle_t* h, } // Utility function to format libuv socket information. -static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) { +static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer, bool exclude_network) { struct sockaddr_storage addr_storage; struct sockaddr* addr = reinterpret_cast(&addr_storage); uv_any_handle* handle = reinterpret_cast(h); @@ -65,7 +66,7 @@ static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) { default: break; } - ReportEndpoint(h, rc == 0 ? addr : nullptr, "localEndpoint", writer); + ReportEndpoint(h, rc == 0 ? addr : nullptr, "localEndpoint", writer, exclude_network); switch (h->type) { case UV_UDP: @@ -77,7 +78,7 @@ static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) { default: break; } - ReportEndpoint(h, rc == 0 ? addr : nullptr, "remoteEndpoint", writer); + ReportEndpoint(h, rc == 0 ? addr : nullptr, "remoteEndpoint", writer, exclude_network); } // Utility function to format libuv pipe information. @@ -155,7 +156,7 @@ static void ReportPath(uv_handle_t* h, JSONWriter* writer) { } // Utility function to walk libuv handles. -void WalkHandle(uv_handle_t* h, void* arg) { +void WalkHandle(uv_handle_t* h, void* arg, bool exclude_network = false) { const char* type = uv_handle_type_name(h->type); JSONWriter* writer = static_cast(arg); uv_any_handle* handle = reinterpret_cast(h); @@ -177,7 +178,7 @@ void WalkHandle(uv_handle_t* h, void* arg) { break; case UV_TCP: case UV_UDP: - ReportEndpoints(h, writer); + ReportEndpoints(h, writer, exclude_network); break; case UV_NAMED_PIPE: ReportPipeEndpoints(h, writer); @@ -267,6 +268,11 @@ void WalkHandle(uv_handle_t* h, void* arg) { } writer->json_end(); } - +void WalkHandleNetwork(uv_handle_t* h, void* arg) { + WalkHandle(h, arg, false); +} +void WalkHandleNoNetwork(uv_handle_t* h, void* arg) { + WalkHandle(h, arg, true); +} } // namespace report } // namespace node diff --git a/test/report/test-report-exclude-network.js b/test/report/test-report-exclude-network.js index c5e50135482f1a..d4bd6b117f6ebd 100644 --- a/test/report/test-report-exclude-network.js +++ b/test/report/test-report-exclude-network.js @@ -38,4 +38,22 @@ describe('report exclude network option', () => { const report = process.report.getReport(); assert.strictEqual(report.header.networkInterfaces, undefined); }); + + it('should not do DNS queries in libuv if exclude network', async () => { + await fetch('http://localhost'); + + process.report.excludeNetwork = false; + let report = process.report.getReport(); + let tcp = report.libuv.find((uv) => uv.type === 'tcp'); + assert.notEqual(tcp, null); + assert.strictEqual(tcp.localEndpoint.host, 'localhost'); + assert.strictEqual(tcp.remoteEndpoint.host, 'localhost'); + + process.report.excludeNetwork = true; + report = process.report.getReport(); + tcp = report.libuv.find((uv) => uv.type === 'tcp'); + assert.notEqual(tcp, null); + assert.strictEqual(tcp.localEndpoint.host, '127.0.0.1'); + assert.strictEqual(tcp.remoteEndpoint.host, '127.0.0.1'); + }); });