SocketFactory.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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(IpAddressInfo 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 == IpAddressFamily.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 IpEndPointInfo(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(IpAddressInfo 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. var localIp = NetworkManager.ToIPAddress(localIpAddress);
  101. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250"), localIp));
  102. return new UdpSocket(retVal, localPort, localIp);
  103. }
  104. catch
  105. {
  106. retVal?.Dispose();
  107. throw;
  108. }
  109. }
  110. /// <summary>
  111. /// Creates a new UDP acceptSocket that is a member of the specified multicast IP address, and binds it to the specified local port.
  112. /// </summary>
  113. /// <param name="ipAddress">The multicast IP address to make the acceptSocket a member of.</param>
  114. /// <param name="multicastTimeToLive">The multicast time to live value for the acceptSocket.</param>
  115. /// <param name="localPort">The number of the local port to bind to.</param>
  116. /// <returns></returns>
  117. public ISocket CreateUdpMulticastSocket(string ipAddress, int multicastTimeToLive, int localPort)
  118. {
  119. if (ipAddress == null)
  120. {
  121. throw new ArgumentNullException(nameof(ipAddress));
  122. }
  123. if (ipAddress.Length == 0)
  124. {
  125. throw new ArgumentException("ipAddress cannot be an empty string.", nameof(ipAddress));
  126. }
  127. if (multicastTimeToLive <= 0)
  128. {
  129. throw new ArgumentException("multicastTimeToLive cannot be zero or less.", nameof(multicastTimeToLive));
  130. }
  131. if (localPort < 0)
  132. {
  133. throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
  134. }
  135. var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
  136. try
  137. {
  138. // not supported on all platforms. throws on ubuntu with .net core 2.0
  139. retVal.ExclusiveAddressUse = false;
  140. }
  141. catch (SocketException)
  142. {
  143. }
  144. try
  145. {
  146. // seeing occasional exceptions thrown on qnap
  147. // System.Net.Sockets.SocketException (0x80004005): Protocol not available
  148. retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  149. }
  150. catch (SocketException)
  151. {
  152. }
  153. try
  154. {
  155. //retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
  156. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
  157. var localIp = IPAddress.Any;
  158. retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse(ipAddress), localIp));
  159. retVal.MulticastLoopback = true;
  160. return new UdpSocket(retVal, localPort, localIp);
  161. }
  162. catch
  163. {
  164. retVal?.Dispose();
  165. throw;
  166. }
  167. }
  168. public Stream CreateNetworkStream(ISocket socket, bool ownsSocket)
  169. => new NetworkStream(((UdpSocket)socket).Socket, ownsSocket);
  170. }
  171. }