2020// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121
2222#include " tcp_wrap.h"
23-
2423#include " connect_wrap.h"
2524#include " connection_wrap.h"
2625#include " env-inl.h"
3433#include " util-inl.h"
3534#include < cerrno>
3635#include < cstdlib>
36+ #ifdef _WIN32
37+ #include < ws2tcpip.h>
38+ #endif
3739#ifndef _WIN32
3840#include < netinet/in.h>
3941#include < sys/socket.h>
@@ -223,16 +225,32 @@ void TCPWrap::SetTOS(const FunctionCallbackInfo<Value>& args) {
223225 int tos;
224226 if (!args[0 ]->Int32Value (env->context ()).To (&tos)) return ;
225227
226- int fd;
228+ // Use uv_os_fd_t (it handles int vs void* automatically)
229+ uv_os_fd_t fd;
227230 int err = uv_fileno (reinterpret_cast <uv_handle_t *>(&wrap->handle_ ), &fd);
228231 if (err != 0 ) {
229232 args.GetReturnValue ().Set (err);
230233 return ;
231234 }
232235
233236#ifdef _WIN32
234- args.GetReturnValue ().Set (UV_ENOSYS);
237+ // Windows implementation
238+ // Windows setsockopt expects 'const char*' for value and 'SOCKET' for fd
239+ if (setsockopt (reinterpret_cast <SOCKET>(fd),
240+ IPPROTO_IP,
241+ IP_TOS,
242+ reinterpret_cast <const char *>(&tos),
243+ sizeof (tos)) == 0 ) {
244+ args.GetReturnValue ().Set (0 );
245+ return ;
246+ }
247+ // Windows does not typically support IPV6_TCLASS in the same way,
248+ // or it might fallback. If you need IPv6 support on Windows,
249+ // you often need explicit version checks or specific headers.
250+ // For now, let's return the error if IPv4 failed.
251+ args.GetReturnValue ().Set (UV_EINVAL);
235252#else
253+ // Linux/macOS implementation
236254 // Try IPv4 first
237255 if (setsockopt (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) == 0 ) {
238256 args.GetReturnValue ().Set (0 );
@@ -255,7 +273,8 @@ void TCPWrap::GetTOS(const FunctionCallbackInfo<Value>& args) {
255273 ASSIGN_OR_RETURN_UNWRAP (
256274 &wrap, args.This (), args.GetReturnValue ().Set (UV_EBADF));
257275
258- int fd;
276+ // Use uv_os_fd_t
277+ uv_os_fd_t fd;
259278 int err = uv_fileno (reinterpret_cast <uv_handle_t *>(&wrap->handle_ ), &fd);
260279 if (err != 0 ) {
261280 args.GetReturnValue ().Set (err);
@@ -266,8 +285,19 @@ void TCPWrap::GetTOS(const FunctionCallbackInfo<Value>& args) {
266285 socklen_t len = sizeof (tos);
267286
268287#ifdef _WIN32
269- args.GetReturnValue ().Set (UV_ENOSYS);
288+ // Windows implementation
289+ // Windows getsockopt expects 'char*' and 'SOCKET'
290+ if (getsockopt (reinterpret_cast <SOCKET>(fd),
291+ IPPROTO_IP,
292+ IP_TOS,
293+ reinterpret_cast <char *>(&tos),
294+ &len) == 0 ) {
295+ args.GetReturnValue ().Set (tos);
296+ return ;
297+ }
298+ args.GetReturnValue ().Set (UV_EINVAL);
270299#else
300+ // Linux/macOS implementation
271301 // Try IPv4 first
272302 if (getsockopt (fd, IPPROTO_IP, IP_TOS, &tos, &len) == 0 ) {
273303 args.GetReturnValue ().Set (tos);
0 commit comments