-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcalculator.c
271 lines (220 loc) · 6.1 KB
/
calculator.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <arpa/inet.h>
#define BUFFER 100
unsigned int ip_number = 0;
unsigned int mask_number = 0, number_of_mask_bits;
void usage(char * program_name, char * error_message)
{
printf("\n%s\nUsage: %s <ip-addr/mask or net-addr/mask>\n\n", error_message, program_name);
exit(EXIT_FAILURE);
}
int get_input_size(char *input)
{
int size = 0;
for(int i = 0; input[i] != 0; i++)
{
size++;
}
return size;
}
int get_number_of_slashes(char *input)
{
int numberOfSlashes = 0;
for(int i = 0; input[i] != 0; i++)
{
if (input[i] == '/') {
numberOfSlashes++;
}
}
return numberOfSlashes;
}
int get_slash_position(char *input)
{
int slashPosition = 0;
for(int i = 0; input[i] != 0; i++)
{
if (input[i] == '/') {
return i;
}
}
return 0;
}
int is_valid_ip_address(char *ipAddress)
{
struct sockaddr_in sa;
int result = inet_pton(AF_INET, ipAddress, &(sa.sin_addr));
return result != 0;
}
int is_valid_mask(char * mask_string)
{
char *end;
int i = strtol(mask_string, &end, 10);
return i < 31;
}
void get_ip_string(char * input, char * destination, int slashPosition) {
for (int i = 0; i < slashPosition; i++) {
destination[i] = input[i];
destination[i + 1] = 0;
}
}
void get_mask_string(char * input, char * destination, int slashPosition) {
for (int i = slashPosition + 1, j = 0; input[i] != 0; i++, j++) {
destination[j] = input[i];
destination[j + 1] = 0;
}
}
void print_ip_address(int * number_addr)
{
unsigned char * raw_ptr; // pointer to get single bytes
int i, aux_int;
raw_ptr = (unsigned char *) number_addr;
for (i = 3; i > -1; i--) // run through the four integer bytes
{
aux_int = raw_ptr[i];
printf("%d", aux_int);
if(i != 0)
printf(".");
}
printf(" ");
}
void binary_print(unsigned int number)
{
unsigned int mask = 0xff000000; // last byte mask
unsigned int shift = 24; // separate bytes
unsigned int byte, byte_iterator, bit_iterator; // auxiliary variables
for(byte_iterator = 0; byte_iterator < 4; byte_iterator++) // byte iterator
{
byte = (number & mask) >> shift;
printf(" ");
for(bit_iterator = 0; bit_iterator < 8; bit_iterator++) // bit iterator
{
if(byte & 128) // print bit values
printf("1");
else
printf("0");
byte <<= 1;
}
mask >>= 8; // adjust mask for next byte
shift -= 8; // adjust shift for next byte
}
}
// store the results in the global variables
void handle_input(char * string)
{
unsigned int i, shift, temp = 0;
// makes the input string an integer number in ip_number
shift = 24;
ip_number = (atoi(&string[0]) << shift);
shift -= 8;
for(i = 0; string[i] != 0; i++)
{
if(string[i] == 46) // check if it's a dot and get the value after it
{
ip_number += (atoi(&string[i + 1]) << shift);
shift -= 8;
}
}
// gets the number of bits of the mask
for(i = 0; string[i] != 0; i++)
{
if(string[i] == 47) // check if it's a slash and get the value after it
{
number_of_mask_bits += atoi(&string[i + 1]);
}
}
// makes the mask an integer number
for(i = 31; i > (31 - number_of_mask_bits); i--)
{
temp = (1 << i);
mask_number += temp;
}
}
int main(int argc, char ** argv)
{
// declarations
unsigned int network_address = 0;
unsigned int broadcast = 0;
unsigned int host_min = 0;
unsigned int host_max = 0;
double number_of_hosts_bits = 0;
char ip_string[15];
char mask_string[4];
if(argc < 2)
{
usage(argv[0], "You must provide a valid ip/mask");
}
int inputSize = get_input_size(argv[1]);
if (inputSize > 18)
{
usage(argv[0], "You must provide a valid ip/mask");
}
int numberOfSlashes = get_number_of_slashes(argv[1]);
if (numberOfSlashes != 1)
{
usage(argv[0], "You must provide a valid ip/mask");
}
int slashPosition = get_slash_position(argv[1]);
if (slashPosition == 0 || slashPosition == inputSize - 1)
{
usage(argv[0], "You must provide a valid ip/mask");
}
get_ip_string(argv[1], ip_string, slashPosition);
if (!is_valid_ip_address(ip_string))
{
usage(argv[0], "You must provide a valid ip/mask");
}
get_mask_string(argv[1], mask_string, slashPosition);
if (!is_valid_mask(mask_string))
{
usage(argv[0], "You must provide a valid ip/mask");
}
printf("\n");
printf("Entry: %s\n", argv[1]);
printf("\n");
// transform the given input in numbers
handle_input(argv[1]);
// calculating values through the global variables
network_address = ip_number & mask_number;
broadcast = ~(mask_number);
broadcast = broadcast + network_address;
host_min = network_address + 1;
host_max = broadcast - 1;
// printing the results
printf("Network address: ");
printf("\t");
binary_print(network_address);
printf("\t");
print_ip_address(&network_address);
printf("\n");
printf("Netmask: ");
printf("\t");
binary_print(mask_number);
printf("\t");
print_ip_address(&mask_number);
printf("(%d bits)", number_of_mask_bits);
printf("\n");
printf("Broadcast addr: ");
printf("\t");
binary_print(broadcast);
printf("\t");
print_ip_address(&broadcast);
printf("\n");
printf("Min Host value: ");
printf("\t");
binary_print(host_min);
printf("\t");
print_ip_address(&host_min);
printf("\n");
printf("Max Host value: ");
printf("\t");
binary_print(host_max);
printf("\t");
print_ip_address(&host_max);
printf("\n");
printf("\n");
number_of_hosts_bits = 32 - number_of_mask_bits;
printf("The possible number of hosts on this network is %d.\n", (int)pow(2, number_of_hosts_bits) - 2);
printf("\n");
}