How-To manage a network
Using the liberate::net::network
class, you can manage the
assignment of individual addresses in the network. Typically, when that is
necessary, each address serves a particular purpose, which we can assign
ourselves.
1liberate::net::network net{"192.168.0.0/16"};
2
3auto addr = net.reserve_address("my first purpose");
The resulting address will not be the first address in the network, but
rather some random-seeming address from the available range. As a matter of
fact, the class hashes the input identifier
string, and picks an address
from the network that corresponds to the hash value. This means that for two
invocations with the same identifier, the same address should be returned.
That is, however, not quite the case. The reserve_address()
function not
only performs the mapping, but also makes the reservation, so that a subsequent
reservation with the same identifier fails. You can, however, release the
reservation before making it again.
1liberate::net::network net{"192.168.0.0/16"};
2
3auto addr1 = net.reserve_address("purpose");
4net.release_address(addr1);
5
6auto addr2 = net.reserve_address("purpose");
7assert(addr1 == addr2);
Of course, you can simply reserve addresses sequentially.
1liberate::net::network net{"192.168.0.0/16"};
2
3auto addr = net.gateway_address();
4net.reserve_address(addr); // => true
5
6++addr;
7net.reserve_address(addr); // => true
8
9// and so forth