ConnectCommand.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Net.Sockets;
  3. using System.Net.Security;
  4. using System.Security.Authentication;
  5. namespace Ant.Service.Utilities
  6. {
  7. /// <summary>
  8. /// Performs the connect to a Pop3 server and returns a Pop3
  9. /// response indicating the attempt to connect results and the
  10. /// network stream to use for all subsequent Pop3 Commands.
  11. /// </summary>
  12. internal sealed class ConnectCommand : Pop3Command<ConnectResponse>
  13. {
  14. private TcpClient _client;
  15. private string _hostname;
  16. private int _port;
  17. private bool _useSsl;
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="ConnectCommand"/> class.
  20. /// </summary>
  21. /// <remarks>
  22. /// Even though a network stream is provided to the base constructor the stream
  23. /// does not already exist so we have to send in a dummy stream until the actual
  24. /// connect has taken place. Then we'll reset network stream to the
  25. /// stream made available by the TcpClient.GetStream() to read the data returned
  26. /// after a connect.
  27. /// </remarks>
  28. /// <param name="client">The client.</param>
  29. /// <param name="hostname">The hostname.</param>
  30. /// <param name="port">The port.</param>
  31. /// <param name="useSsl">if set to <c>true</c> [use SSL].</param>
  32. public ConnectCommand(TcpClient client, string hostname, int port, bool useSsl)
  33. : base(new System.IO.MemoryStream(), false, Pop3State.Unknown)
  34. {
  35. if (client == null)
  36. {
  37. throw new ArgumentNullException("client");
  38. }
  39. if (string.IsNullOrEmpty(hostname))
  40. {
  41. throw new ArgumentNullException("hostname");
  42. }
  43. if (port < 1)
  44. {
  45. throw new ArgumentOutOfRangeException("port");
  46. }
  47. _client = client;
  48. _hostname = hostname;
  49. _port = port;
  50. _useSsl = useSsl;
  51. }
  52. /// <summary>
  53. /// Creates the connect request message.
  54. /// </summary>
  55. /// <returns>A byte[] containing connect request message.</returns>
  56. protected override byte[] CreateRequestMessage()
  57. {
  58. return null;
  59. }
  60. /// <summary>
  61. /// Executes this instance.
  62. /// </summary>
  63. /// <returns></returns>
  64. internal override ConnectResponse Execute(Pop3State currentState)
  65. {
  66. EnsurePop3State(currentState);
  67. try
  68. {
  69. _client.Connect(_hostname, _port);
  70. SetClientStream();
  71. }
  72. catch (SocketException e)
  73. {
  74. throw new Pop3Exception(string.Format("Unable to connect to {0}:{1}.", _hostname, _port), e);
  75. }
  76. return base.Execute(currentState);
  77. }
  78. /// <summary>
  79. /// Sets the client stream.
  80. /// </summary>
  81. private void SetClientStream()
  82. {
  83. if (_useSsl)
  84. {
  85. try
  86. {
  87. NetworkStream = new SslStream(_client.GetStream(), true); //make sure the inner stream stays available for the Pop3Client to make use of.
  88. ((SslStream)NetworkStream).AuthenticateAsClient(_hostname);
  89. }
  90. catch (ArgumentException e)
  91. {
  92. throw new Pop3Exception("Unable to create Ssl Stream for hostname: " + _hostname, e);
  93. }
  94. catch (AuthenticationException e)
  95. {
  96. throw new Pop3Exception("Unable to authenticate ssl stream for hostname: " + _hostname, e);
  97. }
  98. catch (InvalidOperationException e)
  99. {
  100. throw new Pop3Exception("There was a problem attempting to authenticate this SSL stream for hostname: " + _hostname, e);
  101. }
  102. } //wrap NetworkStream in an SSL stream
  103. else
  104. {
  105. NetworkStream = _client.GetStream();
  106. }
  107. }
  108. /// <summary>
  109. /// Creates the response.
  110. /// </summary>
  111. /// <param name="buffer">The buffer.</param>
  112. /// <returns>
  113. /// The <c>Pop3Response</c> containing the results of the
  114. /// Pop3 command execution.
  115. /// </returns>
  116. protected override ConnectResponse CreateResponse(byte[] buffer)
  117. {
  118. Pop3Response response = Pop3Response.CreateResponse(buffer);
  119. return new ConnectResponse(response, NetworkStream);
  120. }
  121. }
  122. }