Files
RedBear-OS/tests/netdb/getaddrinfo.c
T
sourceturner 20d89d166c use ip6 feature flag for IPv6 related definitions
as a consequence, some unit tests have to be fixed, too
2026-06-04 17:48:14 +02:00

62 lines
1.4 KiB
C

// Adapted from https://gist.github.com/jirihnidek/bf7a2363e480491da72301b228b35d5d
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include "test_helpers.h"
#ifdef INET6_ADDRSTRLEN
#define _ADDRSTRLEN INET6_ADDRSTRLEN
#else
#define _ADDRSTRLEN INET_ADDRSTRLEN
#endif
int main(void) {
struct addrinfo hints, *res;
int errcode;
char addrstr[_ADDRSTRLEN];
void *ptr;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;
errcode = getaddrinfo("www.redox-os.org", NULL, &hints, &res);
if (errcode != 0) {
perror("getaddrinfo");
exit(EXIT_FAILURE);
}
while (res) {
switch (res->ai_family) {
case AF_INET:
ptr = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
break;
#ifdef AF_INET6
case AF_INET6:
ptr = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
break;
#endif
default:
ptr = NULL;
}
if (ptr) {
inet_ntop(res->ai_family, ptr, addrstr, _ADDRSTRLEN);
printf(
"IPv%d address: %s (%s)\n",
res->ai_family == AF_INET ? 4 : 6,
addrstr,
res->ai_canonname
);
}
res = res->ai_next;
}
}