37 lines
975 B
C++
37 lines
975 B
C++
#include <iostream>
|
|
#include <string>
|
|
#include <cmath>
|
|
|
|
extern "C" {
|
|
#include <arpa/inet.h>
|
|
}
|
|
|
|
int netcapa4(const std::string& ipStr, int prefix) {
|
|
//uint32_t ipAddress;
|
|
//inet_pton(AF_INET, ipStr.data(), &ipAddress);
|
|
//ipAddress = ntohl(ipAddress);
|
|
|
|
int hostBits = 32 - prefix;
|
|
uint64_t totalIPs = std::pow(2, hostBits);
|
|
//uint64_t usableHosts = (prefix < 31) ? (totalIPs - 2) : totalIPs;
|
|
|
|
//uint32_t subnetMask = (prefix == 0) ? 0 : (~0U << hostBits);
|
|
//uint32_t networkAddress = ipAddress & subnetMask;
|
|
|
|
//struct in_addr netAddr;
|
|
//netAddr.s_addr = htonl(networkAddress);
|
|
//char netStr[INET_ADDRSTRLEN];
|
|
//inet_ntop(AF_INET, &netAddr, netStr, INET_ADDRSTRLEN);
|
|
|
|
//std::cout << "Total IPs: " << totalIPs << "\n";
|
|
//std::cout << "Usable Hosts: " << usableHosts << "\n";
|
|
return totalIPs;
|
|
}
|
|
|
|
int main() {
|
|
auto capa = netcapa4("192.168.1.10", 24);
|
|
std::cout << "Total IPs: " << capa << "\n";
|
|
return 0;
|
|
}
|
|
|