-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
net: add setTypeOfService and getTypeOfService to Socket #61503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
eb53d09
9e7a183
14b3fb0
f339cd0
d32f212
71ecbd7
c0407b2
8439cfd
fe329db
778580a
efb5f88
8dce839
1fb4683
1084571
0a90f1c
603445d
f22a74c
b272fda
c6f1153
fe14b34
041c30a
7516aff
83d875c
c03162b
b884430
c39d13e
98747a7
551a5d3
2bb4591
a2d4c36
b04b62f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -57,6 +57,7 @@ const { | |||||||||||||||||||||||||||
| const assert = require('internal/assert'); | ||||||||||||||||||||||||||||
| const { | ||||||||||||||||||||||||||||
| UV_EADDRINUSE, | ||||||||||||||||||||||||||||
| UV_EBADF, | ||||||||||||||||||||||||||||
| UV_EINVAL, | ||||||||||||||||||||||||||||
| UV_ENOTCONN, | ||||||||||||||||||||||||||||
| UV_ECANCELED, | ||||||||||||||||||||||||||||
|
|
@@ -358,6 +359,7 @@ const kBytesWritten = Symbol('kBytesWritten'); | |||||||||||||||||||||||||||
| const kSetNoDelay = Symbol('kSetNoDelay'); | ||||||||||||||||||||||||||||
| const kSetKeepAlive = Symbol('kSetKeepAlive'); | ||||||||||||||||||||||||||||
| const kSetKeepAliveInitialDelay = Symbol('kSetKeepAliveInitialDelay'); | ||||||||||||||||||||||||||||
| const kSetTOS = Symbol('kSetTOS'); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| function Socket(options) { | ||||||||||||||||||||||||||||
| if (!(this instanceof Socket)) return new Socket(options); | ||||||||||||||||||||||||||||
|
|
@@ -473,6 +475,10 @@ function Socket(options) { | |||||||||||||||||||||||||||
| this[kSetNoDelay] = Boolean(options.noDelay); | ||||||||||||||||||||||||||||
| this[kSetKeepAlive] = Boolean(options.keepAlive); | ||||||||||||||||||||||||||||
| this[kSetKeepAliveInitialDelay] = ~~(options.keepAliveInitialDelay / 1000); | ||||||||||||||||||||||||||||
| if (options.tos !== undefined) { | ||||||||||||||||||||||||||||
| validateInt32(options.tos, 'options.tos', 0, 255); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| this[kSetTOS] = options.tos; | ||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^ This isn't documented, is it? Should it also be called
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i have updated the net.md doc, and will roll out in the next commit. and i have kept ADDME in the version, as i dont have much knowledge about versioning, so kindly update me from your side regarding that, i am attaching a screenshot of that here and code as well
### `new net.Socket([options])`
<!-- YAML
added: v0.3.4
changes:
- version: ADDME
pr-url: https://github.com/nodejs/node/pull/61503
description: Added `typeOfService` option.
- version: v15.14.0
pr-url: https://github.com/nodejs/node/pull/37735
description: AbortSignal support was added.
- version: v12.10.0
pr-url: https://github.com/nodejs/node/pull/25436
description: Added `onread` option.
-->
* `options` {Object} Available options are:
* `allowHalfOpen` {boolean} If set to `false`, then the socket will
automatically end the writable side when the readable side ends. See
[`net.createServer()`][] and the [`'end'`][] event for details. **Default:**
`false`.
* `blockList` {net.BlockList} `blockList` can be used for disabling outbound
access to specific IP addresses, IP ranges, or IP subnets.
* `fd` {number} If specified, wrap around an existing socket with
the given file descriptor, otherwise a new socket will be created.
* `keepAlive` {boolean} If set to `true`, it enables keep-alive functionality on
the socket immediately after the connection is established, similarly on what
is done in [`socket.setKeepAlive()`][]. **Default:** `false`.
* `keepAliveInitialDelay` {number} If set to a positive number, it sets the
initial delay before the first keepalive probe is sent on an idle socket. **Default:** `0`.
* `noDelay` {boolean} If set to `true`, it disables the use of Nagle's algorithm
immediately after the socket is established. **Default:** `false`.
* `onread` {Object} If specified, incoming data is stored in a single `buffer`
and passed to the supplied `callback` when data arrives on the socket.
This will cause the streaming functionality to not provide any data.
The socket will emit events like `'error'`, `'end'`, and `'close'`
as usual. Methods like `pause()` and `resume()` will also behave as
expected.
* `buffer` {Buffer|Uint8Array|Function} Either a reusable chunk of memory to
use for storing incoming data or a function that returns such.
* `callback` {Function} This function is called for every chunk of incoming
data. Two arguments are passed to it: the number of bytes written to
`buffer` and a reference to `buffer`. Return `false` from this function to
implicitly `pause()` the socket. This function will be executed in the
global context.
* `readable` {boolean} Allow reads on the socket when an `fd` is passed,
otherwise ignored. **Default:** `false`.
* `signal` {AbortSignal} An Abort signal that may be used to destroy the
socket.
* `typeOfService` {number} The initial Type of Service (TOS) value.
* `writable` {boolean} Allow writes on the socket when an `fd` is passed,
otherwise ignored. **Default:** `false`.
* Returns: {net.Socket}
Creates a new socket object.
The newly created socket can be either a TCP socket or a streaming [IPC][]
endpoint, depending on what it [`connect()`][`socket.connect()`] to. |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Shut down the socket when we're finished with it. | ||||||||||||||||||||||||||||
| this.on('end', onReadableStreamEnd); | ||||||||||||||||||||||||||||
|
|
@@ -652,6 +658,59 @@ Socket.prototype.setKeepAlive = function(enable, initialDelayMsecs) { | |||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Socket.prototype.setTypeOfService = function(tos) { | ||||||||||||||||||||||||||||
| if (NumberIsNaN(tos)) { | ||||||||||||||||||||||||||||
| throw new ERR_INVALID_ARG_TYPE('tos', 'number', tos); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| validateInt32(tos, 'tos', 0, 255); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (!this._handle) { | ||||||||||||||||||||||||||||
| this[kSetTOS] = tos; | ||||||||||||||||||||||||||||
| return this; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (!this._handle.setTypeOfService) { | ||||||||||||||||||||||||||||
| this[kSetTOS] = tos; | ||||||||||||||||||||||||||||
| return this; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
+667
to
+675
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (tos !== this[kSetTOS]) { | ||||||||||||||||||||||||||||
| this[kSetTOS] = tos; | ||||||||||||||||||||||||||||
| const err = this._handle.setTypeOfService(tos); | ||||||||||||||||||||||||||||
| // On Windows, setting TOS is often restricted or returns error codes even if partially applied. | ||||||||||||||||||||||||||||
| // We treat this as a "best effort" operation and do not throw on Windows. | ||||||||||||||||||||||||||||
| if (err && !isWindows) { | ||||||||||||||||||||||||||||
| throw new ErrnoException(err, 'setTypeOfService'); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return this; | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Socket.prototype.getTypeOfService = function() { | ||||||||||||||||||||||||||||
| if (!this._handle) { | ||||||||||||||||||||||||||||
| // Return cached value if set, otherwise default to 0 | ||||||||||||||||||||||||||||
| return this[kSetTOS] !== undefined ? this[kSetTOS] : 0; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (!this._handle.getTypeOfService) { | ||||||||||||||||||||||||||||
| return this[kSetTOS] !== undefined ? this[kSetTOS] : 0; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
+692
to
+699
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const res = this._handle.getTypeOfService(); | ||||||||||||||||||||||||||||
| if (typeof res === 'number' && res < 0) { | ||||||||||||||||||||||||||||
| // On Windows, getsockopt(IP_TOS) often fails. In that case, fall back | ||||||||||||||||||||||||||||
| // to the cached value we attempted to set, or 0. | ||||||||||||||||||||||||||||
| if (isWindows) { | ||||||||||||||||||||||||||||
| return this[kSetTOS] !== undefined ? this[kSetTOS] : 0; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| throw new ErrnoException(res, 'getTypeOfService'); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| return res; | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Socket.prototype.address = function() { | ||||||||||||||||||||||||||||
| return this._getsockname(); | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
|
|
@@ -1619,6 +1678,15 @@ function afterConnect(status, handle, req, readable, writable) { | |||||||||||||||||||||||||||
| self._handle.setKeepAlive(true, self[kSetKeepAliveInitialDelay]); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (self[kSetTOS] !== undefined && self._handle.setTypeOfService) { | ||||||||||||||||||||||||||||
| const err = self._handle.setTypeOfService(self[kSetTOS]); | ||||||||||||||||||||||||||||
| // On Windows, setting TOS is best-effort. If it fails, we shouldn't destroy | ||||||||||||||||||||||||||||
| // the connection or emit an error, as the socket is otherwise healthy. | ||||||||||||||||||||||||||||
| if (err && err !== UV_EBADF && !isWindows) { | ||||||||||||||||||||||||||||
| self.emit('error', new ErrnoException(err, 'setTypeOfService')); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| self.emit('connect'); | ||||||||||||||||||||||||||||
| self.emit('ready'); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||

Uh oh!
There was an error while loading. Please reload this page.