From a9584c6b392c508e71f6452d7be1200a5914419d Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Wed, 26 Jun 2024 18:32:28 +0200 Subject: Networking stuff TCP is now in a somewhat good state --- userland/libc/include/sys/mman.h | 10 +++-- userland/libc/netdb/getaddrinfo.c | 83 +++++++++++++++++++++------------------ 2 files changed, 51 insertions(+), 42 deletions(-) (limited to 'userland') 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 #include +#include + +#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 -#include -#include -#include #include #include #include +#include #include #include #include #include +#include #include #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; } -- cgit v1.2.3