summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Moss <me@jonathanmoss.me>2018-08-14 10:35:40 -0400
committerJon Moss <me@jonathanmoss.me>2018-12-17 16:40:39 -0500
commit4b96a2a73b8117218c82e4dbbc491c81fd1b1fa4 (patch)
tree1f538e85f60544cac51def45a8fb11bc13bc967c
parent928f77638572e7590c806a6bfb74ea8f1b2fec19 (diff)
downloadandroid-node-v8-4b96a2a73b8117218c82e4dbbc491c81fd1b1fa4.tar.gz
android-node-v8-4b96a2a73b8117218c82e4dbbc491c81fd1b1fa4.tar.bz2
android-node-v8-4b96a2a73b8117218c82e4dbbc491c81fd1b1fa4.zip
src: extract common Bind method
`TCPWrap::Bind` and `TCPWrap::Bind6` share a large amount of functionality, so a common `Bind` was extracted to remove duplication. PR-URL: https://github.com/nodejs/node/pull/22315 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com>
-rw-r--r--src/tcp_wrap.cc43
-rw-r--r--src/tcp_wrap.h5
2 files changed, 25 insertions, 23 deletions
diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc
index 5adab06df4..dac621ec87 100644
--- a/src/tcp_wrap.cc
+++ b/src/tcp_wrap.cc
@@ -225,8 +225,11 @@ void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(err);
}
-
-void TCPWrap::Bind(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) {
TCPWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(&wrap,
args.Holder(),
@@ -234,31 +237,16 @@ void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args) {
Environment* env = wrap->env();
node::Utf8Value ip_address(env->isolate(), args[0]);
int port;
+ unsigned int flags = 0;
if (!args[1]->Int32Value(env->context()).To(&port)) return;
- sockaddr_in addr;
- int err = uv_ip4_addr(*ip_address, port, &addr);
- if (err == 0) {
- err = uv_tcp_bind(&wrap->handle_,
- reinterpret_cast<const sockaddr*>(&addr),
- 0);
+ if (family == AF_INET6 &&
+ !args[2]->Uint32Value(env->context()).To(&flags)) {
+ return;
}
- args.GetReturnValue().Set(err);
-}
+ T addr;
+ int err = uv_ip_addr(*ip_address, port, &addr);
-void TCPWrap::Bind6(const FunctionCallbackInfo<Value>& args) {
- TCPWrap* wrap;
- ASSIGN_OR_RETURN_UNWRAP(&wrap,
- args.Holder(),
- args.GetReturnValue().Set(UV_EBADF));
- Environment* env = wrap->env();
- node::Utf8Value ip6_address(env->isolate(), args[0]);
- int port;
- unsigned int flags;
- if (!args[1]->Int32Value(env->context()).To(&port)) return;
- if (!args[2]->Uint32Value(env->context()).To(&flags)) return;
- sockaddr_in6 addr;
- int err = uv_ip6_addr(*ip6_address, port, &addr);
if (err == 0) {
err = uv_tcp_bind(&wrap->handle_,
reinterpret_cast<const sockaddr*>(&addr),
@@ -267,6 +255,15 @@ void TCPWrap::Bind6(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(err);
}
+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;
diff --git a/src/tcp_wrap.h b/src/tcp_wrap.h
index a554832841..db269f6528 100644
--- a/src/tcp_wrap.h
+++ b/src/tcp_wrap.h
@@ -80,6 +80,11 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {
static void Connect(const v8::FunctionCallbackInfo<v8::Value>& args,
std::function<int(const char* ip_address, T* addr)> uv_ip_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);
#ifdef _WIN32
static void SetSimultaneousAccepts(