views:

311

answers:

5

What is the easiest way to find the next unused IP address on a local adaptors subnet?

IPAddress GetNextFreeIP(NetworkInterface intface)
{
    ...?
}

Update
I've written a BOOTP/TFTP server that could be used in many different scenarios (cross-over cable, small private network, large corporate network). I have to give the small embedded system I'm updating an IP address and would like to find a spare one for it to use...

+1  A: 

If you have DHCP-managed IP ranges, you have an instance you can talk to - the DHCP server (like David recommended)

If you don't have managed IP ranges and no other instance you can talk to, there is no reliable way to tell if an IP is used or unused. IP does not offer such a service itself.

Thorsten79
+1 for DHCP, since this is **exactly** the problem that DHCP was designed to solve!
Aaronaught
Well, DHCP might be configured to give addresses from say 192.168.1.1/24 to 192.168.1.128/24, while addresses from 192.158.1.128/24 to 192.168.1.254/24 are assigned manually. What kind of information you're going to get from DHCP in this scenario?. The only correct answer to the original question is *"you can't"*, as @tuinstoelen and @Thorsten79 pointed out.
Igor Korkhov
A: 

The short answer is you can't, as Thorsten79 just said.

A slightly longer answer:

It depends on your network's configuration: how the admin has decided to allocate ip addresses. Usually there's a mix of manually assigned ip addresses for servers, routers etc, and a set of dhcp assigned ip addresses for workstations and such.

If you can talk to the dhcp server you might find out which addresses in the reserved range are free, but for the rest of the addresses you cannot find out.

The more interesting question is what you are trying to accomplish? Perhaps it can be realized in a different manner?

+1  A: 

Apart from asking the DHCP-server, I guess you could do a broadcast ping on the subnet and collect the responses from all computers and simply sort the IP-addresses to find the next one. This assumes, of course, that all devices are on-line and responds to broadcasts. So in a pretty controlled environment, this could work.

Martin Wickman
Problem is that in any sufficiently advanced network you can never know which hosts are up, which routers filter your ping packets etc. - There's just no reliable way for this in IP itself sadly.
Thorsten79
A: 

From what I've seen of embedded devices with a TFTP server, they boot up with a hard coded IP Address that is documented. After a set period of time (~3-10 seconds), the boot loader transfers control to the application that reads its configuration and sets the IP Address.

If someone wants to use the boot loader to load new firmware, then they need to read the documentation, make sure that IP Address is reachable (on the same subnet), reboot the device, and TFTP the application to the device.

Robert
This device uses BOOTP to get its IP address and TFTP Server address...
Tim
A: 

You are all technically right when you say there is no way to ensure an IP isn't being used. But I've decided to rely on this for now...

    public static IPAddress FindNextFree(this IPAddress address)
    {
        IPAddress workingAddress = address;
        Ping pingSender = new Ping();

        while (true)
        {
            byte[] localBytes = workingAddress.GetAddressBytes();

            localBytes[3]++;
            if (localBytes[3] > 254)
                localBytes[3] = 1;

            workingAddress = new IPAddress(localBytes);

            if (workingAddress.Equals(address))
                throw new TimeoutException("Could not find free IP address");

            PingReply reply = pingSender.Send(workingAddress, 1000);
            if (reply.Status != IPStatus.Success)
            {
                return workingAddress;
            }
        }
    }
Tim