3

I have an application that is meant to be installed into multiple environments, each with the same relative IP addresses.

By way of example, let's say there are three devices (it's much more complicated than this but I've pared it down for simplicity). The mapping of devices and environments to IP addresses is as follows:

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 scenario, every device would have an IP "or-address" of 0.0.0.0 through 0.0.1.255 inclusive (the rightmost nine bits).

The baseline IP for each environment is therefore the leftmost 23 bits (much like found in routing tables) while each device IP address can be obtained by ORing it's or-address with the baseline IP).

Any given device can therefore work out the IP addresses of all other devices in its environment in this manner (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 since I want it to be portable across multiple platforms).

1/ I need to be able to get the current device's local IP address, keeping in mind there's only one address, so no having to worry about multi-NIC or multi-homing.

2/ I need to be able to zero out the rightmost N bits and then OR that value with another address.

What's the easiest way to do these tasks? Once done, getting the string representation (or using the final address directly) should be easy.

kenba
  • 4,303
  • 1
  • 23
  • 40
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Doesn't serve you [this](http://www.boost.org/doc/libs/1_64_0/doc/html/boost_asio/reference/ip__address_v4.html) well enough? – πάντα ῥεῖ Jun 27 '17 at 04:26
  • @πάνταῥεῖ - I see! I was looking at `address` itself which doesn't have the `to_ulong()`. The IPv4 one *does* have this so, once I have a ulong, I can bit-fiddle it easily. Thanks (you should make it an answer by the way). Or I can just delete it if the swarm thinks it's not worthwhile keeping - I'll check back in a few hours for their verdict. – paxdiablo Jun 27 '17 at 04:37
  • 1
    Well, I'm refraining a bit to give answers for low researched questions :-D. But I have to admit we have at least no easy to find dupe. – πάντα ῥεῖ Jun 27 '17 at 04:39

1 Answers1

4

You can use the boost::asio::ip::address_v4 class to do so.

There are functions like construction from unsigned long and to_ulong() that allow you all kinds of bit fiddling on a raw number representation, and change the results back to address_v4.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190