LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-16-2007, 07:30 AM   #1
andreyk
LQ Newbie
 
Registered: Feb 2005
Posts: 4

Rep: Reputation: 0
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?
 
Old 11-16-2007, 07:48 AM   #2
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Rep: Reputation: 18
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.
 
1 members found this post helpful.
Old 11-16-2007, 07:56 AM   #3
andreyk
LQ Newbie
 
Registered: Feb 2005
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by gearoid_murphy View Post
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.
 
Old 11-17-2007, 07:56 AM   #4
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Rep: Reputation: 18
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.
 
Old 11-17-2007, 08:05 AM   #5
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Rep: Reputation: 18
crap, now I see what you mean, ok, sratch that.
 
Old 11-17-2007, 08:10 AM   #6
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Rep: Reputation: 18
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.

Last edited by gearoid_murphy; 11-17-2007 at 09:42 AM.
 
2 members found this post helpful.
Old 11-20-2007, 04:06 AM   #7
andreyk
LQ Newbie
 
Registered: Feb 2005
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by gearoid_murphy View Post
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).
 
Old 08-01-2013, 09:41 AM   #8
gknetw
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Rep: Reputation: Disabled
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 ?
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Per-Packet or Per Destination? caps_phisto Linux - Networking 1 09-30-2006 08:16 AM
How To Stop a UDP Packet Flood ! murder Linux - Newbie 2 09-19-2005 10:14 AM
UDP packet oss dazdaz Linux - Networking 2 04-05-2004 01:30 PM
problem to print source address and destination address jooboo Programming 2 11-26-2003 03:24 PM
UDP: short packet markus1982 Linux - Security 4 08-12-2003 08:03 AM


All times are GMT -5. The time now is 06:15 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration