You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by dm...@apache.org on 2014/12/05 22:46:16 UTC

[1/5] mesos git commit: Created accept, bind, connect and getsockname wrappers

Repository: mesos
Updated Branches:
  refs/heads/master 49d4553a0 -> 83d90df12


Created accept, bind, connect and getsockname wrappers

Created accept, bind, connect and getsockname wrappers in socket.hpp for different protocol families

Review: https://reviews.apache.org/r/28716


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/3f53f7f5
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/3f53f7f5
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/3f53f7f5

Branch: refs/heads/master
Commit: 3f53f7f50dba65c2eabec38d581acc31e2957548
Parents: 49d4553
Author: Evelina Dumitrescu <ev...@gmail.com>
Authored: Fri Dec 5 13:02:40 2014 -0800
Committer: Dominic Hamon <dh...@twitter.com>
Committed: Fri Dec 5 13:03:04 2014 -0800

----------------------------------------------------------------------
 3rdparty/libprocess/include/process/socket.hpp  | 73 +++++++++++++++++++-
 3rdparty/libprocess/src/http.cpp                | 12 ++--
 3rdparty/libprocess/src/process.cpp             | 47 ++++---------
 3rdparty/libprocess/src/tests/http_tests.cpp    | 13 ++--
 3rdparty/libprocess/src/tests/process_tests.cpp | 38 ++++------
 5 files changed, 109 insertions(+), 74 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/3f53f7f5/3rdparty/libprocess/include/process/socket.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/socket.hpp b/3rdparty/libprocess/include/process/socket.hpp
index ab080c1..e237658 100644
--- a/3rdparty/libprocess/include/process/socket.hpp
+++ b/3rdparty/libprocess/include/process/socket.hpp
@@ -37,6 +37,75 @@ inline Try<int> socket(int family, int type, int protocol)
   return s;
 }
 
+// accept, bind, connect, getsockname wrappers for different protocol families
+inline Try<int> accept(int s, sa_family_t family)
+{
+  switch (family) {
+    case AF_INET: {
+      sockaddr_in addr;
+      memset(&addr, 0, sizeof(addr));
+
+      socklen_t addrlen = sizeof(addr);
+
+      int rc = ::accept(s, (sockaddr*) &addr, &addrlen);
+      if (rc < 0)
+         return ErrnoError("Failed to accept");
+
+      return rc;
+    }
+    default:
+      return Error("Unsupported family type: " + stringify(family));
+  }
+}
+
+inline Try<int> bind(int s, const Node& node)
+{
+  sockaddr_in addr;
+  memset(&addr, 0, sizeof(addr));
+  addr.sin_family = AF_INET;
+  addr.sin_addr.s_addr = node.ip;
+  addr.sin_port = htons(node.port);
+
+  int rc = ::bind(s, (sockaddr*) &addr, sizeof(addr));
+  if (rc < 0)
+    return ErrnoError("Failed to bind on " + stringify(node));
+
+  return rc;
+}
+
+inline Try<int> connect(int s, const Node& node)
+{
+  sockaddr_in addr;
+  memset(&addr, 0, sizeof(addr));
+  addr.sin_family = AF_INET;
+  addr.sin_port = htons(node.port);
+  addr.sin_addr.s_addr = node.ip;
+
+  int rc = ::connect(s, (sockaddr*) &addr, sizeof(addr));
+  if (rc < 0)
+    return ErrnoError("Failed to connect to " + stringify(node));
+
+  return rc;
+}
+
+inline Try<Node> getsockname(int s, sa_family_t family)
+{
+  switch (family) {
+    case AF_INET: {
+      sockaddr_in addr;
+      memset(&addr, 0, sizeof(addr));
+
+      socklen_t addrlen = sizeof(addr);
+
+      if(::getsockname(s, (sockaddr*) &addr, &addrlen) < 0)
+        return ErrnoError("Failed to getsockname");
+
+      return Node(addr.sin_addr.s_addr, ntohs(addr.sin_port));
+    }
+    default:
+      return Error("Unsupported family type: " + stringify(family));
+  }
+}
 
 // An abstraction around a socket (file descriptor) that provides
 // reference counting such that the socket is only closed (and thus,
@@ -96,13 +165,13 @@ public:
       // Supported in Linux >= 2.6.27.
 #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
       Try<int> fd =
-        process::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
+        socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
 
       if (fd.isError()) {
         ABORT("Failed to create socket: " + fd.error());
       }
 #else
-      Try<int> fd = process::socket(AF_INET, SOCK_STREAM, 0);
+      Try<int> fd = socket(AF_INET, SOCK_STREAM, 0);
       if (fd.isError()) {
         ABORT("Failed to create socket: " + fd.error());
       }

http://git-wip-us.apache.org/repos/asf/mesos/blob/3f53f7f5/3rdparty/libprocess/src/http.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/http.cpp b/3rdparty/libprocess/src/http.cpp
index b00f333..789be4a 100644
--- a/3rdparty/libprocess/src/http.cpp
+++ b/3rdparty/libprocess/src/http.cpp
@@ -10,6 +10,7 @@
 #include <process/future.hpp>
 #include <process/http.hpp>
 #include <process/io.hpp>
+#include <process/socket.hpp>
 
 #include <stout/lambda.hpp>
 #include <stout/nothing.hpp>
@@ -81,15 +82,10 @@ Future<Response> request(
 
   const string host = stringify(upid.node);
 
-  sockaddr_in addr;
-  memset(&addr, 0, sizeof(addr));
-  addr.sin_family = AF_INET;
-  addr.sin_port = htons(upid.node.port);
-  addr.sin_addr.s_addr = upid.node.ip;
-
-  if (connect(s, (sockaddr*) &addr, sizeof(addr)) < 0) {
+  Try<int> tryConnect = process::connect(s, upid.node);
+  if (tryConnect.isError()) {
     os::close(s);
-    return Failure(ErrnoError("Failed to connect to '" + host + "'"));
+    return Failure(tryConnect.error());
   }
 
   std::ostringstream out;

http://git-wip-us.apache.org/repos/asf/mesos/blob/3f53f7f5/3rdparty/libprocess/src/process.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/process.cpp b/3rdparty/libprocess/src/process.cpp
index 4db7d56..611889b 100644
--- a/3rdparty/libprocess/src/process.cpp
+++ b/3rdparty/libprocess/src/process.cpp
@@ -1272,19 +1272,14 @@ Future<Nothing> connect(const Socket& socket)
 
 Future<Nothing> Socket::Impl::connect(const Node& node)
 {
-  sockaddr_in addr;
-  memset(&addr, 0, sizeof(addr));
-  addr.sin_family = PF_INET;
-  addr.sin_port = htons(node.port);
-  addr.sin_addr.s_addr = node.ip;
-
-  if (::connect(get(), (sockaddr*) &addr, sizeof(addr)) < 0) {
-    if (errno != EINPROGRESS) {
-      return Failure(ErrnoError("Failed to connect socket"));
+  Try<int> tryConnect = process::connect(get(), node);
+  if (tryConnect.isError()) {
+    if (errno == EINPROGRESS) {
+      return io::poll(get(), io::WRITE)
+        .then(lambda::bind(&internal::connect, Socket(shared_from_this())));
     }
 
-    return io::poll(get(), io::WRITE)
-      .then(lambda::bind(&internal::connect, Socket(shared_from_this())));
+    return Failure(tryConnect.error());
   }
 
   return Nothing();
@@ -1389,24 +1384,13 @@ Future<size_t> Socket::Impl::sendfile(int fd, off_t offset, size_t size)
 
 Try<Node> Socket::Impl::bind(const Node& node)
 {
-  sockaddr_in addr;
-  memset(&addr, 0, sizeof(addr));
-  addr.sin_family = PF_INET;
-  addr.sin_addr.s_addr = node.ip;
-  addr.sin_port = htons(node.port);
-
-  if (::bind(get(), (sockaddr*) &addr, sizeof(addr)) < 0) {
-    return Error("Failed to bind: " + string(inet_ntoa(addr.sin_addr)) +
-                 ":" + stringify(node.port));
+  Try<int> tryBind = process::bind(get(), node);
+  if (tryBind.isError()) {
+    return Error(tryBind.error());
   }
 
   // Lookup and store assigned ip and assigned port.
-  socklen_t addrlen = sizeof(addr);
-  if (getsockname(get(), (sockaddr*) &addr, &addrlen) < 0) {
-    return ErrnoError("Failed to bind, getsockname");
-  }
-
-  return Node(addr.sin_addr.s_addr, ntohs(addr.sin_port));
+  return process::getsockname(get(), AF_INET);
 }
 
 
@@ -1423,15 +1407,12 @@ namespace internal {
 
 Future<Socket> accept(int fd)
 {
-  sockaddr_in addr;
-  socklen_t addrlen = sizeof(addr);
-
-  int s = ::accept(fd, (sockaddr*) &addr, &addrlen);
-
-  if (s < 0) {
-    return Failure(ErrnoError("Failed to accept"));
+  Try<int> tryAccept = process::accept(fd, AF_INET);
+  if (tryAccept.isError()) {
+    return Failure(tryAccept.error());
   }
 
+  int s = tryAccept.get();
   Try<Nothing> nonblock = os::nonblock(s);
   if (nonblock.isError()) {
     LOG_IF(INFO, VLOG_IS_ON(1)) << "Failed to accept, nonblock: "

http://git-wip-us.apache.org/repos/asf/mesos/blob/3f53f7f5/3rdparty/libprocess/src/tests/http_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/tests/http_tests.cpp b/3rdparty/libprocess/src/tests/http_tests.cpp
index a90e65f..7286682 100644
--- a/3rdparty/libprocess/src/tests/http_tests.cpp
+++ b/3rdparty/libprocess/src/tests/http_tests.cpp
@@ -12,6 +12,7 @@
 #include <process/gtest.hpp>
 #include <process/http.hpp>
 #include <process/io.hpp>
+#include <process/socket.hpp>
 
 #include <stout/base64.hpp>
 #include <stout/gtest.hpp>
@@ -113,17 +114,13 @@ TEST(HTTP, Endpoints)
   spawn(process);
 
   // First hit '/body' (using explicit sockets and HTTP/1.0).
-  int s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
+  Try<int> trySocket = process::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
 
-  ASSERT_LE(0, s);
+  ASSERT_TRUE(trySocket.isSome());
 
-  sockaddr_in addr;
-  memset(&addr, 0, sizeof(addr));
-  addr.sin_family = PF_INET;
-  addr.sin_port = htons(process.self().node.port);
-  addr.sin_addr.s_addr = process.self().node.ip;
+  int s = trySocket.get();
 
-  ASSERT_EQ(0, connect(s, (sockaddr*) &addr, sizeof(addr)));
+  ASSERT_TRUE(process::connect(s, process.self().node).isSome());
 
   std::ostringstream out;
   out << "GET /" << process.self().id << "/body"

http://git-wip-us.apache.org/repos/asf/mesos/blob/3f53f7f5/3rdparty/libprocess/src/tests/process_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/tests/process_tests.cpp b/3rdparty/libprocess/src/tests/process_tests.cpp
index dec62e8..03917c4 100644
--- a/3rdparty/libprocess/src/tests/process_tests.cpp
+++ b/3rdparty/libprocess/src/tests/process_tests.cpp
@@ -23,6 +23,7 @@
 #include <process/limiter.hpp>
 #include <process/process.hpp>
 #include <process/run.hpp>
+#include <process/socket.hpp>
 #include <process/time.hpp>
 
 #include <stout/duration.hpp>
@@ -1418,17 +1419,13 @@ TEST(Process, remote)
   EXPECT_CALL(process, handler(_, _))
     .WillOnce(FutureSatisfy(&handler));
 
-  int s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
+  Try<int> trySocket = process::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
 
-  ASSERT_LE(0, s);
+  ASSERT_TRUE(trySocket.isSome());
 
-  sockaddr_in addr;
-  memset(&addr, 0, sizeof(addr));
-  addr.sin_family = PF_INET;
-  addr.sin_port = htons(process.self().node.port);
-  addr.sin_addr.s_addr = process.self().node.ip;
+  int s = trySocket.get();
 
-  ASSERT_EQ(0, connect(s, (sockaddr*) &addr, sizeof(addr)));
+  ASSERT_TRUE(process::connect(s, process.self().node).isSome());
 
   Message message;
   message.name = "handler";
@@ -1488,24 +1485,19 @@ TEST(Process, http2)
   spawn(process);
 
   // Create a receiving socket so we can get messages back.
-  int s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
-  ASSERT_LE(0, s);
+  Try<int> trySocket = process::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
+  ASSERT_TRUE(trySocket.isSome());
 
-  // Set up socket.
-  sockaddr_in addr;
-  memset(&addr, 0, sizeof(addr));
-  addr.sin_family = PF_INET;
-  addr.sin_addr.s_addr = INADDR_ANY;
-  addr.sin_port = 0;
+  int s = trySocket.get();
 
-  ASSERT_EQ(0, ::bind(s, (sockaddr*) &addr, sizeof(addr)));
+  ASSERT_TRUE(process::bind(s, Node()).isSome());
 
   // Create a UPID for 'Libprocess-From' based on the IP and port we
   // got assigned.
-  socklen_t addrlen = sizeof(addr);
-  ASSERT_EQ(0, getsockname(s, (sockaddr*) &addr, &addrlen));
+  Try<Node> node = process::getsockname(s, AF_INET);
+  ASSERT_TRUE(node.isSome());
 
-  UPID from("", addr.sin_addr.s_addr, ntohs(addr.sin_port));
+  UPID from("", node.get());
 
   ASSERT_EQ(0, listen(s, 1));
 
@@ -1535,10 +1527,10 @@ TEST(Process, http2)
   post(process.self(), from, name);
 
   // Accept the incoming connection.
-  memset(&addr, 0, sizeof(addr));
-  addrlen = sizeof(addr);
+  Try<int> tryAccept = process::accept(s, AF_INET);
+  ASSERT_TRUE(tryAccept.isSome());
 
-  int c = ::accept(s, (sockaddr*) &addr, &addrlen);
+  int c = tryAccept.get();
   ASSERT_LT(0, c);
 
   const string data = "POST /" + name + " HTTP/1.1";


[2/5] mesos git commit: Introduced getIP and created initialization wrappers for sockaddr_in and addrinfo

Posted by dm...@apache.org.
Introduced getIP and created initialization wrappers for sockaddr_in and addrinfo

Replaced obsolete functions gethostbyname2_r and gethostbyname2 with getaddrinfo
and introduced getIP. Created initialization wrappers for sockaddr_in and
addrinfo.

Review: https://reviews.apache.org/r/28718


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/50c3a852
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/50c3a852
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/50c3a852

Branch: refs/heads/master
Commit: 50c3a85253d75e85597bbde13e393f2e4c6fa16b
Parents: 99bbe8c
Author: Evelina Dumitrescu <ev...@gmail.com>
Authored: Fri Dec 5 13:22:28 2014 -0800
Committer: Dominic Hamon <dh...@twitter.com>
Committed: Fri Dec 5 13:27:53 2014 -0800

----------------------------------------------------------------------
 src/sched/sched.cpp           | 2 +-
 src/tests/allocator_tests.cpp | 2 +-
 src/tests/master_tests.cpp    | 3 ++-
 3 files changed, 4 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/50c3a852/src/sched/sched.cpp
----------------------------------------------------------------------
diff --git a/src/sched/sched.cpp b/src/sched/sched.cpp
index 0b08512..4be08f1 100644
--- a/src/sched/sched.cpp
+++ b/src/sched/sched.cpp
@@ -1174,7 +1174,7 @@ void MesosSchedulerDriver::initialize() {
     framework.set_user(user.get());
   }
   if (framework.hostname().empty()) {
-    framework.set_hostname(os::hostname().get());
+    framework.set_hostname(net::hostname().get());
   }
 
   // Launch a local cluster if necessary.

http://git-wip-us.apache.org/repos/asf/mesos/blob/50c3a852/src/tests/allocator_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/allocator_tests.cpp b/src/tests/allocator_tests.cpp
index 65f05fd..8626362 100644
--- a/src/tests/allocator_tests.cpp
+++ b/src/tests/allocator_tests.cpp
@@ -2029,7 +2029,7 @@ TYPED_TEST(AllocatorTest, WhitelistSlave)
   slave::Flags flags = this->CreateSlaveFlags();
   flags.resources = Some("cpus:2;mem:1024");
 
-  Try<string> hostname = os::hostname();
+  Try<string> hostname = net::hostname();
   ASSERT_SOME(hostname);
   flags.hostname = hostname.get();
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/50c3a852/src/tests/master_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/master_tests.cpp b/src/tests/master_tests.cpp
index 4be4815..7f796be 100644
--- a/src/tests/master_tests.cpp
+++ b/src/tests/master_tests.cpp
@@ -37,6 +37,7 @@
 #include <process/metrics/metrics.hpp>
 
 #include <stout/json.hpp>
+#include <stout/net.hpp>
 #include <stout/option.hpp>
 #include <stout/os.hpp>
 #include <stout/try.hpp>
@@ -1053,7 +1054,7 @@ protected:
 TEST_F(WhitelistTest, WhitelistSlave)
 {
   // Add some hosts to the white list.
-  Try<string> hostname = os::hostname();
+  Try<string> hostname = net::hostname();
   ASSERT_SOME(hostname);
 
   string hosts = hostname.get() + "\n" + "dummy-slave";


[5/5] mesos git commit: Fix some style and naming issues

Posted by dm...@apache.org.
Fix some style and naming issues


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/83d90df1
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/83d90df1
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/83d90df1

Branch: refs/heads/master
Commit: 83d90df125f05ab3c4f1b31311f7e5f1c9d6f8c1
Parents: 0503b67
Author: Dominic Hamon <dh...@twitter.com>
Authored: Fri Dec 5 13:33:59 2014 -0800
Committer: Dominic Hamon <dh...@twitter.com>
Committed: Fri Dec 5 13:33:59 2014 -0800

----------------------------------------------------------------------
 3rdparty/libprocess/include/process/socket.hpp  | 26 +++++++++++---------
 3rdparty/libprocess/src/http.cpp                |  6 ++---
 3rdparty/libprocess/src/process.cpp             | 20 +++++++--------
 3rdparty/libprocess/src/tests/http_tests.cpp    |  6 ++---
 3rdparty/libprocess/src/tests/process_tests.cpp | 18 +++++++-------
 5 files changed, 40 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/83d90df1/3rdparty/libprocess/include/process/socket.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/socket.hpp b/3rdparty/libprocess/include/process/socket.hpp
index 9188e3d..916576f 100644
--- a/3rdparty/libprocess/include/process/socket.hpp
+++ b/3rdparty/libprocess/include/process/socket.hpp
@@ -46,11 +46,12 @@ inline Try<int> accept(int s, sa_family_t family)
       sockaddr_in addr = net::createSockaddrIn(0, 0);
       socklen_t addrlen = sizeof(addr);
 
-      int rc = ::accept(s, (sockaddr*) &addr, &addrlen);
-      if (rc < 0)
-         return ErrnoError("Failed to accept");
+      int accepted = ::accept(s, (sockaddr*) &addr, &addrlen);
+      if (accepted < 0) {
+        return ErrnoError("Failed to accept");
+      }
 
-      return rc;
+      return accepted;
     }
     default:
       return Error("Unsupported family type: " + stringify(family));
@@ -61,22 +62,24 @@ inline Try<int> bind(int s, const Node& node)
 {
   sockaddr_in addr = net::createSockaddrIn(node.ip, node.port);
 
-  int rc = ::bind(s, (sockaddr*) &addr, sizeof(addr));
-  if (rc < 0)
+  int bound = ::bind(s, (sockaddr*) &addr, sizeof(addr));
+  if (bound < 0) {
     return ErrnoError("Failed to bind on " + stringify(node));
+  }
 
-  return rc;
+  return bound;
 }
 
 inline Try<int> connect(int s, const Node& node)
 {
   sockaddr_in addr = net::createSockaddrIn(node.ip, node.port);
 
-  int rc = ::connect(s, (sockaddr*) &addr, sizeof(addr));
-  if (rc < 0)
+  int connected = ::connect(s, (sockaddr*) &addr, sizeof(addr));
+  if (connected < 0) {
     return ErrnoError("Failed to connect to " + stringify(node));
+  }
 
-  return rc;
+  return connected;
 }
 
 inline Try<Node> getsockname(int s, sa_family_t family)
@@ -86,8 +89,9 @@ inline Try<Node> getsockname(int s, sa_family_t family)
       sockaddr_in addr = net::createSockaddrIn(0, 0);
       socklen_t addrlen = sizeof(addr);
 
-      if(::getsockname(s, (sockaddr*) &addr, &addrlen) < 0)
+      if(::getsockname(s, (sockaddr*) &addr, &addrlen) < 0) {
         return ErrnoError("Failed to getsockname");
+      }
 
       return Node(addr.sin_addr.s_addr, ntohs(addr.sin_port));
     }

http://git-wip-us.apache.org/repos/asf/mesos/blob/83d90df1/3rdparty/libprocess/src/http.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/http.cpp b/3rdparty/libprocess/src/http.cpp
index 789be4a..465e447 100644
--- a/3rdparty/libprocess/src/http.cpp
+++ b/3rdparty/libprocess/src/http.cpp
@@ -82,10 +82,10 @@ Future<Response> request(
 
   const string host = stringify(upid.node);
 
-  Try<int> tryConnect = process::connect(s, upid.node);
-  if (tryConnect.isError()) {
+  Try<int> connected = process::connect(s, upid.node);
+  if (connected.isError()) {
     os::close(s);
-    return Failure(tryConnect.error());
+    return Failure(connected.error());
   }
 
   std::ostringstream out;

http://git-wip-us.apache.org/repos/asf/mesos/blob/83d90df1/3rdparty/libprocess/src/process.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/process.cpp b/3rdparty/libprocess/src/process.cpp
index c0d80bf..b87ac22 100644
--- a/3rdparty/libprocess/src/process.cpp
+++ b/3rdparty/libprocess/src/process.cpp
@@ -1271,14 +1271,14 @@ Future<Nothing> connect(const Socket& socket)
 
 Future<Nothing> Socket::Impl::connect(const Node& node)
 {
-  Try<int> tryConnect = process::connect(get(), node);
-  if (tryConnect.isError()) {
+  Try<int> connected = process::connect(get(), node);
+  if (connected.isError()) {
     if (errno == EINPROGRESS) {
       return io::poll(get(), io::WRITE)
         .then(lambda::bind(&internal::connect, Socket(shared_from_this())));
     }
 
-    return Failure(tryConnect.error());
+    return Failure(connected.error());
   }
 
   return Nothing();
@@ -1383,9 +1383,9 @@ Future<size_t> Socket::Impl::sendfile(int fd, off_t offset, size_t size)
 
 Try<Node> Socket::Impl::bind(const Node& node)
 {
-  Try<int> tryBind = process::bind(get(), node);
-  if (tryBind.isError()) {
-    return Error(tryBind.error());
+  Try<int> bound = process::bind(get(), node);
+  if (bound.isError()) {
+    return Error(bound.error());
   }
 
   // Lookup and store assigned ip and assigned port.
@@ -1406,12 +1406,12 @@ namespace internal {
 
 Future<Socket> accept(int fd)
 {
-  Try<int> tryAccept = process::accept(fd, AF_INET);
-  if (tryAccept.isError()) {
-    return Failure(tryAccept.error());
+  Try<int> accepted = process::accept(fd, AF_INET);
+  if (accepted.isError()) {
+    return Failure(accepted.error());
   }
 
-  int s = tryAccept.get();
+  int s = accepted.get();
   Try<Nothing> nonblock = os::nonblock(s);
   if (nonblock.isError()) {
     LOG_IF(INFO, VLOG_IS_ON(1)) << "Failed to accept, nonblock: "

http://git-wip-us.apache.org/repos/asf/mesos/blob/83d90df1/3rdparty/libprocess/src/tests/http_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/tests/http_tests.cpp b/3rdparty/libprocess/src/tests/http_tests.cpp
index 7286682..3bda76d 100644
--- a/3rdparty/libprocess/src/tests/http_tests.cpp
+++ b/3rdparty/libprocess/src/tests/http_tests.cpp
@@ -114,11 +114,11 @@ TEST(HTTP, Endpoints)
   spawn(process);
 
   // First hit '/body' (using explicit sockets and HTTP/1.0).
-  Try<int> trySocket = process::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
+  Try<int> sock = process::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
 
-  ASSERT_TRUE(trySocket.isSome());
+  ASSERT_TRUE(sock.isSome());
 
-  int s = trySocket.get();
+  int s = sock.get();
 
   ASSERT_TRUE(process::connect(s, process.self().node).isSome());
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/83d90df1/3rdparty/libprocess/src/tests/process_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/tests/process_tests.cpp b/3rdparty/libprocess/src/tests/process_tests.cpp
index 03917c4..45be2e1 100644
--- a/3rdparty/libprocess/src/tests/process_tests.cpp
+++ b/3rdparty/libprocess/src/tests/process_tests.cpp
@@ -1419,11 +1419,11 @@ TEST(Process, remote)
   EXPECT_CALL(process, handler(_, _))
     .WillOnce(FutureSatisfy(&handler));
 
-  Try<int> trySocket = process::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
+  Try<int> sock = process::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
 
-  ASSERT_TRUE(trySocket.isSome());
+  ASSERT_TRUE(sock.isSome());
 
-  int s = trySocket.get();
+  int s = sock.get();
 
   ASSERT_TRUE(process::connect(s, process.self().node).isSome());
 
@@ -1485,10 +1485,10 @@ TEST(Process, http2)
   spawn(process);
 
   // Create a receiving socket so we can get messages back.
-  Try<int> trySocket = process::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
-  ASSERT_TRUE(trySocket.isSome());
+  Try<int> sock = process::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
+  ASSERT_TRUE(sock.isSome());
 
-  int s = trySocket.get();
+  int s = sock.get();
 
   ASSERT_TRUE(process::bind(s, Node()).isSome());
 
@@ -1527,10 +1527,10 @@ TEST(Process, http2)
   post(process.self(), from, name);
 
   // Accept the incoming connection.
-  Try<int> tryAccept = process::accept(s, AF_INET);
-  ASSERT_TRUE(tryAccept.isSome());
+  Try<int> accepted = process::accept(s, AF_INET);
+  ASSERT_TRUE(accepted.isSome());
 
-  int c = tryAccept.get();
+  int c = accepted.get();
   ASSERT_LT(0, c);
 
   const string data = "POST /" + name + " HTTP/1.1";


[3/5] mesos git commit: Introduced getIP and initialization wrappers for sockaddr_in and addrinfo

Posted by dm...@apache.org.
Introduced getIP and initialization wrappers for sockaddr_in and addrinfo

Replaced obsolete functions gethostbyname2_r and gethostbyname2 with getaddrinfo
and introduced getIP.  Created initialization wrappers for sockaddr_in and
addrinfo.

Review: https://reviews.apache.org/r/28717


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/99bbe8c0
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/99bbe8c0
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/99bbe8c0

Branch: refs/heads/master
Commit: 99bbe8c0f7d25e31f4a997526b53ae783c8647c3
Parents: 3f53f7f
Author: Evelina Dumitrescu <ev...@gmail.com>
Authored: Fri Dec 5 13:20:32 2014 -0800
Committer: Dominic Hamon <dh...@twitter.com>
Committed: Fri Dec 5 13:27:53 2014 -0800

----------------------------------------------------------------------
 .../3rdparty/stout/include/stout/net.hpp        | 71 ++++++++++++++++++--
 .../3rdparty/stout/include/stout/os.hpp         | 35 ----------
 2 files changed, 67 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/99bbe8c0/3rdparty/libprocess/3rdparty/stout/include/stout/net.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/net.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/net.hpp
index a992bd9..39f6cb0 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/net.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/net.hpp
@@ -61,6 +61,28 @@
 // Network utilities.
 namespace net {
 
+inline struct addrinfo createAddrInfo(int socktype, int family, int flags)
+{
+  struct addrinfo addr;
+  memset(&addr, 0, sizeof(addr));
+  addr.ai_socktype = socktype;
+  addr.ai_family = family;
+  addr.ai_flags |= flags;
+
+  return addr;
+}
+
+// TODO(evelinad): Add createSockaddrIn6 when will support IPv6
+inline struct sockaddr_in createSockaddrIn(uint32_t ip, int port)
+{
+  struct sockaddr_in addr;
+  memset(&addr, 0, sizeof(addr));
+  addr.sin_family = AF_INET;
+  addr.sin_addr.s_addr = ip;
+  addr.sin_port = htons(port);
+
+  return addr;
+}
 // Returns the HTTP response code resulting from attempting to download the
 // specified HTTP or FTP URL into a file at the specified path.
 inline Try<int> download(const std::string& url, const std::string& path)
@@ -110,14 +132,36 @@ inline Try<int> download(const std::string& url, const std::string& path)
 }
 
 
+inline Try<std::string> hostname()
+{
+  char host[512];
+
+  if (gethostname(host, sizeof(host)) < 0) {
+    return ErrnoError();
+  }
+
+  // TODO(evelinad): Add AF_UNSPEC when we will support IPv6
+  struct addrinfo ai = createAddrInfo(SOCK_STREAM, AF_INET, AI_CANONNAME);
+  struct addrinfo *aip;
+
+  int err = getaddrinfo(host, NULL, &ai, &aip);
+
+  if (err != 0 || aip == NULL) {
+    return Error(gai_strerror(err));
+  }
+
+  std::string hostname = aip->ai_canonname;
+  freeaddrinfo(aip);
+
+  return hostname;
+}
+
+
 // Returns a Try of the hostname for the provided IP. If the hostname cannot
 // be resolved, then a string version of the IP address is returned.
 inline Try<std::string> getHostname(uint32_t ip)
 {
-  sockaddr_in addr;
-  memset(&addr, 0, sizeof(addr));
-  addr.sin_family = AF_INET;
-  addr.sin_addr.s_addr = ip;
+  sockaddr_in addr = createSockaddrIn(ip, 0);
 
   char hostname[MAXHOSTNAMELEN];
   int err= getnameinfo(
@@ -135,6 +179,25 @@ inline Try<std::string> getHostname(uint32_t ip)
   return std::string(hostname);
 }
 
+inline Try<uint32_t> getIP(const std::string& hostname, sa_family_t family)
+{
+  struct addrinfo ai, *aip;
+  ai = createAddrInfo(SOCK_STREAM, family, 0);
+
+  int err = getaddrinfo(hostname.c_str(), NULL, &ai, &aip);
+  if (err != 0 || aip == NULL) {
+    return Error(gai_strerror(err));
+  }
+  if (aip->ai_addr == NULL) {
+    return Error("Got no addresses for '" + hostname + "'");
+  }
+
+  uint32_t ip = ((struct sockaddr_in*)(aip->ai_addr))->sin_addr.s_addr;
+  freeaddrinfo(aip);
+
+  return ip;
+}
+
 
 // Returns the names of all the link devices in the system.
 inline Try<std::set<std::string> > links()

http://git-wip-us.apache.org/repos/asf/mesos/blob/99bbe8c0/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
index ec259cd..0446f6a 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
@@ -857,41 +857,6 @@ inline Result<std::string> user(Option<uid_t> uid = None())
 }
 
 
-inline Try<std::string> hostname()
-{
-  char host[512];
-
-  if (gethostname(host, sizeof(host)) < 0) {
-    return ErrnoError();
-  }
-
-  // Allocate temporary buffer for gethostbyname2_r.
-  size_t length = 1024;
-  char* temp = new char[length];
-
-  struct hostent he, *hep = NULL;
-  int result = 0;
-  int herrno = 0;
-
-  while ((result = gethostbyname2_r(host, AF_INET, &he, temp,
-                                    length, &hep, &herrno)) == ERANGE) {
-    // Enlarge the buffer.
-    delete[] temp;
-    length *= 2;
-    temp = new char[length];
-  }
-
-  if (result != 0 || hep == NULL) {
-    delete[] temp;
-    return Error(hstrerror(herrno));
-  }
-
-  std::string hostname = hep->h_name;
-  delete[] temp;
-  return hostname;
-}
-
-
 // Suspends execution for the given duration.
 inline Try<Nothing> sleep(const Duration& duration)
 {


[4/5] mesos git commit: Introduced getIP and initialization wrappers for sockaddr_in and addrinfo

Posted by dm...@apache.org.
Introduced getIP and initialization wrappers for sockaddr_in and addrinfo

Replaced obsolete functions gethostbyname2_r and gethostbyname2 with getaddrinfo
and introduced getIP. Created initialization wrappers for sockaddr_in and
addrinfo.

Review: https://reviews.apache.org/r/28719


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/0503b676
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/0503b676
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/0503b676

Branch: refs/heads/master
Commit: 0503b676293e23d54fdb8e68604b5dd03b4895cf
Parents: 50c3a85
Author: Evelina Dumitrescu <ev...@gmail.com>
Authored: Fri Dec 5 13:27:59 2014 -0800
Committer: Dominic Hamon <dh...@twitter.com>
Committed: Fri Dec 5 13:28:23 2014 -0800

----------------------------------------------------------------------
 3rdparty/libprocess/include/process/socket.hpp | 21 +++---------
 3rdparty/libprocess/src/pid.cpp                | 37 ++++-----------------
 3rdparty/libprocess/src/process.cpp            |  9 +++--
 3 files changed, 15 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/0503b676/3rdparty/libprocess/include/process/socket.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/socket.hpp b/3rdparty/libprocess/include/process/socket.hpp
index e237658..9188e3d 100644
--- a/3rdparty/libprocess/include/process/socket.hpp
+++ b/3rdparty/libprocess/include/process/socket.hpp
@@ -10,6 +10,7 @@
 
 #include <stout/abort.hpp>
 #include <stout/nothing.hpp>
+#include <stout/net.hpp>
 #include <stout/os.hpp>
 #include <stout/try.hpp>
 
@@ -42,9 +43,7 @@ inline Try<int> accept(int s, sa_family_t family)
 {
   switch (family) {
     case AF_INET: {
-      sockaddr_in addr;
-      memset(&addr, 0, sizeof(addr));
-
+      sockaddr_in addr = net::createSockaddrIn(0, 0);
       socklen_t addrlen = sizeof(addr);
 
       int rc = ::accept(s, (sockaddr*) &addr, &addrlen);
@@ -60,11 +59,7 @@ inline Try<int> accept(int s, sa_family_t family)
 
 inline Try<int> bind(int s, const Node& node)
 {
-  sockaddr_in addr;
-  memset(&addr, 0, sizeof(addr));
-  addr.sin_family = AF_INET;
-  addr.sin_addr.s_addr = node.ip;
-  addr.sin_port = htons(node.port);
+  sockaddr_in addr = net::createSockaddrIn(node.ip, node.port);
 
   int rc = ::bind(s, (sockaddr*) &addr, sizeof(addr));
   if (rc < 0)
@@ -75,11 +70,7 @@ inline Try<int> bind(int s, const Node& node)
 
 inline Try<int> connect(int s, const Node& node)
 {
-  sockaddr_in addr;
-  memset(&addr, 0, sizeof(addr));
-  addr.sin_family = AF_INET;
-  addr.sin_port = htons(node.port);
-  addr.sin_addr.s_addr = node.ip;
+  sockaddr_in addr = net::createSockaddrIn(node.ip, node.port);
 
   int rc = ::connect(s, (sockaddr*) &addr, sizeof(addr));
   if (rc < 0)
@@ -92,9 +83,7 @@ inline Try<Node> getsockname(int s, sa_family_t family)
 {
   switch (family) {
     case AF_INET: {
-      sockaddr_in addr;
-      memset(&addr, 0, sizeof(addr));
-
+      sockaddr_in addr = net::createSockaddrIn(0, 0);
       socklen_t addrlen = sizeof(addr);
 
       if(::getsockname(s, (sockaddr*) &addr, &addrlen) < 0)

http://git-wip-us.apache.org/repos/asf/mesos/blob/0503b676/3rdparty/libprocess/src/pid.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/pid.cpp b/3rdparty/libprocess/src/pid.cpp
index a2c620e..085e0b9 100644
--- a/3rdparty/libprocess/src/pid.cpp
+++ b/3rdparty/libprocess/src/pid.cpp
@@ -16,6 +16,7 @@
 #include <process/pid.hpp>
 #include <process/process.hpp>
 
+#include <stout/net.hpp>
 #include <stout/os.hpp>
 
 #include "config.hpp"
@@ -109,42 +110,16 @@ istream& operator >> (istream& stream, UPID& pid)
     return stream;
   }
 
-  hostent he, *hep;
-  char* temp;
-  size_t length;
-  int result;
-  int herrno;
-
-  // Allocate temporary buffer for gethostbyname2_r.
-  length = 1024;
-  temp = new char[length];
-
-  while ((result = gethostbyname2_r(
-      host.c_str(), AF_INET, &he, temp, length, &hep, &herrno)) == ERANGE) {
-    // Enlarge the buffer.
-    delete[] temp;
-    length *= 2;
-    temp = new char[length];
-  }
-
-  if (result != 0 || hep == NULL) {
-    VLOG(2) << "Failed to parse host '" << host
-            << "' because " << hstrerror(herrno);
-    delete[] temp;
-    stream.setstate(std::ios_base::badbit);
-    return stream;
-  }
+  //TODO(evelinad): Extend this to support IPv6
+  Try<uint32_t> ip = net::getIP(host, AF_INET);
 
-  if (hep->h_addr_list[0] == NULL) {
-    VLOG(2) << "Got no addresses for '" << host << "'";
-    delete[] temp;
+  if (ip.isError()) {
+    VLOG(2) << ip.error();
     stream.setstate(std::ios_base::badbit);
     return stream;
   }
 
-  node.ip = *((uint32_t*) hep->h_addr_list[0]);
-
-  delete[] temp;
+  node.ip = ip.get();
 
   str = str.substr(index + 1);
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/0503b676/3rdparty/libprocess/src/process.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/process.cpp b/3rdparty/libprocess/src/process.cpp
index 611889b..c0d80bf 100644
--- a/3rdparty/libprocess/src/process.cpp
+++ b/3rdparty/libprocess/src/process.cpp
@@ -871,14 +871,13 @@ void initialize(const string& delegate)
     }
 
     // Lookup IP address of local hostname.
-    hostent* he;
+    Try<uint32_t> ip = net::getIP(hostname, AF_INET);
 
-    if ((he = gethostbyname2(hostname, AF_INET)) == NULL) {
-      LOG(FATAL) << "Failed to initialize, gethostbyname2: "
-                 << hstrerror(h_errno);
+    if (ip.isError()) {
+      LOG(FATAL) << ip.error();
     }
 
-    __node__.ip = *((uint32_t *) he->h_addr_list[0]);
+    __node__.ip = ip.get();
   }
 
   Try<Nothing> listen = __s__.listen(LISTEN_BACKLOG);