Fixed hostname in statistics to properly be in packed binary format.

This commit is contained in:
Thulinma 2014-10-22 16:50:55 +02:00
parent 7e8aaf8ee0
commit a6a6e296bc
3 changed files with 39 additions and 2 deletions

View file

@ -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

View file

@ -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;
}

View file

@ -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.