Q:

How to get my own IP address from a struct addrinfo

I am binding a socket to my address to listen to connections. To do this, I get my address information using getaddrinfo() syscall, which grants me an ip independent way of doing what I want. The problem is that the structs which this syscall returns have the ip address field all blank. For example: 
struct addrinfo hints, *servinfo, *p;
int sock;

memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;

getaddrinfo(NULL, port, &hints, &servinfo)

for (p = servinfo; p != NULL; p = p->ai_next)
   if (p->ai_family == AF_INET6)
      break;

sock = socket(p->ai_family, p->sock_type, p->protocol);
bind(sock, p->ai_addr, p->ai_addrlen)

In the code above. the *p variable should have some kind of information on an IPv6 address of my machine since the bind succeeds, but the field p->ai_addr->sin6_addr (assume this would work without a casting) is blank. How can I know exactly what address I will be using?

A:

The address is not blank - it is all zeroes, which is 0::0.  This is the special wildcard address which means to bind to all local interfaces.
You should not care what address(es) your host has - this list might change at any time, including immediately after you check it.
Once you have a client connected, you can use getsockname() on the socket returned by accept() to determine which one of your local addresses that client connected to.

