Merge pull request #1582 from Bond-009/ipnetwork

Include library via NuGet instead of via source
pull/1593/head
dkanada 5 years ago committed by GitHub
commit ca8e0796d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -20,6 +20,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="IPNetwork2" Version="2.4.0.126" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Server.Abstractions" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Hosting.Server.Abstractions" Version="2.2.0" />

@ -3,7 +3,6 @@ using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Emby.Server.Implementations.Networking;
using MediaBrowser.Model.Net; using MediaBrowser.Model.Net;
namespace Emby.Server.Implementations.Net namespace Emby.Server.Implementations.Net

@ -1,167 +0,0 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
namespace Emby.Server.Implementations.Networking.IPNetwork
{
/// <summary>
/// Extension methods to convert <see cref="BigInteger"/>
/// instances to hexadecimal, octal, and binary strings.
/// </summary>
public static class BigIntegerExtensions
{
/// <summary>
/// Converts a <see cref="BigInteger"/> to a binary string.
/// </summary>
/// <param name="bigint">A <see cref="BigInteger"/>.</param>
/// <returns>
/// A <see cref="string"/> containing a binary
/// representation of the supplied <see cref="BigInteger"/>.
/// </returns>
public static string ToBinaryString(this BigInteger bigint)
{
var bytes = bigint.ToByteArray();
var idx = bytes.Length - 1;
// Create a StringBuilder having appropriate capacity.
var base2 = new StringBuilder(bytes.Length * 8);
// Convert first byte to binary.
var binary = Convert.ToString(bytes[idx], 2);
// Ensure leading zero exists if value is positive.
if (binary[0] != '0' && bigint.Sign == 1)
{
base2.Append('0');
}
// Append binary string to StringBuilder.
base2.Append(binary);
// Convert remaining bytes adding leading zeros.
for (idx--; idx >= 0; idx--)
{
base2.Append(Convert.ToString(bytes[idx], 2).PadLeft(8, '0'));
}
return base2.ToString();
}
/// <summary>
/// Converts a <see cref="BigInteger"/> to a hexadecimal string.
/// </summary>
/// <param name="bigint">A <see cref="BigInteger"/>.</param>
/// <returns>
/// A <see cref="string"/> containing a hexadecimal
/// representation of the supplied <see cref="BigInteger"/>.
/// </returns>
public static string ToHexadecimalString(this BigInteger bigint)
{
return bigint.ToString("X");
}
/// <summary>
/// Converts a <see cref="BigInteger"/> to a octal string.
/// </summary>
/// <param name="bigint">A <see cref="BigInteger"/>.</param>
/// <returns>
/// A <see cref="string"/> containing an octal
/// representation of the supplied <see cref="BigInteger"/>.
/// </returns>
public static string ToOctalString(this BigInteger bigint)
{
var bytes = bigint.ToByteArray();
var idx = bytes.Length - 1;
// Create a StringBuilder having appropriate capacity.
var base8 = new StringBuilder(((bytes.Length / 3) + 1) * 8);
// Calculate how many bytes are extra when byte array is split
// into three-byte (24-bit) chunks.
var extra = bytes.Length % 3;
// If no bytes are extra, use three bytes for first chunk.
if (extra == 0)
{
extra = 3;
}
// Convert first chunk (24-bits) to integer value.
int int24 = 0;
for (; extra != 0; extra--)
{
int24 <<= 8;
int24 += bytes[idx--];
}
// Convert 24-bit integer to octal without adding leading zeros.
var octal = Convert.ToString(int24, 8);
// Ensure leading zero exists if value is positive.
if (octal[0] != '0')
{
if (bigint.Sign == 1)
{
base8.Append('0');
}
}
// Append first converted chunk to StringBuilder.
base8.Append(octal);
// Convert remaining 24-bit chunks, adding leading zeros.
for (; idx >= 0; idx -= 3)
{
int24 = (bytes[idx] << 16) + (bytes[idx - 1] << 8) + bytes[idx - 2];
base8.Append(Convert.ToString(int24, 8).PadLeft(8, '0'));
}
return base8.ToString();
}
/// <summary>
///
/// Reverse a Positive BigInteger ONLY
/// Bitwise ~ operator
///
/// Input : FF FF FF FF
/// Width : 4
/// Result : 00 00 00 00
///
///
/// Input : 00 00 00 00
/// Width : 4
/// Result : FF FF FF FF
///
/// Input : FF FF FF FF
/// Width : 8
/// Result : FF FF FF FF 00 00 00 00
///
///
/// Input : 00 00 00 00
/// Width : 8
/// Result : FF FF FF FF FF FF FF FF
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <returns></returns>
public static BigInteger PositiveReverse(this BigInteger input, int width)
{
var result = new List<byte>();
var bytes = input.ToByteArray();
var work = new byte[width];
Array.Copy(bytes, 0, work, 0, bytes.Length - 1); // Length -1 : positive BigInteger
for (int i = 0; i < work.Length; i++)
{
result.Add((byte)(~work[i]));
}
result.Add(0); // positive BigInteger
return new BigInteger(result.ToArray());
}
}
}

@ -1,94 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Numerics;
namespace Emby.Server.Implementations.Networking.IPNetwork
{
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
public BigInteger Count => this._ipnetwork.Total;
public IPAddress this[BigInteger i]
{
get
{
if (i >= this.Count)
{
throw new ArgumentOutOfRangeException(nameof(i));
}
byte width = this._ipnetwork.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork ? (byte)32 : (byte)128;
var ipn = this._ipnetwork.Subnet(width);
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 => this[this._enumerator];
#endregion
#region IDisposable Members
public void Dispose()
{
// nothing to dispose
return;
}
#endregion
#region IEnumerator Members
object IEnumerator.Current => this.Current;
public bool MoveNext()
{
this._enumerator++;
if (this._enumerator >= this.Count)
{
return false;
}
return true;
}
public void Reset()
{
this._enumerator = -1;
}
#endregion
#endregion
}
}

@ -1,129 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
namespace Emby.Server.Implementations.Networking.IPNetwork
{
public class IPNetworkCollection : IEnumerable<IPNetwork>, IEnumerator<IPNetwork>
{
private BigInteger _enumerator;
private byte _cidrSubnet;
private IPNetwork _ipnetwork;
private byte _cidr => this._ipnetwork.Cidr;
private BigInteger _broadcast => IPNetwork.ToBigInteger(this._ipnetwork.Broadcast);
private BigInteger _lastUsable => IPNetwork.ToBigInteger(this._ipnetwork.LastUsable);
private BigInteger _network => IPNetwork.ToBigInteger(this._ipnetwork.Network);
#if TRAVISCI
public
#else
internal
#endif
IPNetworkCollection(IPNetwork ipnetwork, byte cidrSubnet)
{
int maxCidr = ipnetwork.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork ? 32 : 128;
if (cidrSubnet > maxCidr)
{
throw new ArgumentOutOfRangeException(nameof(cidrSubnet));
}
if (cidrSubnet < ipnetwork.Cidr)
{
throw new ArgumentException("cidr");
}
this._cidrSubnet = cidrSubnet;
this._ipnetwork = ipnetwork;
this._enumerator = -1;
}
#region Count, Array, Enumerator
public BigInteger Count
{
get
{
var count = BigInteger.Pow(2, this._cidrSubnet - this._cidr);
return count;
}
}
public IPNetwork this[BigInteger i]
{
get
{
if (i >= this.Count)
{
throw new ArgumentOutOfRangeException(nameof(i));
}
var last = this._ipnetwork.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6
? this._lastUsable : this._broadcast;
var increment = (last - this._network) / this.Count;
var uintNetwork = this._network + ((increment + 1) * i);
var ipn = new IPNetwork(uintNetwork, this._ipnetwork.AddressFamily, this._cidrSubnet);
return ipn;
}
}
#endregion
#region IEnumerable Members
IEnumerator<IPNetwork> IEnumerable<IPNetwork>.GetEnumerator()
{
return this;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this;
}
#region IEnumerator<IPNetwork> Members
public IPNetwork Current => this[this._enumerator];
#endregion
#region IDisposable Members
public void Dispose()
{
// nothing to dispose
return;
}
#endregion
#region IEnumerator Members
object IEnumerator.Current => this.Current;
public bool MoveNext()
{
this._enumerator++;
if (this._enumerator >= this.Count)
{
return false;
}
return true;
}
public void Reset()
{
this._enumerator = -1;
}
#endregion
#endregion
}
}

@ -1,24 +0,0 @@
Copyright (c) 2015, lduchosal
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

@ -258,7 +258,7 @@ namespace Emby.Server.Implementations.Networking
if (normalizedSubnet.IndexOf('/') != -1) if (normalizedSubnet.IndexOf('/') != -1)
{ {
var ipnetwork = IPNetwork.IPNetwork.Parse(normalizedSubnet); var ipnetwork = IPNetwork.Parse(normalizedSubnet);
if (ipnetwork.Contains(address)) if (ipnetwork.Contains(address))
{ {
return true; return true;

Loading…
Cancel
Save