SocketFactory.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using Emby.Server.Implementations.Networking;
  6. using MediaBrowser.Model.Net;
  7. namespace Emby.Server.Implementations.Net
  8. {
  9. public class SocketFactory : ISocketFactory
  10. {
  11. // THIS IS A LINKED FILE - SHARED AMONGST MULTIPLE PLATFORMS
  12. // Be careful to check any changes compile and work for all platform projects it is shared in.
  13. // Not entirely happy with this. Would have liked to have done something more generic/reusable,
  14. // but that wasn't really the point so kept to YAGNI principal for now, even if the
  15. // interfaces are a bit ugly, specific and make assumptions.
  16. public ISocket CreateTcpSocket(IPAddress remoteAddress, int remotePort)
  17. {
  18. if (remotePort < 0)
  19. {
  20. throw new ArgumentException("remotePort cannot be less than zero.", nameof(remotePort));
  21. }
  22. var addressFamily = remoteAddress.AddressFamily == AddressFamily.InterNetwork
  23. ? AddressFamily.InterNetwork
  24. : AddressFamily.InterNetworkV6;
  25. var retVal = new Socket(addressFamily, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
  26. try
  27. {
  28. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  29. }
  30. catch (SocketException)
  31. {
  32. // This is not supported on all operating systems (qnap)
  33. }
  34. try
  35. {
  36. return new UdpSocket(retVal, new IPEndPoint(remoteAddress, remotePort));
  37. }
  38. catch
  39. {
  40. retVal?.Dispose();
  41. throw;
  42. }
  43. }
  44. /// <summary>
  45. /// Creates a new UDP acceptSocket and binds it to the specified local port.
  46. /// </summary>
  47. /// <param name="localPort">An integer specifying the local port to bind the acceptSocket to.</param>
  48. public ISocket CreateUdpSocket(int localPort)
  49. {
  50. if (localPort < 0)
  51. {
  52. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  53. }
  54. var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
  55. try
  56. {
  57. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  58. return new UdpSocket(retVal, localPort, IPAddress.Any);
  59. }
  60. catch
  61. {
  62. retVal?.Dispose();
  63. throw;
  64. }
  65. }
  66. public ISocket CreateUdpBroadcastSocket(int localPort)
  67. {
  68. if (localPort < 0)
  69. {
  70. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  71. }
  72. var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
  73. try
  74. {
  75. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  76. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  77. return new UdpSocket(retVal, localPort, IPAddress.Any);
  78. }
  79. catch
  80. {
  81. retVal?.Dispose();
  82. throw;
  83. }
  84. }
  85. /// <summary>
  86. /// Creates a new UDP acceptSocket that is a member of the SSDP multicast local admin group and binds it to the specified local port.
  87. /// </summary>
  88. /// <returns>An implementation of the <see cref="ISocket"/> interface used by RSSDP components to perform acceptSocket operations.</returns>
  89. public ISocket CreateSsdpUdpSocket(IPAddress localIpAddress, int localPort)
  90. {
  91. if (localPort < 0)
  92. {
  93. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  94. }
  95. var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
  96. try
  97. {
  98. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  99. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4);
  100. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250"), localIpAddress));
  101. return new UdpSocket(retVal, localPort, localIpAddress);
  102. }
  103. catch
  104. {
  105. retVal?.Dispose();
  106. throw;
  107. }
  108. }
  109. /// <summary>
  110. /// Creates a new UDP acceptSocket that is a member of the specified multicast IP address, and binds it to the specified local port.
  111. /// </summary>
  112. /// <param name="ipAddress">The multicast IP address to make the acceptSocket a member of.</param>
  113. /// <param name="multicastTimeToLive">The multicast time to live value for the acceptSocket.</param>
  114. /// <param name="localPort">The number of the local port to bind to.</param>
  115. /// <returns></returns>
  116. public ISocket CreateUdpMulticastSocket(string ipAddress, int multicastTimeToLive, int localPort)
  117. {
  118. if (ipAddress == null)
  119. {
  120. throw new ArgumentNullException(nameof(ipAddress));
  121. }
  122. if (ipAddress.Length == 0)
  123. {
  124. throw new ArgumentException("ipAddress cannot be an empty string.", nameof(ipAddress));
  125. }
  126. if (multicastTimeToLive <= 0)
  127. {
  128. throw new ArgumentException("multicastTimeToLive cannot be zero or less.", nameof(multicastTimeToLive));
  129. }
  130. if (localPort < 0)
  131. {
  132. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  133. }
  134. var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
  135. try
  136. {
  137. // not supported on all platforms. throws on ubuntu with .net core 2.0
  138. retVal.ExclusiveAddressUse = false;
  139. }
  140. catch (SocketException)
  141. {
  142. }
  143. try
  144. {
  145. // seeing occasional exceptions thrown on qnap
  146. // System.Net.Sockets.SocketException (0x80004005): Protocol not available
  147. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  148. }
  149. catch (SocketException)
  150. {
  151. }
  152. try
  153. {
  154. //retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
  155. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
  156. var localIp = IPAddress.Any;
  157. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse(ipAddress), localIp));
  158. retVal.MulticastLoopback = true;
  159. return new UdpSocket(retVal, localPort, localIp);
  160. }
  161. catch
  162. {
  163. retVal?.Dispose();
  164. throw;
  165. }
  166. }
  167. public Stream CreateNetworkStream(ISocket socket, bool ownsSocket)
  168. => new NetworkStream(((UdpSocket)socket).Socket, ownsSocket);
  169. }
  170. }