Merge pull request #140 from constcast/hoststatistics-crash
Bugfix: Error out on invalid subnet in HostStatisticsmaster
commit
2e45f1b9e2
|
@ -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)
|
// check if srcIP or dstIP in the subnet (1.1.1.1/16)
|
||||||
// split string at the '/'
|
// split string at the '/'
|
||||||
size_t found = ipSubnet.find_first_of("/");
|
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);
|
std::string ip_str = ipSubnet.substr(0, found);
|
||||||
netSize = atoi(ipSubnet.substr(found + 1).c_str());
|
std::string netmask = ipSubnet.substr(found + 1);
|
||||||
netAddr = *(uint32_t *)gethostbyname(ip_str.c_str())->h_addr;
|
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);
|
logTimer = time(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue