How to get destination address of UDP packet?
Hello,
I need to know destination address of incoming UDP packet, while sockets API (recvfrom) only provide sender address. How to do that? |
err, shouldn't the destination address be the address of the machine running the program?, there may be methods to allow you to peer further down the stack if there is a question of multiple interfaces.
|
Quote:
|
ok, try this, use the recvfrom (check on the man pages) call to receive the data. This will give you a sockaddr pointer containing the data about the incoming connection. The sockaddr structure contains a sa_data member which is simply a 14 byte char array. Somewhere in this array is the data you need.
|
crap, now I see what you mean, ok, sratch that.
|
ok, look at "man 7 ip". In there is an option called IP_PKTINFO. This gives some info about the ip packet, including the header destination address.
First set IP_PKTINFO using setsockpt. Then when the data is recieved, use recvmsg to extract the packet information, which should contain the destination address in the header. This only works for datagram/udp transactions. Let me know how you get on, as I've never done this. |
Quote:
I use recvmsg function to read from socket and receive struct in_pktinfo via msg_control member of struct msghdr. The only thing is I have to pass 24-byte buffer as msg_control (not 12-byte that gives sizeof(struct in_pktinfo)) and use latter 12 bytes of that buffer as struct in_pktinfo after successful call to recvmsg. I use Red Hat Enterprise Linux AS release 4 (Nahant Update 4). |
I got the destination address using ip_pktinfo in multicast program but how do i get the source address from the header. i tried struct msg_hdr->msg_name and pktinfo->ipi_spec_dest. both are giving the same multicast address returned in pktinfo->ipi_addr. how do get the source ip address ?
|
All times are GMT -5. The time now is 12:19 AM. |