我需要验证某个IPv6前缀。我了解到inet_pton()可用于验证IPv6地址本身,但它不接受前缀长度和地址(fec1 :: 9/96)。
如何在C / C ++中验证IPv6前缀?
谢谢!
答案 0 :(得分:1)
IPv6的格式简单。您可以通过解析IPv6字符串来完成它。
这可能会对您有所帮助:
答案 1 :(得分:1)
// @param[in] cidr_ - contains an IPv6 network address (as a string) and a routing prefix
bool valid_ipv6_cidr(std::pair<std::string, uint16_t> const& cidr_) {
if(cidr_.first.find(':') != std::string::npos) {
_error_msg = "IPv6 address does not contain a ':'";
return false;
}
struct in6_addr addr6;
if(!inet_pton(AF_INET6, cidr_.first.c_str(), &(addr6.s6_addr))) {
_error_msg = "invalid IPv6 address";
return false;
}
uint16_t routing_prefix = cidr_.second;
if(routing_prefix > MAX_IPV6_CIDR_MASK) { // MAX_IPV6_CIDR_MASK = 128
_error_msg = "invalid IPv6 CIDR mask";
return false;
}
for(size_t i=0; i<16; i++) {
uint8_t byte_netmask = 0xff;
if(routing_prefix >= 8) {
routing_prefix -= 8;
} else if(routing_prefix == 0) {
byte_netmask = 0;
} else { // routing_prefix is between 1 and 7, inclusive
byte_netmask <<= (8 - routing_prefix);
routing_prefix = 0;
}
//std::cout << "DEBUG: mask=" << std::hex
// << static_cast<unsigned>(byte_netmask) << std::dec << std::endl;
if( (addr6.s6_addr[i] & byte_netmask) != addr6.s6_addr[i] ) {
std::ostringstream oss;
oss << "invalid CIDR: " << cidr_.first << '/'
<< cidr_.second << " - mask (" << std::hex
<< static_cast<unsigned>(byte_netmask) << std::dec
<< ") failed at byte "
<< i << " (" << std::hex
<< static_cast<unsigned>(addr6.s6_addr[i]) << std::dec << ')';
_error_msg = oss.str();
return false;
}
}
// check for special case ..../0
if( (cidr_.second == 0) && (cidr_.first != "::") ) {
_error_msg = "invalid CIDR - /0 found with something other than ::";
return false;
}
return true;
}