-5

I'm receiving from an external Application (which I can't change) and creating Strings from them:

string startAddress = "192.168.10.10";
string endAddress = "192.168.10.20";

What I would like to achieve is to put every IP between those into an array or something similar so I can loop through them.

Here is a dirty way of managing this as I know the IPs should not leave the 192.168.10.0/24 subnet.

    string startAddress = networkRange.from;
    string endAddress = networkRange.to;

    string startAddressPartial = startAddress.Substring(startAddress.LastIndexOf('.') + 1);
    string lastAddressPartial = endAddress.Substring(endAddress.LastIndexOf('.') + 1);

    int startAddressInt = Int32.Parse(startAddressPartial);
    int lastAddressInt = Int32.Parse(lastAddressPartial);

    var networkIPArray = Enumerable.Range(startAddressInt, lastAddressInt).ToArray();

    foreach (int ip in networkIPArray)
    {
        string ipOk = "192.168.10." + ip;
        Console.WriteLine(ipOk);
    }

Thanks,

rgomez
  • 123
  • 2
  • 15
  • 1
    You say you've tried something but haven't shown it. People here won't write the code for you, show us what you've tried. – TheLethalCoder Mar 22 '17 at 17:20
  • 1
    You should post what you have tried. We're here to help, not work for you. – webnoob Mar 22 '17 at 17:20
  • You don't need to store them in an array to loop over them. You could write a simple enumerator class that doesn't need to actually store all the intermediate values. – hatchet - done with SOverflow Mar 22 '17 at 17:22
  • You need to at least explain the scope before getting a meaningful answer. What's the network mask? – itsme86 Mar 22 '17 at 17:25
  • 2
    Look at this - and have a think about it http://stackoverflow.com/questions/461742/how-to-convert-an-ipv4-address-into-a-integer-in-c – Steve Mar 22 '17 at 17:25
  • I've added the code I've tried, I've not done that before as I understand this is not the correct way to manage this. – rgomez Mar 22 '17 at 17:47

1 Answers1

0

behold, LINQ

// don't need to know which is start or end, assuming string compare works as expected.
var addresses = new List<string>() { "192.168.10.10", "192.168.10.20" };

// convert string to a 32 bit int
Func<string, int> IpToInt = s => {
    return (
            // declare a working object
            new List<List<int>>() { 
                // chop the ip string into 4 ints
                s.Split('.').Select(x => int.Parse(x)).ToList()
            // and from the working object, return ...
        }).Select(y =>
            // ... the ip, as a 32 bit int. Note that endieness is wrong (but that doesn't matter), and it's signed.
            (y.ElementAt(0) << 24) + (y.ElementAt(1) << 16) + (y.ElementAt(2) << 8) + y.ElementAt(3)
        ).First();
};

// start range 
Enumerable.Range(0, 
    // address space is the different between the large and smaller ... which should be positive
    IpToInt(addresses.Max()) - IpToInt(addresses.Min()) + 1).ToList().ForEach(x => { 
        // use as necessary
        Console.WriteLine(
            // declare a working object 
            (new List<int>() {
                // start address added with current iteration
                IpToInt(addresses.Min()) + x 
            }).Select(n =>
                // reparse from an int back into a string
                String.Join(".", 
                    // first, convert the 32 bit int back into a list of four
                    (new List<int>(){ (n >> 24) & 0xff, (n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff })
                    // and make it a string for the join
                    .Select(y => y.ToString())
                )
            ).First()
        );
});
BurnsBA
  • 2,890
  • 16
  • 29
  • I could understand the code and yes it works as expected. The issue is when for example you go from `192.168.10.10` to `192.168.10.100` I guess it's because it's considering the wrong Max and Min. – rgomez Mar 22 '17 at 19:53
  • @rgomez `new List() { "192.168.10.10", "192.168.10.100" }.Min()` (and Max() ) works for me – BurnsBA Mar 23 '17 at 14:53