C ++ DGRAM套接字获取RECEIVER地址

时间:2010-10-15 08:26:58

标签: c++ sockets udp receiver

在C ++中,
    如何获取使用recvfrom收到的UDP数据包的接收者地址。我知道它应该是我收到数据包的主机,但是我需要从收到的数据包中提取它,以便验证某些内容。我怎么能这样做?

我发现这样做的一种方法是:

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

但我收到错误:

error: ‘SO_ORIGINAL_DST’ was not declared in this scope

我正在使用相应的标题

#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>

使用netfilter_ipv4会发出其他错误,例如未声明INT_MIN。但是,我认为错误是更基本的,而不是包含正确的标题。

请帮忙。

2 个答案:

答案 0 :(得分:3)

在Linux上,您想使用IP_PKTINFO选项,请参阅ip(7)recvmsg(2)来电。

Stevens举例说明了这一点,但Linux上没有IP_RECVDSTADDRIP_RECVIF选项。

答案 1 :(得分:2)

我构建了一个提取源,目标和接口地址的示例。为简洁起见,未提供错误检查。请参阅此副本: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
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
}