Skip to content

Commit

Permalink
net: add local family
Browse files Browse the repository at this point in the history
  • Loading branch information
theanarkh committed Jul 24, 2022
1 parent 130bdf0 commit 37cded7
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` {number} 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 @@ -834,6 +834,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 @@ -1668,6 +1671,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 37cded7

Please sign in to comment.