IP地址v4/v6等效测试 [英] IP address v4/v6 equivalence testing

查看:163
本文介绍了IP地址v4/v6等效测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用IPv4和IPv6的双堆栈环境中是否可以测试IP地址的等效性?如果可以,怎么办?

Is it possible to test IP addresses for equivalence in a dual stack environment using both IPv4 and IPv6? If so, how?

我的应用程序在Boost ASIO之上使用 websocket ++ .

My application uses websocket++ on top of Boost ASIO.

例如,在我的LAN上,一个应用程序连接到另一个正在监听192.168.1.2的应用程序,但是使用

As an example, on my LAN, one application connects to another listening on 192.168.1.2, but using this answer's IP address getter

std::string s = socket.remote_endpoint().address().to_string();

::ffff:192.168.1.3作为客户的地址.

问题在于,.2将具有其自己的节点列表,该节点列表具有.3的原始v4地址,因此,通过将上面的getter与保存在磁盘上的v4版本进行简单的字符串比较,它将寻求与之的冗余连接.3,即使已经连接.

The problem is that .2 will have its own node list with the original v4 address for .3, so by simple string comparison of the getter above vs a v4 version held on disk, it will seek a redundant connection to .3 even though already connected.

我进一步读到,由于使用点分四点符号::ffff:192.0.2.128也是::ffff:c000:0280.

I've further read that things can get more complicated since by this dotted quad notation, ::ffff:192.0.2.128 is also ::ffff:c000:0280.

我正在构建一个p2p应用程序,该应用程序接受来自不受信任来源的地址,因此,为了防止冗余连接,我需要能够绝对测试其等效性.

I am building a p2p application that accepts addresses from untrusted sources, so to prevent redundant connections, I need to be able to absolutely test for equivalence.

我的意图可以实现吗?如果是这样,怎么办?如果没有,我应该只使用v4吗?我宁愿现在包括将来的功能,也不愿以后再担心集成.

Can my intent be implemented? If so, how? If not, should I only use v4? I'd much rather include future capabilities now rather than worrying about integration later.

推荐答案

我认为您可以只使用ip::tcp::address类的operator==.

I think you can just use the ip::tcp::address classes' operator==.

查看 在Coliru上直播

See it Live On Coliru

#include <boost/asio.hpp>

using boost::asio::ip::address;

address from_string_ip6(std::string const& ip)
{
    address a = address::from_string(ip);
    if (a.is_v4())
        return boost::asio::ip::address_v6::v4_mapped(a.to_v4());
    else
        return a;
}

int main()
{
    address a = from_string_ip6("192.0.2.111");
    address b = from_string_ip6("::ffff:192.0.2.111");
    address c = from_string_ip6("::ffff:c000:026f");

    assert(a == b);
    assert(a == c);
    assert(b == c);
}

更新:如果要包括名称解析,请执行以下操作:

Update If you want to include name-resolution: ipv4 and ipv6 from any valid address

这篇关于IP地址v4/v6等效测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆