SocketAbstraction.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. using System;
  2. using System.Globalization;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Threading;
  6. using Renci.SshNet.Common;
  7. using Renci.SshNet.Messages.Transport;
  8. namespace Renci.SshNet.Abstractions
  9. {
  10. internal static class SocketAbstraction
  11. {
  12. public static bool CanRead(Socket socket)
  13. {
  14. if (socket.Connected)
  15. {
  16. #if FEATURE_SOCKET_POLL
  17. return socket.Poll(-1, SelectMode.SelectRead) && socket.Available > 0;
  18. #endif // FEATURE_SOCKET_POLL
  19. }
  20. return false;
  21. }
  22. public static bool CanWrite(Socket socket)
  23. {
  24. if (socket.Connected)
  25. {
  26. #if FEATURE_SOCKET_POLL
  27. return socket.Poll(-1, SelectMode.SelectWrite);
  28. #endif // FEATURE_SOCKET_POLL
  29. }
  30. return false;
  31. }
  32. public static Socket Connect(IPEndPoint remoteEndpoint, TimeSpan connectTimeout)
  33. {
  34. var socket = new Socket(remoteEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp) {NoDelay = true};
  35. #if FEATURE_SOCKET_EAP
  36. var connectCompleted = new ManualResetEvent(false);
  37. var args = new SocketAsyncEventArgs
  38. {
  39. UserToken = connectCompleted,
  40. RemoteEndPoint = remoteEndpoint
  41. };
  42. args.Completed += ConnectCompleted;
  43. if (socket.ConnectAsync(args))
  44. {
  45. if (!connectCompleted.WaitOne(connectTimeout))
  46. throw new SshOperationTimeoutException(string.Format(CultureInfo.InvariantCulture,
  47. "Connection failed to establish within {0:F0} milliseconds.", connectTimeout.TotalMilliseconds));
  48. }
  49. if (args.SocketError != SocketError.Success)
  50. throw new SocketException((int) args.SocketError);
  51. return socket;
  52. #elif FEATURE_SOCKET_APM
  53. var connectResult = socket.BeginConnect(remoteEndpoint, null, null);
  54. if (!connectResult.AsyncWaitHandle.WaitOne(connectTimeout, false))
  55. throw new SshOperationTimeoutException(string.Format(CultureInfo.InvariantCulture,
  56. "Connection failed to establish within {0:F0} milliseconds.", connectTimeout.TotalMilliseconds));
  57. socket.EndConnect(connectResult);
  58. return socket;
  59. #elif FEATURE_SOCKET_TAP
  60. if (!socket.ConnectAsync(remoteEndpoint).Wait(connectTimeout))
  61. throw new SshOperationTimeoutException(string.Format(CultureInfo.InvariantCulture,
  62. "Connection failed to establish within {0:F0} milliseconds.", connectTimeout.TotalMilliseconds));
  63. return socket;
  64. #else
  65. #error Connecting to a remote endpoint is not implemented.
  66. #endif
  67. }
  68. public static void ClearReadBuffer(Socket socket)
  69. {
  70. try
  71. {
  72. var buffer = new byte[256];
  73. int bytesReceived;
  74. do
  75. {
  76. bytesReceived = ReadPartial(socket, buffer, 0, buffer.Length, TimeSpan.FromSeconds(2));
  77. } while (bytesReceived > 0);
  78. }
  79. catch
  80. {
  81. // ignore any exceptions
  82. }
  83. }
  84. public static int ReadPartial(Socket socket, byte[] buffer, int offset, int size, TimeSpan timeout)
  85. {
  86. #if FEATURE_SOCKET_SYNC
  87. return socket.Receive(buffer, offset, size, SocketFlags.None);
  88. #elif FEATURE_SOCKET_EAP
  89. var receiveCompleted = new ManualResetEvent(false);
  90. var sendReceiveToken = new PartialSendReceiveToken(socket, receiveCompleted);
  91. var args = new SocketAsyncEventArgs
  92. {
  93. RemoteEndPoint = socket.RemoteEndPoint,
  94. UserToken = sendReceiveToken
  95. };
  96. args.Completed += ReceiveCompleted;
  97. args.SetBuffer(buffer, offset, size);
  98. try
  99. {
  100. if (socket.ReceiveAsync(args))
  101. {
  102. if (!receiveCompleted.WaitOne(timeout))
  103. throw new SshOperationTimeoutException(
  104. string.Format(
  105. CultureInfo.InvariantCulture,
  106. "Socket read operation has timed out after {0:F0} milliseconds.",
  107. timeout.TotalMilliseconds));
  108. }
  109. if (args.SocketError != SocketError.Success)
  110. throw new SocketException((int) args.SocketError);
  111. return args.BytesTransferred;
  112. }
  113. finally
  114. {
  115. // initialize token to avoid the waithandle getting used after it's disposed
  116. args.UserToken = null;
  117. args.Dispose();
  118. receiveCompleted.Dispose();
  119. }
  120. #else
  121. #error Receiving data from a Socket is not implemented.
  122. #endif
  123. }
  124. /// <summary>
  125. /// Receives data from a bound <see cref="Socket"/>into a receive buffer.
  126. /// </summary>
  127. /// <param name="socket"></param>
  128. /// <param name="buffer">An array of type <see cref="byte"/> that is the storage location for the received data. </param>
  129. /// <param name="offset">The position in <paramref name="buffer"/> parameter to store the received data.</param>
  130. /// <param name="size">The number of bytes to receive.</param>
  131. /// <param name="timeout">Specifies the amount of time after which the call will time out.</param>
  132. /// <returns>
  133. /// The number of bytes received.
  134. /// </returns>
  135. /// <remarks>
  136. /// If no data is available for reading, the <see cref="Read(Socket,byte[], int, int, TimeSpan)"/> method will
  137. /// block until data is available or the time-out value was exceeded. If the time-out value was exceeded, the
  138. /// <see cref="Read(Socket,byte[], int, int, TimeSpan)"/> call will throw a <see cref="SshOperationTimeoutException"/>.
  139. /// If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the
  140. /// <see cref="Read(Socket,byte[], int, int, TimeSpan)"/> method will complete immediately and throw a <see cref="SocketException"/>.
  141. /// </remarks>
  142. public static int Read(Socket socket, byte[] buffer, int offset, int size, TimeSpan timeout)
  143. {
  144. #if FEATURE_SOCKET_SYNC
  145. var totalBytesRead = 0;
  146. var totalBytesToRead = size;
  147. do
  148. {
  149. try
  150. {
  151. var bytesRead = socket.Receive(buffer, offset + totalBytesRead, totalBytesToRead - totalBytesRead, SocketFlags.None);
  152. if (bytesRead == 0)
  153. return 0;
  154. totalBytesRead += bytesRead;
  155. }
  156. catch (SocketException ex)
  157. {
  158. if (IsErrorResumable(ex.SocketErrorCode))
  159. {
  160. ThreadAbstraction.Sleep(30);
  161. continue;
  162. }
  163. throw;
  164. }
  165. }
  166. while (totalBytesRead < totalBytesToRead);
  167. return totalBytesRead;
  168. #elif FEATURE_SOCKET_EAP
  169. var receiveCompleted = new ManualResetEvent(false);
  170. var sendReceiveToken = new BlockingSendReceiveToken(socket, buffer, offset, size, receiveCompleted);
  171. var args = new SocketAsyncEventArgs
  172. {
  173. UserToken = sendReceiveToken,
  174. RemoteEndPoint = socket.RemoteEndPoint
  175. };
  176. args.Completed += ReceiveCompleted;
  177. args.SetBuffer(buffer, offset, size);
  178. try
  179. {
  180. if (socket.ReceiveAsync(args))
  181. {
  182. if (!receiveCompleted.WaitOne(timeout))
  183. throw new SshOperationTimeoutException(string.Format(CultureInfo.InvariantCulture,
  184. "Socket read operation has timed out after {0:F0} milliseconds.", timeout.TotalMilliseconds));
  185. }
  186. if (args.SocketError != SocketError.Success)
  187. throw new SocketException((int) args.SocketError);
  188. return sendReceiveToken.TotalBytesTransferred;
  189. }
  190. finally
  191. {
  192. // initialize token to avoid the waithandle getting used after it's disposed
  193. args.UserToken = null;
  194. args.Dispose();
  195. receiveCompleted.Dispose();
  196. }
  197. #else
  198. #error Receiving data from a Socket is not implemented.
  199. #endif
  200. }
  201. public static void Send(Socket socket, byte[] data)
  202. {
  203. Send(socket, data, 0, data.Length);
  204. }
  205. public static void Send(Socket socket, byte[] data, int offset, int size)
  206. {
  207. #if FEATURE_SOCKET_SYNC
  208. var totalBytesSent = 0; // how many bytes are already sent
  209. var totalBytesToSend = size;
  210. do
  211. {
  212. try
  213. {
  214. var bytesSent = socket.Send(data, offset + totalBytesSent, totalBytesToSend - totalBytesSent,
  215. SocketFlags.None);
  216. if (bytesSent == 0)
  217. throw new SshConnectionException("An established connection was aborted by the server.",
  218. DisconnectReason.ConnectionLost);
  219. totalBytesSent += bytesSent;
  220. }
  221. catch (SocketException ex)
  222. {
  223. if (IsErrorResumable(ex.SocketErrorCode))
  224. {
  225. // socket buffer is probably full, wait and try again
  226. ThreadAbstraction.Sleep(30);
  227. }
  228. else
  229. throw; // any serious error occurr
  230. }
  231. } while (totalBytesSent < totalBytesToSend);
  232. #elif FEATURE_SOCKET_EAP
  233. var sendCompleted = new ManualResetEvent(false);
  234. var sendReceiveToken = new BlockingSendReceiveToken(socket, data, offset, size, sendCompleted);
  235. var socketAsyncSendArgs = new SocketAsyncEventArgs
  236. {
  237. RemoteEndPoint = socket.RemoteEndPoint,
  238. UserToken = sendReceiveToken
  239. };
  240. socketAsyncSendArgs.SetBuffer(data, offset, size);
  241. socketAsyncSendArgs.Completed += SendCompleted;
  242. try
  243. {
  244. if (socket.SendAsync(socketAsyncSendArgs))
  245. {
  246. if (!sendCompleted.WaitOne())
  247. throw new SocketException((int) SocketError.TimedOut);
  248. }
  249. if (socketAsyncSendArgs.SocketError != SocketError.Success)
  250. throw new SocketException((int) socketAsyncSendArgs.SocketError);
  251. if (sendReceiveToken.TotalBytesTransferred == 0)
  252. throw new SshConnectionException("An established connection was aborted by the server.",
  253. DisconnectReason.ConnectionLost);
  254. }
  255. finally
  256. {
  257. // initialize token to avoid the completion waithandle getting used after it's disposed
  258. socketAsyncSendArgs.UserToken = null;
  259. socketAsyncSendArgs.Dispose();
  260. sendCompleted.Dispose();
  261. }
  262. #else
  263. #error Sending data to a Socket is not implemented.
  264. #endif
  265. }
  266. private static bool IsErrorResumable(SocketError socketError)
  267. {
  268. switch (socketError)
  269. {
  270. case SocketError.WouldBlock:
  271. case SocketError.IOPending:
  272. case SocketError.NoBufferSpaceAvailable:
  273. return true;
  274. default:
  275. return false;
  276. }
  277. }
  278. #if FEATURE_SOCKET_EAP
  279. private static void ConnectCompleted(object sender, SocketAsyncEventArgs e)
  280. {
  281. var eventWaitHandle = (ManualResetEvent) e.UserToken;
  282. if (eventWaitHandle != null)
  283. eventWaitHandle.Set();
  284. }
  285. #endif // FEATURE_SOCKET_EAP
  286. #if FEATURE_SOCKET_EAP && !FEATURE_SOCKET_SYNC
  287. private static void ReceiveCompleted(object sender, SocketAsyncEventArgs e)
  288. {
  289. var sendReceiveToken = (Token) e.UserToken;
  290. if (sendReceiveToken != null)
  291. sendReceiveToken.Process(e);
  292. }
  293. private static void SendCompleted(object sender, SocketAsyncEventArgs e)
  294. {
  295. var sendReceiveToken = (Token) e.UserToken;
  296. if (sendReceiveToken != null)
  297. sendReceiveToken.Process(e);
  298. }
  299. private interface Token
  300. {
  301. void Process(SocketAsyncEventArgs args);
  302. }
  303. private class BlockingSendReceiveToken : Token
  304. {
  305. public BlockingSendReceiveToken(Socket socket, byte[] buffer, int offset, int size, EventWaitHandle completionWaitHandle)
  306. {
  307. _socket = socket;
  308. _buffer = buffer;
  309. _offset = offset;
  310. _bytesToTransfer = size;
  311. _completionWaitHandle = completionWaitHandle;
  312. }
  313. public void Process(SocketAsyncEventArgs args)
  314. {
  315. if (args.SocketError == SocketError.Success)
  316. {
  317. TotalBytesTransferred += args.BytesTransferred;
  318. if (TotalBytesTransferred == _bytesToTransfer)
  319. {
  320. // finished transferring specified bytes
  321. _completionWaitHandle.Set();
  322. return;
  323. }
  324. if (args.BytesTransferred == 0)
  325. {
  326. // remote server closed the connection
  327. _completionWaitHandle.Set();
  328. return;
  329. }
  330. _offset += args.BytesTransferred;
  331. args.SetBuffer(_buffer, _offset, _bytesToTransfer - TotalBytesTransferred);
  332. ResumeOperation(args);
  333. return;
  334. }
  335. if (IsErrorResumable(args.SocketError))
  336. {
  337. ThreadAbstraction.Sleep(30);
  338. ResumeOperation(args);
  339. return;
  340. }
  341. // we're dealing with a (fatal) error
  342. _completionWaitHandle.Set();
  343. }
  344. private void ResumeOperation(SocketAsyncEventArgs args)
  345. {
  346. switch (args.LastOperation)
  347. {
  348. case SocketAsyncOperation.Receive:
  349. _socket.ReceiveAsync(args);
  350. break;
  351. case SocketAsyncOperation.Send:
  352. _socket.SendAsync(args);
  353. break;
  354. }
  355. }
  356. private readonly int _bytesToTransfer;
  357. public int TotalBytesTransferred { get; private set; }
  358. private readonly EventWaitHandle _completionWaitHandle;
  359. private readonly Socket _socket;
  360. private readonly byte[] _buffer;
  361. private int _offset;
  362. }
  363. private class PartialSendReceiveToken : Token
  364. {
  365. public PartialSendReceiveToken(Socket socket, EventWaitHandle completionWaitHandle)
  366. {
  367. _socket = socket;
  368. _completionWaitHandle = completionWaitHandle;
  369. }
  370. public void Process(SocketAsyncEventArgs args)
  371. {
  372. if (args.SocketError == SocketError.Success)
  373. {
  374. _completionWaitHandle.Set();
  375. return;
  376. }
  377. if (IsErrorResumable(args.SocketError))
  378. {
  379. ThreadAbstraction.Sleep(30);
  380. ResumeOperation(args);
  381. return;
  382. }
  383. // we're dealing with a (fatal) error
  384. _completionWaitHandle.Set();
  385. }
  386. private void ResumeOperation(SocketAsyncEventArgs args)
  387. {
  388. switch (args.LastOperation)
  389. {
  390. case SocketAsyncOperation.Receive:
  391. _socket.ReceiveAsync(args);
  392. break;
  393. case SocketAsyncOperation.Send:
  394. _socket.SendAsync(args);
  395. break;
  396. }
  397. }
  398. private readonly EventWaitHandle _completionWaitHandle;
  399. private readonly Socket _socket;
  400. }
  401. #endif // FEATURE_SOCKET_EAP && !FEATURE_SOCKET_SYNC
  402. }
  403. }