Skip to content

Commit

Permalink
report: add support for UDP connected sockets
Browse files Browse the repository at this point in the history
PR-URL: #27072
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Santiago Gimeno <[email protected]>
  • Loading branch information
richardlau committed Apr 5, 2019
1 parent 181052d commit e303270
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
14 changes: 10 additions & 4 deletions src/node_report_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,17 @@ static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) {
}
ReportEndpoint(h, rc == 0 ? addr : nullptr, "localEndpoint", writer);

if (h->type == UV_TCP) {
// Get the remote end of the connection.
rc = uv_tcp_getpeername(&handle->tcp, addr, &addr_size);
ReportEndpoint(h, rc == 0 ? addr : nullptr, "remoteEndpoint", writer);
switch (h->type) {
case UV_UDP:
rc = uv_udp_getpeername(&handle->udp, addr, &addr_size);
break;
case UV_TCP:
rc = uv_tcp_getpeername(&handle->tcp, addr, &addr_size);
break;
default:
break;
}
ReportEndpoint(h, rc == 0 ? addr : nullptr, "remoteEndpoint", writer);
}

// Utility function to format libuv path information.
Expand Down
23 changes: 19 additions & 4 deletions test/report/test-report-uv-handles.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ if (process.argv[2] === 'child') {
// Datagram socket for udp uv handles.
const dgram = require('dgram');
const udp_socket = dgram.createSocket('udp4');
udp_socket.bind({});
const connected_udp_socket = dgram.createSocket('udp4');
udp_socket.bind({}, common.mustCall(() => {
connected_udp_socket.connect(udp_socket.address().port);
}));

// Simple server/connection to create tcp uv handles.
const server = http.createServer((req, res) => {
Expand All @@ -50,6 +53,7 @@ if (process.argv[2] === 'child') {
server.close(() => {
if (watcher) watcher.close();
fs.unwatchFile(__filename);
connected_udp_socket.close();
udp_socket.close();
process.removeListener('disconnect', exit);
});
Expand Down Expand Up @@ -97,6 +101,7 @@ if (process.argv[2] === 'child') {
const prefix = common.isWindows ? '\\\\?\\' : '';
const expected_filename = `${prefix}${__filename}`;
const found_tcp = [];
const found_udp = [];
// Functions are named to aid debugging when they are not called.
const validators = {
fs_event: common.mustCall(function fs_event_validator(handle) {
Expand Down Expand Up @@ -138,10 +143,17 @@ if (process.argv[2] === 'child') {
assert.strictEqual(handle.repeat, 0);
}),
udp: common.mustCall(function udp_validator(handle) {
assert.strictEqual(handle.localEndpoint.port,
child_data.udp_address.port);
if (handle.remoteEndpoint === null) {
assert.strictEqual(handle.localEndpoint.port,
child_data.udp_address.port);
found_udp.push('unconnected');
} else {
assert.strictEqual(handle.remoteEndpoint.port,
child_data.udp_address.port);
found_udp.push('connected');
}
assert(handle.is_referenced);
}),
}, 2),
};
console.log(report.libuv);
for (const entry of report.libuv) {
Expand All @@ -150,6 +162,9 @@ if (process.argv[2] === 'child') {
for (const socket of ['listening', 'inbound', 'outbound']) {
assert(found_tcp.includes(socket), `${socket} TCP socket was not found`);
}
for (const socket of ['connected', 'unconnected']) {
assert(found_udp.includes(socket), `${socket} UDP socket was not found`);
}

// Common report tests.
helper.validateContent(stdout);
Expand Down

0 comments on commit e303270

Please sign in to comment.