views:

32

answers:

1

In C++,
how can I get the receiver address of the UDP packet which I have received using recvfrom. I know that it should be the same host on which I am receiving the packet, but I need to extract it from the received packet, in order to verify something. How can I do this?

I found that one way of doing this is:

int r = getsockopt(receiver_sock, SOL_IP, SO_ORIGINAL_DST, (struct sockaddr *) &sender_addr, (socklen_t *)&addr_len);`

But I get the error:

error: ‘SO_ORIGINAL_DST’ was not declared in this scope

I am using the appropriate headers

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include </usr/src/linux-headers-2.6.32-21/include/linux/netfilter_ipv4.h>
#include <arpa/inet.h>    
#include <linux/netfilter.h>

Using netfilter_ipv4 gives other errors like INT_MIN not declared. However, I think the mistake is something more fundamental rather than inclusion of proper header.

Please help.

+2  A: 

On Linux you want to use IP_PKTINFO option, see ip(7), and the recvmsg(2) call.

Stevens has examples of doing this but with IP_RECVDSTADDR and IP_RECVIF options that are not available on Linux.

Nikolai N Fetissov
Thank you so much for replying. I tried IP_PKTINFO as well.. but the real problem seems to be different. Would you mind looking at this question http://stackoverflow.com/questions/3951043/c-iptables-redirection-forming-separate-packets. Thanks a lot.
SkypeMeSM