4

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.

SkypeMeSM
  • 3,197
  • 8
  • 43
  • 61
  • possible duplicate of [How to tell which interface the socket received the message from?](http://stackoverflow.com/questions/603577/how-to-tell-which-interface-the-socket-received-the-message-from) – Jason C Oct 26 '13 at 18:20

2 Answers2

4

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 Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • 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 Oct 16 '10 at 22:17
3

I've constructed an example that extracts the source, destination and interface addresses. For brevity, no error checking is provided. See this duplicate: Get destination address of a received UDP packet.

// sock is bound AF_INET socket, usually SOCK_DGRAM
// include struct in_pktinfo in the message "ancilliary" control data
int opt = 1;
setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt));
// the control data is dumped here
char cmbuf[0x100];
// the remote/source sockaddr is put here
struct sockaddr_in peeraddr;
// if you want access to the data you need to init the msg_iovec fields
struct msghdr mh = {
    .msg_name = &peeraddr,
    .msg_namelen = sizeof(peeraddr),
    .msg_control = cmbuf,
    .msg_controllen = sizeof(cmbuf),
};
recvmsg(sock, &mh, 0);
for ( // iterate through all the control headers
    struct cmsghdr *cmsg = CMSG_FIRSTHDR(&mh);
    cmsg != NULL;
    cmsg = CMSG_NXTHDR(&mh, cmsg))
{
    // ignore the control headers that don't match what we want
    if (cmsg->cmsg_level != IPPROTO_IP ||
        cmsg->cmsg_type != IP_PKTINFO)
    {
        continue;
    }
    struct in_pktinfo *pi = CMSG_DATA(cmsg);
    // at this point, peeraddr is the source sockaddr
    // pi->ipi_spec_dst is the destination in_addr
    // pi->ipi_addr is the receiving interface in_addr
}
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
  • 2
    What `opt` should be in line `setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt));` ? – andrybak Oct 04 '13 at 13:44
  • @andrybak latest reply ever: I have added the value above. See https://man7.org/linux/man-pages/man7/ip.7.html under Socket Options. – Matt Joiner Sep 20 '22 at 23:45