Upon receiving a UDP packet, I need to respond to the sender with the address he used to send the packet to which I'm replying.
The recvfrom
call lets me get the address of the sender, but how do I get the destination address of the received packet, which should match the address of one of the local host's interfaces?
I've constructed an example that extracts the source, destination and interface addresses. For brevity, no error checking is provided.
You set the IP_PKTINFO option using setsockopt and then use recvmsg and get a in_pktinfo structure in the msg_control member of struct msghdr. the in_pktinfo has a field with the destination address of the packet.
See: http://www.linuxquestions.org/questions/programming-9/how-to-get-destination-address-of-udp-packet-600103/ where I found the answer for more details.