Skip to content

Commit

Permalink
src: add args validation method
Browse files Browse the repository at this point in the history
  • Loading branch information
ardinugrxha committed Jan 7, 2025
1 parent b0c65bb commit 8bfc0fb
Showing 1 changed file with 67 additions and 77 deletions.
144 changes: 67 additions & 77 deletions src/tcp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

#include <cstdlib>


namespace node {

using v8::Boolean;
Expand Down Expand Up @@ -68,7 +67,6 @@ MaybeLocal<Object> TCPWrap::Instantiate(Environment* env,
constructor->NewInstance(env->context(), 1, &type_value));
}


void TCPWrap::Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
Expand Down Expand Up @@ -124,9 +122,7 @@ void TCPWrap::Initialize(Local<Object> target,
NODE_DEFINE_CONSTANT(constants, SERVER);
NODE_DEFINE_CONSTANT(constants, UV_TCP_IPV6ONLY);
NODE_DEFINE_CONSTANT(constants, UV_TCP_REUSEPORT);
target->Set(context,
env->constants_string(),
constants).Check();
target->Set(context, env->constants_string(), constants).Check();
}

void TCPWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
Expand Down Expand Up @@ -174,15 +170,13 @@ void TCPWrap::New(const FunctionCallbackInfo<Value>& args) {
new TCPWrap(env, args.This(), provider);
}


TCPWrap::TCPWrap(Environment* env, Local<Object> object, ProviderType provider)
: ConnectionWrap(env, object, provider) {
int r = uv_tcp_init(env->event_loop(), &handle_);
CHECK_EQ(r, 0); // How do we proxy this error up to javascript?
// Suggestion: uv_tcp_init() returns void.
}


void TCPWrap::SetNoDelay(const FunctionCallbackInfo<Value>& args) {
TCPWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(
Expand All @@ -192,7 +186,6 @@ void TCPWrap::SetNoDelay(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(err);
}


void TCPWrap::SetKeepAlive(const FunctionCallbackInfo<Value>& args) {
TCPWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(
Expand All @@ -205,7 +198,6 @@ void TCPWrap::SetKeepAlive(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(err);
}


#ifdef _WIN32
void TCPWrap::SetSimultaneousAccepts(const FunctionCallbackInfo<Value>& args) {
TCPWrap* wrap;
Expand All @@ -217,7 +209,6 @@ void TCPWrap::SetSimultaneousAccepts(const FunctionCallbackInfo<Value>& args) {
}
#endif


void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) {
TCPWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(
Expand All @@ -228,8 +219,7 @@ void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) {
int fd = static_cast<int>(val);
int err = uv_tcp_open(&wrap->handle_, fd);

if (err == 0)
wrap->set_fd(fd);
if (err == 0) wrap->set_fd(fd);

args.GetReturnValue().Set(err);
}
Expand Down Expand Up @@ -259,9 +249,8 @@ void TCPWrap::Bind(
int err = uv_ip_addr(*ip_address, port, &addr);

if (err == 0) {
err = uv_tcp_bind(&wrap->handle_,
reinterpret_cast<const sockaddr*>(&addr),
flags);
err = uv_tcp_bind(
&wrap->handle_, reinterpret_cast<const sockaddr*>(&addr), flags);
}
args.GetReturnValue().Set(err);
}
Expand All @@ -270,50 +259,47 @@ void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args) {
Bind<sockaddr_in>(args, AF_INET, uv_ip4_addr);
}


void TCPWrap::Bind6(const FunctionCallbackInfo<Value>& args) {
Bind<sockaddr_in6>(args, AF_INET6, uv_ip6_addr);
}


void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) {
TCPWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(
&wrap, args.This(), args.GetReturnValue().Set(UV_EBADF));
Environment* env = wrap->env();
int backlog;
if (!args[0]->Int32Value(env->context()).To(&backlog)) return;
int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_),
backlog,
OnConnection);
int err = uv_listen(
reinterpret_cast<uv_stream_t*>(&wrap->handle_), backlog, OnConnection);
args.GetReturnValue().Set(err);
}


void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args) {
CHECK(args[2]->IsUint32());
Environment* env = Environment::GetCurrent(args);
int backlog;
if (!args[2]->Int32Value(env->context()).To(&backlog)) return;
// explicit cast to fit to libuv's type expectation
int port = static_cast<int>(args[2].As<Uint32>()->Value());
Connect<sockaddr_in>(args,
[port](const char* ip_address, sockaddr_in* addr) {
return uv_ip4_addr(ip_address, port, addr);
Connect<sockaddr_in>(args, [port](const char* ip_address, sockaddr_in* addr) {
return uv_ip4_addr(ip_address, port, addr);
});
}


void TCPWrap::Connect6(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK(args[2]->IsUint32());

int port;
if (!args[2]->Int32Value(env->context()).To(&port)) return;
Connect<sockaddr_in6>(args,
[port](const char* ip_address, sockaddr_in6* addr) {
return uv_ip6_addr(ip_address, port, addr);
});
return uv_ip6_addr(ip_address, port, addr);
});
}

template <typename T>
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args,
void TCPWrap::Connect(
const FunctionCallbackInfo<Value>& args,
std::function<int(const char* ip_address, T* addr)> uv_ip_addr) {
Environment* env = Environment::GetCurrent(args);

Expand Down Expand Up @@ -390,61 +376,65 @@ MaybeLocal<Object> AddressToJS(Environment* env,

int port;

if (info.IsEmpty())
info = Object::New(env->isolate());
if (info.IsEmpty()) info = Object::New(env->isolate());

switch (addr->sa_family) {
case AF_INET6:
a6 = reinterpret_cast<const sockaddr_in6*>(addr);
uv_inet_ntop(AF_INET6, &a6->sin6_addr, ip, sizeof ip);
// Add an interface identifier to a link local address.
if (IN6_IS_ADDR_LINKLOCAL(&a6->sin6_addr) && a6->sin6_scope_id > 0) {
const size_t addrlen = strlen(ip);
CHECK_LT(addrlen, sizeof(ip));
ip[addrlen] = '%';
size_t scopeidlen = sizeof(ip) - addrlen - 1;
CHECK_GE(scopeidlen, UV_IF_NAMESIZE);
const int r = uv_if_indextoiid(a6->sin6_scope_id,
ip + addrlen + 1,
&scopeidlen);
if (r) {
env->ThrowUVException(r, "uv_if_indextoiid");
return {};
case AF_INET6:
a6 = reinterpret_cast<const sockaddr_in6*>(addr);
uv_inet_ntop(AF_INET6, &a6->sin6_addr, ip, sizeof ip);
// Add an interface identifier to a link local address.
if (IN6_IS_ADDR_LINKLOCAL(&a6->sin6_addr) && a6->sin6_scope_id > 0) {
const size_t addrlen = strlen(ip);
CHECK_LT(addrlen, sizeof(ip));
ip[addrlen] = '%';
size_t scopeidlen = sizeof(ip) - addrlen - 1;
CHECK_GE(scopeidlen, UV_IF_NAMESIZE);
const int r =
uv_if_indextoiid(a6->sin6_scope_id, ip + addrlen + 1, &scopeidlen);
if (r) {
env->ThrowUVException(r, "uv_if_indextoiid");
return {};
}
}
}
port = ntohs(a6->sin6_port);
info->Set(env->context(),
env->address_string(),
OneByteString(env->isolate(), ip)).Check();
info->Set(env->context(), env->family_string(), env->ipv6_string()).Check();
info->Set(env->context(),
env->port_string(),
Integer::New(env->isolate(), port)).Check();
break;

case AF_INET:
a4 = reinterpret_cast<const sockaddr_in*>(addr);
uv_inet_ntop(AF_INET, &a4->sin_addr, ip, sizeof ip);
port = ntohs(a4->sin_port);
info->Set(env->context(),
env->address_string(),
OneByteString(env->isolate(), ip)).Check();
info->Set(env->context(), env->family_string(), env->ipv4_string()).Check();
info->Set(env->context(),
env->port_string(),
Integer::New(env->isolate(), port)).Check();
break;

default:
info->Set(env->context(),
env->address_string(),
String::Empty(env->isolate())).Check();
port = ntohs(a6->sin6_port);
info->Set(env->context(),
env->address_string(),
OneByteString(env->isolate(), ip))
.Check();
info->Set(env->context(), env->family_string(), env->ipv6_string())
.Check();
info->Set(env->context(),
env->port_string(),
Integer::New(env->isolate(), port))
.Check();
break;

case AF_INET:
a4 = reinterpret_cast<const sockaddr_in*>(addr);
uv_inet_ntop(AF_INET, &a4->sin_addr, ip, sizeof ip);
port = ntohs(a4->sin_port);
info->Set(env->context(),
env->address_string(),
OneByteString(env->isolate(), ip))
.Check();
info->Set(env->context(), env->family_string(), env->ipv4_string())
.Check();
info->Set(env->context(),
env->port_string(),
Integer::New(env->isolate(), port))
.Check();
break;

default:
info->Set(env->context(),
env->address_string(),
String::Empty(env->isolate()))
.Check();
}

return scope.Escape(info);
}


} // namespace node

NODE_BINDING_CONTEXT_AWARE_INTERNAL(tcp_wrap, node::TCPWrap::Initialize)
Expand Down

0 comments on commit 8bfc0fb

Please sign in to comment.