How do I find out which IP address the OS chose to use, when I send a packet from a UDP socket bound to INADDR_ANY?
int s = socket(AF_INET, SOCK_DGRAM, 0);
sockaddr_in src;
src.sin_family = AF_INET;
src.sin_port = 12345;
src.sin_addr.s_addr = INADDR_ANY;
bind(s, (struct sockaddr*)&src, sizeof(src));
char msg[] = "Hello";
sockaddr_in dest;
dest.sin_family = AF_INET;
dest.sin_port = 12345;
dest.sin_addr.s_addr = (in_addr_t)0xdeadbeef;
sendto(s, msg, sizeof(msg), 0, (struct sockaddr*)&dest, sizeof(dest));
What source address was used to send the packet? Ideally there would be a sendtofrom()
function like recvfrom()
which returns the address the kernel chose.
In my application, I bind to INADDR_ANY and send a packet to a STUN server (I don't want to have to play around with the routing table to pick a source address: the kernel's choice is fine). But, to do ICE I then need to obtain the "base address" of the "server-reflexive address", ie. I need to know which local address was used to send the STUN request. I would accept Windows- or POSIX-specific answers suggesting looking up an address from the routing table.