c++ - Why are linux's IPv4 address taking 16 bytes instead of 4 -


i'm trying resolve domain name ipv4 address.

here's minimal code of i'm doing

addrinfo hints, *results; memset(&hints, 0, sizeof(hints)); hints.ai_family = af_inet; int error = getaddrinfo("google.com", 0, &hints, &results); for(addrinfo* info = results; info; info = info->ai_next) {     std::cout << (info->ai_family == af_inet6 ? "ip6" : "ip4")               << '[' << info->ai_addrlen << "]: ";     for(uint = 0; < info->ai_addrlen; ++i)         std::cout << int(((uint8_t*)info->ai_addr)[i]) << ' ';     std::cout << std::endl; } freeaddrinfo(results); 

when run this, prints:

ip4[16]: 2 0 0 0 216 58 208 238 0 0 0 0 0 0 0 0  ip4[16]: 2 0 0 0 216 58 208 238 0 0 0 0 0 0 0 0  ip4[16]: 2 0 0 0 216 58 208 238 0 0 0 0 0 0 0 0 

the actual ip of google.com 216.58.208.238. reason, ip v4, ai_addr contains 16 bytes , stores address @ bytes 4 8.

my question is: what 12 other bytes ?

i read the docs, , googled bit found nothing on addr_len. note didn't set ai_v4mapped flag.

i'm not sure whether required standard, struct sockaddr defined contain short address family , 14 bytes of address-family-specific data.

every type used address must compatible that, thus, @ least same length.

for struct sockaddr_in (the 1 ipv4), looks this:

struct sockaddr_in {     short            sin_family;     unsigned short   sin_port;     struct in_addr   sin_addr;     char             sin_zero[8]; }; 

2 bytes address family, 2 bytes port, 4 bytes ipv4 address , 8 bytes of padding reach length of struct sockaddr.

that said: question slightly wrong, not ipv4 address (that struct in_addr indeed 4 bytes) socket address ipv4. still, 8 bytes "wasted" here.


Comments