Merge pull request #140 from constcast/hoststatistics-crash

Bugfix: Error out on invalid subnet in HostStatistics
master
Lothar Braun 2020-05-27 08:51:56 +02:00 committed by GitHub
commit 2e45f1b9e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 2 deletions

View File

@ -33,9 +33,23 @@ HostStatistics::HostStatistics(std::string ipSubnet, std::string addrFilter, std
// check if srcIP or dstIP in the subnet (1.1.1.1/16)
// split string at the '/'
size_t found = ipSubnet.find_first_of("/");
if (found == string::npos){
THROWEXCEPTION("HostStatistics: Received invalid subnet: '%s'", ipSubnet.c_str());
}
std::string ip_str = ipSubnet.substr(0, found);
netSize = atoi(ipSubnet.substr(found + 1).c_str());
netAddr = *(uint32_t *)gethostbyname(ip_str.c_str())->h_addr;
std::string netmask = ipSubnet.substr(found + 1);
try {
netSize = std::stoi(netmask);
if (netSize > 32)
throw std::runtime_error("Invalid subnet masksize");
} catch (...) {
THROWEXCEPTION("HostStatistics: Invalid subnet mask in subnet: '%s'", netmask.c_str());
}
struct hostent* host = gethostbyname(ip_str.c_str());
if (host == NULL) {
THROWEXCEPTION("HostStatistics: Invalid IP address in subnet: '%s'", ip_str.c_str());
}
netAddr = *(uint32_t *)host->h_addr;
logTimer = time(NULL);
}