-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
net: add setTOS and getTOS 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
Open
amyssnippet
wants to merge
7
commits into
nodejs:main
Choose a base branch
from
amyssnippet:fix/61489
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+220
−2
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eb53d09
net: add setTOS and getTOS to Socket
amyssnippet 9e7a183
added doc
amyssnippet 14b3fb0
fixed some lint issues
amyssnippet f339cd0
already complete with validation and platform specific tests
amyssnippet d32f212
added: REPLACEME
amyssnippet 71ecbd7
common.mustCall used
amyssnippet 42d5142
undo: unrelated header was removed irrelevantly
amyssnippet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -32,9 +32,11 @@ | |
| #include "stream_base-inl.h" | ||
| #include "stream_wrap.h" | ||
| #include "util-inl.h" | ||
|
|
||
| #include <cstdlib> | ||
amyssnippet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #ifndef _WIN32 | ||
| #include <netinet/in.h> | ||
| #include <sys/socket.h> | ||
| #endif | ||
|
|
||
| namespace node { | ||
|
|
||
|
|
@@ -106,6 +108,8 @@ void TCPWrap::Initialize(Local<Object> target, | |
| GetSockOrPeerName<TCPWrap, uv_tcp_getpeername>); | ||
| SetProtoMethod(isolate, t, "setNoDelay", SetNoDelay); | ||
| SetProtoMethod(isolate, t, "setKeepAlive", SetKeepAlive); | ||
| SetProtoMethod(isolate, t, "setTOS", SetTOS); | ||
| SetProtoMethod(isolate, t, "getTOS", GetTOS); | ||
| SetProtoMethod(isolate, t, "reset", Reset); | ||
|
|
||
| #ifdef _WIN32 | ||
|
|
@@ -145,6 +149,8 @@ void TCPWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) { | |
| registry->Register(GetSockOrPeerName<TCPWrap, uv_tcp_getpeername>); | ||
| registry->Register(SetNoDelay); | ||
| registry->Register(SetKeepAlive); | ||
| registry->Register(SetTOS); | ||
| registry->Register(GetTOS); | ||
| registry->Register(Reset); | ||
| #ifdef _WIN32 | ||
| registry->Register(SetSimultaneousAccepts); | ||
|
|
@@ -208,6 +214,75 @@ void TCPWrap::SetKeepAlive(const FunctionCallbackInfo<Value>& args) { | |
| args.GetReturnValue().Set(err); | ||
| } | ||
|
|
||
| void TCPWrap::SetTOS(const FunctionCallbackInfo<Value>& args) { | ||
| TCPWrap* wrap; | ||
| ASSIGN_OR_RETURN_UNWRAP( | ||
| &wrap, args.This(), args.GetReturnValue().Set(UV_EBADF)); | ||
| Environment* env = wrap->env(); | ||
| int tos; | ||
| if (!args[0]->Int32Value(env->context()).To(&tos)) return; | ||
|
|
||
| int fd; | ||
| int err = uv_fileno(reinterpret_cast<uv_handle_t*>(&wrap->handle_), &fd); | ||
| if (err != 0) { | ||
| args.GetReturnValue().Set(err); | ||
| return; | ||
| } | ||
|
|
||
| #ifdef _WIN32 | ||
| args.GetReturnValue().Set(UV_ENOSYS); | ||
| #else | ||
| // Try IPv4 first | ||
| if (setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) == 0) { | ||
| args.GetReturnValue().Set(0); | ||
| return; | ||
| } | ||
|
|
||
| // If IPv4 failed, try IPv6 | ||
| if (setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) == 0) { | ||
| args.GetReturnValue().Set(0); | ||
| return; | ||
| } | ||
|
|
||
| // If both failed, return the negative errno | ||
| args.GetReturnValue().Set(-errno); | ||
|
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. Where is errno declared? |
||
| #endif | ||
| } | ||
|
|
||
| void TCPWrap::GetTOS(const FunctionCallbackInfo<Value>& args) { | ||
| TCPWrap* wrap; | ||
| ASSIGN_OR_RETURN_UNWRAP( | ||
| &wrap, args.This(), args.GetReturnValue().Set(UV_EBADF)); | ||
|
|
||
| int fd; | ||
| int err = uv_fileno(reinterpret_cast<uv_handle_t*>(&wrap->handle_), &fd); | ||
| if (err != 0) { | ||
| args.GetReturnValue().Set(err); | ||
| return; | ||
| } | ||
|
|
||
| int tos = 0; | ||
| socklen_t len = sizeof(tos); | ||
|
|
||
| #ifdef _WIN32 | ||
| args.GetReturnValue().Set(UV_ENOSYS); | ||
| #else | ||
| // Try IPv4 first | ||
| if (getsockopt(fd, IPPROTO_IP, IP_TOS, &tos, &len) == 0) { | ||
| args.GetReturnValue().Set(tos); | ||
| return; | ||
| } | ||
|
|
||
| // If IPv4 failed, try IPv6 | ||
| if (getsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, &len) == 0) { | ||
| args.GetReturnValue().Set(tos); | ||
| return; | ||
| } | ||
|
|
||
| // If both failed, return the negative errno | ||
| args.GetReturnValue().Set(-errno); | ||
| #endif | ||
| } | ||
|
|
||
| #ifdef _WIN32 | ||
| void TCPWrap::SetSimultaneousAccepts(const FunctionCallbackInfo<Value>& args) { | ||
|
|
||
This file contains hidden or 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 hidden or 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,72 @@ | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const net = require('net'); | ||
|
|
||
| const isWindows = common.isWindows; | ||
|
|
||
| const server = net.createServer( | ||
| common.mustCall((socket) => { | ||
| socket.end(); | ||
| }), | ||
| ); | ||
|
|
||
| server.listen( | ||
| 0, | ||
| common.mustCall(() => { | ||
| const port = server.address().port; | ||
| const client = net.connect(port); | ||
|
|
||
| client.on( | ||
| 'connect', | ||
| common.mustCall(() => { | ||
| // TEST 1: setTOS validation | ||
| // Should throw if value is not a number or out of range | ||
| assert.throws(() => client.setTOS('invalid'), { | ||
| code: 'ERR_INVALID_ARG_TYPE', | ||
| }); | ||
| assert.throws(() => client.setTOS(NaN), { | ||
| code: 'ERR_INVALID_ARG_TYPE', | ||
| }); | ||
| assert.throws(() => client.setTOS(256), { | ||
| code: 'ERR_OUT_OF_RANGE', | ||
| }); | ||
| assert.throws(() => client.setTOS(-1), { | ||
| code: 'ERR_OUT_OF_RANGE', | ||
| }); | ||
|
|
||
| // TEST 2: setting and getting TOS | ||
| const tosValue = 0x10; // IPTOS_LOWDELAY (16) | ||
|
|
||
| if (isWindows) { | ||
| // On Windows, your implementation returns UV_ENOSYS, which throws in JS | ||
| assert.throws(() => client.setTOS(tosValue), { | ||
| code: 'ENOSYS', | ||
| }); | ||
| } else { | ||
| // On POSIX (Linux/macOS), this should succeed | ||
| client.setTOS(tosValue); | ||
|
|
||
| // Verify values | ||
| // Note: Some OSs might mask the value (e.g. Linux sometimes masks ECN bits), | ||
| // but usually 0x10 should return 0x10. | ||
| const got = client.getTOS(); | ||
| assert.strictEqual( | ||
| got, | ||
| tosValue, | ||
| `Expected TOS ${tosValue}, got ${got}`, | ||
| ); | ||
| } | ||
|
|
||
| client.end(); | ||
| }), | ||
| ); | ||
|
|
||
| client.on( | ||
| 'end', | ||
| common.mustCall(() => { | ||
| server.close(); | ||
| }), | ||
| ); | ||
| }), | ||
| ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.