From 1a66fa4db78984cdd8f09c1f3b68c3742b2a652d Mon Sep 17 00:00:00 2001 From: theanarkh Date: Wed, 27 Jul 2022 06:37:49 +0800 Subject: [PATCH] net: add local family PR-URL: https://github.com/nodejs/node/pull/43975 Reviewed-By: Paolo Insogna Reviewed-By: Antoine du Hamel Reviewed-By: Matteo Collina Reviewed-By: Luigi Pinca --- doc/api/net.md | 11 +++++++++++ lib/net.js | 4 ++++ test/parallel/test-net-local-address-port.js | 1 + 3 files changed, 16 insertions(+) diff --git a/doc/api/net.md b/doc/api/net.md index ea4ad263755eee..04b483584395e6 100644 --- a/doc/api/net.md +++ b/doc/api/net.md @@ -270,6 +270,7 @@ TCP server, the argument is as follows, otherwise the argument is `undefined`. * `data` {Object} The argument passed to event listener. * `localAddress` {string} Local address. * `localPort` {number} Local port. + * `localFamily` {string} Local family. * `remoteAddress` {string} Remote address. * `remotePort` {number} Remote port. * `remoteFamily` {string} Remote IP family. `'IPv4'` or `'IPv6'`. @@ -1007,6 +1008,16 @@ added: v0.9.6 The numeric representation of the local port. For example, `80` or `21`. +### `socket.localFamily` + + + +* {string} + +The string representation of the local IP family. `'IPv4'` or `'IPv6'`. + ### `socket.pause()` * Returns: {net.Socket} The socket itself. diff --git a/lib/net.js b/lib/net.js index 5f2aa9c5de1ed8..6b5ce6f91eb308 100644 --- a/lib/net.js +++ b/lib/net.js @@ -849,6 +849,9 @@ protoGetter('localPort', function localPort() { return this._getsockname().port; }); +protoGetter('localFamily', function localFamily() { + return this._getsockname().family; +}); Socket.prototype[kAfterAsyncWrite] = function() { this[kLastWriteQueueSize] = 0; @@ -1683,6 +1686,7 @@ function onconnection(err, clientHandle) { clientHandle.getsockname(localInfo); data.localAddress = localInfo.address; data.localPort = localInfo.port; + data.localFamily = localInfo.family; } if (clientHandle.getpeername) { const remoteInfo = ObjectCreate(null); diff --git a/test/parallel/test-net-local-address-port.js b/test/parallel/test-net-local-address-port.js index dfd7ef359b71d2..cfc6f61ef35ad8 100644 --- a/test/parallel/test-net-local-address-port.js +++ b/test/parallel/test-net-local-address-port.js @@ -27,6 +27,7 @@ const net = require('net'); const server = net.createServer(common.mustCall(function(socket) { assert.strictEqual(socket.localAddress, common.localhostIPv4); assert.strictEqual(socket.localPort, this.address().port); + assert.strictEqual(socket.localFamily, this.address().family); socket.on('end', function() { server.close(); });