From 96f3c7312032c56387fc774a13901fc32d341ba1 Mon Sep 17 00:00:00 2001 From: Pavel Odintsov Date: Sat, 14 Feb 2015 21:01:42 +0300 Subject: [PATCH] Complete test solution for CIDR issue --- tests/test_cidr.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/tests/test_cidr.cpp b/tests/test_cidr.cpp index ab7feb7..fd10228 100644 --- a/tests/test_cidr.cpp +++ b/tests/test_cidr.cpp @@ -7,6 +7,16 @@ #include #include +uint32_t convert_cidr_to_binary_netmask(unsigned int cidr) { + uint32_t binary_netmask = 0xFFFFFFFF; + binary_netmask = binary_netmask << ( 32 - cidr ); + // htonl from host byte order to network + // ntohl from network byte order to host + + // We need network byte order at output + return htonl(binary_netmask); +} + uint32_t convert_ip_as_string_to_uint(std::string ip) { struct in_addr ip_addr; inet_aton(ip.c_str(), &ip_addr); @@ -16,9 +26,16 @@ uint32_t convert_ip_as_string_to_uint(std::string ip) { } int main() { + uint32_t network_zero = convert_ip_as_string_to_uint("10.10.10.0"); + uint32_t network_200 = convert_ip_as_string_to_uint("10.10.10.200"); + + uint32_t binary_netmask = convert_cidr_to_binary_netmask(24); + + uint32_t generated_subnet_address = network_200 & binary_netmask; + std::cout<<"network byte order"<