Search code examples
javajavascriptipipv4

Getting IP address list between two IP addresses


I am writing a program that gets From IP address and To IP address from the user and displays the list of IP addresses between them. For example, if the user gives 10.0.0.1 and 10.0.0.5 then I will display the five IP addresses between these two. The current solution that is coming in my mind are:

  1. To have a list of all IP addresses and then look for the resultant IP address list
  2. Use a nested loop

What solution should I adopt between these (or suggest a better solution)? For the first solution, what is the link for IP address table/list?

What is the solution in terms of JavaScript or Java?


Solution

  • First split the IP addresses with .. From the first IP address, start increasing the fourth part up to 255 and then add 1 to the third part and set the fourth one to 1. Until you reach the to IP address.

    • IP address bytes -> bits -> Int32
    • From: 10.0.10.10 -> 00001010 00000000 00001010 00001010 -> 167774730
    • To: 10.1.45.1 -> 00001010 00000001 00101101 00000001 -> 167849217
    • Start count from From to To and just check the unwanted bytes which is 11111111 and 00000000.

    That's all.