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:18 UTC
[3/5] mesos git commit: Introduced getIP and initialization wrappers
for sockaddr_in and addrinfo
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)
{