开发者

Validating IPv6 netmask prefix

开发者 https://www.devze.com 2023-04-11 03:11 出处:网络
I\'ve a requirement to validate a certain IPv6 prefix. I learned that inet_pton() can be used to validate the IPv6 address itself, but it does not accept the prefix length along with the address (fec1

I've a requirement to validate a certain IPv6 prefix. I learned that inet_pton() can be used to validate the IPv6 address itself, but it does not accept the prefix length along with the address (fec1::9/96).

How开发者_开发技巧 can the IPv6 prefix be validated in C/C++?

Thanks!


IPv6 have a simple format. You may do it by parsing IPv6 string.

This may help you:

  • A Recommendation for IPv6 Address Text Representation(RFC5952)
  • IPv6 (wiki)


// @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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消