2015-04-29 18:48:34 +00:00
|
|
|
|
using MediaBrowser.Common;
|
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2014-06-22 05:52:31 +00:00
|
|
|
|
using MediaBrowser.Common.Events;
|
2014-04-27 03:42:05 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2015-04-29 18:48:34 +00:00
|
|
|
|
using MediaBrowser.Controller.Dlna;
|
2014-04-25 17:30:41 +00:00
|
|
|
|
using MediaBrowser.Dlna.Server;
|
2014-03-24 12:47:39 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-11-07 00:16:16 +00:00
|
|
|
|
using System.Globalization;
|
2014-03-24 12:47:39 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
2014-06-29 17:35:05 +00:00
|
|
|
|
using System.Text;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
using System.Threading;
|
2014-11-07 00:16:16 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2015-11-14 19:41:49 +00:00
|
|
|
|
using Microsoft.Win32;
|
2014-03-24 12:47:39 +00:00
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
namespace MediaBrowser.Dlna.Ssdp
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2015-04-29 18:48:34 +00:00
|
|
|
|
public class SsdpHandler : IDisposable, ISsdpHandler
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2016-02-13 19:41:19 +00:00
|
|
|
|
private Socket _multicastSocket;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
|
2014-03-24 12:47:39 +00:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
|
|
|
|
|
const string SSDPAddr = "239.255.255.250";
|
|
|
|
|
const int SSDPPort = 1900;
|
2014-04-25 17:30:41 +00:00
|
|
|
|
private readonly string _serverSignature;
|
2014-03-24 12:47:39 +00:00
|
|
|
|
|
|
|
|
|
private readonly IPAddress _ssdpIp = IPAddress.Parse(SSDPAddr);
|
2014-04-25 17:30:41 +00:00
|
|
|
|
private readonly IPEndPoint _ssdpEndp = new IPEndPoint(IPAddress.Parse(SSDPAddr), SSDPPort);
|
2014-03-24 12:47:39 +00:00
|
|
|
|
|
2014-04-10 15:06:54 +00:00
|
|
|
|
private Timer _notificationTimer;
|
2014-04-27 03:42:05 +00:00
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
private bool _isDisposed;
|
2016-04-02 04:16:18 +00:00
|
|
|
|
private readonly Dictionary<string, List<UpnpDevice>> _devices = new Dictionary<string, List<UpnpDevice>>();
|
2014-04-27 03:42:05 +00:00
|
|
|
|
|
2015-04-29 18:48:34 +00:00
|
|
|
|
private readonly IApplicationHost _appHost;
|
|
|
|
|
|
2016-02-13 19:41:19 +00:00
|
|
|
|
private readonly int _unicastPort = 1901;
|
|
|
|
|
private UdpClient _unicastClient;
|
|
|
|
|
|
2015-04-29 18:48:34 +00:00
|
|
|
|
public SsdpHandler(ILogger logger, IServerConfigurationManager config, IApplicationHost appHost)
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_config = config;
|
2015-04-29 18:48:34 +00:00
|
|
|
|
_appHost = appHost;
|
2014-04-28 15:05:28 +00:00
|
|
|
|
|
2014-06-29 17:35:05 +00:00
|
|
|
|
_config.NamedConfigurationUpdated += _config_ConfigurationUpdated;
|
2015-04-29 18:48:34 +00:00
|
|
|
|
_serverSignature = GenerateServerSignature();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GenerateServerSignature()
|
|
|
|
|
{
|
|
|
|
|
var os = Environment.OSVersion;
|
|
|
|
|
var pstring = os.Platform.ToString();
|
|
|
|
|
switch (os.Platform)
|
|
|
|
|
{
|
|
|
|
|
case PlatformID.Win32NT:
|
|
|
|
|
case PlatformID.Win32S:
|
|
|
|
|
case PlatformID.Win32Windows:
|
|
|
|
|
pstring = "WIN";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String.Format(
|
2015-05-04 17:46:44 +00:00
|
|
|
|
"{0}{1}/{2}.{3} UPnP/1.0 DLNADOC/1.5 Emby/{4}",
|
2015-04-29 18:48:34 +00:00
|
|
|
|
pstring,
|
|
|
|
|
IntPtr.Size * 8,
|
|
|
|
|
os.Version.Major,
|
|
|
|
|
os.Version.Minor,
|
|
|
|
|
_appHost.ApplicationVersion
|
|
|
|
|
);
|
2014-04-28 15:05:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-29 17:35:05 +00:00
|
|
|
|
void _config_ConfigurationUpdated(object sender, ConfigurationUpdateEventArgs e)
|
2014-04-28 15:05:28 +00:00
|
|
|
|
{
|
2014-06-29 17:35:05 +00:00
|
|
|
|
if (string.Equals(e.Key, "dlna", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
ReloadAliveNotifier();
|
|
|
|
|
}
|
2014-04-25 17:30:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<UpnpDevice> RegisteredDevices
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-04-02 04:16:18 +00:00
|
|
|
|
lock (_devices)
|
|
|
|
|
{
|
|
|
|
|
var devices = _devices.ToList();
|
2015-12-08 16:10:27 +00:00
|
|
|
|
|
2016-04-02 04:16:18 +00:00
|
|
|
|
return devices.SelectMany(i => i.Value).ToList();
|
|
|
|
|
}
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-27 03:42:05 +00:00
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
public void Start()
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2016-01-29 02:59:00 +00:00
|
|
|
|
DisposeSocket();
|
|
|
|
|
StopAliveNotifier();
|
2014-04-10 15:06:54 +00:00
|
|
|
|
|
2016-01-29 02:59:00 +00:00
|
|
|
|
RestartSocketListener();
|
2014-04-28 15:05:28 +00:00
|
|
|
|
ReloadAliveNotifier();
|
2016-01-29 02:59:00 +00:00
|
|
|
|
|
|
|
|
|
SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
|
2015-11-14 19:41:49 +00:00
|
|
|
|
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Mode == PowerModes.Resume)
|
|
|
|
|
{
|
2016-01-29 02:59:00 +00:00
|
|
|
|
Start();
|
2015-11-14 19:41:49 +00:00
|
|
|
|
}
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-13 19:41:19 +00:00
|
|
|
|
public async void SendDatagram(string msg,
|
2014-09-06 04:21:23 +00:00
|
|
|
|
EndPoint endpoint,
|
|
|
|
|
EndPoint localAddress,
|
2015-10-18 01:18:29 +00:00
|
|
|
|
bool isBroadcast,
|
2016-02-13 20:07:31 +00:00
|
|
|
|
int sendCount = 3)
|
2014-04-25 17:30:41 +00:00
|
|
|
|
{
|
2016-02-12 04:33:14 +00:00
|
|
|
|
var enableDebugLogging = _config.GetDlnaConfiguration().EnableDebugLog;
|
2015-05-04 17:44:25 +00:00
|
|
|
|
|
2015-01-31 23:12:22 +00:00
|
|
|
|
for (var i = 0; i < sendCount; i++)
|
2014-04-25 17:30:41 +00:00
|
|
|
|
{
|
2016-01-29 02:59:00 +00:00
|
|
|
|
if (i > 0)
|
2015-01-31 23:12:22 +00:00
|
|
|
|
{
|
2016-03-01 16:17:03 +00:00
|
|
|
|
await Task.Delay(500).ConfigureAwait(false);
|
2015-01-31 23:12:22 +00:00
|
|
|
|
}
|
2014-04-25 17:30:41 +00:00
|
|
|
|
|
2016-01-29 02:59:00 +00:00
|
|
|
|
var dgram = new Datagram(endpoint, localAddress, _logger, msg, isBroadcast, enableDebugLogging);
|
|
|
|
|
dgram.Send();
|
2015-01-31 23:12:22 +00:00
|
|
|
|
}
|
2014-04-25 17:30:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-10 04:29:04 +00:00
|
|
|
|
private void RestartSocketListener()
|
|
|
|
|
{
|
|
|
|
|
if (_isDisposed)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-02-13 19:41:19 +00:00
|
|
|
|
_multicastSocket = CreateMulticastSocket();
|
2015-05-10 04:29:04 +00:00
|
|
|
|
|
|
|
|
|
_logger.Info("MultiCast socket created");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error creating MultiCast socket", ex);
|
|
|
|
|
//StartSocketRetryTimer();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
public void Dispose()
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2014-06-29 17:35:05 +00:00
|
|
|
|
_config.NamedConfigurationUpdated -= _config_ConfigurationUpdated;
|
2015-11-14 19:41:49 +00:00
|
|
|
|
SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
|
2014-04-28 15:05:28 +00:00
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
_isDisposed = true;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
DisposeSocket();
|
2016-01-29 02:59:00 +00:00
|
|
|
|
StopAliveNotifier();
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
private void DisposeSocket()
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2016-02-13 19:41:19 +00:00
|
|
|
|
if (_multicastSocket != null)
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2016-02-13 19:41:19 +00:00
|
|
|
|
_multicastSocket.Close();
|
|
|
|
|
_multicastSocket.Dispose();
|
|
|
|
|
_multicastSocket = null;
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
2014-04-10 15:06:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
private Socket CreateMulticastSocket()
|
|
|
|
|
{
|
|
|
|
|
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
|
|
|
|
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
|
|
|
|
|
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
|
|
|
|
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4);
|
|
|
|
|
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(_ssdpIp, 0));
|
2014-04-10 15:06:54 +00:00
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
socket.Bind(new IPEndPoint(IPAddress.Any, SSDPPort));
|
|
|
|
|
|
|
|
|
|
return socket;
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void NotifyAll()
|
|
|
|
|
{
|
2016-02-12 04:33:14 +00:00
|
|
|
|
var enableDebugLogging = _config.GetDlnaConfiguration().EnableDebugLog;
|
2015-05-01 03:00:54 +00:00
|
|
|
|
|
|
|
|
|
if (enableDebugLogging)
|
2014-04-21 16:02:30 +00:00
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Sending alive notifications");
|
|
|
|
|
}
|
2014-04-25 17:30:41 +00:00
|
|
|
|
foreach (var d in RegisteredDevices)
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2016-02-13 20:07:31 +00:00
|
|
|
|
NotifyDevice(d, "alive", enableDebugLogging);
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-13 20:07:31 +00:00
|
|
|
|
private void NotifyDevice(UpnpDevice dev, string type, bool logMessage)
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2014-04-25 17:30:41 +00:00
|
|
|
|
const string header = "NOTIFY * HTTP/1.1";
|
2014-04-20 05:21:08 +00:00
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
var values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
2014-03-24 12:47:39 +00:00
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
// If needed later for non-server devices, these headers will need to be dynamic
|
|
|
|
|
values["HOST"] = "239.255.255.250:1900";
|
|
|
|
|
values["CACHE-CONTROL"] = "max-age = 600";
|
|
|
|
|
values["LOCATION"] = dev.Descriptor.ToString();
|
|
|
|
|
values["SERVER"] = _serverSignature;
|
|
|
|
|
values["NTS"] = "ssdp:" + type;
|
|
|
|
|
values["NT"] = dev.Type;
|
|
|
|
|
values["USN"] = dev.USN;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
|
2015-05-01 03:00:54 +00:00
|
|
|
|
if (logMessage)
|
2014-04-21 16:02:30 +00:00
|
|
|
|
{
|
|
|
|
|
_logger.Debug("{0} said {1}", dev.USN, type);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-13 19:41:19 +00:00
|
|
|
|
var msg = new SsdpMessageBuilder().BuildMessage(header, values);
|
|
|
|
|
|
2016-05-10 03:36:43 +00:00
|
|
|
|
SendDatagram(msg, _ssdpEndp, new IPEndPoint(dev.Address, 0), true, 2);
|
2016-02-17 21:24:01 +00:00
|
|
|
|
//SendUnicastRequest(msg, 1);
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-16 17:09:32 +00:00
|
|
|
|
public void RegisterNotification(string uuid, Uri descriptionUri, IPAddress address, IEnumerable<string> services)
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2016-04-02 04:16:18 +00:00
|
|
|
|
lock (_devices)
|
|
|
|
|
{
|
|
|
|
|
List<UpnpDevice> list;
|
|
|
|
|
List<UpnpDevice> dl;
|
|
|
|
|
if (_devices.TryGetValue(uuid, out dl))
|
|
|
|
|
{
|
|
|
|
|
list = dl;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
list = new List<UpnpDevice>();
|
|
|
|
|
_devices[uuid] = list;
|
|
|
|
|
}
|
2014-03-24 12:47:39 +00:00
|
|
|
|
|
2016-04-02 04:16:18 +00:00
|
|
|
|
list.AddRange(services.Select(i => new UpnpDevice(uuid, i, descriptionUri, address)));
|
2014-03-24 12:47:39 +00:00
|
|
|
|
|
2016-04-02 04:16:18 +00:00
|
|
|
|
NotifyAll();
|
|
|
|
|
_logger.Debug("Registered mount {0} at {1}", uuid, descriptionUri);
|
|
|
|
|
}
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-16 17:09:32 +00:00
|
|
|
|
public void UnregisterNotification(string uuid)
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2016-04-02 04:16:18 +00:00
|
|
|
|
lock (_devices)
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2016-04-02 04:16:18 +00:00
|
|
|
|
List<UpnpDevice> dl;
|
|
|
|
|
if (_devices.TryGetValue(uuid, out dl))
|
2014-04-10 15:06:54 +00:00
|
|
|
|
{
|
2016-04-02 04:16:18 +00:00
|
|
|
|
_devices.Remove(uuid);
|
|
|
|
|
foreach (var d in dl.ToList())
|
|
|
|
|
{
|
|
|
|
|
NotifyDevice(d, "byebye", true);
|
|
|
|
|
}
|
2014-04-10 15:06:54 +00:00
|
|
|
|
|
2016-04-02 04:16:18 +00:00
|
|
|
|
_logger.Debug("Unregistered mount {0}", uuid);
|
|
|
|
|
}
|
2014-04-10 15:06:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly object _notificationTimerSyncLock = new object();
|
2014-04-28 15:05:28 +00:00
|
|
|
|
private int _aliveNotifierIntervalMs;
|
|
|
|
|
private void ReloadAliveNotifier()
|
2014-04-10 15:06:54 +00:00
|
|
|
|
{
|
2015-05-01 03:00:54 +00:00
|
|
|
|
var config = _config.GetDlnaConfiguration();
|
|
|
|
|
|
|
|
|
|
if (!config.BlastAliveMessages)
|
2014-04-20 05:21:08 +00:00
|
|
|
|
{
|
2016-01-29 02:59:00 +00:00
|
|
|
|
StopAliveNotifier();
|
2014-04-20 05:21:08 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-01 03:00:54 +00:00
|
|
|
|
var intervalMs = config.BlastAliveMessageIntervalSeconds * 1000;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
|
2014-04-28 15:05:28 +00:00
|
|
|
|
if (_notificationTimer == null || _aliveNotifierIntervalMs != intervalMs)
|
2014-04-10 15:06:54 +00:00
|
|
|
|
{
|
2014-04-28 15:05:28 +00:00
|
|
|
|
lock (_notificationTimerSyncLock)
|
2014-04-10 15:06:54 +00:00
|
|
|
|
{
|
2014-04-28 15:05:28 +00:00
|
|
|
|
if (_notificationTimer == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Starting alive notifier");
|
|
|
|
|
const int initialDelayMs = 3000;
|
|
|
|
|
_notificationTimer = new Timer(state => NotifyAll(), null, initialDelayMs, intervalMs);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Updating alive notifier");
|
|
|
|
|
_notificationTimer.Change(intervalMs, intervalMs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_aliveNotifierIntervalMs = intervalMs;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-29 02:59:00 +00:00
|
|
|
|
private void StopAliveNotifier()
|
2014-04-10 15:06:54 +00:00
|
|
|
|
{
|
|
|
|
|
lock (_notificationTimerSyncLock)
|
|
|
|
|
{
|
|
|
|
|
if (_notificationTimer != null)
|
|
|
|
|
{
|
2014-04-28 15:05:28 +00:00
|
|
|
|
_logger.Debug("Stopping alive notifier");
|
2014-04-10 15:06:54 +00:00
|
|
|
|
_notificationTimer.Dispose();
|
|
|
|
|
_notificationTimer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
2016-02-13 19:41:19 +00:00
|
|
|
|
|
|
|
|
|
public class UdpState
|
|
|
|
|
{
|
|
|
|
|
public UdpClient UdpClient;
|
|
|
|
|
public IPEndPoint EndPoint;
|
|
|
|
|
}
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|