2021-02-23 16:45:10 +01:00
|
|
|
using System;
|
2022-07-21 19:38:19 +02:00
|
|
|
using System.Collections.Generic;
|
2023-02-16 18:15:12 +01:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
2020-07-13 17:33:39 +02:00
|
|
|
using System.Net.Sockets;
|
2020-01-12 18:59:10 +01:00
|
|
|
using System.Threading;
|
2019-01-27 15:40:37 +01:00
|
|
|
using System.Threading.Tasks;
|
2019-01-13 20:20:41 +01:00
|
|
|
using Emby.Server.Implementations.Udp;
|
2022-02-14 14:39:33 +01:00
|
|
|
using MediaBrowser.Common.Configuration;
|
2022-07-21 09:42:45 +02:00
|
|
|
using MediaBrowser.Common.Net;
|
2014-07-28 00:01:29 +02:00
|
|
|
using MediaBrowser.Controller;
|
2013-05-19 00:07:59 +02:00
|
|
|
using MediaBrowser.Controller.Plugins;
|
2020-05-02 18:56:09 +02:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2019-01-13 20:20:41 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-05-19 00:07:59 +02:00
|
|
|
|
2016-11-04 19:56:47 +01:00
|
|
|
namespace Emby.Server.Implementations.EntryPoints
|
2013-05-19 00:07:59 +02:00
|
|
|
{
|
2013-05-27 20:34:03 +02:00
|
|
|
/// <summary>
|
2023-10-11 00:02:37 +02:00
|
|
|
/// Class responsible for registering all UDP broadcast endpoints and their handlers.
|
2013-05-27 20:34:03 +02:00
|
|
|
/// </summary>
|
2020-01-12 18:59:10 +01:00
|
|
|
public sealed class UdpServerEntryPoint : IServerEntryPoint
|
2013-05-19 00:07:59 +02:00
|
|
|
{
|
|
|
|
/// <summary>
|
2019-11-01 18:38:54 +01:00
|
|
|
/// The port of the UDP server.
|
2013-05-19 00:07:59 +02:00
|
|
|
/// </summary>
|
2019-11-01 18:38:54 +01:00
|
|
|
public const int PortNumber = 7359;
|
2013-05-19 00:07:59 +02:00
|
|
|
|
2013-05-27 20:34:03 +02:00
|
|
|
/// <summary>
|
2019-12-10 16:22:03 +01:00
|
|
|
/// The logger.
|
2013-05-27 20:34:03 +02:00
|
|
|
/// </summary>
|
2020-06-06 02:15:56 +02:00
|
|
|
private readonly ILogger<UdpServerEntryPoint> _logger;
|
2014-07-28 00:01:29 +02:00
|
|
|
private readonly IServerApplicationHost _appHost;
|
2020-05-02 18:56:09 +02:00
|
|
|
private readonly IConfiguration _config;
|
2022-02-14 14:39:33 +01:00
|
|
|
private readonly IConfigurationManager _configurationManager;
|
2022-07-21 09:42:45 +02:00
|
|
|
private readonly INetworkManager _networkManager;
|
2013-05-19 00:07:59 +02:00
|
|
|
|
2019-11-01 18:38:54 +01:00
|
|
|
/// <summary>
|
|
|
|
/// The UDP server.
|
|
|
|
/// </summary>
|
2023-10-08 00:40:58 +02:00
|
|
|
private readonly List<UdpServer> _udpServers;
|
|
|
|
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
private bool _disposed;
|
2013-09-25 02:54:51 +02:00
|
|
|
|
2013-05-27 20:34:03 +02:00
|
|
|
/// <summary>
|
2014-08-20 00:28:35 +02:00
|
|
|
/// Initializes a new instance of the <see cref="UdpServerEntryPoint" /> class.
|
2013-05-27 20:34:03 +02:00
|
|
|
/// </summary>
|
2021-08-29 00:32:50 +02:00
|
|
|
/// <param name="logger">Instance of the <see cref="ILogger{UdpServerEntryPoint}"/> interface.</param>
|
|
|
|
/// <param name="appHost">Instance of the <see cref="IServerApplicationHost"/> interface.</param>
|
|
|
|
/// <param name="configuration">Instance of the <see cref="IConfiguration"/> interface.</param>
|
2022-02-14 14:39:33 +01:00
|
|
|
/// <param name="configurationManager">Instance of the <see cref="IConfigurationManager"/> interface.</param>
|
2022-07-21 09:42:45 +02:00
|
|
|
/// <param name="networkManager">Instance of the <see cref="INetworkManager"/> interface.</param>
|
2019-11-01 18:38:54 +01:00
|
|
|
public UdpServerEntryPoint(
|
2020-01-12 18:59:10 +01:00
|
|
|
ILogger<UdpServerEntryPoint> logger,
|
2020-05-02 18:56:09 +02:00
|
|
|
IServerApplicationHost appHost,
|
2022-02-14 14:39:33 +01:00
|
|
|
IConfiguration configuration,
|
2022-07-21 09:42:45 +02:00
|
|
|
IConfigurationManager configurationManager,
|
|
|
|
INetworkManager networkManager)
|
2013-05-19 00:07:59 +02:00
|
|
|
{
|
|
|
|
_logger = logger;
|
2014-07-28 00:01:29 +02:00
|
|
|
_appHost = appHost;
|
2020-05-02 18:56:09 +02:00
|
|
|
_config = configuration;
|
2022-02-14 14:39:33 +01:00
|
|
|
_configurationManager = configurationManager;
|
2022-07-21 09:42:45 +02:00
|
|
|
_networkManager = networkManager;
|
2022-07-21 19:38:19 +02:00
|
|
|
_udpServers = new List<UdpServer>();
|
2013-05-19 00:07:59 +02:00
|
|
|
}
|
|
|
|
|
2019-11-01 18:38:54 +01:00
|
|
|
/// <inheritdoc />
|
2020-04-05 18:10:56 +02:00
|
|
|
public Task RunAsync()
|
2013-05-19 00:07:59 +02:00
|
|
|
{
|
2021-02-23 16:45:10 +01:00
|
|
|
CheckDisposed();
|
|
|
|
|
2022-03-08 22:11:06 +01:00
|
|
|
if (!_configurationManager.GetNetworkConfiguration().AutoDiscovery)
|
2022-02-14 14:39:33 +01:00
|
|
|
{
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2020-07-13 16:12:51 +02:00
|
|
|
try
|
|
|
|
{
|
2023-10-11 00:02:37 +02:00
|
|
|
// Linux needs to bind to the broadcast addresses to get broadcast traffic
|
|
|
|
// Windows receives broadcast fine when binding to just the interface, it is unable to bind to broadcast addresses
|
|
|
|
if (OperatingSystem.IsLinux())
|
2022-07-21 09:42:45 +02:00
|
|
|
{
|
2023-10-11 00:02:37 +02:00
|
|
|
// Add global broadcast listener
|
2023-02-23 13:55:27 +01:00
|
|
|
var server = new UdpServer(_logger, _appHost, _config, IPAddress.Broadcast, PortNumber);
|
|
|
|
server.Start(_cancellationTokenSource.Token);
|
|
|
|
_udpServers.Add(server);
|
2022-10-15 17:27:37 +02:00
|
|
|
|
2023-10-11 00:02:37 +02:00
|
|
|
// Add bind address specific broadcast listeners
|
2023-02-16 18:15:12 +01:00
|
|
|
// IPv6 is currently unsupported
|
|
|
|
var validInterfaces = _networkManager.GetInternalBindAddresses().Where(i => i.AddressFamily == AddressFamily.InterNetwork);
|
|
|
|
foreach (var intf in validInterfaces)
|
2022-07-21 09:42:45 +02:00
|
|
|
{
|
2023-11-10 16:59:45 +01:00
|
|
|
var broadcastAddress = NetworkUtils.GetBroadcastAddress(intf.Subnet);
|
2023-02-23 13:55:27 +01:00
|
|
|
_logger.LogDebug("Binding UDP server to {Address} on port {PortNumber}", broadcastAddress, PortNumber);
|
2022-10-15 17:27:37 +02:00
|
|
|
|
2023-02-23 13:55:27 +01:00
|
|
|
server = new UdpServer(_logger, _appHost, _config, broadcastAddress, PortNumber);
|
2023-02-16 18:15:12 +01:00
|
|
|
server.Start(_cancellationTokenSource.Token);
|
|
|
|
_udpServers.Add(server);
|
2022-07-21 10:20:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-11 00:02:37 +02:00
|
|
|
// Add bind address specific broadcast listeners
|
|
|
|
// IPv6 is currently unsupported
|
|
|
|
var validInterfaces = _networkManager.GetInternalBindAddresses().Where(i => i.AddressFamily == AddressFamily.InterNetwork);
|
|
|
|
foreach (var intf in validInterfaces)
|
|
|
|
{
|
|
|
|
var intfAddress = intf.Address;
|
|
|
|
_logger.LogDebug("Binding UDP server to {Address} on port {PortNumber}", intfAddress, PortNumber);
|
|
|
|
|
|
|
|
var server = new UdpServer(_logger, _appHost, _config, intfAddress, PortNumber);
|
|
|
|
server.Start(_cancellationTokenSource.Token);
|
|
|
|
_udpServers.Add(server);
|
|
|
|
}
|
2023-02-15 22:34:44 +01:00
|
|
|
}
|
2020-07-13 16:12:51 +02:00
|
|
|
}
|
2020-07-13 17:33:39 +02:00
|
|
|
catch (SocketException ex)
|
2020-07-13 16:12:51 +02:00
|
|
|
{
|
2020-07-13 16:39:14 +02:00
|
|
|
_logger.LogWarning(ex, "Unable to start AutoDiscovery listener on UDP port {PortNumber}", PortNumber);
|
2020-07-13 16:12:51 +02:00
|
|
|
}
|
|
|
|
|
2020-04-05 18:10:56 +02:00
|
|
|
return Task.CompletedTask;
|
2013-05-19 00:07:59 +02:00
|
|
|
}
|
|
|
|
|
2021-02-23 16:45:10 +01:00
|
|
|
private void CheckDisposed()
|
|
|
|
{
|
|
|
|
if (_disposed)
|
|
|
|
{
|
2023-10-11 00:02:37 +02:00
|
|
|
throw new ObjectDisposedException(GetType().Name);
|
2021-02-23 16:45:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 18:59:10 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
public void Dispose()
|
2013-05-19 00:07:59 +02:00
|
|
|
{
|
2020-01-12 18:59:10 +01:00
|
|
|
if (_disposed)
|
2013-05-19 00:07:59 +02:00
|
|
|
{
|
2020-01-12 18:59:10 +01:00
|
|
|
return;
|
2013-05-19 00:07:59 +02:00
|
|
|
}
|
2020-01-12 18:59:10 +01:00
|
|
|
|
|
|
|
_cancellationTokenSource.Cancel();
|
2020-02-06 15:20:23 +01:00
|
|
|
_cancellationTokenSource.Dispose();
|
2023-02-16 18:15:12 +01:00
|
|
|
foreach (var server in _udpServers)
|
|
|
|
{
|
|
|
|
server.Dispose();
|
|
|
|
}
|
2020-01-12 18:59:10 +01:00
|
|
|
|
2023-02-16 18:15:12 +01:00
|
|
|
_udpServers.Clear();
|
2020-01-12 18:59:10 +01:00
|
|
|
_disposed = true;
|
2013-05-19 00:07:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|