C ++将IPv4映射到IPv6

时间:2020-05-25 09:11:30

标签: c++ boost

我有以下代码将IPv4映射到IPv6地址:

std::string ipV6Address;
boost::system::error_code status;
boost::asio::ip::address address = boost::asio::ip::address::from_string(ipStr, status);
if (status != boost::system::errc::success) {
    throw std::runtime_error("Cannot parse ip address: " + ipStr);
} else if (address.is_v6()) {
    ipV6Address = address.to_string();
} else if (address.is_v4()) {
    ipV6Address = boost::asio::ip::address_v6::v4_mapped(address.to_v4()).to_string();
}

std::cout << ipStr << = << ipV6Address << std::endl;

它与以下地址完美配合:

2607:f0d0:1002:0051:0000:0000:0000:0004 = 2607:f0d0:1002:51::4
127.0.0.1 = ::ffff:127.0.0.1
192.168.12.44 = ::ffff:192.168.12.44

但是它不适用于以下有效位地址:

2607:f0d0:1002:0051:0000:0000:0000:0004/32 -> exception
127.0.0.1/24  -> exception
192.168.12.44/24  -> exception

如何添加对有效位的支持?

0 个答案:

没有答案