SecureStreamSocketImpl.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // SecureStreamSocketImpl.h
  3. //
  4. // $Id: //poco/Main/NetSSL_OpenSSL/include/Poco/Net/SecureStreamSocketImpl.h#7 $
  5. //
  6. // Library: NetSSL_OpenSSL
  7. // Package: SSLSockets
  8. // Module: SecureStreamSocketImpl
  9. //
  10. // Definition of the SecureStreamSocketImpl class.
  11. //
  12. // Copyright (c) 2006-2009, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef NetSSL_SecureStreamSocketImpl_INCLUDED
  38. #define NetSSL_SecureStreamSocketImpl_INCLUDED
  39. #include "Poco/Net/NetSSL.h"
  40. #include "Poco/Net/SecureSocketImpl.h"
  41. #include "Poco/Net/StreamSocketImpl.h"
  42. #include "Poco/Net/Context.h"
  43. #include "Poco/Net/X509Certificate.h"
  44. namespace Poco {
  45. namespace Net {
  46. class NetSSL_API SecureStreamSocketImpl: public StreamSocketImpl
  47. /// This class implements a SSL stream socket.
  48. {
  49. public:
  50. SecureStreamSocketImpl(Context::Ptr pContext);
  51. /// Creates the SecureStreamSocketImpl.
  52. SecureStreamSocketImpl(StreamSocketImpl* pStreamSocket, Context::Ptr pContext);
  53. /// Creates the SecureStreamSocketImpl.
  54. SocketImpl* acceptConnection(SocketAddress& clientAddr);
  55. /// Not supported by a SecureStreamSocket.
  56. ///
  57. /// Throws a Poco::InvalidAccessException.
  58. void acceptSSL();
  59. /// Performs a SSL server-side handshake.
  60. void connect(const SocketAddress& address);
  61. /// Initializes the socket and establishes a connection to
  62. /// the TCP server at the given address.
  63. ///
  64. /// Can also be used for UDP sockets. In this case, no
  65. /// connection is established. Instead, incoming and outgoing
  66. /// packets are restricted to the specified address.
  67. void connect(const SocketAddress& address, const Poco::Timespan& timeout);
  68. /// Initializes the socket, sets the socket timeout and
  69. /// establishes a connection to the TCP server at the given address.
  70. void connectNB(const SocketAddress& address);
  71. /// Initializes the socket and establishes a connection to
  72. /// the TCP server at the given address. Prior to opening the
  73. /// connection the socket is set to nonblocking mode.
  74. void connectSSL();
  75. /// Performs a SSL client-side handshake on an already connected TCP socket.
  76. void bind(const SocketAddress& address, bool reuseAddress = false);
  77. /// Not supported by a SecureStreamSocket.
  78. ///
  79. /// Throws a Poco::InvalidAccessException.
  80. void listen(int backlog = 64);
  81. /// Not supported by a SecureStreamSocket.
  82. ///
  83. /// Throws a Poco::InvalidAccessException.
  84. void close();
  85. /// Close the socket.
  86. int sendBytes(const void* buffer, int length, int flags = 0);
  87. /// Sends the contents of the given buffer through
  88. /// the socket. Any specified flags are ignored.
  89. ///
  90. /// Returns the number of bytes sent, which may be
  91. /// less than the number of bytes specified.
  92. int receiveBytes(void* buffer, int length, int flags = 0);
  93. /// Receives data from the socket and stores it
  94. /// in buffer. Up to length bytes are received.
  95. ///
  96. /// Returns the number of bytes received.
  97. int sendTo(const void* buffer, int length, const SocketAddress& address, int flags = 0);
  98. /// Not supported by a SecureStreamSocket.
  99. ///
  100. /// Throws a Poco::InvalidAccessException.
  101. int receiveFrom(void* buffer, int length, SocketAddress& address, int flags = 0);
  102. /// Not supported by a SecureStreamSocket.
  103. ///
  104. /// Throws a Poco::InvalidAccessException.
  105. void sendUrgent(unsigned char data);
  106. /// Not supported by a SecureStreamSocket.
  107. ///
  108. /// Throws a Poco::InvalidAccessException.
  109. void shutdownReceive();
  110. /// Shuts down the receiving part of the socket connection.
  111. ///
  112. /// Since SSL does not support a half shutdown, this does
  113. /// nothing.
  114. void shutdownSend();
  115. /// Shuts down the receiving part of the socket connection.
  116. ///
  117. /// Since SSL does not support a half shutdown, this does
  118. /// nothing.
  119. void shutdown();
  120. /// Shuts down the SSL connection.
  121. void setPeerHostName(const std::string& hostName);
  122. /// Sets the peer host name for certificate validation purposes.
  123. const std::string& getPeerHostName() const;
  124. /// Returns the peer host name.
  125. X509Certificate peerCertificate() const;
  126. /// Returns the peer's X509 certificate.
  127. Context::Ptr context() const;
  128. /// Returns the SSL context used by this socket.
  129. protected:
  130. ~SecureStreamSocketImpl();
  131. /// Destroys the SecureStreamSocketImpl.
  132. static int lastError();
  133. static void error();
  134. static void error(const std::string& arg);
  135. static void error(int code);
  136. static void error(int code, const std::string& arg);
  137. private:
  138. SecureStreamSocketImpl(const SecureStreamSocketImpl&);
  139. SecureStreamSocketImpl& operator = (const SecureStreamSocketImpl&);
  140. SecureSocketImpl _impl;
  141. std::string _peerHostName;
  142. friend class SecureSocketImpl;
  143. };
  144. //
  145. // inlines
  146. //
  147. inline const std::string& SecureStreamSocketImpl::getPeerHostName() const
  148. {
  149. return _peerHostName;
  150. }
  151. inline Context::Ptr SecureStreamSocketImpl::context() const
  152. {
  153. return _impl.context();
  154. }
  155. inline int SecureStreamSocketImpl::lastError()
  156. {
  157. return SocketImpl::lastError();
  158. }
  159. inline void SecureStreamSocketImpl::error()
  160. {
  161. return SocketImpl::error();
  162. }
  163. inline void SecureStreamSocketImpl::error(const std::string& arg)
  164. {
  165. return SocketImpl::error(arg);
  166. }
  167. inline void SecureStreamSocketImpl::error(int code)
  168. {
  169. return SocketImpl::error(code);
  170. }
  171. inline void SecureStreamSocketImpl::error(int code, const std::string& arg)
  172. {
  173. return SocketImpl::error(code, arg);
  174. }
  175. } } // namespace Poco::Net
  176. #endif // NetSSL_SecureStreamSocketImpl_INCLUDED