Rea*_*nly 12 sockets networking libc
如果套接字绑定到IN6ADDR_ANY
或者INADDR_ANY
您使用调用,例如recvfrom()
在套接字上接收消息.有没有办法找出消息来自哪个接口?
在IPv6链接范围消息的情况下,我希望from参数recvfrom()
将scope_id
字段初始化为接口Id.不幸的是,它0
在我的测试程序中设置.
谁知道找到这些信息的方法?
dwc是对的,IPV6_PKTINFO适用于Linux上的IPv6.
此外,IP_PKTINFO将适用于IPv4 - 您可以在联机帮助页ip(7)中查看详细信息
我已经构建了一个提取源、目标和接口地址的示例。为简洁起见,不提供错误检查。请参阅此副本: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
}
Run Code Online (Sandbox Code Playgroud)
除了绑定到每个接口之外,我本身不知道使用 IPv4 的方法。
IPv6 添加了 IPV6_PKTINFO 套接字选项来解决此缺点。该选项生效后,astruct in6_pktinfo
将作为辅助数据返回。