2

Ive ran into a slight "problem" if you can call it that. Im trying to learn subnets and make a subnet calculator. Everything is working but the problem arises when you get a netmask like 255.255.0.0 which gives you a possible ~65k ish addresses.

I programatically retrieve my IP and netmask and from those I calculate the first and the last possible IP address. After that I split them into two separate lists. After this I use two for loops like this:

        //Third octet
        else if (firstIpList[2].ToString() != lastIpList[2].ToString())
        {
            for (int i = Convert.ToInt32(firstIpList[2]); i < Convert.ToInt32(lastIpList[2]); i++)
            {
                for (int x = Convert.ToInt32(firstIpList[3]); x < Convert.ToInt32(lastIpList[3]); x++)
                {
                    ipRange.Add(firstIpList[0].ToString() + "." + firstIpList[1].ToString() + "." + i.ToString() + "." + x.ToString());
                }
            }
        }

This simply checks the 3rd entry in the list which is the third octet, the fourth octet is not that demanding due to the low amount of addresses and the second while being more demanding is less common so Im using the third as an example.

What this does is loop lets say 10.23.0 -> 255. and for each of those another 0 -> 255.

Yes I have the broadcast and ID in it but thats not the point, the point is that this is a loop that runs 65025 times which as you probably guess is very time consuming. Is there a way to make this more effective?

I was thinking of addrange at first since for 0 - 255 in the third octet I want to add 0-255 in the fourth aswell, so could I make another list which takes one copy of the fourth octet range and foreach number in the third octet addrange my list of fourth octets. But this would leave me with a list of items that look like 10.23.0 -> 255 and each of those items have 0->255 as subitems right?

Gvs
  • 267
  • 1
  • 6
  • 16
  • It's considerably easier to treat IP (v4) addresses and masks as 32bit integers. – spender Apr 10 '13 at 09:16
  • Yea probably is but I really didnt know how to do all the calculations to find the ip address and mask and at the time this was the only solution I could think of, not optimized or probably even good for what its for but its what I could come up with. Atleast it works, so far anyway :p. – Gvs Apr 10 '13 at 09:22

0 Answers0