Skip to content

Commit

Permalink
net: add local family
Browse files Browse the repository at this point in the history
PR-URL: #43975
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
theanarkh authored and juanarbol committed Oct 11, 2022
1 parent 579e066 commit 092239a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
11 changes: 11 additions & 0 deletions doc/api/net.md
Expand Up @@ -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'`.
Expand Down Expand Up @@ -1007,6 +1008,16 @@ added: v0.9.6

The numeric representation of the local port. For example, `80` or `21`.

### `socket.localFamily`

<!-- YAML
added: REPLACEME
-->

* {string}

The string representation of the local IP family. `'IPv4'` or `'IPv6'`.

### `socket.pause()`

* Returns: {net.Socket} The socket itself.
Expand Down
4 changes: 4 additions & 0 deletions lib/net.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-net-local-address-port.js
Expand Up @@ -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();
});
Expand Down

0 comments on commit 092239a

Please sign in to comment.