Skip to content

Commit dd14b0e

Browse files
committed
src: network queries in getReport libuv with --report-exclude-network
1 parent 26dae59 commit dd14b0e

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

src/node_report.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ static void WriteNodeReport(Isolate* isolate,
202202

203203
writer.json_arraystart("libuv");
204204
if (env != nullptr) {
205-
uv_walk(env->event_loop(), WalkHandle, static_cast<void*>(&writer));
205+
uv_walk(env->event_loop(), exclude_network ? WalkHandleNoNetwork : WalkHandleNetwork, static_cast<void*>(&writer));
206206

207207
writer.json_start();
208208
writer.json_keyvalue("type", "loop");

src/node_report.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
namespace node {
2020
namespace report {
2121
// Function declarations - utility functions in src/node_report_utils.cc
22-
void WalkHandle(uv_handle_t* h, void* arg);
22+
void WalkHandleNetwork(uv_handle_t* h, void* arg);
23+
void WalkHandleNoNetwork(uv_handle_t* h, void* arg);
2324

2425
template <typename T>
2526
std::string ValueToHexString(T value) {

src/node_report_utils.cc

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ static constexpr auto null = JSONWriter::Null{};
1212
static void ReportEndpoint(uv_handle_t* h,
1313
struct sockaddr* addr,
1414
const char* name,
15-
JSONWriter* writer) {
15+
JSONWriter* writer,
16+
bool exclude_network) {
1617
if (addr == nullptr) {
1718
writer->json_keyvalue(name, null);
1819
return;
@@ -26,7 +27,7 @@ static void ReportEndpoint(uv_handle_t* h,
2627
reinterpret_cast<sockaddr_in*>(addr)->sin_port :
2728
reinterpret_cast<sockaddr_in6*>(addr)->sin6_port);
2829

29-
if (uv_getnameinfo(h->loop, &endpoint, nullptr, addr, NI_NUMERICSERV) == 0) {
30+
if (!exclude_network && uv_getnameinfo(h->loop, &endpoint, nullptr, addr, NI_NUMERICSERV) == 0) {
3031
host = endpoint.host;
3132
DCHECK_EQ(port, std::stoi(endpoint.service));
3233
} else {
@@ -48,7 +49,7 @@ static void ReportEndpoint(uv_handle_t* h,
4849
}
4950

5051
// Utility function to format libuv socket information.
51-
static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) {
52+
static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer, bool exclude_network) {
5253
struct sockaddr_storage addr_storage;
5354
struct sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
5455
uv_any_handle* handle = reinterpret_cast<uv_any_handle*>(h);
@@ -65,7 +66,7 @@ static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) {
6566
default:
6667
break;
6768
}
68-
ReportEndpoint(h, rc == 0 ? addr : nullptr, "localEndpoint", writer);
69+
ReportEndpoint(h, rc == 0 ? addr : nullptr, "localEndpoint", writer, exclude_network);
6970

7071
switch (h->type) {
7172
case UV_UDP:
@@ -77,7 +78,7 @@ static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) {
7778
default:
7879
break;
7980
}
80-
ReportEndpoint(h, rc == 0 ? addr : nullptr, "remoteEndpoint", writer);
81+
ReportEndpoint(h, rc == 0 ? addr : nullptr, "remoteEndpoint", writer, exclude_network);
8182
}
8283

8384
// Utility function to format libuv pipe information.
@@ -155,7 +156,7 @@ static void ReportPath(uv_handle_t* h, JSONWriter* writer) {
155156
}
156157

157158
// Utility function to walk libuv handles.
158-
void WalkHandle(uv_handle_t* h, void* arg) {
159+
void WalkHandle(uv_handle_t* h, void* arg, bool exclude_network = false) {
159160
const char* type = uv_handle_type_name(h->type);
160161
JSONWriter* writer = static_cast<JSONWriter*>(arg);
161162
uv_any_handle* handle = reinterpret_cast<uv_any_handle*>(h);
@@ -177,7 +178,7 @@ void WalkHandle(uv_handle_t* h, void* arg) {
177178
break;
178179
case UV_TCP:
179180
case UV_UDP:
180-
ReportEndpoints(h, writer);
181+
ReportEndpoints(h, writer, exclude_network);
181182
break;
182183
case UV_NAMED_PIPE:
183184
ReportPipeEndpoints(h, writer);
@@ -267,6 +268,11 @@ void WalkHandle(uv_handle_t* h, void* arg) {
267268
}
268269
writer->json_end();
269270
}
270-
271+
void WalkHandleNetwork(uv_handle_t* h, void* arg) {
272+
WalkHandle(h, arg, false);
273+
}
274+
void WalkHandleNoNetwork(uv_handle_t* h, void* arg) {
275+
WalkHandle(h, arg, true);
276+
}
271277
} // namespace report
272278
} // namespace node

test/report/test-report-exclude-network.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,22 @@ describe('report exclude network option', () => {
3838
const report = process.report.getReport();
3939
assert.strictEqual(report.header.networkInterfaces, undefined);
4040
});
41+
42+
it('should not do DNS queries in libuv if exclude network', async () => {
43+
await fetch('http://127.0.0.1');
44+
45+
process.report.excludeNetwork = false;
46+
let report = process.report.getReport();
47+
let tcp = report.libuv.find((uv) => uv.type === 'tcp');
48+
assert.notEqual(tcp, null);
49+
assert.strictEqual(tcp.localEndpoint.host, 'localhost');
50+
assert.strictEqual(tcp.remoteEndpoint.host, 'localhost');
51+
52+
process.report.excludeNetwork = true;
53+
report = process.report.getReport();
54+
tcp = report.libuv.find((uv) => uv.type === 'tcp');
55+
assert.notEqual(tcp, null);
56+
assert.strictEqual(tcp.localEndpoint.host, '127.0.0.1');
57+
assert.strictEqual(tcp.remoteEndpoint.host, '127.0.0.1');
58+
});
4159
});

0 commit comments

Comments
 (0)