diff options
author | Anton Kling <anton@kling.gg> | 2024-06-26 18:32:28 +0200 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-06-26 18:36:22 +0200 |
commit | a9584c6b392c508e71f6452d7be1200a5914419d (patch) | |
tree | 8f0e856c5d84a47d7bab467766a52b067647e1aa /userland | |
parent | 33e1b11555d3557a36bd69d63f5bf0c290b5d462 (diff) |
Networking stuff
TCP is now in a somewhat good state
Diffstat (limited to 'userland')
-rw-r--r-- | userland/libc/include/sys/mman.h | 10 | ||||
-rw-r--r-- | userland/libc/netdb/getaddrinfo.c | 83 |
2 files changed, 51 insertions, 42 deletions
diff --git a/userland/libc/include/sys/mman.h b/userland/libc/include/sys/mman.h index 7cba68f..d60997d 100644 --- a/userland/libc/include/sys/mman.h +++ b/userland/libc/include/sys/mman.h @@ -1,14 +1,16 @@ #ifndef MMAP_H #define MMAP_H -#include <stdint.h> #include <stddef.h> +#include <stdint.h> + +#define MAP_FAILED ((void *)-1) #define PROT_READ (1 << 0) #define PROT_WRITE (1 << 1) -#define MAP_PRIVATE (1 << 0) -#define MAP_ANONYMOUS (1<< 1) -#define MAP_SHARED (1<< 2) +#define MAP_PRIVATE (1 << 0) +#define MAP_ANONYMOUS (1 << 1) +#define MAP_SHARED (1 << 2) void *mmap(void *addr, size_t length, int prot, int flags, int fd, size_t offset); diff --git a/userland/libc/netdb/getaddrinfo.c b/userland/libc/netdb/getaddrinfo.c index d550b39..f7e89c0 100644 --- a/userland/libc/netdb/getaddrinfo.c +++ b/userland/libc/netdb/getaddrinfo.c @@ -1,14 +1,12 @@ -#include <assert.h> -#include <stdint.h> -#include <stdlib.h> -#include <tb/sv.h> #include <arpa/inet.h> #include <assert.h> #include <ctype.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> +#include <tb/sv.h> #include <unistd.h> #define PORT 53 @@ -137,6 +135,7 @@ int getaddrinfo(const char *restrict node, const char *restrict service, close(sockfd); return EAI_FAIL; } + close(sockfd); if (0 != memcmp(buffer, &id, sizeof(u16))) { close(sockfd); @@ -163,7 +162,7 @@ int getaddrinfo(const char *restrict node, const char *restrict service, } u16 ancount = ntohs(*(u16 *)(buffer + sizeof(u16) * 3)); - if (1 != ancount) { + if (0 == ancount) { close(sockfd); return EAI_NONAME; // TODO: Check if this is correct } @@ -192,44 +191,52 @@ int getaddrinfo(const char *restrict node, const char *restrict service, } */ - // type - u16 type = ntohs(*(u16 *)(answer + sizeof(u16))); - u16 class = ntohs(*(u16 *)(answer + sizeof(u16) * 2)); - if (1 != type) { - close(sockfd); - return EAI_FAIL; - } - if (1 != class) { - close(sockfd); - return EAI_FAIL; - } - - u16 rdlength = ntohs(*(u16 *)(answer + sizeof(u16) * 3 + sizeof(u32))); + for (u16 i = 0; i < answer_length;) { + // type + u16 type = ntohs(*(u16 *)(answer + sizeof(u16))); + u16 class = ntohs(*(u16 *)(answer + sizeof(u16) * 2)); + int ignore = 0; + u16 inc = 0; + if (1 != type) { + ignore = 1; + } + if (1 != class) { + ignore = 1; + } + inc += sizeof(u16) * 3; - if (4 != rdlength) { - close(sockfd); - return EAI_FAIL; - } - u32 ip = *(u32 *)(answer + sizeof(u16) * 4 + sizeof(u32)); + u16 rdlength = ntohs(*(u16 *)(answer + sizeof(u16) * 3 + sizeof(u32))); + inc += sizeof(u16); + inc += rdlength; + inc += sizeof(u32); - if (res) { - *res = calloc(1, sizeof(struct addrinfo)); - if (!(*res)) { - close(sockfd); - return EAI_MEMORY; + if (4 != rdlength) { + ignore = 1; } - struct sockaddr_in *sa = calloc(1, sizeof(struct sockaddr_in)); - if (!sa) { - free(*res); - close(sockfd); - return EAI_MEMORY; + if (!ignore) { + u32 ip = *(u32 *)(answer + sizeof(u16) * 4 + sizeof(u32)); + + if (res) { + *res = calloc(1, sizeof(struct addrinfo)); + if (!(*res)) { + close(sockfd); + return EAI_MEMORY; + } + struct sockaddr_in *sa = calloc(1, sizeof(struct sockaddr_in)); + if (!sa) { + free(*res); + close(sockfd); + return EAI_MEMORY; + } + (*res)->ai_addr = (struct sockaddr *)sa; + sa->sin_addr.s_addr = ip; + sa->sin_port = service_to_port(service); + sa->sin_family = AF_INET; + } } - (*res)->ai_addr = (struct sockaddr *)sa; - sa->sin_addr.s_addr = ip; - sa->sin_port = service_to_port(service); - sa->sin_family = AF_INET; + i += inc; + answer += inc; } - close(sockfd); return 0; } |