Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions src/tcp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,11 @@ void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) {
}

template <typename T>
void TCPWrap::Bind(
const FunctionCallbackInfo<Value>& args,
int family,
std::function<int(const char* ip_address, int port, T* addr)> uv_ip_addr) {
void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args,
int family,
int (*uv_ip_addr)(const char* ip_address,
int port,
T* addr)) {
TCPWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(
&wrap, args.This(), args.GetReturnValue().Set(UV_EBADF));
Expand Down Expand Up @@ -430,29 +431,18 @@ void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) {
}

void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args) {
CHECK(args[2]->IsUint32());
// 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, uv_ip4_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);
});
Connect<sockaddr_in6>(args, uv_ip6_addr);
}

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

TCPWrap* wrap;
Expand All @@ -462,14 +452,17 @@ void TCPWrap::Connect(
CHECK(args[0]->IsObject());
CHECK(args[1]->IsString());

int port;
if (!args[2]->Int32Value(env->context()).To(&port)) return;

Local<Object> req_wrap_obj = args[0].As<Object>();
node::Utf8Value ip_address(env->isolate(), args[1]);

ERR_ACCESS_DENIED_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kNet, ip_address.ToStringView(), args);

T addr;
int err = uv_ip_addr(*ip_address, &addr);
int err = uv_ip_addr(*ip_address, port, &addr);

if (err == 0) {
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap);
Expand Down
13 changes: 8 additions & 5 deletions src/tcp_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,16 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {
static void Connect6(const v8::FunctionCallbackInfo<v8::Value>& args);
template <typename T>
static void Connect(const v8::FunctionCallbackInfo<v8::Value>& args,
std::function<int(const char* ip_address, T* addr)> uv_ip_addr);
int (*uv_ip_addr)(const char* ip_address,
int port,
T* addr));
static void Open(const v8::FunctionCallbackInfo<v8::Value>& args);
template <typename T>
static void Bind(
const v8::FunctionCallbackInfo<v8::Value>& args,
int family,
std::function<int(const char* ip_address, int port, T* addr)> uv_ip_addr);
static void Bind(const v8::FunctionCallbackInfo<v8::Value>& args,
int family,
int (*uv_ip_addr)(const char* ip_address,
int port,
T* addr));
static void Reset(const v8::FunctionCallbackInfo<v8::Value>& args);
int Reset(v8::Local<v8::Value> close_callback = v8::Local<v8::Value>());

Expand Down
Loading