From 7425d91de606980f3c0855a59c84af03082a6183 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Wed, 16 Mar 2016 19:46:30 +0100 Subject: [PATCH] Fixed #135: getPeerAddress() is incorrect. Error handling. --- src/vmime/platforms/posix/posixSocket.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/vmime/platforms/posix/posixSocket.cpp b/src/vmime/platforms/posix/posixSocket.cpp index 0f417360..b0e7e6fd 100644 --- a/src/vmime/platforms/posix/posixSocket.cpp +++ b/src/vmime/platforms/posix/posixSocket.cpp @@ -516,15 +516,20 @@ const string posixSocket::getPeerAddress() const sockaddr peer; socklen_t peerLen = sizeof(peer); - getpeername(m_desc, reinterpret_cast (&peer), &peerLen); + if (getpeername(m_desc, &peer, &peerLen) != 0) + { + throwSocketError(errno); + } // Convert to numerical presentation format - char numericAddress[1024]; + char buf[INET6_ADDRSTRLEN]; - if (inet_ntop(peer.sa_family, &peer, numericAddress, sizeof(numericAddress)) != NULL) - return string(numericAddress); + if (!inet_ntop(peer.sa_family, &(reinterpret_cast (&peer))->sin_addr, buf, sizeof(buf))) + { + throwSocketError(errno); + } - return ""; // should not happen + return string(buf); } @@ -534,7 +539,10 @@ const string posixSocket::getPeerName() const sockaddr peer; socklen_t peerLen = sizeof(peer); - getpeername(m_desc, reinterpret_cast (&peer), &peerLen); + if (getpeername(m_desc, &peer, &peerLen) != 0) + { + throwSocketError(errno); + } // If server address as specified when connecting is a numeric // address, try to get a host name for it