summaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/posix/getaddrinfo.c
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2024-03-07 17:28:00 -0500
committerIan Moffett <ian@osmora.org>2024-03-07 17:28:32 -0500
commitbd5969fc876a10b18613302db7087ef3c40f18e1 (patch)
tree7c2b8619afe902abf99570df2873fbdf40a4d1a1 /lib/mlibc/tests/posix/getaddrinfo.c
parenta95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff)
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/tests/posix/getaddrinfo.c')
-rw-r--r--lib/mlibc/tests/posix/getaddrinfo.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/mlibc/tests/posix/getaddrinfo.c b/lib/mlibc/tests/posix/getaddrinfo.c
new file mode 100644
index 0000000..d52c93f
--- /dev/null
+++ b/lib/mlibc/tests/posix/getaddrinfo.c
@@ -0,0 +1,52 @@
+#include <netdb.h>
+#include <assert.h>
+#include <stddef.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <stdio.h>
+
+int main() {
+ struct addrinfo *res = NULL;
+ struct addrinfo hints = {0};
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = IPPROTO_TCP;
+
+ int ret = getaddrinfo(NULL, "443", &hints, &res);
+ assert(ret == 0);
+
+ struct sockaddr_in *addr = (struct sockaddr_in*)(res[0].ai_addr);
+ assert(addr->sin_port == htons(443));
+ assert(res[0].ai_socktype == SOCK_STREAM);
+ assert(res[0].ai_protocol == IPPROTO_TCP);
+
+ freeaddrinfo(res);
+ res = NULL;
+
+ /* check we can resolve any domain */
+ ret = getaddrinfo("example.net", NULL, &hints, &res);
+ assert(ret == 0);
+
+ freeaddrinfo(res);
+ res = NULL;
+
+ hints.ai_flags = AI_NUMERICHOST;
+ ret = getaddrinfo("10.10.10.10", NULL, &hints, &res);
+ assert(ret == 0);
+
+ addr = (struct sockaddr_in*)res[0].ai_addr;
+ assert((addr->sin_addr.s_addr & 0xFF) == 10);
+ assert(((addr->sin_addr.s_addr >> 8) & 0xFF) == 10);
+ assert(((addr->sin_addr.s_addr >> 16) & 0xFF) == 10);
+ assert(((addr->sin_addr.s_addr >> 24) & 0xFF) == 10);
+
+ freeaddrinfo(res);
+ res = NULL;
+
+ ret = getaddrinfo("example.net", NULL, &hints, &res);
+ assert(ret == EAI_NONAME);
+
+ freeaddrinfo(res);
+
+ return 0;
+}