收到UDP数据包后,我需要用发送者使用的地址回复他。
recvfrom
调用让我能够获取发送者的地址,但是如何获得接收到的数据包的目标地址,这个地址应该与本地主机接口的一个地址匹配呢?
收到UDP数据包后,我需要用发送者使用的地址回复他。
recvfrom
调用让我能够获取发送者的地址,但是如何获得接收到的数据包的目标地址,这个地址应该与本地主机接口的一个地址匹配呢?
我已经构建了一个示例,用于提取源地址、目的地址和接口地址。为了简洁起见,没有提供错误检查。
// 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
}
您可以使用setsockopt设置IP_PKTINFO选项,然后使用recvmsg并在struct msghdr的msg_control成员中获取in_pktinfo结构。in_pktinfo具有一个字段,其中包含数据包的目标地址。
请参见:http://www.linuxquestions.org/questions/programming-9/how-to-get-destination-address-of-udp-packet-600103/以获取更多详细信息。
0.0.0.0:0
(或[::]:0
)的监听套接字,使用getsockname
函数并不是很有用。对于 TCP,在accept
之后您会得到一个本地地址,但是对于 UDP……我不确定如何回答 OP 的问题。 - ephemientgetsockname(2)
。听起来好像会行:),现在我有个人的利益,要找到一个解决方案给他 :)。 - sarnold