-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
131 lines (108 loc) · 3.9 KB
/
main.c
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/17 22:53:07 by apuchill #+# #+# */
/* Updated: 2020/02/17 22:53:07 by apuchill ### ########.fr */
/* */
/* ************************************************************************** */
#include "header.h"
int main(int argc, char const *argv[])
{
printf(HEADER_L("\n========================== ∙ netwhat - IP calculator ∙ =========================\n"));
printf(HEADER_T(" by apuchill ") HEADER_A("@42saopaulo\n"));
unsigned int CIDR;
unsigned int mask[4] = { 0 };
unsigned int IP[4] = { 0 };
unsigned int subnet[4] = { 0 };
unsigned int host[4] = { 0 };
unsigned int range[4] = { 0 };
int i;
int avail_IP_addrs;
CIDR = 0;
i = 0;
/*
** If subnet mask or CIDR is provided
*/
if (argc == 2)
{
if (strchr(argv[1], '.'))
CIDR = get_CIDR(argv[1], mask);
else
CIDR = get_mask(argv[1], mask);
avail_IP_addrs = pow(2, 32 - CIDR);
printf(PROVIDED("* Provided Mask:"));
printf(" %u (CIDR)\n\t\t %u.%u.%u.%u\n\t\t ", CIDR, mask[0], mask[1], mask[2], mask[3]);
print_binary_IP(mask);
printf(CYAN("\n\n* Max nbr of hosts:"));
printf(" %d hosts\n", (avail_IP_addrs <= 2) ? avail_IP_addrs : avail_IP_addrs - 2);
printf(PETROL_B("* Total addresses:") " %d addresses (including host and broadcast)", avail_IP_addrs);
}
/*
** If IP address is provided
*/
else if (argc == 3)
{
get_IP(argv[1], IP);
if (strchr(argv[2], '.'))
CIDR = get_CIDR(argv[2], mask);
else
CIDR = get_mask(argv[2], mask);
avail_IP_addrs = pow(2, 32 - CIDR);
i = 0;
while (i < 4)
{
subnet[i] = IP[i] & mask[i];
range[i] = subnet[i];
i++;
}
i = 0;
while (i < 4)
{
host[i] = IP[i] & ~mask[i];
i++;
}
range[3] += (avail_IP_addrs - 1);
if (avail_IP_addrs > 255)
range[3] -= (avail_IP_addrs % 256);
i = 3;
while (i > 0)
{
while (range[i] > 255)
{
range[i - 1]++;
range[i] -= 256;
}
i--;
}
printf(PROVIDED("* Provided IP:\t"));
printf(" %u.%u.%u.%u\n\t\t ", IP[0], IP[1], IP[2], IP[3]);
print_binary_IP(IP);
printf(PROVIDED("\n* Provided Mask:"));
printf(" %u (CIDR)\n\t\t %u.%u.%u.%u\n\t\t ", CIDR, mask[0], mask[1], mask[2], mask[3]);
print_binary_IP(mask);
printf(PINK("\n\n* Host:\t"));
printf("%u.%u.%u.%u / %u\n\t", subnet[0], subnet[1], subnet[2], subnet[3], CIDR);
print_binary_IP(subnet);
printf(LILAC("\n* Host range:"));
printf(" %u.%u.%u.%u - %u.%u.%u.%u", subnet[0], subnet[1], subnet[2], subnet[3] + 1, range[0], range[1], range[2], range[3] - 1);
/*
printf(BLUE("\n\n* IP (No Host):\t"));
printf("%u.%u.%u.%u\n\t\t", host[0], host[1], host[2], host[3]);
print_binary_IP(host);
*/
printf(CYAN("\n\n* Max nbr of hosts:"));
printf(" %d hosts\n", (avail_IP_addrs <= 2) ? avail_IP_addrs : avail_IP_addrs - 2);
printf(PETROL_B("* Total addresses:"));
printf(" %d addresses (including host and broadcast)", avail_IP_addrs);
}
else
{
printf(RED("\nError: Please provide a valid subnet mask, CIDR and/or IP address"));
}
printf(HEADER_L("\n\n================================================================================\n\n"));
return (0);
}