3 //=============================================================================
5 * @file SSL_SOCK_Stream.h
7 * @author Ossama Othman <ossama@uci.edu>
8 * @author Carlos O'Ryan <coryan@uci.edu>
9 * @author John Heitmann
11 //=============================================================================
14 #ifndef ACE_SSL_SOCK_STREAM_H
15 #define ACE_SSL_SOCK_STREAM_H
17 #include /**/ "ace/pre.h"
19 #include "SSL_Export.h"
21 #if !defined (ACE_LACKS_PRAGMA_ONCE)
23 #endif /* ACE_LACKS_PRAGMA_ONCE */
25 // This must be included before any <openssl> include on LynxOS
26 #include "ace/os_include/os_stdio.h"
28 #include <openssl/err.h>
31 #include "SSL_Context.h"
33 #include "ace/SOCK_Stream.h"
35 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
38 * @class ACE_SSL_SOCK_Stream
40 * @brief Defines methods in the ACE_SSL_SOCK_Stream abstraction.
42 * This class encapsulates the methods and functionality necessary to
43 * send and receive data over TLS/SSL.
45 * Since SSL is record-oriented, some additional steps must be taken
46 * to make the ACE_SSL_SOCK_Stream interact properly with the
47 * Reactor (if one is used) when performing non-blocking IO. In
48 * particular, if ::SSL_pending (ssl), where "ssl" is a pointer to the
49 * SSL data structure returned from ACE_SSL_SOCK_Stream::ssl(),
50 * returns a non-zero value then the event handler that calls the IO
51 * methods in this class should return a value greater than zero to
52 * force the Reactor to invoke the event handler before polling for
53 * additional events (e.g. blocking on select()).
55 * @note The user must currently ensure that only one thread services
56 * a given SSL session at any given time since underlying SSL
57 * implementations, such as OpenSSL, are not entirely
58 * thread-safe or reentrant.
60 class ACE_SSL_Export ACE_SSL_SOCK_Stream
: public ACE_SSL_SOCK
65 * @param context Pointer to @c ACE_SSL_Context instance containing
66 * the OpenSSL @c SSL data structure to be associated
67 * with this @c ACE_SSL_SOCK_Stream. The @c SSL data
68 * structure will be copied to make it at least
69 * logically independent of the supplied @a context.
71 ACE_SSL_SOCK_Stream (ACE_SSL_Context
*context
=
72 ACE_SSL_Context::instance ());
75 ~ACE_SSL_SOCK_Stream ();
77 /// Send an @a n byte buffer to the ssl socket using the semantics of
80 * ACE_SSL supports no flags for sending at this time.
82 ssize_t
send (const void *buf
,
86 /// Recv an @a n byte buffer from the ssl socket using the semantics of
89 * ACE_SSL supports MSG_PEEK, but no other flags at this time.
91 ssize_t
recv (void *buf
,
95 /// Send an @a n byte buffer to the ssl socket using the semantics of
97 ssize_t
send (const void *buf
,
100 /// Recv an @a n byte buffer from the ssl socket using the semantics of
102 ssize_t
recv (void *buf
,
105 /// Send an iovec of size @a n to the ssl socket.
107 * Note that it is not possible to perform a "scattered" write with
108 * the underlying OpenSSL implementation. As such, the expected
109 * semantics are not fully reproduced with this implementation.
111 ssize_t
sendv (const iovec iov
[],
113 const ACE_Time_Value
*timeout
= 0) const;
116 * Allows a client to read from a socket without having to provide a
117 * buffer to read. This method determines how much data is in the
118 * socket, allocates a buffer of this size, reads in the data, and
119 * returns the number of bytes read. The caller is responsible for
120 * deleting the member in the iov_base field of io_vec using
121 * delete [] io_vec->iov_base.
123 ssize_t
recvv (iovec
*io_vec
,
124 const ACE_Time_Value
*timeout
= 0) const;
127 * Wait to timeout amount of time to send up to n bytes into buf
128 * (uses the send() call). If send() times out -1 is returned with
129 * errno == ETIME. If it succeeds the number of bytes sent is
130 * returned. No flags are supported.
132 ssize_t
send (const void *buf
,
135 const ACE_Time_Value
*timeout
) const;
138 * Wait up to timeout amount of time to receive up to @a n bytes into
139 * @a buf (uses the recv() call). If recv() times out -1 is returned
140 * with errno == ETIME. If it succeeds the number of bytes received
141 * is returned. MSG_PEEK is the only supported flag.
143 ssize_t
recv (void *buf
,
146 const ACE_Time_Value
*timeout
) const;
149 * Wait to to timeout amount of time to send up to @a n bytes into
150 * @a buf (uses the send() call). If send() times out
151 * a -1 is returned with errno == ETIME. If it succeeds the
152 * number of bytes sent is returned.
154 ssize_t
send (const void *buf
,
156 const ACE_Time_Value
*timeout
) const;
159 * Wait up to timeout amount of time to receive up to @a n bytes
160 * into @a buf (uses the recv() call). If recv() times
161 * out a -1 is returned with @c errno == ETIME. If it succeeds the
162 * number of bytes received is returned.
164 ssize_t
recv (void *buf
,
166 const ACE_Time_Value
*timeout
) const;
168 /// Send @a n varargs messages to the connected ssl socket.
169 ssize_t
send (size_t n
,
172 /// Recv @a n varargs messages to the connected ssl socket.
173 ssize_t
recv (size_t n
,
176 /// Send @a n bytes, keep trying until n are sent.
177 ssize_t
send_n (const void *buf
, int n
) const;
179 /// Recv @a n bytes, keep trying until @a n are received.
180 ssize_t
recv_n (void *buf
, int n
) const;
183 * @note In the following four methods, only MSG_PEEK is supported
184 * for recv_n(), and no flags are supported for send_n().
187 /// Send @a n bytes, keep trying until @a n are sent.
188 ssize_t
send_n (const void *buf
, int n
, int flags
) const;
190 /// Recv @a n bytes, keep trying until @a n are sent.
191 ssize_t
recv_n (void *buf
, int n
, int flags
) const;
194 * Try to send exactly @a len bytes into @a buf (uses the send() call).
195 * If send() blocks for longer than timeout the number of bytes
196 * actually sent is returned with errno == ETIME. If a timeout does
197 * not occur, send_n() return len (i.e., the number of bytes
198 * requested to be sent).
200 ssize_t
send_n (const void *buf
,
203 const ACE_Time_Value
*timeout
,
204 size_t *bytes_transferred
= 0) const;
207 * Try to send exactly @a len bytes into @a buf (uses the send() call).
208 * If send() blocks for longer than timeout the number of bytes
209 * actually sent is returned with errno == ETIME. If a timeout does
210 * not occur, send_n() return len (i.e., the number of bytes
211 * requested to be sent).
213 ssize_t
send_n (const void *buf
,
215 const ACE_Time_Value
*timeout
,
216 size_t *bytes_transferred
= 0) const;
219 * Try to receive exactly @a len bytes into @a buf (uses the recv() call).
220 * The ACE_Time_Value indicates how long to blocking trying to
221 * receive. If timeout == 0, the caller will block until action is
222 * possible, else will wait until the relative time specified in
223 * timeout elapses). If recv() blocks for longer than timeout the
224 * number of bytes actually read is returned with errno == ETIME.
225 * If a timeout does not occur, recv_n return len (i.e., the number
226 * of bytes requested to be read).
228 ssize_t
recv_n (void *buf
,
231 const ACE_Time_Value
*timeout
,
232 size_t *bytes_transferred
= 0) const;
235 * Try to receive exactly len bytes into buf (uses the recv() call).
236 * The ACE_Time_Value indicates how long to blocking trying to
237 * receive. If timeout == 0, the caller will block until action is
238 * possible, else will wait until the relative time specified in
239 * timeout elapses). If recv() blocks for longer than timeout the
240 * number of bytes actually read is returned with errno == ETIME.
241 * If a timeout does not occur, recv_n return len (i.e., the number
242 * of bytes requested to be read).
244 ssize_t
recv_n (void *buf
,
246 const ACE_Time_Value
*timeout
,
247 size_t *bytes_transferred
= 0) const;
251 * Send an iovec of size n to the connected socket. Will block
252 * until all bytes are sent or an error occurs.
254 ssize_t
sendv_n (const iovec iov
[],
257 /// Receive an iovec of size n to the connected socket.
258 ssize_t
recvv_n (iovec iov
[],
262 * Selectively close endpoints.
265 /// Close down the reader.
268 /// Close down the writer.
272 ///Close down the socket.
276 typedef ACE_INET_Addr PEER_ADDR
;
278 /// Declare the dynamic allocation hooks.
279 ACE_ALLOC_HOOK_DECLARE
;
281 /// Overridden set_handle() method.
283 * Only an ACE_SSL_SOCK_Acceptor or ACE_SSL_SOCK_Connector should
284 * access this method since some state in the underlying "ssl_" data
285 * structure is set during SSL connection establishment.
287 void set_handle (ACE_HANDLE fd
);
289 /// Return a pointer to the underlying SSL structure.
293 * Return the address of the remotely connected peer (if there is
294 * one), in the referenced ACE_Addr. Returns 0 if successful, else
297 * @note If the TCP connection has been completed but the SSL
298 * connection has not been completed yet, -1 will be
301 int get_remote_addr (ACE_Addr
&) const;
303 /// Return the underlying ACE_SOCK_Stream which ACE_SSL runs atop of.
304 ACE_SOCK_Stream
& peer ();
307 /// Underlying send() helper method common to all public send()
309 ssize_t
send_i (const void *buf
,
313 /// Underlying send() helper method common to all public send()
315 ssize_t
recv_i (void *buf
,
318 const ACE_Time_Value
*timeout
) const;
321 void operator= (const ACE_SSL_SOCK_Stream
&) = delete;
322 ACE_SSL_SOCK_Stream (const ACE_SSL_SOCK_Stream
&) = delete;
328 /// The stream which works under the ssl connection.
329 ACE_SOCK_Stream stream_
;
332 ACE_END_VERSIONED_NAMESPACE_DECL
334 #if defined (__ACE_INLINE__)
335 #include "SSL_SOCK_Stream.inl"
336 #endif /* __ACE_INLINE__ */
338 #include /**/ "ace/post.h"
340 #endif /* ACE_SSL_SOCK_STREAM_H */