From 44a270fa6fed9ef806e02a200af0bb14b5cf0a0e Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 27 Oct 2017 00:14:45 -0400 Subject: [PATCH] cache subnet results --- .../Networking/NetworkManager.cs | 76 +++++++++---------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/Emby.Server.Implementations/Networking/NetworkManager.cs b/Emby.Server.Implementations/Networking/NetworkManager.cs index be85db80a..7664ed442 100644 --- a/Emby.Server.Implementations/Networking/NetworkManager.cs +++ b/Emby.Server.Implementations/Networking/NetworkManager.cs @@ -120,6 +120,36 @@ namespace Emby.Server.Implementations.Networking endpoint.StartsWith("169.", StringComparison.OrdinalIgnoreCase) ) { + var subnets = GetSubnets(endpointFirstPart); + + foreach (var subnet_Match in subnets) + { + //Logger.Debug("subnet_Match:" + subnet_Match); + + if (endpoint.StartsWith(subnet_Match + ".", StringComparison.OrdinalIgnoreCase)) + { + return true; + } + } + } + + return false; + } + + private Dictionary> _subnetLookup = new Dictionary>(StringComparer.Ordinal); + private List GetSubnets(string endpointFirstPart) + { + List subnets; + + lock (_subnetLookup) + { + if (_subnetLookup.TryGetValue(endpointFirstPart, out subnets)) + { + return subnets; + } + + subnets = new List(); + foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces()) { foreach (UnicastIPAddressInformation unicastIPAddressInformation in adapter.GetIPProperties().UnicastAddresses) @@ -135,51 +165,19 @@ namespace Emby.Server.Implementations.Networking var subnet_Match = String.Join(".", unicastIPAddressInformation.Address.ToString().Split('.').Take(subnet_Test).ToArray()); - if (endpoint.StartsWith(subnet_Match + ".", StringComparison.OrdinalIgnoreCase)) + // TODO: Is this check necessary? + if (adapter.OperationalStatus == OperationalStatus.Up) { - return true; + subnets.Add(subnet_Match); } } } } + + _subnetLookup[endpointFirstPart] = subnets; + + return subnets; } - - return false; - } - - private Dictionary _subnetLookup = new Dictionary(StringComparer.Ordinal); - private string GetSubnet(string endpointFirstPart) - { - string subnet_Match = ""; - - lock (_subnetLookup) - { - if (_subnetLookup.TryGetValue(endpointFirstPart, out subnet_Match)) - { - return subnet_Match; - } - - foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces()) - foreach (UnicastIPAddressInformation unicastIPAddressInformation in adapter.GetIPProperties().UnicastAddresses) - if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork && endpointFirstPart == unicastIPAddressInformation.Address.ToString().Split('.')[0]) - { - int subnet_Test = 0; - foreach (string part in unicastIPAddressInformation.IPv4Mask.ToString().Split('.')) - { - if (part.Equals("0")) break; - subnet_Test++; - } - - subnet_Match = String.Join(".", unicastIPAddressInformation.Address.ToString().Split('.').Take(subnet_Test).ToArray()); - } - - if (!string.IsNullOrWhiteSpace(subnet_Match)) - { - _subnetLookup[endpointFirstPart] = subnet_Match; - } - } - - return subnet_Match; } private bool Is172AddressPrivate(string endpoint)