Fixed #135: getPeerAddress() is incorrect. Error handling.

This commit is contained in:
Vincent Richard 2016-03-16 19:46:30 +01:00
parent 4fd8976515
commit 7425d91de6

View File

@ -516,15 +516,20 @@ const string posixSocket::getPeerAddress() const
sockaddr peer; sockaddr peer;
socklen_t peerLen = sizeof(peer); socklen_t peerLen = sizeof(peer);
getpeername(m_desc, reinterpret_cast <sockaddr*>(&peer), &peerLen); if (getpeername(m_desc, &peer, &peerLen) != 0)
{
throwSocketError(errno);
}
// Convert to numerical presentation format // Convert to numerical presentation format
char numericAddress[1024]; char buf[INET6_ADDRSTRLEN];
if (inet_ntop(peer.sa_family, &peer, numericAddress, sizeof(numericAddress)) != NULL) if (!inet_ntop(peer.sa_family, &(reinterpret_cast <struct sockaddr_in *>(&peer))->sin_addr, buf, sizeof(buf)))
return string(numericAddress); {
throwSocketError(errno);
}
return ""; // should not happen return string(buf);
} }
@ -534,7 +539,10 @@ const string posixSocket::getPeerName() const
sockaddr peer; sockaddr peer;
socklen_t peerLen = sizeof(peer); socklen_t peerLen = sizeof(peer);
getpeername(m_desc, reinterpret_cast <sockaddr*>(&peer), &peerLen); if (getpeername(m_desc, &peer, &peerLen) != 0)
{
throwSocketError(errno);
}
// If server address as specified when connecting is a numeric // If server address as specified when connecting is a numeric
// address, try to get a host name for it // address, try to get a host name for it