|
|
|
@ -4,8 +4,6 @@
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include "subnetting.h"
|
|
|
|
|
|
|
|
|
|
enum NetworkClass {CLASS_A, CLASS_B, CLASS_C};
|
|
|
|
|
|
|
|
|
|
// This section of code consitutes a more Java-like version of finding a preix
|
|
|
|
|
// A C programmer is more likely to simply do the binary math.
|
|
|
|
|
const unsigned int prefix_map[9] = {0, 128, 192, 224, 240, 248, 252, 254, 255};
|
|
|
|
@ -17,21 +15,6 @@ unsigned int* subnet_to_octet_array(unsigned int octets[4], char subnet_mask[16]
|
|
|
|
|
return octets;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int mask_to_prefix(char mask[16]){
|
|
|
|
|
unsigned int octet[4];
|
|
|
|
|
int pref = 0;
|
|
|
|
|
subnet_to_octet_array(octet, mask);
|
|
|
|
|
for (int i = 0; i < 4; ++i){
|
|
|
|
|
for (int j = 0; j < 9; ++j){
|
|
|
|
|
if (octet[i] == prefix_map[j]){
|
|
|
|
|
pref += j;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return pref;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* prefix_to_mask(char subnet_mask[16], unsigned int prefix){
|
|
|
|
|
unsigned int octets[5];
|
|
|
|
|
for (int i = 0; i < 5; ++i){
|
|
|
|
@ -73,25 +56,6 @@ int prefix_for_hosts(int hosts_required){
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* octet_array_to_string(char subnet_mask[16], char octets[4]){
|
|
|
|
|
sprintf(subnet_mask, "%d.%d.%d.%d", octets[0], octets[1], octets[2], octets[3]);
|
|
|
|
|
return subnet_mask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int network_type_based_on_subnet(int subnet_arr[4]){
|
|
|
|
|
if (subnet_arr[0] != 255){
|
|
|
|
|
return -1;
|
|
|
|
|
} else if (subnet_arr[1] != 255){
|
|
|
|
|
return CLASS_A;
|
|
|
|
|
} else if (subnet_arr[2] != 255){
|
|
|
|
|
return CLASS_B;
|
|
|
|
|
} else if (subnet_arr[3] != 255){
|
|
|
|
|
return CLASS_C;
|
|
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char network_class(int subnet_arr[4]){
|
|
|
|
|
if (subnet_arr[0] != 255){
|
|
|
|
|
return '?';
|
|
|
|
@ -147,15 +111,3 @@ char* usable_host_addresses(char result[33], char ip[16], char subnet[16]){
|
|
|
|
|
sprintf(result, "%s-%s", slow_addrs, shigh_addrs);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int get_ui_class(unsigned int ip){
|
|
|
|
|
unsigned int fo = ip >> 24;
|
|
|
|
|
if (fo >= 1 && fo <= 126){
|
|
|
|
|
return CLASS_A;
|
|
|
|
|
} else if (fo >= 128 && fo <= 191){
|
|
|
|
|
return CLASS_B;
|
|
|
|
|
} else if (fo >= 192 && fo <= 223){
|
|
|
|
|
return CLASS_C;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|