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 | |
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-- | LICENSE | 37 | ||||
-rw-r--r-- | csci4211/ipaddress.c | 62 |
2 files changed, 82 insertions, 17 deletions
@@ -1,21 +1,24 @@ -MIT License +This is free and unencumbered software released into the public domain. -Copyright (c) 2018-2021 Matt Strapp +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +For more information, please refer to <http://unlicense.org/>
\ No newline at end of file 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 |