Skip to content

Commit

Permalink
net: add local family
Browse files Browse the repository at this point in the history
PR-URL: nodejs#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 Fyko committed Sep 15, 2022
1 parent 0a90434 commit 33951ac
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
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,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 @@ -1047,6 +1048,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
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,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 @@ -1669,6 +1672,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
Original file line number Diff line number Diff line change
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 33951ac

Please sign in to comment.