From 87259631e4f9baf4cafb55a75db16ca9cc20d40e Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Mon, 10 Dec 2012 22:59:19 +0100 Subject: SSL server identity check. --- src/platforms/windows/windowsSocket.cpp | 67 +++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'src/platforms/windows/windowsSocket.cpp') diff --git a/src/platforms/windows/windowsSocket.cpp b/src/platforms/windows/windowsSocket.cpp index dc6f1b47..abc16d70 100644 --- a/src/platforms/windows/windowsSocket.cpp +++ b/src/platforms/windows/windowsSocket.cpp @@ -91,6 +91,8 @@ void windowsSocket::connect(const vmime::string& address, const vmime::port_t po memcpy(reinterpret_cast (&addr.sin_addr), hostInfo->h_addr, hostInfo->h_length); } + m_serverAddress = address; + // Get a new socket m_desc = ::socket(AF_INET, SOCK_STREAM, 0); @@ -156,6 +158,71 @@ void windowsSocket::disconnect() } +static bool isNumericAddress(const char* address) +{ + struct addrinfo hint, *info = NULL; + memset(&hint, 0, sizeof(hint)); + + hint.ai_family = AF_UNSPEC; + hint.ai_flags = AI_NUMERICHOST; + + if (getaddrinfo(address, 0, &hint, &info) == 0) + { + freeaddrinfo(info); + return true; + } + else + { + return false; + } +} + + +const string windowsSocket::getPeerAddress() const +{ + // Get address of connected peer + sockaddr peer; + socklen_t peerLen = sizeof(peer); + + getpeername(m_desc, reinterpret_cast (&peer), &peerLen); + + // Convert to numerical presentation format + char numericAddress[1024]; + + if (inet_ntop(peer.sa_family, &peer, numericAddress, sizeof(numericAddress)) != NULL) + return string(numericAddress); + + return ""; // should not happen +} + + +const string windowsSocket::getPeerName() const +{ + // Get address of connected peer + sockaddr peer; + socklen_t peerLen = sizeof(peer); + + getpeername(m_desc, reinterpret_cast (&peer), &peerLen); + + // If server address as specified when connecting is a numeric + // address, try to get a host name for it + if (isNumericAddress(m_serverAddress.c_str())) + { + char host[NI_MAXHOST + 1]; + char service[NI_MAXSERV + 1]; + + if (getnameinfo(reinterpret_cast (&peer), peerLen, + host, sizeof(host), service, sizeof(service), + /* flags */ NI_NAMEREQD) == 0) + { + return string(host); + } + } + + return m_serverAddress; +} + + windowsSocket::size_type windowsSocket::getBlockSize() const { return 16384; // 16 KB -- cgit v1.2.3