Switch to header-style development; fix a formatting wrror.

master
Tait Hoyem 4 years ago
parent 119e89f5be
commit b349bd09f4

@ -1,4 +1,18 @@
default: all
CC?=gcc
default: build
all:
gcc -o subnetting subnetting.c -lm
all: build tewt
subnetting.o:
${CC} -c -o subnetting.o subnetting.c -lm -O2
build: subnetting.o
${CC} -o subnetting subnetting.o main.c -lm -O2
test: subnetting.o
${CC} -o test subnetting.o main.c -lm -O2
clean:
rm subnetting
rm *o
rm test

@ -0,0 +1,55 @@
#include <stdio.h>
#include <errno.h>
#include "subnetting.h"
int main(){
// actual program
char* print_format = {"%18s | %33s | %18s | %16s | /%2d\n"};
int num_of_networks;
int hosts_per_network;
char file_location[255];
FILE* print_file;
printf("Where would you like to save these tables? ");
if (scanf("%s", file_location) != 1){
fprintf(stderr, "There was an error processing the filename!\n");
}
print_file = fopen(file_location, "w");
fprintf(print_file, "----------------------------------------------------------------------------------------------------\n");
printf("How many networks do you need? ");
if (scanf("%d", &num_of_networks) != 1){
fprintf(stderr, "There was an error reading the number of networks!\n");
}
char ip[16];
unsigned int ui_ip;
printf("Enter a base IP address to start configuring. (Usually 192.168.x.0, 172.17.x.0, etc...)\nBase IP: ");
if (scanf("%s", ip) != 1){
fprintf(stderr, "There was an error reading the base IP address!\n");
}
ui_ip = octets_to_ui(ip);
fprintf(print_file, print_format, "Network Address", "Host Range", "Broadcast Address", "Subnet Mask", 0);
for (int i = 0; i < num_of_networks; ++i){
int hosts;
char subnt_s[16];
char broadcast_addr[16];
char network_addr[16];
char usable_range[33];
unsigned int subnt;
int this_prefix;
printf("How many hosts do you need on this network [%d]? ", i);
if (scanf("%d", &hosts) != 1){
fprintf(stderr, "Errpr readomg number of hosts!\n");
}
this_prefix = prefix_for_hosts(hosts);
prefix_to_mask(subnt_s, this_prefix);
subnt = octets_to_ui(subnt_s);
broadcast_address(broadcast_addr, ip, subnt_s);
network_address(network_addr, ip, subnt_s);
usable_host_addresses(usable_range, ip, subnt_s);
fprintf(print_file, print_format, network_addr, usable_range, broadcast_addr, subnt_s, this_prefix);
ui_ip = octets_to_ui(broadcast_addr) + 1;
ui_to_octets(ip, ui_ip);
}
return 0;
}

@ -2,6 +2,7 @@
#include <math.h>
#include <string.h>
#include <stdbool.h>
#include "subnetting.h"
enum NetworkClass {CLASS_A, CLASS_B, CLASS_C};
@ -158,101 +159,3 @@ int get_ui_class(unsigned int ip){
}
return -1;
}
int main(){
char* mask1 = {"255.255.255.192"};
char mask2[16];
int prefix, hosts, networks;
unsigned int octets[4];
prefix = mask_to_prefix(mask1);
fprintf(stdout, "Prefix of %s is %d\n", mask1, prefix);
prefix_to_mask(mask2, prefix);
fprintf(stdout, "Subnet of %d is %s\n", prefix, mask2);
hosts = hosts_for_prefix(prefix);
printf("It can support up to %d hosts.\n", hosts);
networks = subnets_for_prefix(prefix);
printf("It can support ip to %d subnets.\n", networks);
subnet_to_octet_array(octets, mask1);
for (int i = 0; i < 4; ++i){
printf("\tPt %d: %d\n", i+1, octets[i]);
}
int prefix64,prefix2000;
char subnet64[16];
char subnet2000[16];
int subarr64[4];
int subarr2000[4];
prefix64 = prefix_for_hosts(64);
prefix2000 = prefix_for_hosts(2000);
prefix_to_mask(subnet64, prefix64);
prefix_to_mask(subnet2000, prefix2000);
subnet_to_octet_array(subarr64, subnet64);
subnet_to_octet_array(subarr2000, subnet2000);
printf("64 hosts requires a prefix of %d (%s).\n", prefix64, subnet64);
printf("2000 hosts requires a prefix of %d (%s).\n", prefix2000, subnet2000);
printf("A prefix of %d can support %d hosts.\n", prefix2000, hosts_for_prefix(prefix2000));
printf("%s indicates a class %c network.\n", subnet64, network_class(subarr64));
printf("%s indicates a class %c network.\n", subnet2000, network_class(subarr2000));
char* ip1 = {"192.168.16.122"};
char* sub1_ip1 = { "255.255.255.240" };
char na_ip1[16];
char ba_ip1[16];
char ra_ip1[33];
network_address(na_ip1, ip1, sub1_ip1);
broadcast_address(ba_ip1, ip1, sub1_ip1);
usable_host_addresses(ra_ip1, ip1, sub1_ip1);
printf("%s (%s)'s network address is %s.\n", ip1, sub1_ip1, na_ip1);
printf("%s (%s)'s broadcast address is %s.\n", ip1, sub1_ip1, ba_ip1);
printf("The host range is %s\n", ra_ip1);
char* ip2 = {"172.17.130.222"};
char* sub2_ip2 = { "255.255.128.0" };
char na_ip2[16];
network_address(na_ip2, ip2, sub2_ip2);
printf("%s (%s)'s network address is %s.\n", ip2, sub2_ip2, na_ip2);
// actual program
char* print_format = {"%18s | %33s | %18s | %16s | /%2d\n"};
int num_of_networks;
int hosts_per_network;
char file_location[255];
FILE* print_file;
printf("Where would you like to save these tables? ");
scanf("%s", file_location);
print_file = fopen(file_location, "w");
fprintf(print_file, "---------------------------------------------------------------------------------------------------------------------\n");
printf("How many networks do you need? ");
scanf("%d", &num_of_networks);
char ip[16];
unsigned int ui_ip;
printf("Enter a base IP address to start configuring. (Usually 192.168.x.0, 172.17.x.0, etc...)\nBase IP: ");
scanf("%s", ip);
ui_ip = octets_to_ui(ip);
fprintf(print_file, print_format, "Network Address", "Host Range", "Broadcast Address", "Subnet Mask", 0);
for (int i = 0; i < num_of_networks; ++i){
int hosts;
char subnt_s[16];
char broadcast_addr[16];
char network_addr[16];
char usable_range[33];
unsigned int subnt;
int this_prefix;
printf("How many hosts do you need on this network [%d]? ", i);
scanf("%d", &hosts);
this_prefix = prefix_for_hosts(hosts);
prefix_to_mask(subnt_s, this_prefix);
subnt = octets_to_ui(subnt_s);
broadcast_address(broadcast_addr, ip, subnt_s);
network_address(network_addr, ip, subnt_s);
usable_host_addresses(usable_range, ip, subnt_s);
fprintf(print_file, print_format, network_addr, usable_range, broadcast_addr, subnt_s, this_prefix);
ui_ip = octets_to_ui(broadcast_addr) + 1;
ui_to_octets(ip, ui_ip);
}
return 0;
}

@ -0,0 +1,20 @@
#ifndef SUBNETTING_H
#define SUBNETTING_H
int mask_to_prefix(char mask[16]);
char* prefix_to_mask(char subnet_mask[16], unsigned int prefix);
int hosts_for_prefix(int prefix);
int subnets_for_prefix(int prefix);
void ui_to_octets(char addr[16], unsigned int subnet_mask);
int prefix_for_hosts(int hosts_required);
char* octet_array_to_string(char subnet_mask[16], char octets[4]);
int network_type_based_on_subnet(int subnet_arr[4]);
char network_class(int subnet_arr[4]);
char* network_address(char result[16], char ip[16], char subnet[16]);
char* broadcast_address(char result[16], char ip[16], char subnet[16]);
char* usable_host_addresses(char result[33], char ip[16], char subnet[16]);
int get_ui_class(unsigned int ip);
unsigned int* subnet_to_octet_array(unsigned int octets[4], char subnet_mask[16]);
unsigned int octets_to_ui(char octets[16]);
unsigned int bdr_addr(unsigned int ip, unsigned int subnet);
unsigned int net_addr(unsigned int ip, unsigned int subnet);
#endif

@ -0,0 +1,255 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "subnetting.h"
int tests_run = 0;
int tests_passed = 0;
int tests_failed = 0;
const char* FAIL = "FAIL";
const char* PASS = "PASS";
const char* EQ = "==";
const char* NEQ = "!=";
const char* GREEN_TEXT = "\e[32m";
const char* RED_TEXT = "\e[31m";
const char* CLEAR_FORMAT = "\e[0m";
const char* BOLD_TEXT = "\e[1m";
const char* PASS_FORMAT = "\e[32m[%s]: \"%s\" %s \"%s\"\e[0m\n";
const char* FAIL_FORMAT = "\e[31m[%s]: \"%s\" %s \"%s\"\e[0m\n";
const char* PASS_CHAR_FORMAT = "\e[32m[%s]: \'%c\' %s \'%c\'\e[0m\n";
const char* FAIL_CHAR_FORMAT = "\e[31m[%s]: \'%c\' %s \'%c\'\e[0m\n";
const char* FUNCTION_CALL_CFORMAT = "%s(\'%c\'):\n";
const char* FUNCTION_CALL_SFORMAT = "%s(\"%s\"):\n";
void assert_char_eq(const char c1, const char c2){
tests_run++;
if (c1 == c2){
printf(PASS_CHAR_FORMAT, PASS, c1, EQ, c2);
tests_passed++;
} else {
printf(FAIL_CHAR_FORMAT, FAIL, c1, NEQ, c2);
tests_failed++;
}
}
void assert_str_eq(const char* s1, const char* s2){
tests_run++;
if (strcmp(s1, s2) == 0){
printf(PASS_FORMAT, PASS, s1, EQ, s2);
tests_passed++;
} else {
printf(FAIL_FORMAT, FAIL, s1, NEQ, s2);
tests_failed++;
}
}
void assert_str_neq(const char* s1, const char* s2){
tests_run++;
if (strcmp(s1, s2) != 0){
printf(PASS_FORMAT, PASS, s1, NEQ, s2);
tests_passed++;
} else {
printf(FAIL_FORMAT, FAIL, s1, EQ, s2);
tests_failed++;
}
}
void assert_ctm(char input, char* output){
const char* result = char_to_morse(input);
printf(FUNCTION_CALL_CFORMAT, "char_to_morse", input);
assert_str_eq(result, output);
}
void assert_stm(char* input, char* output){
char* result;
//char* result = malloc(sizeof(char)*strlen(input)*8);
// fill with blanks in case of previous data
// strcpy(result, "");
result = string_to_morse(input);
printf(FUNCTION_CALL_SFORMAT, "string_to_morse", input);
assert_str_eq(result, output);
}
void assert_mtc(char* input, char output){
char result = morse_to_char(input);
printf(FUNCTION_CALL_SFORMAT, "morse_to_char", input);
assert_char_eq(result, output);
}
void assert_mts(char* input, char* output){
char* result = morse_to_string(input);
printf(FUNCTION_CALL_SFORMAT, "morse_to_string", input);
assert_str_eq(result, output);
}
void test_tests(){
assert_str_eq(".", ".");
assert_str_neq("+", "-");
}
void test_to_morse(){
assert_ctm('a', ".-");
assert_ctm('A', ".-");
assert_ctm('b', "-...");
assert_ctm('B', "-...");
assert_ctm('c', "-.-.");
assert_ctm('C', "-.-.");
assert_ctm('d', "-..");
assert_ctm('D', "-..");
assert_ctm('e', ".");
assert_ctm('E', ".");
assert_ctm('f', "..-.");
assert_ctm('F', "..-.");
assert_ctm('g', "--.");
assert_ctm('G', "--.");
assert_ctm('h', "....");
assert_ctm('H', "....");
assert_ctm('i', "..");
assert_ctm('I', "..");
assert_ctm('j', ".---");
assert_ctm('J', ".---");
assert_ctm('k', "-.-");
assert_ctm('K', "-.-");
assert_ctm('l', ".-..");
assert_ctm('L', ".-..");
assert_ctm('m', "--");
assert_ctm('M', "--");
assert_ctm('n', "-.");
assert_ctm('N', "-.");
assert_ctm('o', "---");
assert_ctm('O', "---");
assert_ctm('p', ".--.");
assert_ctm('P', ".--.");
assert_ctm('q', "--.-");
assert_ctm('Q', "--.-");
assert_ctm('r', ".-.");
assert_ctm('R', ".-.");
assert_ctm('s', "...");
assert_ctm('S', "...");
assert_ctm('t', "-");
assert_ctm('T', "-");
assert_ctm('u', "..-");
assert_ctm('U', "..-");
assert_ctm('v', "...-");
assert_ctm('V', "...-");
assert_ctm('w', ".--");
assert_ctm('W', ".--");
assert_ctm('x', "-..-");
assert_ctm('X', "-..-");
assert_ctm('y', "-.--");
assert_ctm('Y', "-.--");
assert_ctm('z', "--..");
assert_ctm('0', "-----");
assert_ctm('1', ".----");
assert_ctm('2', "..---");
assert_ctm('3', "...--");
assert_ctm('4', "....-");
assert_ctm('5', ".....");
assert_ctm('6', "-....");
assert_ctm('7', "--...");
assert_ctm('8', "---..");
assert_ctm('9', "----.");
assert_ctm(' ', "/");
assert_ctm('!', "-.-.--");
}
void test_string_to_morse(){
assert_stm("Wee!", ".-- . . -.-.--");
assert_stm("Hello", ".... . .-.. .-.. ---");
assert_stm("world", ".-- --- .-. .-.. -..");
assert_stm("Hello world!", ".... . .-.. .-.. --- / .-- --- .-. .-.. -.. -.-.--");
assert_stm("I! HATE! YOU!", ".. -.-.-- / .... .- - . -.-.-- / -.-- --- ..- -.-.--");
assert_stm("Whisky and rye", ".-- .... .. ... -.- -.-- / .- -. -.. / .-. -.-- .");
}
void test_morse_to_char(){
assert_mtc(".-", 'A');
assert_mtc("-...", 'B');
assert_mtc("-.-.", 'C');
assert_mtc("-..", 'D');
assert_mtc(".", 'E');
assert_mtc("..-.", 'F');
assert_mtc("--.", 'G');
assert_mtc("....", 'H');
assert_mtc("..", 'I');
assert_mtc(".---", 'J');
assert_mtc("-.-", 'K');
assert_mtc(".-..", 'L');
assert_mtc("--", 'M');
assert_mtc("-.", 'N');
assert_mtc("---", 'O');
assert_mtc(".--.", 'P');
assert_mtc("--.-", 'Q');
assert_mtc(".-.", 'R');
assert_mtc("...", 'S');
assert_mtc("-", 'T');
assert_mtc("..-", 'U');
assert_mtc("...-", 'V');
assert_mtc(".--", 'W');
assert_mtc("-..-", 'X');
assert_mtc("-.--", 'Y');
assert_mtc("--..", 'Z');
}
void test_morse_to_string(){
assert_mts("... --- ...", "SOS");
assert_mts(". . . . .", "EEEEE");
assert_mts("- ... ....", "TSH");
assert_mts("- - - - ... ... ... ... .... .... .... .... / .... .. .. .. ..", "TTTTSSSSHHHH HIIII");
assert_mts("- .- .-. ..-. / -- .- .-.. .- -.- --- ...-", "TARF MALAKOV");
assert_mts("- .- .. -", "TAIT");
assert_mts("- .- .-.. -.-", "TALK");
assert_mts("- .- ... -.- / -- .- ... - . .-.", "TASK MASTER");
assert_mts("- .- .-. ..-. / -- .- .-.. .- -.- --- ...-", "TARF MALAKOV");
assert_mts("- .- ..-", "TAU");
}
void all_tests(){
test_tests();
printf("test_tests tests complete!\n");
test_to_morse();
printf("char_to_morse tests complete!\n");
test_string_to_morse();
printf("string_to_morse tests complete!\n");
test_morse_to_char();
printf("test_morse_to_char tests complete!\n");
test_morse_to_string();
printf("test_morse_to_string tests complete!\n");
}
int main(int argc, char **argv) {
bool done_printing_passed_tests = false;
all_tests();
if (tests_run == tests_passed){
printf("ALL TESTS PASSED\n");
}
printf("%s", RED_TEXT);
for (int i = 0; i < tests_failed; i++){
printf("=");
}
printf("%s", CLEAR_FORMAT);
printf("%s", GREEN_TEXT);
for (int i = 0; i < tests_passed; i++){
printf("=");
}
printf("\n");
printf("%s", CLEAR_FORMAT);
printf("Tests run: %d", tests_run);
printf(" | ");
printf("%s", BOLD_TEXT);
printf("%s", GREEN_TEXT);
printf("%d passed", tests_passed);
printf("%s", CLEAR_FORMAT);
printf(" | ");
printf("%s", BOLD_TEXT);
printf("%s", RED_TEXT);
printf("%d failed \n", tests_failed);
return 0;
}

@ -0,0 +1,58 @@
#include <stdio.h>
#include "subnetting.h"
int main(){
char* mask1 = {"255.255.255.192"};
char mask2[16];
int prefix, hosts, networks;
unsigned int octets[4];
prefix = mask_to_prefix(mask1);
fprintf(stdout, "Prefix of %s is %d\n", mask1, prefix);
prefix_to_mask(mask2, prefix);
fprintf(stdout, "Subnet of %d is %s\n", prefix, mask2);
hosts = hosts_for_prefix(prefix);
printf("It can support up to %d hosts.\n", hosts);
networks = subnets_for_prefix(prefix);
printf("It can support ip to %d subnets.\n", networks);
subnet_to_octet_array(octets, mask1);
for (int i = 0; i < 4; ++i){
printf("\tPt %d: %d\n", i+1, octets[i]);
}
int prefix64,prefix2000;
char subnet64[16];
char subnet2000[16];
int subarr64[4];
int subarr2000[4];
prefix64 = prefix_for_hosts(64);
prefix2000 = prefix_for_hosts(2000);
prefix_to_mask(subnet64, prefix64);
prefix_to_mask(subnet2000, prefix2000);
subnet_to_octet_array(subarr64, subnet64);
subnet_to_octet_array(subarr2000, subnet2000);
printf("64 hosts requires a prefix of %d (%s).\n", prefix64, subnet64);
printf("2000 hosts requires a prefix of %d (%s).\n", prefix2000, subnet2000);
printf("A prefix of %d can support %d hosts.\n", prefix2000, hosts_for_prefix(prefix2000));
printf("%s indicates a class %c network.\n", subnet64, network_class(subarr64));
printf("%s indicates a class %c network.\n", subnet2000, network_class(subarr2000));
char* ip1 = {"192.168.16.122"};
char* sub1_ip1 = { "255.255.255.240" };
char na_ip1[16];
char ba_ip1[16];
char ra_ip1[33];
network_address(na_ip1, ip1, sub1_ip1);
broadcast_address(ba_ip1, ip1, sub1_ip1);
usable_host_addresses(ra_ip1, ip1, sub1_ip1);
printf("%s (%s)'s network address is %s.\n", ip1, sub1_ip1, na_ip1);
printf("%s (%s)'s broadcast address is %s.\n", ip1, sub1_ip1, ba_ip1);
printf("The host range is %s\n", ra_ip1);
char* ip2 = {"172.17.130.222"};
}
Loading…
Cancel
Save