I wrote a very simple test program for getaddrinfo:
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
int main() {
struct addrinfo hints;
struct addrinfo *res, *rp;
char hoststr[64], servstr[8];
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
getaddrinfo(NULL, "9998", &hints, &res);
for (rp = res; rp != NULL; rp = rp->ai_next) {
getnameinfo(rp->ai_addr, rp->ai_addrlen, hoststr, sizeof(hoststr),
servstr, sizeof(servstr), NI_NUMERICHOST | NI_NUMERICSERV);
printf("%s:%s\n", hoststr, servstr);
}
}
When I compile and run this program, it's giving the IPv4 address before the IPv6 address:
# gcc -o getaddrinfo getaddrinfo.c
# ./getaddrinfo
0.0.0.0:9998
:::9998
As far as I can tell from other sources, IPv6 addresses are supposed to be preferred over IPv4. I'm using the default /etc/gai.conf which implies that IPv6 should be preferred over IPv4. So why is getaddrinfo sorting this way?
# ip addr show lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever