diff options
author | Aaron Ball <nullspoon@iohq.net> | 2015-12-18 00:36:18 -0700 |
---|---|---|
committer | Aaron Ball <nullspoon@iohq.net> | 2015-12-18 00:36:18 -0700 |
commit | 4b36d6b6f40ea773388669373cf96e65ae3aaaa3 (patch) | |
tree | f070434f9c93a5c66a9960def95a327b3e6f9406 | |
download | netexpand-4b36d6b6f40ea773388669373cf96e65ae3aaaa3.tar.gz netexpand-4b36d6b6f40ea773388669373cf96e65ae3aaaa3.tar.xz |
Initial commit.
Almost there. We can currently calculate an ipv4, increment, convert to
int. Working on converting to string though. Also need slash syntax
expansion still.
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 13 | ||||
-rw-r--r-- | src/common.c | 34 | ||||
-rw-r--r-- | src/common.h | 2 | ||||
-rw-r--r-- | src/ipv4.c | 77 | ||||
-rw-r--r-- | src/ipv4.h | 13 | ||||
-rw-r--r-- | src/main.c | 26 |
7 files changed, 167 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bdd4b77 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +obj +netexpand diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..12e0bab --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +out = netexpand +warn = -Wall -Wpedantic +cc = cc +std = c99 + + +all: + $(cc) $(warn) -std=$(std) $(dbg) $(CFLAGS) src/common.c -c -o obj/common.o + $(cc) $(warn) -std=$(std) $(dbg) $(CFLAGS) src/ipv4.c -c -o obj/ipv4.o + $(cc) $(warn) -std=$(std) $(dbg) $(CFLAGS) src/main.c -lm obj/*.o -o $(out) + +debug: + make all dbg='-g' diff --git a/src/common.c b/src/common.c new file mode 100644 index 0000000..4ae509a --- /dev/null +++ b/src/common.c @@ -0,0 +1,34 @@ +#include "common.h" + +/** + * The complimentary function to the posix atoi. It turns an integer into a + * string. + * + * @param i Integer to be converted to string + * @param out Output char array. Be sure it has enough room for the output + * string. + * + * @return int The character count of the output char array + */ +int itoa(int i, char* out) { + // To keep track of where we are in the array + int n = 0; + while(i > 9) { + printf(" %d\n", i); + // This is some crazy simple math here + out[n] = (i%10) + 48; + // It's an int, so the last number will be stripped + i = i/10; + n++; + } + // Have to do this one more time to ensure we get the last number on + // zero-ended ints + out[n] = (i%10) + 48; + // Account for the last char + n++; + // Annnd the null byte + out[n] = '\0'; + n++; + return n; +} + diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..e358cdf --- /dev/null +++ b/src/common.h @@ -0,0 +1,2 @@ +#include <stdio.h> +int itoa(int i, char* out); diff --git a/src/ipv4.c b/src/ipv4.c new file mode 100644 index 0000000..2d721de --- /dev/null +++ b/src/ipv4.c @@ -0,0 +1,77 @@ +#include "ipv4.h" + +void ipv4_new(ipv4* ip) { + ip->octets[0] = 0; + ip->octets[1] = 0; + ip->octets[2] = 0; + ip->octets[3] = 0; +} + + +unsigned int ipv4_to_int(ipv4* addr) { + unsigned int out; + out = addr->octets[3] * 16777216; // 2^24 or 256^3 + out += addr->octets[2] * 65536; // 2^16 or 256^2 + out += addr->octets[1] * 256; // 2^8 or 256^1 + out += addr->octets[0]; // 2^1 or na + + return out; +} + + +ipv4 str_to_ipv4(char* ip) { + int octet = 3; + int cursor = 0; + int i = 0; + char buf[4]; + ipv4 out; + + // Initialize + ipv4_new(&out); + + while(ip[i] != '\0') { + if(ip[i] == '.') { + // Copy the string contents + strncpy(buf, &ip[cursor], i-cursor); + // Place the trailing null byte + buf[i-cursor] = '\0'; + // Convert string octet to int + out.octets[octet] = atoi(buf); + + cursor = i+1; + octet--; + } + i++; + } + // For the last octet, one more do-over + strncpy(buf, &ip[cursor], i-cursor); + buf[i-cursor] = '\0'; + out.octets[octet] = atoi(buf); + + return out; +} + + +void ipv4_to_str(ipv4* ip, char* out) { + int octet = 3; + int cursor = 0; + + out[0] = '\0'; + while(octet >= 0) { + int len = itoa(ip->octets[octet], &out[cursor]); + cursor += len; + out[cursor] = '.'; + cursor++; + printf("%d\n", octet); + } +} + + +void ipv4_inc(ipv4* ip) { + int octet = 0; + if(ip->octets[octet] == 255) { + ip->octets[octet] = 0; + octet++; + } + ip->octets[octet]++; +} diff --git a/src/ipv4.h b/src/ipv4.h new file mode 100644 index 0000000..0d86a8a --- /dev/null +++ b/src/ipv4.h @@ -0,0 +1,13 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include "common.h" + +typedef struct { + int octets[4]; +} ipv4; + +unsigned int ipv4_to_int(ipv4*); +ipv4 str_to_ipv4(char*); +void ipv4_to_str(ipv4*, char*); +void ipv4_inc(ipv4*); diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..35f3d5a --- /dev/null +++ b/src/main.c @@ -0,0 +1,26 @@ +#include <stdio.h> +#include <stdlib.h> +#include "ipv4.h" + + +int main(int argc, char* argv[]) { + if(argc == 1) { + printf("Please specify at least one ip address.\n"); + return 1; + } + + char buf[20]; + + ipv4 ip = str_to_ipv4(argv[1]); + printf("%u\n", ipv4_to_int(&ip)); + + ipv4_inc(&ip); + + ipv4_to_str(&ip, buf); + printf("%s\n", buf); + + // char test[40]; + // ipv4_to_str(ip, test); + + return 0; +} |