LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to get destination address of UDP packet? (https://www.linuxquestions.org/questions/programming-9/how-to-get-destination-address-of-udp-packet-600103/)

andreyk 11-16-2007 07:30 AM

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?

gearoid_murphy 11-16-2007 07:48 AM

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.

andreyk 11-16-2007 07:56 AM

Quote:

Originally Posted by gearoid_murphy (Post 2960925)
err, shouldn't the destination address be the address of the machine running the program?

I receive multicast packets, destination address of such packet is a multicast group address. That's the thing I want to know - address of multicast group received packet was sent to.

gearoid_murphy 11-17-2007 07:56 AM

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.

gearoid_murphy 11-17-2007 08:05 AM

crap, now I see what you mean, ok, sratch that.

gearoid_murphy 11-17-2007 08:10 AM

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.

andreyk 11-20-2007 04:06 AM

Quote:

Originally Posted by gearoid_murphy (Post 2961986)
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.

Thanks, this works fine.
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).

gknetw 08-01-2013 09:41 AM

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.