IPv4和IPv6的任何有效地址 [英] ipv4 and ipv6 from any valid address
问题描述
我试图让无论从任何字符串地址的IPv4和IPv6地址,IPv4的通过,IPv6或DNS地址吧。
I'm trying to get both the ipv4 and ipv6 address from any string address, be it by ipv4, ipv6, or DNS address.
我可以创建自己的功能,这样做,但我试图采取<一个href=\"http://stackoverflow.com/questions/24194631/ip-address-v4-v6-equivalence-testing#comment37352027_24194676\">expert建议并利用内置的功能。
I can create my own function to do so, but I'm trying to take expert advice and utilize built-in capabilities.
有没有办法输入一个地址字符串
任何格式,并且同时拥有的的IPv4 和的的IPv6 助推地址回来了?
Is there a way to input an address string
of any format, and have both ipv4 and ipv6 boost addresses returned?
推荐答案
从DNS名称获得一个地址,涉及到...查询的命名服务器(DNS!)。如果要列举的结果,在ASIO使用解析:
Getting an address from the DNS name involves... querying a naming server (DNS!). If you want to enumerate the results, use a resolver in asio:
简单的例子:
#include <boost/asio.hpp>
#include <boost/function_output_iterator.hpp>
#include <set>
using boost::asio::ip::address;
std::set<address> unique_endpoints(std::string const& ip)
{
using resolver = boost::asio::ip::tcp::resolver;
boost::asio::io_service ios; // TODO use existing service / resolver
resolver r(ios);
std::set<address> unique;
for (auto it = r.resolve({ip, ""}); it != resolver::iterator {}; ++it)
{
//std::cout << "Resolved: " << it->host_name() << " -> " << it->endpoint() << " " << it->service_name() << "\n";
address a = it->endpoint().address();
if (a.is_v4())
unique.insert(boost::asio::ip::address_v6::v4_mapped(a.to_v4()));
else
unique.insert(a);
}
return unique;
}
template <typename S>
bool endpoints_overlap(S const& a, S const& b)
{
bool matching_found = false;
std::set_intersection(
a.begin(), a.end(), b.begin(), b.end(),
boost::make_function_output_iterator([&](address const&) { matching_found = true; }));
return matching_found;
}
int main()
{
auto h = unique_endpoints("bbs2.fritz.box");
auto a = unique_endpoints("192.168.2.111");
auto b = unique_endpoints("::ffff:192.168.2.111");
auto c = unique_endpoints("::ffff:c0a8:026f");
assert(endpoints_overlap(a, b));
assert(endpoints_overlap(a, c));
assert(endpoints_overlap(b, c));
assert(endpoints_overlap(h, a));
assert(endpoints_overlap(h, b));
assert(endpoints_overlap(h, c));
}
请注意,该测试将决定端点当DNS响应的一个相匹配重叠。这可能不可能永远在一个集群环境真(?不是专家那里),你可能的也的想用这种算法(未测试)之前检测到广播地址。
Note that this test will decide that endpoints overlap when one of the DNS responses matches. This might not always be true in a cluster setting (? no expert there) and you might also want to detect broadcast addresses before using this algorithm (not tested).
还请注意,我不认为有一种方法来检测实际的的主机等价的(意思是,如果主机有多个物理/逻辑网卡,它们将出现作为传输层单独的服务器)。
Note also, I don't think there's a way to detect equivalence of actual hosts (meaning, if a host has several physical/logical NICs, they will appear as separate servers on the transport level).
最后,在实际的应用程序,你会想要做的异步解析(使用 async_resolve
)
Finally, in a real-world application you will want to do the resolving asynchronously (use async_resolve
)
这篇关于IPv4和IPv6的任何有效地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!