From e3032708e056665de09fa5738c15b3526f883b70 Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Wed, 3 Apr 2019 12:19:32 -0400 Subject: [PATCH] report: add support for UDP connected sockets PR-URL: https://github.com/nodejs/node/pull/27072 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Santiago Gimeno --- src/node_report_utils.cc | 14 ++++++++++---- test/report/test-report-uv-handles.js | 23 +++++++++++++++++++---- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/node_report_utils.cc b/src/node_report_utils.cc index 40a9b9bec83d12..5013fd7a875216 100644 --- a/src/node_report_utils.cc +++ b/src/node_report_utils.cc @@ -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. diff --git a/test/report/test-report-uv-handles.js b/test/report/test-report-uv-handles.js index 65468bb48d1955..f03791f13461bc 100644 --- a/test/report/test-report-uv-handles.js +++ b/test/report/test-report-uv-handles.js @@ -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) => { @@ -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); }); @@ -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) { @@ -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) { @@ -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);