summaryrefslogtreecommitdiff
path: root/cpp/src
diff options
context:
space:
mode:
authorMarc Laukien <marc@zeroc.com>2003-12-11 13:50:24 +0000
committerMarc Laukien <marc@zeroc.com>2003-12-11 13:50:24 +0000
commit80a2f14f44ea4e0e8db5726a29fea041aa1ab9a3 (patch)
tree442ce2ec34c763591e440462d87ca6767220a141 /cpp/src
parentUse a single connection with multiple subdirs (diff)
downloadice-80a2f14f44ea4e0e8db5726a29fea041aa1ab9a3.tar.bz2
ice-80a2f14f44ea4e0e8db5726a29fea041aa1ab9a3.tar.xz
ice-80a2f14f44ea4e0e8db5726a29fea041aa1ab9a3.zip
gethostbyname_r
Diffstat (limited to 'cpp/src')
-rw-r--r--cpp/src/Ice/Network.cpp104
-rw-r--r--cpp/src/Ice/Network.h1
-rw-r--r--cpp/src/Ice/TcpTransceiver.cpp2
-rw-r--r--cpp/src/IceSSL/SslTransceiver.cpp4
4 files changed, 98 insertions, 13 deletions
diff --git a/cpp/src/Ice/Network.cpp b/cpp/src/Ice/Network.cpp
index 0f62100639d..3514147aab1 100644
--- a/cpp/src/Ice/Network.cpp
+++ b/cpp/src/Ice/Network.cpp
@@ -20,6 +20,14 @@ using namespace std;
using namespace Ice;
using namespace IceInternal;
+#ifdef __sun
+# define INADDR_NONE -1
+#endif
+
+#ifdef _WIN32
+# define HAVE_NO_GETHOSTBYNAME_R
+#endif
+
bool
IceInternal::interrupted()
{
@@ -179,16 +187,38 @@ IceInternal::closeSocket(SOCKET fd)
{
#ifdef _WIN32
int error = WSAGetLastError();
- closesocket(fd);
+ if(closesocket(fd) == SOCKET_ERROR)
+ {
+ SocketException ex(__FILE__, __LINE__);
+ ex.error = getSocketErrno();
+ throw ex;
+ }
WSASetLastError(error);
#else
int error = errno;
+ if(close(fd) == SOCKET_ERROR)
+ {
+ SocketException ex(__FILE__, __LINE__);
+ ex.error = getSocketErrno();
+ throw ex;
+ }
close(fd);
errno = error;
#endif
}
void
+IceInternal::shutdownSocket(SOCKET fd)
+{
+ if(shutdown(fd, SHUT_WR) == SOCKET_ERROR)
+ {
+ SocketException ex(__FILE__, __LINE__);
+ ex.error = getSocketErrno();
+ throw ex;
+ }
+}
+
+void
IceInternal::setBlock(SOCKET fd, bool block)
{
if(block)
@@ -546,7 +576,9 @@ repeatAccept:
return ret;
}
+#ifdef HAVE_NO_GETHOSTBYNAME_R
static IceUtil::Mutex getHostByNameMutex;
+#endif
void
IceInternal::getAddress(const string& host, int port, struct sockaddr_in& addr)
@@ -556,20 +588,18 @@ IceInternal::getAddress(const string& host, int port, struct sockaddr_in& addr)
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(host.c_str());
-#ifdef __sun
-#define INADDR_NONE -1
-#endif
-
if(addr.sin_addr.s_addr == INADDR_NONE)
{
- IceUtil::Mutex::Lock sync(getHostByNameMutex);
-
struct hostent* entry;
int retry = 5;
+#ifdef HAVE_NO_GETHOSTBYNAME_R
+
+ IceUtil::Mutex::Lock sync(getHostByNameMutex);
+
do
{
- entry = gethostbyname(host.c_str());
+ entry = gethostbyname(host);
}
#ifdef _WIN32
while(!entry && WSAGetLastError() == WSATRY_AGAIN && --retry >= 0);
@@ -589,6 +619,32 @@ IceInternal::getAddress(const string& host, int port, struct sockaddr_in& addr)
throw ex;
}
+#else
+
+ struct hostent entryBuf;
+ vector<char> buf(1024);
+ int res;
+ int herr = 0;
+
+ do
+ {
+ while((res = gethostbyname_r(host.c_str(), &entryBuf, &buf[0], buf.size(), &entry, &herr)) == ERANGE)
+ {
+ buf.resize(buf.size() * 2);
+ }
+ }
+ while(res && herr == TRY_AGAIN && --retry >= 0);
+
+ if(res)
+ {
+ DNSException ex(__FILE__, __LINE__);
+ ex.error = herr;
+ ex.host = host;
+ throw ex;
+ }
+
+#endif
+
memcpy(&addr.sin_addr, entry->h_addr, entry->h_length);
}
}
@@ -605,11 +661,13 @@ IceInternal::getLocalHost(bool numeric)
}
{
- IceUtil::Mutex::Lock sync(getHostByNameMutex);
-
struct hostent* entry;
int retry = 5;
+#ifdef HAVE_NO_GETHOSTBYNAME_R
+
+ IceUtil::Mutex::Lock sync(getHostByNameMutex);
+
do
{
entry = gethostbyname(host);
@@ -632,6 +690,32 @@ IceInternal::getLocalHost(bool numeric)
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 && herr == TRY_AGAIN && --retry >= 0);
+
+ if(res)
+ {
+ DNSException ex(__FILE__, __LINE__);
+ ex.error = herr;
+ ex.host = host;
+ throw ex;
+ }
+
+#endif
+
if(numeric)
{
struct sockaddr_in addr;
diff --git a/cpp/src/Ice/Network.h b/cpp/src/Ice/Network.h
index 3bdd541d513..cfe05602f45 100644
--- a/cpp/src/Ice/Network.h
+++ b/cpp/src/Ice/Network.h
@@ -76,6 +76,7 @@ ICE_PROTOCOL_API bool recvTruncated();
ICE_PROTOCOL_API SOCKET createSocket(bool);
ICE_PROTOCOL_API void closeSocket(SOCKET);
+ICE_PROTOCOL_API void shutdownSocket(SOCKET);
ICE_PROTOCOL_API void setBlock(SOCKET, bool);
ICE_PROTOCOL_API void setTcpNoDelay(SOCKET);
diff --git a/cpp/src/Ice/TcpTransceiver.cpp b/cpp/src/Ice/TcpTransceiver.cpp
index e5003c94954..f723066876d 100644
--- a/cpp/src/Ice/TcpTransceiver.cpp
+++ b/cpp/src/Ice/TcpTransceiver.cpp
@@ -56,7 +56,7 @@ IceInternal::TcpTransceiver::shutdown()
}
assert(_fd != INVALID_SOCKET);
- ::shutdown(_fd, SHUT_WR); // Shutdown socket for writing
+ shutdownSocket(_fd);
}
void
diff --git a/cpp/src/IceSSL/SslTransceiver.cpp b/cpp/src/IceSSL/SslTransceiver.cpp
index 164c642aec6..c452ad67610 100644
--- a/cpp/src/IceSSL/SslTransceiver.cpp
+++ b/cpp/src/IceSSL/SslTransceiver.cpp
@@ -102,7 +102,7 @@ IceSSL::SslTransceiver::shutdown()
while((shutdown == 0) && (retries < 0));
assert(_fd != INVALID_SOCKET);
- ::shutdown(_fd, SHUT_WR); // Shutdown socket for writing
+ shutdownSocket(_fd);
}
void
@@ -453,7 +453,7 @@ IceSSL::SslTransceiver::internalShutdown(int timeout)
if(retCode == 1)
{
// Shutdown successful - shut down the socket for writing.
- ::shutdown(SSL_get_fd(_sslConnection), SHUT_WR);
+ shutdownSocket(SSL_get_fd(_sslConnection));
}
else if(retCode == -1)
{