From b01d169d28cb7cfffa33796dfe7bf8be5570593a Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Thu, 21 Jul 2022 09:42:45 +0200 Subject: [PATCH] Implement discovery respecting bind addresses --- .../EntryPoints/UdpServerEntryPoint.cs | 20 ++++++++++++++++--- Emby.Server.Implementations/Udp/UdpServer.cs | 4 +++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Emby.Server.Implementations/EntryPoints/UdpServerEntryPoint.cs b/Emby.Server.Implementations/EntryPoints/UdpServerEntryPoint.cs index e45baedd7..9ac2310b4 100644 --- a/Emby.Server.Implementations/EntryPoints/UdpServerEntryPoint.cs +++ b/Emby.Server.Implementations/EntryPoints/UdpServerEntryPoint.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Emby.Server.Implementations.Udp; using Jellyfin.Networking.Configuration; using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Net; using MediaBrowser.Controller; using MediaBrowser.Controller.Plugins; using Microsoft.Extensions.Configuration; @@ -29,6 +30,7 @@ namespace Emby.Server.Implementations.EntryPoints private readonly IServerApplicationHost _appHost; private readonly IConfiguration _config; private readonly IConfigurationManager _configurationManager; + private readonly INetworkManager _networkManager; /// /// The UDP server. @@ -44,16 +46,19 @@ namespace Emby.Server.Implementations.EntryPoints /// Instance of the interface. /// Instance of the interface. /// Instance of the interface. + /// Instance of the interface. public UdpServerEntryPoint( ILogger logger, IServerApplicationHost appHost, IConfiguration configuration, - IConfigurationManager configurationManager) + IConfigurationManager configurationManager, + INetworkManager networkManager) { _logger = logger; _appHost = appHost; _config = configuration; _configurationManager = configurationManager; + _networkManager = networkManager; } /// @@ -68,8 +73,17 @@ namespace Emby.Server.Implementations.EntryPoints try { - _udpServer = new UdpServer(_logger, _appHost, _config, PortNumber); - _udpServer.Start(_cancellationTokenSource.Token); + foreach (var bindAddress in _networkManager.GetInternalBindAddresses()) + { + if (bindAddress.AddressFamily == AddressFamily.InterNetworkV6) + { + // Not supporting IPv6 right now + continue; + } + + _udpServer = new UdpServer(_logger, _appHost, _config, bindAddress.Address, PortNumber); + _udpServer.Start(_cancellationTokenSource.Token); + } } catch (SocketException ex) { diff --git a/Emby.Server.Implementations/Udp/UdpServer.cs b/Emby.Server.Implementations/Udp/UdpServer.cs index 937e792f5..a3bbd6df0 100644 --- a/Emby.Server.Implementations/Udp/UdpServer.cs +++ b/Emby.Server.Implementations/Udp/UdpServer.cs @@ -37,18 +37,20 @@ namespace Emby.Server.Implementations.Udp /// The logger. /// The application host. /// The configuration manager. + /// The bind address. /// The port. public UdpServer( ILogger logger, IServerApplicationHost appHost, IConfiguration configuration, + IPAddress bindAddress, int port) { _logger = logger; _appHost = appHost; _config = configuration; - _endpoint = new IPEndPoint(IPAddress.Any, port); + _endpoint = new IPEndPoint(bindAddress, port); _udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); _udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);