0

From my understanding DHCP automatically assigns an IP address to a device. If i want to manually set the IP address of a device within the Host's (Windows Host) private network within the range 192.168.0.0 - 192.168.255.255 . How can i check if that address has been already taken.

I want to write a function that will increment through valid addresses with the private network range. If valid and not used then return that address. I need some way to implement the ip_is_valid(int*ip_addr) function.

bool assign_ip( int* ip_addr)
{
    int new_ip_addr[4] = {192,168,0,0};
    for (int i = 0; i < 255; i++)
    {
        new_ip_addr[2] = i
        for (int j = 0; j <255; j++)
        {
            new_ip_addr[3] = i;
            if (ip_is_valid(new_ip_addr))
            {
                ip_addr[0] = new_ip_addr[0];
                ip_addr[1] = new_ip_addr[1];
                ip_addr[2] = new_ip_addr[2];
                ip_addr[3] = new_ip_addr[3];
                return(true);
            }
        }
    }
    return(false);
}

What existing C++ library can i use in order to access currently released IPs or what simple way can i check to see if the IP is used (ping?).

EDIT: I think i may have miss worded my original question. The "Network" that i am using is just the PC's private addressing space. It is local and isolated. For example if i plug in an Ethernet device the PC would assign it a private IP address within the range 192.168.0.0 - 192.168.255.255. Only to be used by the host machine. The manual address i am setting is temporary. Will be used for less than a minute then discarded. I am not worried about any networking constraints or problems just how i can check if an ip has been used.

E-rap
  • 13
  • 4

2 Answers2

3

Don't do that. Even if it would work, you can't stop your DHCP server from assigning that address later to another machine.

You should limit the address range which is used by your DHCP server in the configuration. Then you can assign the rest of the adresses statically.

Sleafar
  • 1,486
  • 8
  • 10
  • This is for a temporary testing environment. The address will only be used within this check and the rest of the test and then reset moments afterwords. – E-rap Aug 10 '15 at 22:56
  • What it's for doesn't make any difference. Follow this advice. Don't create a time bomb. – user207421 Aug 10 '15 at 22:59
  • 1
    If you still aren't convinced, think what will happen if 2 machines in your network do this at the same time. You are asking for trouble. – Sleafar Aug 10 '15 at 23:00
  • Or think what will happen when somebody else runs this program in a couple of years' time. – user207421 Aug 10 '15 at 23:17
  • this is local. So within the host. Only accessible by the host i presume. Only things that have been given addresses are the Ethernet ports within the PC. otherwise addressing should be safe. I am not keeping the manual address with the device. I am checking if the manual config mode works properly. I Just want to check if it was in use before hand to make sure i do not conflict with a previously assigned ip. – E-rap Aug 10 '15 at 23:18
  • This is local within the DHCP subnet, and the executable file could end up anywhere. – user207421 Aug 10 '15 at 23:25
1

If you set the address of your device manually, a lot of weird things can happen:

  • you risk to use an existing address of a machine which is not yet connected. The DHCP leases can be given for 24 hours or more, and if the other machine is switched on again, it will get its adress again. So you have to PCs with the same IP address ! Imagine that you have the same telephone number than your neighbour: it would be a mess !

  • you risk to use an existing address of a machine which is hidden to you or don't anwser your request due to network security

  • you risk to use an adress which is incompatible with your network adressing scheme. Basically your machine will be completely isolated, as network neighbours and routers won't recognize this address. (Example: use of 192.168.0.0 range, when the network is in the 10.0.0.0, or use of an address that your router think is in a different subnet)

Basically, what you want to achieve won't work ! Not because of the C++ part, but because of network constraints.

If it's for experimenting at home, you can read about NMAP, which is a tool that scans network for existing addresses. The rough approach is to ping addresses and wait for returns. But as said security measures generally defeat this approach. In corporate environment, you shouldn't even try, as your PC would quickly be reported as suspicious to corporate security.

Community
  • 1
  • 1
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Thanks. So the best way to check if an address has been connected is through pining it. Is there a list stored somewhere that i can access which contains all the allocated ip addresses allocated by the host's local DHCP? – E-rap Aug 10 '15 at 23:39
  • The DHCP server must hold a list of leases. But the DHCP protocol doesn't foresse any querying, so it's only available on the server side, and the way to access it depends on the implementation stack. And don't forget: there could be several DHCP servers – Christophe Aug 10 '15 at 23:47