The Free Software Media System - Server Backend & API
Whilst fixing issues with SSDP on devices with multiple interfaces, i came across a design issue in the current code - namely interfaces without a gateway were ignored.
BaronGreenback2aaecb8e148a
diff --git a/Emby.Dlna/Main/DlnaEntryPoint.cs b/Emby.Dlna/Main/DlnaEntryPoint.csindex c5d60b2a05..7213504831 100644--- a/Emby.Dlna/Main/DlnaEntryPoint.cs+++ b/Emby.Dlna/Main/DlnaEntryPoint.cs@@ -266,6 +266,12 @@ namespace Emby.Dlna.Maincontinue;}+ // Limit to LAN addresses only+ if (!_networkManager.IsAddressInSubnets(address, true, true))+ {+ continue;+ }+var fullService = "urn:schemas-upnp-org:device:MediaServer:1";_logger.LogInformation("Registering publisher for {0} on {1}", fullService, address);diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.csindex 33aec1a06b..97fc2c0048 100644--- a/Emby.Server.Implementations/ApplicationHost.cs+++ b/Emby.Server.Implementations/ApplicationHost.cs@@ -1274,7 +1274,7 @@ namespace Emby.Server.Implementationsif (addresses.Count == 0){- addresses.AddRange(_networkManager.GetLocalIpAddresses(ServerConfigurationManager.Configuration.IgnoreVirtualInterfaces));+ addresses.AddRange(_networkManager.GetLocalIpAddresses());}var resultList = new List<IPAddress>();diff --git a/Emby.Server.Implementations/Networking/NetworkManager.cs b/Emby.Server.Implementations/Networking/NetworkManager.csindex b3e88b6675..465530888e 100644--- a/Emby.Server.Implementations/Networking/NetworkManager.cs+++ b/Emby.Server.Implementations/Networking/NetworkManager.cs@@ -56,13 +56,13 @@ namespace Emby.Server.Implementations.NetworkingNetworkChanged?.Invoke(this, EventArgs.Empty);}- public IPAddress[] GetLocalIpAddresses(bool ignoreVirtualInterface = true)+ public IPAddress[] GetLocalIpAddresses(){lock (_localIpAddressSyncLock){if (_localIpAddresses == null){- var addresses = GetLocalIpAddressesInternal(ignoreVirtualInterface).ToArray();+ var addresses = GetLocalIpAddressesInternal().ToArray();_localIpAddresses = addresses;}@@ -71,35 +71,37 @@ namespace Emby.Server.Implementations.Networking}}- private List<IPAddress> GetLocalIpAddressesInternal(bool ignoreVirtualInterface)+ private List<IPAddress> GetLocalIpAddressesInternal(){- var list = GetIPsDefault(ignoreVirtualInterface).ToList();+ var list = GetIPsDefault().ToList();if (list.Count == 0){list = GetLocalIpAddressesFallback().GetAwaiter().GetResult().ToList();}- var listClone = list.ToList();+ var listClone = new List<IPAddress>();- return list- .OrderBy(i => i.AddressFamily == AddressFamily.InterNetwork ? 0 : 1)- .ThenBy(i => listClone.IndexOf(i))- .Where(FilterIpAddress)- .GroupBy(i => i.ToString())- .Select(x => x.First())- .ToList();- }+ var subnets = LocalSubnetsFn();- private static bool FilterIpAddress(IPAddress address)- {- if (address.IsIPv6LinkLocal- || address.ToString().StartsWith("169.", StringComparison.OrdinalIgnoreCase))+ foreach (var i in list){- return false;+ if (i.IsIPv6LinkLocal || i.ToString().StartsWith("169.254.", StringComparison.OrdinalIgnoreCase))+ {+ continue;+ }+ if (Array.IndexOf(subnets, "[" + i.ToString() + "]") == -1)+ {+ listClone.Add(i);+ }}- return true;+ return listClone+ .OrderBy(i => i.AddressFamily == AddressFamily.InterNetwork ? 0 : 1)+ // .ThenBy(i => listClone.IndexOf(i))+ .GroupBy(i => i.ToString())+ .Select(x => x.First())+ .ToList();}public bool IsInPrivateAddressSpace(string endpoint)@@ -107,6 +109,7 @@ namespace Emby.Server.Implementations.Networkingreturn IsInPrivateAddressSpace(endpoint, true);}+ // checks if the address in endpoint is an RFC1918, RFC1122, or RFC3927 addressprivate bool IsInPrivateAddressSpace(string endpoint, bool checkSubnets){if (string.Equals(endpoint, "::1", StringComparison.OrdinalIgnoreCase))@@ -128,23 +131,28 @@ namespace Emby.Server.Implementations.Networking}// Private address space:- // http://en.wikipedia.org/wiki/Private_network- if (endpoint.StartsWith("172.", StringComparison.OrdinalIgnoreCase))+ if (endpoint.ToLower() == "localhost"){- return Is172AddressPrivate(endpoint);+ return true;}- if (endpoint.StartsWith("localhost", StringComparison.OrdinalIgnoreCase) ||- endpoint.StartsWith("127.", StringComparison.OrdinalIgnoreCase) ||- endpoint.StartsWith("169.", StringComparison.OrdinalIgnoreCase))+ try{- return true;- }+ byte[] octet = IPAddress.Parse(endpoint).GetAddressBytes();- if (checkSubnets && endpoint.StartsWith("192.168", StringComparison.OrdinalIgnoreCase))+ if ((octet[0] == 10) ||+ (octet[0] == 172 && (octet[1] >= 16 && octet[1] <= 31)) || // RFC1918+ (octet[0] == 192 && octet[1] == 168) || // RFC1918+ (octet[0] == 127) || // RFC1122+ (octet[0] == 169 && octet[1] == 254)) // RFC3927+ {+ return false;+ }+ }+ catch{- return true;+ // return false;}if (checkSubnets && IsInPrivateAddressSpaceAndLocalSubnet(endpoint))@@ -177,6 +185,7 @@ namespace Emby.Server.Implementations.Networkingreturn false;}+ // Gives a list of possible subnets from the system whose interface ip starts with endpointFirstPartprivate List<string> GetSubnets(string endpointFirstPart){lock (_subnetLookupLock)@@ -222,19 +231,6 @@ namespace Emby.Server.Implementations.Networking}}- private static bool Is172AddressPrivate(string endpoint)- {- for (var i = 16; i <= 31; i++)- {- if (endpoint.StartsWith("172." + i.ToString(CultureInfo.InvariantCulture) + ".", StringComparison.OrdinalIgnoreCase))- {- return true;- }- }-- return false;- }-public bool IsInLocalNetwork(string endpoint){return IsInLocalNetworkInternal(endpoint, true);@@ -245,23 +241,57 @@ namespace Emby.Server.Implementations.Networkingreturn IsAddressInSubnets(IPAddress.Parse(addressString), addressString, subnets);}+ // returns true if address is in the LAN list in the config file+ // always returns false if address has been excluded from the LAN if excludeInterfaces is true+ // and excludes RFC addresses if excludeRFC is true+ public bool IsAddressInSubnets(IPAddress address, bool excludeInterfaces, bool excludeRFC)+ {+ byte[] octet = address.GetAddressBytes();++ if ((octet[0] == 127) || // RFC1122+ (octet[0] == 169 && octet[1] == 254)) // RFC3927+ {+ // don't use on loopback or 169 interfaces+ return false;+ }++ string addressString = address.ToString();+ string excludeAddress = "[" + addressString + "]";+ var subnets = LocalSubnetsFn();++ // Exclude any addresses if they appear in the LAN list in [ ]+ if (Array.IndexOf(subnets, excludeAddress) != -1)+ {+ return false;+ }+ return IsAddressInSubnets(address, addressString, subnets);+ }++ // Checks to see if address/addressString (same but different type) falls within subnets[]private static bool IsAddressInSubnets(IPAddress address, string addressString, string[] subnets){foreach (var subnet in subnets){var normalizedSubnet = subnet.Trim();-+ // is the subnet a host address and does it match the address being passes?if (string.Equals(normalizedSubnet, addressString, StringComparison.OrdinalIgnoreCase)){return true;}-+ // parse CIDR subnets and see if address falls within it.if (normalizedSubnet.Contains('/', StringComparison.Ordinal)){- var ipNetwork = IPNetwork.Parse(normalizedSubnet);- if (ipNetwork.Contains(address))+ try{- return true;+ var ipNetwork = IPNetwork.Parse(normalizedSubnet);+ if (ipNetwork.Contains(address))+ {+ return true;+ }+ }+ catch+ {+ // Ignoring - invalid subnet passed encountered.}}}@@ -359,8 +389,8 @@ namespace Emby.Server.Implementations.Networking{return Dns.GetHostAddressesAsync(hostName);}-- private IEnumerable<IPAddress> GetIPsDefault(bool ignoreVirtualInterface)++ private IEnumerable<IPAddress> GetIPsDefault(){IEnumerable<NetworkInterface> interfaces;@@ -380,15 +410,7 @@ namespace Emby.Server.Implementations.Networking{var ipProperties = network.GetIPProperties();- // Try to exclude virtual adapters- // http://stackoverflow.com/questions/8089685/c-sharp-finding-my-machines-local-ip-address-and-not-the-vms- var addr = ipProperties.GatewayAddresses.FirstOrDefault();- if (addr == null- || (ignoreVirtualInterface- && (addr.Address.Equals(IPAddress.Any) || addr.Address.Equals(IPAddress.IPv6Any))))- {- return Enumerable.Empty<IPAddress>();- }+ // Exclude any addresses if they appear in the LAN list in [ ]return ipProperties.UnicastAddresses.Select(i => i.Address)@@ -494,15 +516,12 @@ namespace Emby.Server.Implementations.Networkingforeach (NetworkInterface ni in interfaces){- if (ni.GetIPProperties().GatewayAddresses.FirstOrDefault() != null)+ foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses){- foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)+ if (ip.Address.Equals(address) && ip.IPv4Mask != null){- if (ip.Address.Equals(address) && ip.IPv4Mask != null)- {- return ip.IPv4Mask;- }- }+ return ip.IPv4Mask;+ }}}diff --git a/MediaBrowser.Common/Net/INetworkManager.cs b/MediaBrowser.Common/Net/INetworkManager.csindex 3ba75abd85..b7ec1d122c 100644--- a/MediaBrowser.Common/Net/INetworkManager.cs+++ b/MediaBrowser.Common/Net/INetworkManager.cs@@ -41,10 +41,12 @@ namespace MediaBrowser.Common.Net/// <returns><c>true</c> if [is in local network] [the specified endpoint]; otherwise, <c>false</c>.</returns>bool IsInLocalNetwork(string endpoint);- IPAddress[] GetLocalIpAddresses(bool ignoreVirtualInterface);+ IPAddress[] GetLocalIpAddresses();bool IsAddressInSubnets(string addressString, string[] subnets);+ bool IsAddressInSubnets(IPAddress address, bool excludeInterfaces, bool excludeRFC);+bool IsInSameSubnet(IPAddress address1, IPAddress address2, IPAddress subnetMask);IPAddress GetLocalIpSubnetMask(IPAddress address);diff --git a/RSSDP/SsdpCommunicationsServer.cs b/RSSDP/SsdpCommunicationsServer.csindex 18097ef241..47da520056 100644--- a/RSSDP/SsdpCommunicationsServer.cs+++ b/RSSDP/SsdpCommunicationsServer.cs@@ -370,13 +370,13 @@ namespace Rssdp.Infrastructureif (_enableMultiSocketBinding){- foreach (var address in _networkManager.GetLocalIpAddresses(_config.Configuration.IgnoreVirtualInterfaces))+ foreach (var address in _networkManager.GetLocalIpAddresses()){if (address.AddressFamily == AddressFamily.InterNetworkV6){// Not support IPv6 right nowcontinue;- }+ }try{diff --git a/RSSDP/SsdpDeviceLocator.cs b/RSSDP/SsdpDeviceLocator.csindex 59a2710d58..b62c50e28d 100644--- a/RSSDP/SsdpDeviceLocator.cs+++ b/RSSDP/SsdpDeviceLocator.cs@@ -357,7 +357,7 @@ namespace Rssdp.Infrastructureprivate void ProcessSearchResponseMessage(HttpResponseMessage message, IPAddress localIpAddress){if (!message.IsSuccessStatusCode) return;-+var location = GetFirstHeaderUriValue("Location", message);if (location != null){