如何获得*从UDP端点我* IP [英] How to get *my* ip from udp endpoint
问题描述
Boost.Asio的的 UDP ::端点
有一个成员是远程地址。因为我在听在多个接口(像这样):
Boost.Asio's udp::endpoint
has a member that is remote address. Because I'm listening on multiple interfaces (like this):
udp_socket(io_service, udp::endpoint(udp::v4(), port))
在我的处理程序,我不知道是哪个网络接口接收数据包。
In my handler, I do not know which network interface received the packet.
如果没有遍历网络接口和寻找端点地址和我的每个接口IP之间的相似性,我可以得到我的,我得到消息,从接口的IP?
Without iterating over network interfaces and looking for a similarity between the endpoint address and my IP on each interface, can I get my IP for the interface that I got message from?
推荐答案
没有。 Boost.Asio的不公开,以确定数据报的目的地址的能力。
No. Boost.Asio does not expose the ability to identify the datagram's destination address.
的<一个href=\"http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/basic_datagram_socket/async_receive_from/overload1.html\"相对=nofollow> 插座:: aync_receive_from()
和<一个href=\"http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/basic_datagram_socket/receive_from/overload1.html\"相对=nofollow> 插座:: receive_from()
操作中执行本地套接字操作<一个href=\"https://github.com/boostorg/asio/blob/boost-1.55.0/include/boost/asio/detail/impl/socket_ops.ipp#L870\"相对=nofollow> 的boost ::支持ASIO ::详细:: socket_ops :: recvfrom的()
。在Windows中, WSARecvFrom()
没有提供方法来提取协议栈下层的信息。在Linux上, IP_PKTINFO
插座选项可以提供给面向数据报套接字实现 recvfrom的()
来填充 msghdr.msg_control
与辅助信息,如在其上接收数据包的接口索引,并在报头中的目的地址缓冲区。然而,Boost.Asio的<一href=\"https://github.com/boostorg/asio/blob/4f11be2/include/boost/asio/detail/impl/socket_ops.ipp#L910-L915\"相对=nofollow>实施不使用 msg_control域
字段:
The socket::aync_receive_from()
and socket::receive_from()
operations perform the native socket operations within boost::asio::detail::socket_ops::recvfrom()
. On Windows, WSARecvFrom()
provides no way to extract lower layer information from the protocol stack. On Linux, the IP_PKTINFO
socket option can provided to datagram oriented sockets enabling recvfrom()
to populate the msghdr.msg_control
buffer with ancillary information, such as the interface index on which the packet was received and the destination address in the packet header. However, the Boost.Asio implementation does not use themsg_control
field:
msghdr msg = msghdr();
init_msghdr_msg_name(msg.msg_name, addr);
msg.msg_namelen = static_cast<int>(*addrlen);
msg.msg_iov = bufs;
msg.msg_iovlen = static_cast<int>(count);
signed_size_type result = error_wrapper(::recvmsg(s, &msg, flags), ec);
如提出这个回答,人们可以:
- 使用较低级别的系统调用。 (即
IP_PKTINFO
和recvfrom的()
) - 显式插座每个接口绑定,而不是
INADDR_ANY
,引起<一个href=\"http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/basic_datagram_socket/local_endpoint/overload1.html\"相对=nofollow>插座:: local_endpoint()
返回一个特定的接口。完成处理程序将需要一些方法来获取套接字的本地端点该操作被调用,比如通过传递给插座或本地端点引用的boost :: bind()的
。
- Use lower-level system calls. (i.e.
IP_PKTINFO
andrecvfrom()
) - Explicitly bind a socket to each interface rather than
INADDR_ANY
, causing thesocket::local_endpoint()
to return a specific interface. The completion handlers will need some way to obtain the local endpoint on the socket for which the operation was invoked, such as passing a reference to the socket or its local endpoint viaboost::bind()
.
这篇关于如何获得*从UDP端点我* IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!