/ # cat test.c && ./test -v && strace ./test && ifconfig -a #include #include #include #include #include void print_ai( struct addrinfo *ai ) { unsigned family; char addrs[ INET6_ADDRSTRLEN ]; unsigned port; struct sockaddr_in *sai; struct sockaddr_in6 *sai6; if ( ai->ai_addr ) { switch( ai->ai_addr->sa_family ) { case AF_INET: sai = ai->ai_addr; inet_ntop( AF_INET, & sai->sin_addr, addrs, sizeof( addrs ) ); break; case AF_INET6: sai6 = ai->ai_addr; inet_ntop( AF_INET6, & sai6->sin6_addr, addrs, sizeof( addrs ) ); break; default: break; } } printf( "struct addrinfo {" "\n" " ai_flags: %x" "\n" " ai_family: %u" "\n" " ai_socktype: %u" "\n" " ai_protocol: %u" "\n" " ai_addrlen: %u" "\n" " ai_addr: " "\n" " family: %u" "\n" " addr: %s" "\n" " port: %u" "\n" " ai_canonname: %s" "\n" " ai_next: %p" "\n" "} " "\n", ai->ai_flags, ai->ai_family, ai->ai_socktype, ai->ai_protocol, ai->ai_addrlen, NULL == ai->ai_addr ? "??" : ai->ai_addr->sa_family, NULL == ai->ai_addr ? "??" : addrs, NULL == ai->ai_addr ? "??" : port, ai->ai_canonname ? ai->ai_canonname : "(null)", ai->ai_next ); } int main( int argc, char *argv[] ) { int r; struct addrinfo hints = { .ai_family = PF_UNSPEC, .ai_socktype = SOCK_STREAM, .ai_flags = AI_V4MAPPED | AI_ADDRCONFIG, }; struct addrinfo *res; struct addrinfo *it; bool verbose = argc > 1; r = getaddrinfo( "localhost", NULL, & hints, & res ); if ( 0 != r ) { perror( "getaddrinfo" ); return r; } if ( verbose ) { for( it = res; NULL != it; it = it->ai_next ) { print_ai( it ); } } freeaddrinfo( res ); return r; } struct addrinfo { ai_flags: 0 ai_family: 2 ai_socktype: 1 ai_protocol: 6 ai_addrlen: 16 ai_addr: family: 2 addr: 127.0.0.1 port: 0 ai_canonname: localhost ai_next: 0x55d919dce0d0 } struct addrinfo { ai_flags: 0 ai_family: 10 ai_socktype: 1 ai_protocol: 6 ai_addrlen: 28 ai_addr: family: 10 addr: ::1 port: 0 ai_canonname: localhost ai_next: 0 } execve("./test", ["./test"], 0x7ffcb6904280 /* 6 vars */) = 0 arch_prctl(ARCH_SET_FS, 0x7fc81f0cdb88) = 0 set_tid_address(0x7fc81f0cdbc0) = 101 mprotect(0x7fc81f0ca000, 4096, PROT_READ) = 0 mprotect(0x55eccc5f2000, 4096, PROT_READ) = 0 open("/etc/hosts", O_RDONLY|O_CLOEXEC) = 3 fcntl(3, F_SETFD, FD_CLOEXEC) = 0 readv(3, [{iov_base="", iov_len=0}, {iov_base="127.0.0.1\tlocalhost\n::1\tlocalhos"..., iov_len=1024}], 2) = 174 readv(3, [{iov_base="", iov_len=0}, {iov_base="", iov_len=1024}], 2) = 0 close(3) = 0 socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_UDP) = 3 connect(3, {sa_family=AF_INET6, sin6_port=htons(65535), inet_pton(AF_INET6, "::ffff:127.0.0.1", &sin6_addr), sin6_flowinfo=htonl(0), sin6_scope_id=0}, 28) = 0 getsockname(3, {sa_family=AF_INET6, sin6_port=htons(53878), inet_pton(AF_INET6, "::ffff:127.0.0.1", &sin6_addr), sin6_flowinfo=htonl(0), sin6_scope_id=0}, [28]) = 0 close(3) = 0 socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_UDP) = 3 connect(3, {sa_family=AF_INET6, sin6_port=htons(65535), inet_pton(AF_INET6, "::1", &sin6_addr), sin6_flowinfo=htonl(0), sin6_scope_id=0}, 28) = -1 EADDRNOTAVAIL (Address not available) close(3) = 0 exit_group(0) = ? +++ exited with 0 +++ eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:03 inet addr:172.17.0.3 Bcast:172.17.255.255 Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:26909 errors:0 dropped:0 overruns:0 frame:0 TX packets:13292 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:39372973 (37.5 MiB) TX bytes:720894 (703.9 KiB) ip6tnl0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 NOARP MTU:1452 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) tunl0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-87-AF-00-00-00-00-00-00-00-00 NOARP MTU:1480 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)