Skip to content

Commit

Permalink
net: add ability to reset a tcp socket
Browse files Browse the repository at this point in the history
make it possible to forcibly rest a tcp socket:

* add a new method `Socket.prototype.resetAndDestroy`

* add a new flag `resetAndClosing` to make `_destroy` calls
the `reset` instead of close while destroying a Socket.

* add new methods `TCPWrap::Reset` to be a wrap of `uv_tcp_close_reset`

* change `HandleWrap::state_` from private to protected.
This is essential for keeping the same behaviour between
`TCPWrap::Reset` and `HandleWrap::Close`

Fixes: nodejs#27428
  • Loading branch information
PupilTong authored and PupilTong committed May 18, 2022
1 parent cfa6d57 commit a417977
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 6 deletions.
14 changes: 14 additions & 0 deletions doc/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,20 @@ added: v0.5.10

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

### `socket.resetAndDestroy()`

<!-- YAML
added: REPLACEME
-->

* Returns: {net.Socket}

Close the TCP connection by sending an RST packet and destroy the stream.
If this TCP socket is in connecting status, it will send an RST packet and destroy this TCP socket once it is connected.
Otherwise, it will call `socket.destroy` this socket with an `ERR_SOCKET_CLOSED` Error.
If this is not a TCP socket (for example, a pipe), calling this method will immediately throw an `ERR_INVALID_HANDLE_TYPE` Error.
See [`writable.destroy()`][] for further details.

### `socket.resume()`

* Returns: {net.Socket} The socket itself.
Expand Down
24 changes: 18 additions & 6 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,12 +642,17 @@ Socket.prototype.end = function(data, encoding, callback) {
};

Socket.prototype.resetAndDestroy = function() {
if (!(this._handle instanceof TCP))
throw new ERR_INVALID_HANDLE_TYPE();
if (this._handle) {
debug('reset');
this.resetAndClosing = true;
return this.destroy();
if (!(this._handle instanceof TCP))
throw new ERR_INVALID_HANDLE_TYPE();
if (this.connecting) {
debug('reset wait for connection');
this.once('connect', () => this._reset());
} else {
this._reset();
}
} else {
this.destroy(new ERR_SOCKET_CLOSED());
}
return this;
};
Expand Down Expand Up @@ -722,12 +727,13 @@ Socket.prototype._destroy = function(exception, cb) {
this[kBytesWritten] = this._handle.bytesWritten;

if (this.resetAndClosing) {
this.resetAndClosing = false;
const err = this._handle.reset(() => {
debug('emit close');
this.emit('close', isException);
});
if (err)
throw errnoException(err, 'reset');
this.emit('error', errnoException(err, 'reset'));
} else {
this._handle.close(() => {
debug('emit close');
Expand All @@ -752,6 +758,12 @@ Socket.prototype._destroy = function(exception, cb) {
}
};

Socket.prototype._reset = function() {
debug('reset connection');
this.resetAndClosing = true;
return this.destroy();
};

Socket.prototype._getpeername = function() {
if (!this._handle || !this._handle.getpeername) {
return this._peername || {};
Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-net-connect-reset-after-destroy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
const common = require('../common');
const net = require('net');
const assert = require('assert');

const server = net.createServer();
server.listen(0, common.mustCall(function() {
const port = server.address().port;
const conn = net.createConnection(port);
server.on('connection', (socket) => {
socket.on('error', common.expectsError({
code: 'ECONNRESET',
message: 'read ECONNRESET',
name: 'Error'
}));
});

conn.on('connect', common.mustCall(function() {
assert.strictEqual(conn, conn.resetAndDestroy().destroy());
conn.on('error', common.mustNotCall());

conn.write(Buffer.from('fzfzfzfzfz'), common.expectsError({
code: 'ERR_STREAM_DESTROYED',
message: 'Cannot call write after a stream was destroyed',
name: 'Error'
}));
server.close();
}));
}));
13 changes: 13 additions & 0 deletions test/parallel/test-net-connect-reset-before-connected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';
const common = require('../common');
const net = require('net');

const server = net.createServer();
server.listen(0);
const port = server.address().port;
const socket = net.connect(port, common.localhostIPv4, common.mustNotCall());
socket.on('error', common.mustNotCall());
server.close();
socket.resetAndDestroy();
// `reset` waiting socket connected to sent the RST packet
socket.destroy();
20 changes: 20 additions & 0 deletions test/parallel/test-net-connect-reset-until-connected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const common = require('../common');
const net = require('net');

const server = net.createServer();
server.listen(0, common.mustCall(function() {
const port = server.address().port;
const conn = net.createConnection(port);
conn.on('close', common.mustCall());
server.on('connection', (socket) => {
socket.on('error', common.expectsError({
code: 'ECONNRESET',
message: 'read ECONNRESET',
name: 'Error'
}));
server.close();
});
conn.resetAndDestroy();
}));
13 changes: 13 additions & 0 deletions test/parallel/test-net-connect-reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';
const common = require('../common');
const net = require('net');

const socket = new net.Socket();
socket.resetAndDestroy();
// Emit error if socket is not connecting/connected
socket.on('error', common.mustCall(
common.expectsError({
code: 'ERR_SOCKET_CLOSED',
name: 'Error'
}))
);
36 changes: 36 additions & 0 deletions test/parallel/test-net-server-reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');

const sockets = [];

const server = net.createServer(function(c) {
c.on('close', common.mustCall());

sockets.push(c);

if (sockets.length === 2) {
assert.strictEqual(server.close(), server);
sockets.forEach((c) => c.resetAndDestroy());
}
});

server.on('close', common.mustCall());

assert.strictEqual(server, server.listen(0, () => {
net.createConnection(server.address().port)
.on('error', common.mustCall(
common.expectsError({
code: 'ECONNRESET',
name: 'Error'
}))
);
net.createConnection(server.address().port)
.on('error', common.mustCall(
common.expectsError({
code: 'ECONNRESET',
name: 'Error'
}))
);
}));
30 changes: 30 additions & 0 deletions test/parallel/test-net-socket-reset-send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const common = require('../common');
const net = require('net');
const assert = require('assert');

const server = net.createServer();
server.listen(0, common.mustCall(() => {
const port = server.address().port;
const conn = net.createConnection(port);
server.on('connection', (socket) => {
socket.on('error', common.expectsError({
code: 'ECONNRESET',
message: 'read ECONNRESET',
name: 'Error'
}));
});

conn.on('connect', common.mustCall(() => {
assert.strictEqual(conn, conn.resetAndDestroy().destroy());
conn.on('error', common.mustNotCall());

conn.write(Buffer.from('fzfzfzfzfz'), common.expectsError({
code: 'ERR_STREAM_DESTROYED',
message: 'Cannot call write after a stream was destroyed',
name: 'Error'
}));
server.close();
}));
}));
15 changes: 15 additions & 0 deletions test/parallel/test-net-socket-reset-twice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
const common = require('../common');
const net = require('net');

const server = net.createServer();
server.listen(0);
const port = server.address().port;
const conn = net.createConnection(port);

conn.on('error', common.mustCall(() => {
conn.resetAndDestroy();
}));

conn.on('close', common.mustCall());
server.close();

0 comments on commit a417977

Please sign in to comment.