-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subnet_Calculator.cpp
108 lines (86 loc) · 3.42 KB
/
Subnet_Calculator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <string>
#include <bitset>
#include <regex>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
struct SubnetCalculator {
std::string ip_address;
int cidr;
SubnetCalculator(std::string ip, int c) : ip_address(ip), cidr(c) {}
uint32_t ipToUInt(const std::string& ip) {
struct sockaddr_in sa;
inet_pton(AF_INET, ip.c_str(), &(sa.sin_addr));
return ntohl(sa.sin_addr.s_addr);
}
std::string uintToIp(uint32_t ip) {
struct in_addr addr;
addr.s_addr = htonl(ip);
char str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &addr, str, INET_ADDRSTRLEN);
return std::string(str);
}
void calculate() {
uint32_t ip = ipToUInt(ip_address);
uint32_t mask = (0xFFFFFFFF << (32 - cidr)) & 0xFFFFFFFF;
uint32_t network = ip & mask;
uint32_t broadcast = network | ~mask;
int total_hosts = (1 << (32 - cidr));
int usable_hosts = total_hosts - 2;
std::cout << "IP Address: " << ip_address << std::endl;
std::cout << "Network Address: " << uintToIp(network) << std::endl;
std::cout << "Usable Host IP Range: " << uintToIp(network + 1) << " - " << uintToIp(broadcast - 1) << std::endl;
std::cout << "Broadcast Address: " << uintToIp(broadcast) << std::endl;
std::cout << "Total Number of Hosts: " << total_hosts << std::endl;
std::cout << "Number of Usable Hosts: " << usable_hosts << std::endl;
std::cout << "Subnet Mask: " << uintToIp(mask) << std::endl;
std::cout << "CIDR Notation: /" << cidr << std::endl;
std::cout << "IP Type: " << (ip >= ipToUInt("10.0.0.0") && ip <= ipToUInt("10.255.255.255") ? "Private" : "Public") << std::endl;
}
};
bool isValidIP(const std::string& ip) {
std::regex ip_regex(
R"(^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$)");
return std::regex_match(ip, ip_regex);
}
bool isValidCIDR(int cidr) {
return (cidr >= 0 && cidr <= 32);
}
int main() {
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
while (true) {
std::string ip;
int cidr;
do {
system("cls");
std::cout << "Enter IP address: ";
std::cin >> ip;
if (!isValidIP(ip)) {
std::cout << "Invalid IP address format. Please try again." << std::endl;
system("pause");
}
} while (!isValidIP(ip));
do {
std::cout << "Enter CIDR notation (0-32): ";
std::cin >> cidr;
std::cout << "\n\n";
if (!isValidCIDR(cidr)) {
std::cout << "Invalid CIDR notation. Please enter a number between 0 and 32." << std::endl;
system("pause");
}
} while (!isValidCIDR(cidr));
SubnetCalculator calculator(ip, cidr);
calculator.calculate();
std::cout << "Press Enter to calculate another subnet, or type 'exit' to quit." << std::endl;
std::cin.ignore(); // Ignore the newline character left in the buffer
std::string exit_choice;
std::getline(std::cin, exit_choice);
if (exit_choice == "exit") {
break;
}
}
WSACleanup();
return 0;
}