Ipv4Mask and Ipv6Prefix can be set to "wrong" values

The classes Ipv4Mask and Ipv6Prefix are ment to store a mask (or a prefix), and NOT something else.

Alas, you can do something silly like:

    Ipv4Mask test1 = Ipv4Mask("192.168.42.0");
    Ipv6Prefix test2 = Ipv6Prefix("2001:bd8::");

    std::cout << test1 << " " << test2 << std::endl;

and the output will be "192.168.42.0 /29"

The problem is that it seems correct, but it's not. The classes are used to bit-wise combine addresses, like "Ip1 & mask == Ip2 & mask", and using a mask that is not a "real" mask will lead to horrible bugs (and difficult to spot too).

Given that the ultimate goal might lead to a full class removal, in the meantime we should at least minimize the mistakes by checking in the constructors that the provided mask is a real mask.

The mockup code for Ipv4Mask is as simple as:

NS_ASSERT_MSG((std::countl_one(m_mask)+std::countr_zero(m_mask) == 32), "Invalid mask: " << m_mask);

The checks for IPv6 are a bit more complicated because the data are stored as an array of uint8_t. I'd gladly use uint128_t to store the daddress and the mask, but it's non-standard.

Assignee Loading
Time tracking Loading