I want to receive UDP messages in Elixir and need to get the destination IP where the packets have been sent to.
In iex i can open a socket with this command:
{:ok, socket} = :gen_udp.open(19_999, [{:active, true}])
When i receive a message i will get it in the following format:
{:udp, port, source_ip, source_port, data}
The destination IP is not part of the message. According to UDP specification the source IP is not part of the UDP packet, but it is part of the IP pseudo header. When i use tcpdump i can see the target IPs of the packets.
Is there any way/parameter to get the IP or Pseudo header of the UDP packet?
1条答案
按热度按时间8hhllhi21#
The destination port is
19_999
for sure.To figure out which IP is receiving the current packet, you need to know the configuration of all your network adapters, including the virtual ones. After that, you can get the destination IP from the source IP and network configuration because only the devices in the same network can talk to each other. You just need to find the IP that is in the same network as the source IP, among all the network adapters. You can judge whether 2 devices are in the same network by applying the netmask to the 2 IP addresses, and see if the results are the same.
By the way, if your server is behind NAT, you can't get the real source IP address of the client if it's not in the same network as the server. You only get the source IP address of the last hop (which is usually the IP address of your network gateway).
Getting the network configuration
You'll get a list that looks like
Since we only need the
addr
and thenetmask
, we can just do a furtherEnum.map
on the list:Apply a netmask to an IP address
Just bitwise and them together.
Find the destination IP
UPDATE
If possible I suggest that you open the UDP socket on only one of your network adapters. You can use the
{ifaddr, inet:socket_address()}
option to achieve that, for example:In this way, you can 100% be sure that the destination IP is 192.168.0.10. Besides, it also adds a little bit of security.