2019-02-09 09:10:33 +00:00
|
|
|
using System;
|
2019-01-13 19:54:44 +00:00
|
|
|
using System.Collections;
|
2018-09-12 17:26:21 +00:00
|
|
|
using System.Collections.Generic;
|
2019-02-09 09:10:33 +00:00
|
|
|
using System.Net;
|
2018-09-12 17:26:21 +00:00
|
|
|
using System.Numerics;
|
|
|
|
|
2019-02-09 09:10:33 +00:00
|
|
|
namespace Emby.Server.Implementations.Networking.IPNetwork
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
public class IPAddressCollection : IEnumerable<IPAddress>, IEnumerator<IPAddress>
|
|
|
|
{
|
|
|
|
|
|
|
|
private IPNetwork _ipnetwork;
|
|
|
|
private BigInteger _enumerator;
|
|
|
|
|
|
|
|
internal IPAddressCollection(IPNetwork ipnetwork)
|
|
|
|
{
|
|
|
|
this._ipnetwork = ipnetwork;
|
|
|
|
this._enumerator = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region Count, Array, Enumerator
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
public BigInteger Count => this._ipnetwork.Total;
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
public IPAddress this[BigInteger i]
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (i >= this.Count)
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(i));
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
2019-02-09 09:10:33 +00:00
|
|
|
byte width = this._ipnetwork.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork ? (byte)32 : (byte)128;
|
2019-01-13 20:37:13 +00:00
|
|
|
var ipn = this._ipnetwork.Subnet(width);
|
2018-09-12 17:26:21 +00:00
|
|
|
return ipn[i].Network;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IEnumerable Members
|
|
|
|
|
|
|
|
IEnumerator<IPAddress> IEnumerable<IPAddress>.GetEnumerator()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
#region IEnumerator<IPNetwork> Members
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
public IPAddress Current => this[this._enumerator];
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IDisposable Members
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
// nothing to dispose
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IEnumerator Members
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
object IEnumerator.Current => this.Current;
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
public bool MoveNext()
|
|
|
|
{
|
|
|
|
this._enumerator++;
|
|
|
|
if (this._enumerator >= this.Count)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
{
|
|
|
|
this._enumerator = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
2019-01-13 19:30:03 +00:00
|
|
|
}
|