aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Net/SocketFactory.cs
blob: 2bcd5eab291a7f3a6800099cd4cf1508d502dcc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using MediaBrowser.Model.Net;

namespace Emby.Server.Implementations.Net
{
    /// <summary>
    /// Factory class to create different kinds of sockets.
    /// </summary>
    public class SocketFactory : ISocketFactory
    {
        /// <inheritdoc />
        public Socket CreateUdpBroadcastSocket(int localPort)
        {
            if (localPort < 0)
            {
                throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
            }

            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            try
            {
                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 socket;
            }
            catch
            {
                socket.Dispose();

                throw;
            }
        }

        /// <inheritdoc />
        public Socket CreateSsdpUdpSocket(IPData bindInterface, int localPort)
        {
            var interfaceAddress = bindInterface.Address;
            ArgumentNullException.ThrowIfNull(interfaceAddress);

            if (localPort < 0)
            {
                throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
            }

            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            try
            {
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                socket.Bind(new IPEndPoint(interfaceAddress, localPort));

                return socket;
            }
            catch
            {
                socket.Dispose();

                throw;
            }
        }

        /// <inheritdoc />
        public Socket CreateUdpMulticastSocket(IPAddress multicastAddress, IPData bindInterface, int multicastTimeToLive, int localPort)
        {
            var bindIPAddress = bindInterface.Address;
            ArgumentNullException.ThrowIfNull(multicastAddress);
            ArgumentNullException.ThrowIfNull(bindIPAddress);

            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));
            }

            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            try
            {
                socket.MulticastLoopback = false;
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
                socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);

                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));
                }

                return socket;
            }
            catch
            {
                socket.Dispose();

                throw;
            }
        }
    }
}