From a6a6e296bceb3db91b65547a19dc3f638d43e1a1 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Wed, 22 Oct 2014 16:50:55 +0200 Subject: [PATCH] Fixed hostname in statistics to properly be in packed binary format. --- lib/shared_memory.cpp | 5 ++++- lib/socket.cpp | 35 ++++++++++++++++++++++++++++++++++- lib/socket.h | 1 + 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/shared_memory.cpp b/lib/shared_memory.cpp index 9fa606a3..d8a7cea6 100644 --- a/lib/shared_memory.cpp +++ b/lib/shared_memory.cpp @@ -546,12 +546,15 @@ namespace IPC { ///\brief Sets the host of this connection void statExchange::host(std::string name) { + if (name.size() < 16){ + memset(data+32, 0, 16); + } memcpy(data + 32, name.c_str(), std::min((int)name.size(), 16)); } ///\brief Gets the host of this connection std::string statExchange::host() { - return std::string(data + 32, std::min((int)strlen(data + 32), 16)); + return std::string(data + 32, 16); } ///\brief Sets the name of the stream this user is viewing diff --git a/lib/socket.cpp b/lib/socket.cpp index dab7d591..5aa905ba 100644 --- a/lib/socket.cpp +++ b/lib/socket.cpp @@ -581,6 +581,40 @@ std::string Socket::Connection::getHost() { return remotehost; } +/// Gets hostname for connection, if available. +/// Guaranteed to be either empty or 16 bytes long. +std::string Socket::Connection::getBinHost() { + if (remotehost.size()){ + struct addrinfo * result, *rp, hints; + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_ADDRCONFIG; + hints.ai_protocol = 0; + hints.ai_canonname = NULL; + hints.ai_addr = NULL; + hints.ai_next = NULL; + int s = getaddrinfo(remotehost.c_str(), 0, &hints, &result); + if (s != 0) { + DEBUG_MSG(DLVL_FAIL, "Could not resolve '%s'! Error: %s", remotehost.c_str(), gai_strerror(s)); + return ""; + } + char tmpBuffer[17] = "\000\000\000\000\000\000\000\000\000\000\377\377\000\000\000\000"; + for (rp = result; rp != NULL; rp = rp->ai_next) { + if (rp->ai_family == AF_INET){ + memcpy(tmpBuffer + 12, &((sockaddr_in *)rp->ai_addr)->sin_addr.s_addr, 4); + } + if (rp->ai_family == AF_INET6){ + memcpy(tmpBuffer, ((sockaddr_in6 *)rp->ai_addr)->sin6_addr.s6_addr, 16); + } + } + freeaddrinfo(result); + return std::string(tmpBuffer, 16); + }else{ + return ""; + } +} + /// Sets hostname for connection manually. /// Overwrites the detected host, thus possibily making it incorrect. void Socket::Connection::setHost(std::string host) { @@ -618,7 +652,6 @@ bool Socket::Connection::isAddress(std::string addr) { hints.ai_addr = NULL; hints.ai_next = NULL; int s = getaddrinfo(addr.c_str(), 0, &hints, &result); - DEBUG_MSG(DLVL_DEVEL, "Meh: %s", addr.c_str()); if (s != 0) { return false; } diff --git a/lib/socket.h b/lib/socket.h index 2599a221..5fbd5380 100644 --- a/lib/socket.h +++ b/lib/socket.h @@ -72,6 +72,7 @@ namespace Socket { void setBlocking(bool blocking); ///< Set this socket to be blocking (true) or nonblocking (false). bool isBlocking(); ///< Check if this socket is blocking (true) or nonblocking (false). std::string getHost(); ///< Gets hostname for connection, if available. + std::string getBinHost(); void setHost(std::string host); ///< Sets hostname for connection manually. int getSocket(); ///< Returns internal socket number. std::string getError(); ///< Returns a string describing the last error that occured.