Get destination address of a received UDP packet
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.
Getting the destination IP of incoming UDP packet in C++
GetTcpTable() does not give you the destination IP of TCP packets. It merely gives you a list of currently listening TCP ports and active TCP connections.
GetUdpTable() can give you the list of currently listening UDP ports, where UDP packets can be sent to. There are no connections in UDP.
But, if you want to know the actual destination IP for each UDP packet received, and without having to use a low-level capture library like WinPCap, then you can use the
WSARecvMsg() function to receive your UDP packets, rather than using the
recvfrom() or even
WSARecvMsg() can report metadata about each packet, most notably the
IP_PKTINFO (IPv4) and
IPV6_PKTINFO (IPv6) control blocks, which specify the destination IP of the packet and the index of the network interface that received the packet.
You need to use
WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER) to obtain a pointer to the
WSARecvMsg() function (see Why is the WSARecvMsg function implemented as a function pointer and can this pointer be reused? for the reason why), and use
setsockopt() to enable the
IPV6_PKTINFO socket option, before you start calling
WSARecvMsg() to receive packets.
See Function to retrieve the header destination address from a packet in windows XP for an example.
Obtain local IP address on receiving UDP
LocalAddr effectively returns the
address value passed to
ListenPacket. If this is not specific enough for you, you'll need to create multiple listeners, one for each IP address on your server.
How to find the destination address of a UDP packet using boost::asio?
Unfortunately this is not possible with boost::asio, and usually is not "the way to do" it, as you try to access Transport Layer information at the Application Layer.
So you basically have two options:
a) Write non-portable system code with for example IP_PKTINFO or SO_BINDTODEVICE on Linux. Example Code can be found on the boost asio mailing list here
b) use two distinct sockets, one for the multicast and one for the unicast. You therefore need to specify a listen_address other than "0.0.0.0" on each socket.
This Question on SO might also be helpful: Using a specific network interface for a socket in windows
How to Use Sed to Extract Substring
How to Use Variables in Bash Sed Command, Specific Example
How to Measure the Actual Memory Usage of an Application or Process
What's the Difference of Section and Segment in Elf File Format
What Is the Meaning of So_Reuseaddr (Setsockopt Option) - Linux
How Many Socket Connections Possible
Forward Host Port to Docker Container
"/Usr/Bin/Ld: Cannot Find -Lz"
Get the Perl Rename Utility Instead of the Built-In Rename
How to Shutdown a Spring Boot Application in a Correct Way
How to Remove the Lines Which Appear on File B from Another File A
How to Merge Two Files Using Awk
Command Line Program to Create Website Screenshots (On Linux)
How to Generate New Variable Names on the Fly in a Shell Script
Printing an Integer as a String With At&T Syntax, With Linux System Calls Instead of Printf