How can I manipulate IP addresses (masking, or'ing, etc.) with asio as?
I have an application designed to be installed in multiple environments, each with the same relative IP addresses.
As an example, let's say there are three devices (it's much more complicated than this, but I've simplified it for simplicity). Displaying devices and environments by IP address looks like this:
Environ A, baseline = 192.0.0.0, range = 192.0.0.0 - 192.0.1.255
device-1 = OR:0.0.0.5 = 192.0.0.5
device-2 = OR:0.0.0.200 = 192.0.0.200
device-3 = OR:0.0.1.17 = 192.0.1.17
Environ B, baseline = 192.0.2.0, range = 192.0.2.0 - 192.0.3.255
device-1 = OR:0.0.0.5 = 192.0.2.5
device-2 = OR:0.0.0.200 = 192.0.2.200
device-3 = OR:0.0.1.17 = 192.0.3.17
Environ C, baseline = 192.0.4.0, range = 192.0.4.0 - 192.0.5.255
device-1 = OR:0.0.0.5 = 192.0.4.5
device-2 = OR:0.0.0.200 = 192.0.4.200
device-3 = OR:0.0.1.17 = 192.0.5.17
Environ D, baseline = 192.0.6.0, range = 192.0.6.0 - 192.0.7.255
device-1 = OR:0.0.0.5 = 192.0.6.5
device-2 = OR:0.0.0.200 = 192.0.6.200
device-3 = OR:0.0.1.17 = 192.0.7.17
In this case, each device will have an IP address or an address from 0.0.0.0
up to 0.0.1.255
inclusive (rightmost nine bits).
So the base IP for each medium is the leftmost 23 bits (as in the routing tables), while each device's IP address can be obtained by ORing it or the base IP address).
Thus, any given device can thus handle the IP addresses of all other devices in its environment (it gets the baseline from its own IP address). There is no need to communicate between environments.
So I have a few things I need to do (strong preference for using Boost as I want it to be portable across multiple platforms).
1 / I need to be able to access the local IP address of the local device with only one address in mind, so you don't need to worry about multi-NIC or ellipsis.
2 / I need to be able to zero out the rightmost N bits and then OR that matter with a different address.
What's the easiest way to accomplish these tasks? Once this is done, getting the string representation (or using the final address directly) should be easy.
source to share
You can use the class for this boost::asio::ip::address_v4
.
There are functions such as the fromunsigned long
and constructto_ulong()
that allow you to use all kinds of bit-bits in the raw number representation and change the results to address_v4
.
source to share