summaryrefslogtreecommitdiff
path: root/cpp/src
diff options
context:
space:
mode:
authorBernard Normier <bernard@zeroc.com>2004-01-22 00:47:14 +0000
committerBernard Normier <bernard@zeroc.com>2004-01-22 00:47:14 +0000
commit4b789259c8584c15e6c7e135a52daf49b2c36d8d (patch)
treedde81d28227076d2a97a93b5624bef241417f3d8 /cpp/src
parentadd --noclose, --nochdir; close fds after initializing communicator (diff)
downloadice-4b789259c8584c15e6c7e135a52daf49b2c36d8d.tar.bz2
ice-4b789259c8584c15e6c7e135a52daf49b2c36d8d.tar.xz
ice-4b789259c8584c15e6c7e135a52daf49b2c36d8d.zip
switchhed to getaddrinfo() + inet_ntoa cleanup
Diffstat (limited to 'cpp/src')
-rw-r--r--cpp/src/Ice/Network.cpp263
-rw-r--r--cpp/src/Ice/Network.h1
-rwxr-xr-xcpp/src/Ice/Service.cpp2
3 files changed, 112 insertions, 154 deletions
diff --git a/cpp/src/Ice/Network.cpp b/cpp/src/Ice/Network.cpp
index ce0c02b1aaa..84efc64a952 100644
--- a/cpp/src/Ice/Network.cpp
+++ b/cpp/src/Ice/Network.cpp
@@ -23,15 +23,6 @@ using namespace IceInternal;
# define INADDR_NONE -1
#endif
-#ifdef _WIN32
-# define HAVE_NO_GETHOSTBYNAME_R
-#endif
-
-#ifdef HAVE_NO_GETHOSTBYNAME_R
-# include <IceUtil/StaticMutex.h>
-static IceUtil::StaticMutex getHostByNameMutex;
-#endif
-
bool
IceInternal::interrupted()
{
@@ -607,62 +598,60 @@ IceInternal::getAddress(const string& host, int port, struct sockaddr_in& addr)
if(addr.sin_addr.s_addr == INADDR_NONE)
{
- struct hostent* entry;
- int retry = 5;
-#ifdef HAVE_NO_GETHOSTBYNAME_R
-
- IceUtil::StaticMutex::Lock sync(getHostByNameMutex);
+#ifdef _WIN32
+ //
+ // Windows XP has getaddrinfo(), but we don't want to require XP to run Ice.
+ //
+ //
+ // gethostbyname() is thread safe on Windows, with a separate hostent per thread
+ //
+ struct hostent* entry;
+ int retry = 5;
do
{
entry = gethostbyname(host.c_str());
}
-#ifdef _WIN32
- while(!entry && WSAGetLastError() == WSATRY_AGAIN && --retry >= 0);
-#else
- while(!entry && h_errno == TRY_AGAIN && --retry >= 0);
-#endif
-
- if(!entry)
+ while(entry == 0 && WSAGetLastError() == WSATRY_AGAIN && --retry >= 0);
+
+ if(entry == 0)
{
DNSException ex(__FILE__, __LINE__);
-#ifdef _WIN32
+
ex.error = WSAGetLastError();
-#else
- ex.error = h_errno;
-#endif
ex.host = host;
throw ex;
}
+ memcpy(&addr.sin_addr, entry->h_addr, entry->h_length);
#else
+ struct addrinfo* info = 0;
+ int retry = 5;
- struct hostent entryBuf;
- vector<char> buf(1024);
- int res;
- int herr = 0;
-
+ struct addrinfo hints = { 0 };
+ hints.ai_family = PF_INET;
+
+ int rs = 0;
do
{
- while((res = gethostbyname_r(host.c_str(), &entryBuf, &buf[0], buf.size(), &entry, &herr)) == ERANGE)
- {
- buf.resize(buf.size() * 2);
- }
- }
- while(res != 0 && herr == TRY_AGAIN && --retry >= 0);
-
- if(res)
+ rs = getaddrinfo(host.c_str(), 0, &hints, &info);
+ } while(info == 0 && rs == EAI_AGAIN && --retry >= 0);
+
+ if(rs != 0)
{
DNSException ex(__FILE__, __LINE__);
- ex.error = herr;
+ ex.error = rs;
ex.host = host;
throw ex;
}
-#endif
+ assert(info->ai_family == PF_INET);
+ struct sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(info->ai_addr);
- memcpy(&addr.sin_addr, entry->h_addr, entry->h_length);
+ addr.sin_addr.s_addr = sin->sin_addr.s_addr;
+ freeaddrinfo(info);
+#endif
}
}
@@ -677,74 +666,78 @@ IceInternal::getLocalHost(bool numeric)
throw ex;
}
- {
- struct hostent* entry;
- int retry = 5;
-
-#ifdef HAVE_NO_GETHOSTBYNAME_R
-
- IceUtil::StaticMutex::Lock sync(getHostByNameMutex);
-
- do
- {
- entry = gethostbyname(host);
- }
#ifdef _WIN32
- while(!entry && WSAGetLastError() == WSATRY_AGAIN && --retry >= 0);
-#else
- while(!entry && h_errno == TRY_AGAIN && --retry >= 0);
-#endif
+ //
+ // Windows XP has getaddrinfo(), but we don't want to require XP to run Ice.
+ //
- if(!entry)
- {
- DNSException ex(__FILE__, __LINE__);
-#ifdef _WIN32
- ex.error = WSAGetLastError();
-#else
- ex.error = h_errno;
-#endif
- ex.host = host;
- throw ex;
- }
-
-#else
-
- struct hostent entryBuf;
- vector<char> buf(1024);
- int res;
- int herr = 0;
-
- do
- {
- while((res = gethostbyname_r(host, &entryBuf, &buf[0], buf.size(), &entry, &herr)) == ERANGE)
- {
- buf.resize(buf.size() * 2);
- }
- }
- while(res != 0 && herr == TRY_AGAIN && --retry >= 0);
-
- if(res)
- {
- DNSException ex(__FILE__, __LINE__);
- ex.error = herr;
- ex.host = host;
- throw ex;
- }
+ //
+ // gethostbyname() is thread safe on Windows, with a separate hostent per thread
+ //
+ struct hostent* entry;
+ int retry = 5;
+ do
+ {
+ entry = gethostbyname(host);
+ }
+ while(entry == 0 && WSAGetLastError() == WSATRY_AGAIN && --retry >= 0);
+
+ if(entry == 0)
+ {
+ DNSException ex(__FILE__, __LINE__);
+ ex.error = WSAGetLastError();
+ ex.host = host;
+ throw ex;
+ }
-#endif
+ if(numeric)
+ {
+ struct sockaddr_in addr;
+ memset(&addr, 0, sizeof(struct sockaddr_in));
+ memcpy(&addr.sin_addr, entry->h_addr, entry->h_length);
+ return string(inet_ntoa(addr.sin_addr));
+ }
+ else
+ {
+ return string(entry->h_name);
+ }
- if(numeric)
- {
- struct sockaddr_in addr;
- memset(&addr, 0, sizeof(struct sockaddr_in));
- memcpy(&addr.sin_addr, entry->h_addr, entry->h_length);
- return string(inet_ntoa(addr.sin_addr));
- }
- else
- {
- return string(entry->h_name);
- }
+#else
+
+ struct addrinfo* info = 0;
+ int retry = 5;
+
+ struct addrinfo hints = { 0 };
+ hints.ai_family = PF_INET;
+
+ int rs = 0;
+ do
+ {
+ rs = getaddrinfo(host, 0, &hints, &info);
+ } while(info == 0 && rs == EAI_AGAIN && --retry >= 0);
+
+ if(rs != 0)
+ {
+ DNSException ex(__FILE__, __LINE__);
+ ex.error = rs;
+ ex.host = host;
+ throw ex;
+ }
+
+ string result;
+ if(numeric)
+ {
+ assert(info->ai_family == PF_INET);
+ struct sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(info->ai_addr);
+ result = inet_ntoa(sin->sin_addr);
+ }
+ else
+ {
+ result = info->ai_canonname;
}
+ freeaddrinfo(info);
+ return result;
+#endif
}
bool
@@ -1017,29 +1010,7 @@ IceInternal::errorToString(int error)
string
IceInternal::errorToStringDNS(int error)
{
- switch(error)
- {
- case NETDB_SUCCESS:
- return "no problem";
-
- case NETDB_INTERNAL:
- return "internal problem";
-
- case HOST_NOT_FOUND:
- return "no such host is known";
-
- case TRY_AGAIN:
- return "temporary error, try again";
-
- case NO_RECOVERY:
- return "unexpected server failure";
-
- case NO_DATA:
- return "name has no IP address";
-
- default:
- return "unknown DNS error";
- }
+ return gai_strerror(error);
}
#endif
@@ -1054,18 +1025,6 @@ IceInternal::lastErrorToString()
#endif
}
-string
-IceInternal::lastErrorToStringDNS()
-{
-#ifdef _WIN32
- return errorToStringDNS(WSAGetLastError());
-#else
- return errorToStringDNS(h_errno);
-#endif
-}
-
-static IceUtil::Mutex inetNtoaMutex;
-
std::string
IceInternal::fdToString(SOCKET fd)
{
@@ -1103,28 +1062,26 @@ IceInternal::fdToString(SOCKET fd)
}
ostringstream s;
-
+ s << "local address = " << inet_ntoa(localAddr.sin_addr) << ':' << ntohs(localAddr.sin_port);
+ if(peerNotConnected)
{
- IceUtil::Mutex::Lock sync(inetNtoaMutex);
-
- s << "local address = " << inet_ntoa(localAddr.sin_addr) << ':' << ntohs(localAddr.sin_port);
- if(peerNotConnected)
- {
- s << "\nremote address = <not connected>";
- }
- else
- {
- s << "\nremote address = " << inet_ntoa(remoteAddr.sin_addr) << ':' << ntohs(remoteAddr.sin_port);
- }
+ s << "\nremote address = <not connected>";
+ }
+ else
+ {
+ s << "\nremote address = " << inet_ntoa(remoteAddr.sin_addr) << ':' << ntohs(remoteAddr.sin_port);
}
-
return s.str();
}
std::string
IceInternal::addrToString(const struct sockaddr_in& addr)
{
- IceUtil::Mutex::Lock sync(inetNtoaMutex);
+ //
+ // inet_ntoa uses thread-specific data on Windows, Linux, Solaris
+ // and HP-UX
+ //
+
ostringstream s;
s << inet_ntoa(addr.sin_addr) << ':' << ntohs(addr.sin_port);
return s.str();
diff --git a/cpp/src/Ice/Network.h b/cpp/src/Ice/Network.h
index cfe05602f45..d0db8d92527 100644
--- a/cpp/src/Ice/Network.h
+++ b/cpp/src/Ice/Network.h
@@ -100,7 +100,6 @@ ICE_PROTOCOL_API void createPipe(SOCKET fds[2]);
ICE_PROTOCOL_API std::string errorToString(int);
ICE_PROTOCOL_API std::string errorToStringDNS(int);
ICE_PROTOCOL_API std::string lastErrorToString();
-ICE_PROTOCOL_API std::string lastErrorToStringDNS();
ICE_PROTOCOL_API std::string fdToString(SOCKET);
ICE_PROTOCOL_API std::string addrToString(const struct sockaddr_in&);
diff --git a/cpp/src/Ice/Service.cpp b/cpp/src/Ice/Service.cpp
index d7edd280df9..a6b75381593 100755
--- a/cpp/src/Ice/Service.cpp
+++ b/cpp/src/Ice/Service.cpp
@@ -26,6 +26,8 @@
#else
# include <Ice/Logger.h>
# include <Ice/Network.h>
+# include <sys/types.h>
+# include <sys/stat.h>
#endif
#include <fstream>