Skip to content

Commit a00c190

Browse files
committed
report: fix network queries in getReport libuv with exclude-network
1 parent 8807549 commit a00c190

File tree

4 files changed

+90
-22
lines changed

4 files changed

+90
-22
lines changed

src/node_report.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ 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(),
206+
exclude_network ? WalkHandleNoNetwork : WalkHandleNetwork,
207+
static_cast<void*>(&writer));
206208

207209
writer.json_start();
208210
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: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,51 @@ 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;
1920
}
2021

2122
uv_getnameinfo_t endpoint;
2223
char* host = nullptr;
23-
char hostbuf[INET6_ADDRSTRLEN];
2424
const int family = addr->sa_family;
2525
const int port = ntohs(family == AF_INET ?
2626
reinterpret_cast<sockaddr_in*>(addr)->sin_port :
2727
reinterpret_cast<sockaddr_in6*>(addr)->sin6_port);
2828

29-
if (uv_getnameinfo(h->loop, &endpoint, nullptr, addr, NI_NUMERICSERV) == 0) {
29+
writer->json_objectstart(name);
30+
if (!exclude_network &&
31+
uv_getnameinfo(h->loop, &endpoint, nullptr, addr, NI_NUMERICSERV) == 0) {
3032
host = endpoint.host;
3133
DCHECK_EQ(port, std::stoi(endpoint.service));
34+
writer->json_keyvalue("host", host);
35+
}
36+
37+
if (family == AF_INET) {
38+
char ipbuf[INET_ADDRSTRLEN];
39+
if (uv_ip4_name(
40+
reinterpret_cast<sockaddr_in*>(addr), ipbuf, sizeof(ipbuf)) == 0) {
41+
writer->json_keyvalue("ip4", ipbuf);
42+
if (host == nullptr) writer->json_keyvalue("host", ipbuf);
43+
}
3244
} else {
33-
const void* src = family == AF_INET ?
34-
static_cast<void*>(
35-
&(reinterpret_cast<sockaddr_in*>(addr)->sin_addr)) :
36-
static_cast<void*>(
37-
&(reinterpret_cast<sockaddr_in6*>(addr)->sin6_addr));
38-
if (uv_inet_ntop(family, src, hostbuf, sizeof(hostbuf)) == 0) {
39-
host = hostbuf;
45+
char ipbuf[INET6_ADDRSTRLEN];
46+
if (uv_ip6_name(
47+
reinterpret_cast<sockaddr_in6*>(addr), ipbuf, sizeof(ipbuf)) == 0) {
48+
writer->json_keyvalue("ip6", ipbuf);
49+
if (host == nullptr) writer->json_keyvalue("host", ipbuf);
4050
}
4151
}
42-
writer->json_objectstart(name);
43-
if (host != nullptr) {
44-
writer->json_keyvalue("host", host);
45-
}
4652
writer->json_keyvalue("port", port);
4753
writer->json_objectend();
4854
}
4955

5056
// Utility function to format libuv socket information.
51-
static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) {
57+
static void ReportEndpoints(uv_handle_t* h,
58+
JSONWriter* writer,
59+
bool exclude_network) {
5260
struct sockaddr_storage addr_storage;
5361
struct sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
5462
uv_any_handle* handle = reinterpret_cast<uv_any_handle*>(h);
@@ -65,7 +73,8 @@ static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) {
6573
default:
6674
break;
6775
}
68-
ReportEndpoint(h, rc == 0 ? addr : nullptr, "localEndpoint", writer);
76+
ReportEndpoint(
77+
h, rc == 0 ? addr : nullptr, "localEndpoint", writer, exclude_network);
6978

7079
switch (h->type) {
7180
case UV_UDP:
@@ -77,7 +86,8 @@ static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) {
7786
default:
7887
break;
7988
}
80-
ReportEndpoint(h, rc == 0 ? addr : nullptr, "remoteEndpoint", writer);
89+
ReportEndpoint(
90+
h, rc == 0 ? addr : nullptr, "remoteEndpoint", writer, exclude_network);
8191
}
8292

8393
// Utility function to format libuv pipe information.
@@ -155,7 +165,7 @@ static void ReportPath(uv_handle_t* h, JSONWriter* writer) {
155165
}
156166

157167
// Utility function to walk libuv handles.
158-
void WalkHandle(uv_handle_t* h, void* arg) {
168+
void WalkHandle(uv_handle_t* h, void* arg, bool exclude_network = false) {
159169
const char* type = uv_handle_type_name(h->type);
160170
JSONWriter* writer = static_cast<JSONWriter*>(arg);
161171
uv_any_handle* handle = reinterpret_cast<uv_any_handle*>(h);
@@ -177,7 +187,7 @@ void WalkHandle(uv_handle_t* h, void* arg) {
177187
break;
178188
case UV_TCP:
179189
case UV_UDP:
180-
ReportEndpoints(h, writer);
190+
ReportEndpoints(h, writer, exclude_network);
181191
break;
182192
case UV_NAMED_PIPE:
183193
ReportPipeEndpoints(h, writer);
@@ -267,6 +277,11 @@ void WalkHandle(uv_handle_t* h, void* arg) {
267277
}
268278
writer->json_end();
269279
}
270-
280+
void WalkHandleNetwork(uv_handle_t* h, void* arg) {
281+
WalkHandle(h, arg, false);
282+
}
283+
void WalkHandleNoNetwork(uv_handle_t* h, void* arg) {
284+
WalkHandle(h, arg, true);
285+
}
271286
} // namespace report
272287
} // namespace node

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
require('../common');
3+
const http = require('node:http');
34
const assert = require('node:assert');
45
const { spawnSync } = require('node:child_process');
56
const tmpdir = require('../common/tmpdir');
@@ -38,4 +39,53 @@ describe('report exclude network option', () => {
3839
const report = process.report.getReport();
3940
assert.strictEqual(report.header.networkInterfaces, undefined);
4041
});
42+
43+
it('should not do DNS queries in libuv if exclude network', async () => {
44+
const server = http.createServer(function(req, res) {
45+
res.writeHead(200, { 'Content-Type': 'text/plain' });
46+
res.end();
47+
});
48+
const port = await new Promise((resolve) => server.listen(0, async () => {
49+
await Promise.all([
50+
fetch('http://127.0.0.1:' + server.address().port),
51+
fetch('http://[::1]:' + server.address().port),
52+
]);
53+
resolve(server.address().port);
54+
server.close();
55+
}));
56+
process.report.excludeNetwork = false;
57+
let report = process.report.getReport();
58+
let tcp = report.libuv.filter((uv) => uv.type === 'tcp' && uv.remoteEndpoint?.port === port);
59+
assert.strictEqual(tcp.length, 2);
60+
const findHandle = (host, local = true, ip4 = true) => {
61+
return tcp.some(
62+
({ [local ? 'localEndpoint' : 'remoteEndpoint']: ep }) =>
63+
(ep[ip4 ? 'ip4' : 'ip6'] === (ip4 ? '127.0.0.1' : '::1') && ep.host === host),
64+
);
65+
};
66+
try {
67+
assert.ok(findHandle('localhost'), 'local localhost handle not found');
68+
assert.ok(findHandle('localhost', false), 'remote localhost handle not found');
69+
70+
assert.ok(findHandle('ip6-localhost', true, false), 'local ip6-localhost handle not found');
71+
assert.ok(findHandle('ip6-localhost', false, false), 'remote ip6-localhost handle not found');
72+
} catch (e) {
73+
throw new Error(e.message + ' in ' + JSON.stringify(tcp, null, 2));
74+
}
75+
76+
process.report.excludeNetwork = true;
77+
report = process.report.getReport();
78+
tcp = report.libuv.filter((uv) => uv.type === 'tcp' && uv.remoteEndpoint?.port === port);
79+
80+
try {
81+
assert.strictEqual(tcp.length, 2);
82+
assert.ok(findHandle('127.0.0.1'), 'local 127.0.0.1 handle not found');
83+
assert.ok(findHandle('127.0.0.1', false), 'remote 127.0.0.1 handle not found');
84+
85+
assert.ok(findHandle('::1', true, false), 'local ::1 handle not found');
86+
assert.ok(findHandle('::1', false, false), 'remote ::1 handle not found');
87+
} catch (e) {
88+
throw new Error(e.message + ' in ' + JSON.stringify(tcp, null, 2));
89+
}
90+
});
4191
});

0 commit comments

Comments
 (0)