summaryrefslogtreecommitdiff
path: root/src/libc/net/arpainet.c
diff options
context:
space:
mode:
authordzwdz2023-12-25 20:12:44 +0100
committerdzwdz2023-12-25 20:12:44 +0100
commit4be1fd62131f7e186e6f92f1bb5a356dc1ac1951 (patch)
tree54de3a2d99fe9d6e93ad45c5107aff5420f9900d /src/libc/net/arpainet.c
parentb9f5f92bff69059471a76e73539780eedb356455 (diff)
user/libc: reorganize net stuff, basic hosts-only gethostbyname()
/usr/share/hosts because i don't have /etc/ yet and i don't feel like creating it.
Diffstat (limited to 'src/libc/net/arpainet.c')
-rw-r--r--src/libc/net/arpainet.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/libc/net/arpainet.c b/src/libc/net/arpainet.c
new file mode 100644
index 0000000..62efb62
--- /dev/null
+++ b/src/libc/net/arpainet.c
@@ -0,0 +1,38 @@
+#include <arpa/inet.h>
+#include <stdlib.h>
+
+uint32_t htonl(uint32_t n) {
+ return ((n & 0xFF000000) >> 24)
+ | ((n & 0x00FF0000) >> 8)
+ | ((n & 0x0000FF00) << 8)
+ | ((n & 0x000000FF) << 24);
+}
+
+uint16_t htons(uint16_t n) {
+ return (n >> 8) | (n << 8);
+}
+
+uint32_t ntohl(uint32_t n) {
+ return htonl(n);
+}
+
+uint16_t ntohs(uint16_t n) {
+ return htons(n);
+}
+
+int inet_aton(const char *s, struct in_addr *dest) {
+ if (!s) return -1;
+
+ uint32_t ip = strtol(s, (char**)&s, 0);
+ int parts = 1;
+ for (; parts < 4; parts++) {
+ if (!*s) break;
+ if (*s++ != '.') return 0;
+ ip <<= 8;
+ ip += strtol(s, (char**)&s, 0);
+ }
+ if (parts > 1)
+ ip = ((ip & 0xFFFFFF00) << 8 * (4 - parts)) | (ip & 0xFF);
+ dest->s_addr = htonl(ip);
+ return 1;
+}