diff options
author | Matt Strapp <matt@mattstrapp.net> | 2022-01-27 16:06:24 -0600 |
---|---|---|
committer | Matt Strapp <matt@mattstrapp.net> | 2022-01-27 16:06:24 -0600 |
commit | 0c827110ee79265cf697e6f78a95b0bbb6947198 (patch) | |
tree | a892ec5bcc6f52b952c2a3abe487477e53ff29a6 /csci4211 | |
parent | Work on resume (diff) | |
download | homework-0c827110ee79265cf697e6f78a95b0bbb6947198.tar homework-0c827110ee79265cf697e6f78a95b0bbb6947198.tar.gz homework-0c827110ee79265cf697e6f78a95b0bbb6947198.tar.bz2 homework-0c827110ee79265cf697e6f78a95b0bbb6947198.tar.lz homework-0c827110ee79265cf697e6f78a95b0bbb6947198.tar.xz homework-0c827110ee79265cf697e6f78a95b0bbb6947198.tar.zst homework-0c827110ee79265cf697e6f78a95b0bbb6947198.zip |
Start 4211 fun
Signed-off-by: Matt Strapp <matt@mattstrapp.net>
Diffstat (limited to '')
-rw-r--r-- | csci4211/ipaddress.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/csci4211/ipaddress.c b/csci4211/ipaddress.c new file mode 100644 index 0000000..662bf99 --- /dev/null +++ b/csci4211/ipaddress.c @@ -0,0 +1,62 @@ +// Author Name: Matt Strapp +// Date: 2022-01-26 +// x500: strap012 + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int IPv4ToInt(char *ipv4, int isBigEndian) { + unsigned int ipv4Int = 0; + short int i = 0; + char *chunk = strtok(ipv4, "."); + while (chunk != NULL) { + int ipv4Segment = atoi(chunk); + if (ipv4Segment > 255 || ipv4Segment < 0) { + printf("ERROR\n"); + exit(0); + } + ipv4Int = (isBigEndian) ? (ipv4Int << 8) | ipv4Segment + : (ipv4Int >> 8) + (ipv4Segment << 24); + i++; + chunk = strtok(NULL, "."); + } + if (i != 4) { + printf("ERROR\n"); + exit(0); + } + return ipv4Int; +} + +char *IntToIPv4(long ipv4_int, int isBigEndian) { + if (ipv4_int > 4294967295 || ipv4_int < 0) { + printf("ERROR\n"); + exit(0); + } + char *ipv4 = (char *)calloc(16, sizeof(char)); + int i = 0; + while (i < 4) { + int ipv4Segment = (isBigEndian) ? (ipv4_int >> (8 * (3 - i))) & 255 + : (ipv4_int >> (8 * i)) & 255; + sprintf(ipv4 + strlen(ipv4), "%d", ipv4Segment); + if (i != 3) + sprintf(ipv4 + strlen(ipv4), "."); + i++; + } + return ipv4; +} + +int main(int argc, char **argv) { + int toHuman = atoi(argv[1]); + int isBigEndian = atoi(argv[3]); + if (toHuman) { + long toIpv4 = strtol(argv[2], NULL, 10); + char *ret = IntToIPv4(toIpv4, isBigEndian); + printf("%s\n", ret); + free(ret); + } else { + unsigned int ret = IPv4ToInt(argv[2], isBigEndian); + printf("%u\n", ret); + return 0; + } +}
\ No newline at end of file |