diff options
| author | Erwin de Haan <EraYaN@users.noreply.github.com> | 2019-01-06 21:50:43 +0100 |
|---|---|---|
| committer | Erwin de Haan <EraYaN@users.noreply.github.com> | 2019-01-10 20:38:53 +0100 |
| commit | ec1f5dc317182582ebff843c9e8a4d5277405469 (patch) | |
| tree | 6514de336cc9aa94becb3fbd767285dfa61d0b1b /Emby.Server.Implementations/Networking | |
| parent | 3d867c2c46cec39b669bb8647efef677f32b8a8d (diff) | |
Mayor code cleanup
Add Argument*Exceptions now use proper nameof operators.
Added exception messages to quite a few Argument*Exceptions.
Fixed rethorwing to be proper syntax.
Added a ton of null checkes. (This is only a start, there are about 500 places that need proper null handling)
Added some TODOs to log certain exceptions.
Fix sln again.
Fixed all AssemblyInfo's and added proper copyright (where I could find them)
We live in *current year*.
Fixed the use of braces.
Fixed a ton of properties, and made a fair amount of functions static that should be and can be static.
Made more Methods that should be static static.
You can now use static to find bad functions!
Removed unused variable. And added one more proper XML comment.
Diffstat (limited to 'Emby.Server.Implementations/Networking')
4 files changed, 79 insertions, 156 deletions
diff --git a/Emby.Server.Implementations/Networking/IPNetwork/IPAddressCollection.cs b/Emby.Server.Implementations/Networking/IPNetwork/IPAddressCollection.cs index 2b31a0a32..7fe6a8d3d 100644 --- a/Emby.Server.Implementations/Networking/IPNetwork/IPAddressCollection.cs +++ b/Emby.Server.Implementations/Networking/IPNetwork/IPAddressCollection.cs @@ -19,13 +19,7 @@ namespace System.Net #region Count, Array, Enumerator - public BigInteger Count - { - get - { - return this._ipnetwork.Total; - } - } + public BigInteger Count => this._ipnetwork.Total; public IPAddress this[BigInteger i] { @@ -33,7 +27,7 @@ namespace System.Net { if (i >= this.Count) { - throw new ArgumentOutOfRangeException("i"); + throw new ArgumentOutOfRangeException(nameof(i)); } byte width = this._ipnetwork.AddressFamily == Sockets.AddressFamily.InterNetwork ? (byte)32 : (byte)128; IPNetworkCollection ipn = this._ipnetwork.Subnet(width); @@ -57,10 +51,7 @@ namespace System.Net #region IEnumerator<IPNetwork> Members - public IPAddress Current - { - get { return this[this._enumerator]; } - } + public IPAddress Current => this[this._enumerator]; #endregion @@ -76,10 +67,7 @@ namespace System.Net #region IEnumerator Members - object IEnumerator.Current - { - get { return this.Current; } - } + object IEnumerator.Current => this.Current; public bool MoveNext() { diff --git a/Emby.Server.Implementations/Networking/IPNetwork/IPNetwork.cs b/Emby.Server.Implementations/Networking/IPNetwork/IPNetwork.cs index 8d0fb7997..ba9cfff55 100644 --- a/Emby.Server.Implementations/Networking/IPNetwork/IPNetwork.cs +++ b/Emby.Server.Implementations/Networking/IPNetwork/IPNetwork.cs @@ -41,44 +41,19 @@ namespace System.Net /// <summary> /// Network address /// </summary> - public IPAddress Network - { - get - { - - return IPNetwork.ToIPAddress(this._network, this._family); - } - } + public IPAddress Network => IPNetwork.ToIPAddress(this._network, this._family); /// <summary> /// Address Family /// </summary> - public AddressFamily AddressFamily - { - get - { - return this._family; - } - } + public AddressFamily AddressFamily => this._family; - private BigInteger _netmask - { - get - { - return IPNetwork.ToUint(this._cidr, this._family); - } - } + private BigInteger _netmask => IPNetwork.ToUint(this._cidr, this._family); /// <summary> /// Netmask /// </summary> - public IPAddress Netmask - { - get - { - return IPNetwork.ToIPAddress(this._netmask, this._family); - } - } + public IPAddress Netmask => IPNetwork.ToIPAddress(this._netmask, this._family); private BigInteger _broadcast { @@ -171,13 +146,7 @@ namespace System.Net /// <summary> /// The CIDR netmask notation /// </summary> - public byte Cidr - { - get - { - return this._cidr; - } - } + public byte Cidr => this._cidr; #endregion @@ -195,7 +164,7 @@ namespace System.Net int maxCidr = family == Sockets.AddressFamily.InterNetwork ? 32 : 128; if (cidr > maxCidr) { - throw new ArgumentOutOfRangeException("cidr"); + throw new ArgumentOutOfRangeException(nameof(cidr)); } this._ipaddress = ipaddress; @@ -427,7 +396,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentNullException("ipaddress"); + throw new ArgumentNullException(nameof(ipaddress)); } ipnetwork = null; return; @@ -437,7 +406,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentNullException("netmask"); + throw new ArgumentNullException(nameof(netmask)); } ipnetwork = null; return; @@ -477,7 +446,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentNullException("network"); + throw new ArgumentNullException(nameof(network)); } ipnetwork = null; return; @@ -538,7 +507,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentNullException("ipaddress"); + throw new ArgumentNullException(nameof(ipaddress)); } ipnetwork = null; return; @@ -548,7 +517,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentNullException("netmask"); + throw new ArgumentNullException(nameof(netmask)); } ipnetwork = null; return; @@ -596,7 +565,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentNullException("ipaddress"); + throw new ArgumentNullException(nameof(ipaddress)); } ipnetwork = null; return; @@ -680,7 +649,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentNullException("ipaddress"); + throw new ArgumentNullException(nameof(ipaddress)); } uintIpAddress = null; return; @@ -751,7 +720,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentOutOfRangeException("cidr"); + throw new ArgumentOutOfRangeException(nameof(cidr)); } uintNetmask = null; return; @@ -761,7 +730,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentOutOfRangeException("cidr"); + throw new ArgumentOutOfRangeException(nameof(cidr)); } uintNetmask = null; return; @@ -872,7 +841,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentNullException("netmask"); + throw new ArgumentNullException(nameof(netmask)); } cidr = null; return; @@ -976,7 +945,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentOutOfRangeException("cidr"); + throw new ArgumentOutOfRangeException(nameof(cidr)); } netmask = null; return; @@ -1042,7 +1011,7 @@ namespace System.Net if (netmask == null) { - throw new ArgumentNullException("netmask"); + throw new ArgumentNullException(nameof(netmask)); } BigInteger uintNetmask = IPNetwork.ToBigInteger(netmask); bool valid = IPNetwork.InternalValidNetmask(uintNetmask, netmask.AddressFamily); @@ -1145,7 +1114,7 @@ namespace System.Net if (ipaddress == null) { - throw new ArgumentNullException("ipaddress"); + throw new ArgumentNullException(nameof(ipaddress)); } if (AddressFamily != ipaddress.AddressFamily) @@ -1174,7 +1143,7 @@ namespace System.Net if (network2 == null) { - throw new ArgumentNullException("network2"); + throw new ArgumentNullException(nameof(network2)); } BigInteger uintNetwork = _network; @@ -1203,7 +1172,7 @@ namespace System.Net if (network2 == null) { - throw new ArgumentNullException("network2"); + throw new ArgumentNullException(nameof(network2)); } BigInteger uintNetwork = _network; @@ -1242,37 +1211,19 @@ namespace System.Net /// 10.0.0.0/8 /// </summary> /// <returns></returns> - public static IPNetwork IANA_ABLK_RESERVED1 - { - get - { - return _iana_ablock_reserved.Value; - } - } + public static IPNetwork IANA_ABLK_RESERVED1 => _iana_ablock_reserved.Value; /// <summary> /// 172.12.0.0/12 /// </summary> /// <returns></returns> - public static IPNetwork IANA_BBLK_RESERVED1 - { - get - { - return _iana_bblock_reserved.Value; - } - } + public static IPNetwork IANA_BBLK_RESERVED1 => _iana_bblock_reserved.Value; /// <summary> /// 192.168.0.0/16 /// </summary> /// <returns></returns> - public static IPNetwork IANA_CBLK_RESERVED1 - { - get - { - return _iana_cblock_reserved.Value; - } - } + public static IPNetwork IANA_CBLK_RESERVED1 => _iana_cblock_reserved.Value; /// <summary> /// return true if ipaddress is contained in @@ -1285,7 +1236,7 @@ namespace System.Net if (ipaddress == null) { - throw new ArgumentNullException("ipaddress"); + throw new ArgumentNullException(nameof(ipaddress)); } return IPNetwork.IANA_ABLK_RESERVED1.Contains(ipaddress) @@ -1356,7 +1307,7 @@ namespace System.Net { if (trySubnet == false) { - throw new ArgumentNullException("network"); + throw new ArgumentNullException(nameof(network)); } ipnetworkCollection = null; return; @@ -1367,7 +1318,7 @@ namespace System.Net { if (trySubnet == false) { - throw new ArgumentOutOfRangeException("cidr"); + throw new ArgumentOutOfRangeException(nameof(cidr)); } ipnetworkCollection = null; return; @@ -1438,7 +1389,7 @@ namespace System.Net { if (trySupernet == false) { - throw new ArgumentNullException("network1"); + throw new ArgumentNullException(nameof(network1)); } supernet = null; return; @@ -1448,7 +1399,7 @@ namespace System.Net { if (trySupernet == false) { - throw new ArgumentNullException("network2"); + throw new ArgumentNullException(nameof(network2)); } supernet = null; return; @@ -1492,7 +1443,7 @@ namespace System.Net { if (trySupernet == false) { - throw new ArgumentOutOfRangeException("network"); + throw new ArgumentOutOfRangeException(nameof(trySupernet), "TrySupernet was false while the first and last networks are not adjacent."); } supernet = null; return; @@ -1536,8 +1487,7 @@ namespace System.Net /// 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23 /// 192.168.0.0/24 + 192.168.1.0/24 + 192.168.2.0/24 + 192.168.3.0/24 = 192.168.0.0/22 /// </summary> - /// <param name="ipnetworks"></param> - /// <param name="supernet"></param> + /// <param name="ipnetworks">The IP networks</param> /// <returns></returns> public static IPNetwork[] Supernet(IPNetwork[] ipnetworks) { @@ -1573,7 +1523,7 @@ namespace System.Net { if (trySupernet == false) { - throw new ArgumentNullException("ipnetworks"); + throw new ArgumentNullException(nameof(ipnetworks)); } supernet = null; return false; @@ -1684,12 +1634,12 @@ namespace System.Net if (string.IsNullOrEmpty(start)) { - throw new ArgumentNullException("start"); + throw new ArgumentNullException(nameof(start)); } if (string.IsNullOrEmpty(end)) { - throw new ArgumentNullException("end"); + throw new ArgumentNullException(nameof(end)); } IPAddress startIP; @@ -1750,7 +1700,7 @@ namespace System.Net { if (tryWide == false) { - throw new ArgumentNullException("ipnetworks"); + throw new ArgumentNullException(nameof(ipnetworks)); } ipnetwork = null; return; @@ -1973,7 +1923,7 @@ namespace System.Net #region IComparable<IPNetwork> Members - public static Int32 Compare(IPNetwork left, IPNetwork right) + public static int Compare(IPNetwork left, IPNetwork right) { // two null IPNetworks are equal if (ReferenceEquals(left, null) && ReferenceEquals(right, null)) return 0; @@ -1994,12 +1944,12 @@ namespace System.Net return result; } - public Int32 CompareTo(IPNetwork other) + public int CompareTo(IPNetwork other) { return Compare(this, other); } - public Int32 CompareTo(Object obj) + public int CompareTo(object obj) { // null is at less if (obj == null) return 1; @@ -2012,7 +1962,7 @@ namespace System.Net { throw new ArgumentException( "The supplied parameter is an invalid type. Please supply an IPNetwork type.", - "obj"); + nameof(obj)); } // perform the comparision @@ -2023,17 +1973,17 @@ namespace System.Net #region IEquatable<IPNetwork> Members - public static Boolean Equals(IPNetwork left, IPNetwork right) + public static bool Equals(IPNetwork left, IPNetwork right) { return Compare(left, right) == 0; } - public Boolean Equals(IPNetwork other) + public bool Equals(IPNetwork other) { return Equals(this, other); } - public override Boolean Equals(Object obj) + public override bool Equals(object obj) { return Equals(this, obj as IPNetwork); } @@ -2042,22 +1992,22 @@ namespace System.Net #region Operators - public static Boolean operator ==(IPNetwork left, IPNetwork right) + public static bool operator ==(IPNetwork left, IPNetwork right) { return Equals(left, right); } - public static Boolean operator !=(IPNetwork left, IPNetwork right) + public static bool operator !=(IPNetwork left, IPNetwork right) { return !Equals(left, right); } - public static Boolean operator <(IPNetwork left, IPNetwork right) + public static bool operator <(IPNetwork left, IPNetwork right) { return Compare(left, right) < 0; } - public static Boolean operator >(IPNetwork left, IPNetwork right) + public static bool operator >(IPNetwork left, IPNetwork right) { return Compare(left, right) > 0; } diff --git a/Emby.Server.Implementations/Networking/IPNetwork/IPNetworkCollection.cs b/Emby.Server.Implementations/Networking/IPNetwork/IPNetworkCollection.cs index 35cff88dc..48d404124 100644 --- a/Emby.Server.Implementations/Networking/IPNetwork/IPNetworkCollection.cs +++ b/Emby.Server.Implementations/Networking/IPNetwork/IPNetworkCollection.cs @@ -11,22 +11,12 @@ namespace System.Net private byte _cidrSubnet; private IPNetwork _ipnetwork; - private byte _cidr - { - get { return this._ipnetwork.Cidr; } - } - private BigInteger _broadcast - { - get { return IPNetwork.ToBigInteger(this._ipnetwork.Broadcast); } - } - private BigInteger _lastUsable - { - get { return IPNetwork.ToBigInteger(this._ipnetwork.LastUsable); } - } - private BigInteger _network - { - get { return IPNetwork.ToBigInteger(this._ipnetwork.Network); } - } + 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 @@ -38,7 +28,7 @@ namespace System.Net int maxCidr = ipnetwork.AddressFamily == Sockets.AddressFamily.InterNetwork ? 32 : 128; if (cidrSubnet > maxCidr) { - throw new ArgumentOutOfRangeException("cidrSubnet"); + throw new ArgumentOutOfRangeException(nameof(cidrSubnet)); } if (cidrSubnet < ipnetwork.Cidr) @@ -68,7 +58,7 @@ namespace System.Net { if (i >= this.Count) { - throw new ArgumentOutOfRangeException("i"); + throw new ArgumentOutOfRangeException(nameof(i)); } BigInteger last = this._ipnetwork.AddressFamily == Sockets.AddressFamily.InterNetworkV6 @@ -96,10 +86,7 @@ namespace System.Net #region IEnumerator<IPNetwork> Members - public IPNetwork Current - { - get { return this[this._enumerator]; } - } + public IPNetwork Current => this[this._enumerator]; #endregion @@ -115,10 +102,7 @@ namespace System.Net #region IEnumerator Members - object IEnumerator.Current - { - get { return this.Current; } - } + object IEnumerator.Current => this.Current; public bool MoveNext() { diff --git a/Emby.Server.Implementations/Networking/NetworkManager.cs b/Emby.Server.Implementations/Networking/NetworkManager.cs index 260d20e35..dbf89d7e1 100644 --- a/Emby.Server.Implementations/Networking/NetworkManager.cs +++ b/Emby.Server.Implementations/Networking/NetworkManager.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Globalization; using System.Linq; @@ -114,7 +114,7 @@ namespace Emby.Server.Implementations.Networking .ToList(); } - private bool FilterIpAddress(IPAddress address) + private static bool FilterIpAddress(IPAddress address) { var addressString = address.ToString(); @@ -228,7 +228,7 @@ namespace Emby.Server.Implementations.Networking subnet_Test++; } - var subnet_Match = String.Join(".", unicastIPAddressInformation.Address.ToString().Split('.').Take(subnet_Test).ToArray()); + var subnet_Match = string.Join(".", unicastIPAddressInformation.Address.ToString().Split('.').Take(subnet_Test).ToArray()); // TODO: Is this check necessary? if (adapter.OperationalStatus == OperationalStatus.Up) @@ -245,7 +245,7 @@ namespace Emby.Server.Implementations.Networking } } - private bool Is172AddressPrivate(string endpoint) + private static bool Is172AddressPrivate(string endpoint) { for (var i = 16; i <= 31; i++) { @@ -268,7 +268,7 @@ namespace Emby.Server.Implementations.Networking return IsAddressInSubnets(IPAddress.Parse(addressString), addressString, subnets); } - private bool IsAddressInSubnets(IPAddress address, string addressString, string[] subnets) + private static bool IsAddressInSubnets(IPAddress address, string addressString, string[] subnets) { foreach (var subnet in subnets) { @@ -296,7 +296,7 @@ namespace Emby.Server.Implementations.Networking { if (string.IsNullOrEmpty(endpoint)) { - throw new ArgumentNullException("endpoint"); + throw new ArgumentNullException(nameof(endpoint)); } IPAddress address; @@ -380,7 +380,7 @@ namespace Emby.Server.Implementations.Networking return false; } - private Task<IPAddress[]> GetIpAddresses(string hostName) + private static Task<IPAddress[]> GetIpAddresses(string hostName) { return Dns.GetHostAddressesAsync(hostName); } @@ -436,7 +436,7 @@ namespace Emby.Server.Implementations.Networking .ToList(); } - private async Task<IEnumerable<IPAddress>> GetLocalIpAddressesFallback() + private static async Task<IEnumerable<IPAddress>> GetLocalIpAddressesFallback() { var host = await Dns.GetHostEntryAsync(Dns.GetHostName()).ConfigureAwait(false); @@ -480,7 +480,7 @@ namespace Emby.Server.Implementations.Networking return _macAddresses; } - private List<string> GetMacAddressesInternal() + private static List<string> GetMacAddressesInternal() { return NetworkInterface.GetAllNetworkInterfaces() .Where(i => i.NetworkInterfaceType != NetworkInterfaceType.Loopback) @@ -497,8 +497,9 @@ namespace Emby.Server.Implementations.Networking return physicalAddress.ToString(); } - catch (Exception ex) + catch (Exception) { + //TODO Log exception. return null; } }) @@ -526,7 +527,7 @@ namespace Emby.Server.Implementations.Networking /// <exception cref="System.FormatException"></exception> private static async Task<IPEndPoint> Parse(string endpointstring, int defaultport) { - if (String.IsNullOrEmpty(endpointstring) + if (string.IsNullOrEmpty(endpointstring) || endpointstring.Trim().Length == 0) { throw new ArgumentException("Endpoint descriptor may not be empty."); @@ -536,7 +537,7 @@ namespace Emby.Server.Implementations.Networking (defaultport < IPEndPoint.MinPort || defaultport > IPEndPoint.MaxPort)) { - throw new ArgumentException(String.Format("Invalid default port '{0}'", defaultport)); + throw new ArgumentException(string.Format("Invalid default port '{0}'", defaultport)); } string[] values = endpointstring.Split(new char[] { ':' }); @@ -557,7 +558,7 @@ namespace Emby.Server.Implementations.Networking //could [a:b:c]:d if (values[0].StartsWith("[") && values[values.Length - 2].EndsWith("]")) { - string ipaddressstring = String.Join(":", values.Take(values.Length - 1).ToArray()); + string ipaddressstring = string.Join(":", values.Take(values.Length - 1).ToArray()); ipaddy = IPAddress.Parse(ipaddressstring); port = GetPort(values[values.Length - 1]); } @@ -569,11 +570,11 @@ namespace Emby.Server.Implementations.Networking } else { - throw new FormatException(String.Format("Invalid endpoint ipaddress '{0}'", endpointstring)); + throw new FormatException(string.Format("Invalid endpoint ipaddress '{0}'", endpointstring)); } if (port == -1) - throw new ArgumentException(String.Format("No port specified: '{0}'", endpointstring)); + throw new ArgumentException(string.Format("No port specified: '{0}'", endpointstring)); return new IPEndPoint(ipaddy, port); } @@ -590,11 +591,11 @@ namespace Emby.Server.Implementations.Networking { int port; - if (!Int32.TryParse(p, out port) + if (!int.TryParse(p, out port) || port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort) { - throw new FormatException(String.Format("Invalid end point port '{0}'", p)); + throw new FormatException(string.Format("Invalid end point port '{0}'", p)); } return port; @@ -611,7 +612,7 @@ namespace Emby.Server.Implementations.Networking var hosts = await Dns.GetHostAddressesAsync(p).ConfigureAwait(false); if (hosts == null || hosts.Length == 0) - throw new ArgumentException(String.Format("Host not found: {0}", p)); + throw new ArgumentException(string.Format("Host not found: {0}", p)); return hosts[0]; } |
