-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Fixes: #27428 PR-URL: #43112 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
- Loading branch information
Showing
12 changed files
with
232 additions
and
5 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
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(); |