aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Net/SocketFactory.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Emby.Server.Implementations/Net/SocketFactory.cs')
-rw-r--r--Emby.Server.Implementations/Net/SocketFactory.cs102
1 files changed, 35 insertions, 67 deletions
diff --git a/Emby.Server.Implementations/Net/SocketFactory.cs b/Emby.Server.Implementations/Net/SocketFactory.cs
index 137728616..51e92953d 100644
--- a/Emby.Server.Implementations/Net/SocketFactory.cs
+++ b/Emby.Server.Implementations/Net/SocketFactory.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
#pragma warning disable CS1591
using System;
@@ -11,77 +9,64 @@ namespace Emby.Server.Implementations.Net
{
public class SocketFactory : ISocketFactory
{
- public ISocket CreateUdpBroadcastSocket(int localPort)
+ /// <inheritdoc />
+ public Socket CreateUdpBroadcastSocket(int localPort)
{
if (localPort < 0)
{
throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
}
- var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
+ var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try
{
- retVal.EnableBroadcast = true;
- retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
- retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
+ socket.EnableBroadcast = true;
+ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
+ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
+ socket.Bind(new IPEndPoint(IPAddress.Any, localPort));
- return new UdpSocket(retVal, localPort, IPAddress.Any);
+ return socket;
}
catch
{
- retVal?.Dispose();
+ socket?.Dispose();
throw;
}
}
- /// <summary>
- /// Creates a new UDP acceptSocket that is a member of the SSDP multicast local admin group and binds it to the specified local port.
- /// </summary>
- /// <returns>An implementation of the <see cref="ISocket"/> interface used by RSSDP components to perform acceptSocket operations.</returns>
- public ISocket CreateSsdpUdpSocket(IPAddress localIpAddress, int localPort)
+ /// <inheritdoc />
+ public Socket CreateSsdpUdpSocket(IPData bindInterface, int localPort)
{
+ ArgumentNullException.ThrowIfNull(bindInterface.Address);
+
if (localPort < 0)
{
throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
}
- var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
+ var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try
{
- retVal.EnableBroadcast = true;
- retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
- retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4);
+ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
+ socket.Bind(new IPEndPoint(bindInterface.Address, localPort));
- retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250"), localIpAddress));
- return new UdpSocket(retVal, localPort, localIpAddress);
+ return socket;
}
catch
{
- retVal?.Dispose();
+ socket?.Dispose();
throw;
}
}
- /// <summary>
- /// Creates a new UDP acceptSocket that is a member of the specified multicast IP address, and binds it to the specified local port.
- /// </summary>
- /// <param name="ipAddress">The multicast IP address to make the acceptSocket a member of.</param>
- /// <param name="multicastTimeToLive">The multicast time to live value for the acceptSocket.</param>
- /// <param name="localPort">The number of the local port to bind to.</param>
- /// <returns></returns>
- public ISocket CreateUdpMulticastSocket(string ipAddress, int multicastTimeToLive, int localPort)
+ /// <inheritdoc />
+ public Socket CreateUdpMulticastSocket(IPAddress multicastAddress, IPData bindInterface, int multicastTimeToLive, int localPort)
{
- if (ipAddress == null)
- {
- throw new ArgumentNullException(nameof(ipAddress));
- }
-
- if (ipAddress.Length == 0)
- {
- throw new ArgumentException("ipAddress cannot be an empty string.", nameof(ipAddress));
- }
+ var bindIPAddress = bindInterface.Address;
+ ArgumentNullException.ThrowIfNull(multicastAddress);
+ ArgumentNullException.ThrowIfNull(bindIPAddress);
if (multicastTimeToLive <= 0)
{
@@ -93,43 +78,26 @@ namespace Emby.Server.Implementations.Net
throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
}
- var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
-
- try
- {
- // not supported on all platforms. throws on ubuntu with .net core 2.0
- retVal.ExclusiveAddressUse = false;
- }
- catch (SocketException)
- {
- }
-
- try
- {
- // seeing occasional exceptions thrown on qnap
- // System.Net.Sockets.SocketException (0x80004005): Protocol not available
- retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
- }
- catch (SocketException)
- {
- }
+ var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try
{
- retVal.EnableBroadcast = true;
- // retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
- retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
-
- var localIp = IPAddress.Any;
+ var interfaceIndex = bindInterface.Index;
+ var interfaceIndexSwapped = (int)IPAddress.HostToNetworkOrder(interfaceIndex);
- retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse(ipAddress), localIp));
- retVal.MulticastLoopback = true;
+ socket.MulticastLoopback = false;
+ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
+ socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
+ socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
+ socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, interfaceIndexSwapped);
+ socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(multicastAddress, interfaceIndex));
+ socket.Bind(new IPEndPoint(multicastAddress, localPort));
- return new UdpSocket(retVal, localPort, localIp);
+ return socket;
}
catch
{
- retVal?.Dispose();
+ socket?.Dispose();
throw;
}