forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net: add ability to reset a tcp socket
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
Showing
9 changed files
with
188 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
})); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
})) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
})) | ||
); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
})); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |