2019-01-13 20:54:44 +01:00
|
|
|
using System;
|
2023-10-11 00:02:37 +02:00
|
|
|
using System.Linq;
|
2016-11-04 09:31:05 +01:00
|
|
|
using System.Net;
|
2023-10-11 00:02:37 +02:00
|
|
|
using System.Net.NetworkInformation;
|
2016-11-04 09:31:05 +01:00
|
|
|
using System.Net.Sockets;
|
|
|
|
using MediaBrowser.Model.Net;
|
|
|
|
|
2017-08-16 08:43:41 +02:00
|
|
|
namespace Emby.Server.Implementations.Net
|
2016-11-04 09:31:05 +01:00
|
|
|
{
|
2023-10-11 00:02:37 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Factory class to create different kinds of sockets.
|
|
|
|
/// </summary>
|
2016-11-04 09:31:05 +01:00
|
|
|
public class SocketFactory : ISocketFactory
|
|
|
|
{
|
2021-09-03 18:46:34 +02:00
|
|
|
/// <inheritdoc />
|
2023-02-16 18:15:12 +01:00
|
|
|
public Socket CreateUdpBroadcastSocket(int localPort)
|
2017-03-13 05:08:23 +01:00
|
|
|
{
|
2019-03-13 17:51:33 +01:00
|
|
|
if (localPort < 0)
|
|
|
|
{
|
|
|
|
throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
|
|
|
|
}
|
2017-03-13 05:08:23 +01:00
|
|
|
|
2023-02-16 18:15:12 +01:00
|
|
|
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
2017-03-13 05:08:23 +01:00
|
|
|
try
|
|
|
|
{
|
2023-02-16 18:15:12 +01:00
|
|
|
socket.EnableBroadcast = true;
|
|
|
|
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
|
|
|
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
|
|
|
|
socket.Bind(new IPEndPoint(IPAddress.Any, localPort));
|
2017-03-13 05:08:23 +01:00
|
|
|
|
2023-02-16 18:15:12 +01:00
|
|
|
return socket;
|
2017-03-13 05:08:23 +01:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2023-10-08 00:46:15 +02:00
|
|
|
socket.Dispose();
|
2017-03-13 05:08:23 +01:00
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2017-08-17 22:19:02 +02:00
|
|
|
|
2021-09-03 18:46:34 +02:00
|
|
|
/// <inheritdoc />
|
2023-02-16 18:15:12 +01:00
|
|
|
public Socket CreateSsdpUdpSocket(IPData bindInterface, int localPort)
|
2016-11-04 09:31:05 +01:00
|
|
|
{
|
2023-10-11 00:02:37 +02:00
|
|
|
var interfaceAddress = bindInterface.Address;
|
|
|
|
ArgumentNullException.ThrowIfNull(interfaceAddress);
|
2023-02-16 18:15:12 +01:00
|
|
|
|
2019-03-13 17:51:33 +01:00
|
|
|
if (localPort < 0)
|
|
|
|
{
|
|
|
|
throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
|
|
|
|
}
|
2016-11-04 09:31:05 +01:00
|
|
|
|
2023-02-16 18:15:12 +01:00
|
|
|
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
2016-11-04 09:31:05 +01:00
|
|
|
try
|
|
|
|
{
|
2023-02-16 18:15:12 +01:00
|
|
|
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
2023-10-11 00:02:37 +02:00
|
|
|
socket.Bind(new IPEndPoint(interfaceAddress, localPort));
|
2016-12-04 22:30:38 +01:00
|
|
|
|
2023-02-16 18:15:12 +01:00
|
|
|
return socket;
|
2016-11-04 09:31:05 +01:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2023-10-08 00:46:15 +02:00
|
|
|
socket.Dispose();
|
2016-11-04 09:31:05 +01:00
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 18:46:34 +02:00
|
|
|
/// <inheritdoc />
|
2023-02-16 18:15:12 +01:00
|
|
|
public Socket CreateUdpMulticastSocket(IPAddress multicastAddress, IPData bindInterface, int multicastTimeToLive, int localPort)
|
2016-11-04 09:31:05 +01:00
|
|
|
{
|
2023-02-16 18:15:12 +01:00
|
|
|
var bindIPAddress = bindInterface.Address;
|
|
|
|
ArgumentNullException.ThrowIfNull(multicastAddress);
|
|
|
|
ArgumentNullException.ThrowIfNull(bindIPAddress);
|
2022-07-21 09:26:18 +02:00
|
|
|
|
2019-03-13 17:51:33 +01:00
|
|
|
if (multicastTimeToLive <= 0)
|
|
|
|
{
|
|
|
|
throw new ArgumentException("multicastTimeToLive cannot be zero or less.", nameof(multicastTimeToLive));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (localPort < 0)
|
|
|
|
{
|
|
|
|
throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
|
|
|
|
}
|
2016-11-04 09:31:05 +01:00
|
|
|
|
2023-02-16 18:15:12 +01:00
|
|
|
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
2017-11-10 22:22:38 +01:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2023-02-16 18:15:12 +01:00
|
|
|
socket.MulticastLoopback = false;
|
|
|
|
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
|
|
|
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
|
|
|
|
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
|
2023-10-11 00:02:37 +02:00
|
|
|
|
|
|
|
if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
|
|
|
{
|
|
|
|
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(multicastAddress));
|
|
|
|
socket.Bind(new IPEndPoint(multicastAddress, localPort));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Only create socket if interface supports multicast
|
|
|
|
var interfaceIndex = bindInterface.Index;
|
|
|
|
var interfaceIndexSwapped = IPAddress.HostToNetworkOrder(interfaceIndex);
|
|
|
|
|
|
|
|
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(multicastAddress, interfaceIndex));
|
|
|
|
socket.Bind(new IPEndPoint(bindIPAddress, localPort));
|
|
|
|
}
|
2020-06-24 18:23:16 +02:00
|
|
|
|
2023-02-16 18:15:12 +01:00
|
|
|
return socket;
|
2016-11-04 09:31:05 +01:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2023-10-08 00:46:15 +02:00
|
|
|
socket.Dispose();
|
2016-11-04 09:31:05 +01:00
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|