List IP all addresses in a subnet
Asked Answered
P

2

7

I need to get all of the IP addresses contained in within a subnet and I'm trying to do it using IPnetwork

For example the subnet 192.168.1.0/29 would have the following output:

        // Output
        // 192.168.1.0
        // 192.168.1.1
        // 192.168.1.2
        // 192.168.1.3
        // 192.168.1.4
        // 192.168.1.5
        // 192.168.1.6
        // 192.168.1.7

Here is my code:

        IPNetwork ipn = IPNetwork.Parse("192.168.1.0/29");
        IPAddressCollection ips = IPNetwork.ListIPAddress(ipn);

        foreach (IPAddress ip in ips)
        {
            Console.WriteLine(ip);
        }

        // Output
        // 192.168.1.0
        // 192.168.1.0
        // 192.168.1.0
        // 192.168.1.0
        // 192.168.1.0
        // 192.168.1.0

As you can see, this is not the desired result. What am I missing? Is there another tool or method to get this job done? I have manage to hack something up, but it ain't pretty and I'm not sure if it's properly enumerating larger subnets.

Pentothal answered 10/7, 2010 at 15:23 Comment(2)
This looks like a bug in the ipnetwork library you're using.Manor
I was hoping to latch on to somebody that has used this library in the past without having to fix the guy's code...... It get's recommendations a lot for folks wanting do c# subnetting, so I guess I was fishing for the fix ;).Pentothal
O
10

ipnetwork library has been updated (to version 1.3.1) with patch and a testunit covering this issue. It can be downloaded at IPnetwork

Oleaceous answered 22/7, 2010 at 7:59 Comment(2)
thanks for the update. I'm happy to accept your answer. Oh, and the library is working great, thanks so much for making it available.Pentothal
You're welcome. I'm glad to hear about people using it. Regards.Oleaceous
P
2

I fixed the code in the IPAddressCollection class. It will now show all IP addresses in the subnet including network, gateway, broadcast. For example, a /29 would return ips .1 - .7.

Here's the amended fix.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections;

namespace LukeSkywalker.IPNetwork
{
    public class IPAddressCollection : IEnumerable<IPAddress>, IEnumerator<IPAddress>
    {
        private IPNetwork _ipnetwork;
        private double _enumerator;

        internal IPAddressCollection(IPNetwork ipnetwork)
        {
            this._ipnetwork = ipnetwork;
            this._enumerator = -1;
        }

        #region Count, Array, Enumerator

        public double Count
        {
            get
            {
                // return this._ipnetwork.Usable;
                return this._ipnetwork.Usable + 2;
            }
        }

        public IPAddress this[double i]
        {
            get
            {
                if (i >= this.Count)
                {
                    throw new ArgumentOutOfRangeException("i");
                }

                IPNetworkCollection ipn = IPNetwork.Subnet(this._ipnetwork, 32);

                // return ipn[0].Network;

                return ipn[i].Network;
            }
        }

        #endregion

        #region IEnumerable Members

        IEnumerator<IPAddress> IEnumerable<IPAddress>.GetEnumerator()
        {
            return this;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this;
        }

        #region IEnumerator<IPNetwork> Members

        public IPAddress Current
        {
            get { return this[this._enumerator]; }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            // nothing to dispose
            return;
        }

        #endregion

        #region IEnumerator Members

        object IEnumerator.Current
        {
            get { return this.Current; }
        }

        public bool MoveNext()
        {
            this._enumerator++;
            if (this._enumerator >= this.Count)
            {
                return false;
            }
            return true;
        }

        public void Reset()
        {
            this._enumerator = -1;
        }

        #endregion

        #endregion
    }
}
Pentothal answered 12/7, 2010 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.