2 #include "SSL_Asynch_BIO.h"
4 #if OPENSSL_VERSION_NUMBER > 0x0090581fL && (defined (ACE_WIN32) || (defined (ACE_HAS_AIO_CALLS)))
6 #include "SSL_Asynch_Stream.h"
7 #include "ace/OS_NS_string.h"
8 #include "ace/Truncate.h"
10 #if (defined (ACE_HAS_VERSIONED_NAMESPACE) && ACE_HAS_VERSIONED_NAMESPACE == 1)
11 # define ACE_ASYNCH_BIO_WRITE_NAME ACE_PREPROC_CONCATENATE(ACE_VERSIONED_NAMESPACE_NAME, _ACE_Asynch_BIO_write)
12 # define ACE_ASYNCH_BIO_READ_NAME ACE_PREPROC_CONCATENATE(ACE_VERSIONED_NAMESPACE_NAME, _ACE_Asynch_BIO_read)
13 # define ACE_ASYNCH_BIO_PUTS_NAME ACE_PREPROC_CONCATENATE(ACE_VERSIONED_NAMESPACE_NAME, _ACE_Asynch_BIO_puts)
14 # define ACE_ASYNCH_BIO_CTRL_NAME ACE_PREPROC_CONCATENATE(ACE_VERSIONED_NAMESPACE_NAME, _ACE_Asynch_BIO_ctrl)
15 # define ACE_ASYNCH_BIO_NEW_NAME ACE_PREPROC_CONCATENATE(ACE_VERSIONED_NAMESPACE_NAME, _ACE_Asynch_BIO_new)
16 # define ACE_ASYNCH_BIO_FREE_NAME ACE_PREPROC_CONCATENATE(ACE_VERSIONED_NAMESPACE_NAME, _ACE_Asynch_BIO_free)
18 # define ACE_ASYNCH_BIO_WRITE_NAME ACE_Asynch_BIO_write
19 # define ACE_ASYNCH_BIO_READ_NAME ACE_Asynch_BIO_read
20 # define ACE_ASYNCH_BIO_PUTS_NAME ACE_Asynch_BIO_puts
21 # define ACE_ASYNCH_BIO_CTRL_NAME ACE_Asynch_BIO_ctrl
22 # define ACE_ASYNCH_BIO_NEW_NAME ACE_Asynch_BIO_new
23 # define ACE_ASYNCH_BIO_FREE_NAME ACE_Asynch_BIO_free
24 #endif /* ACE_HAS_VERSIONED_NAMESPACE == 1 */
27 * @name OpenSSL BIO Helper Methods for use with ACE's Asynchronous
33 int ACE_ASYNCH_BIO_WRITE_NAME (BIO
*pBIO
, const char *buf
, int len
);
34 int ACE_ASYNCH_BIO_READ_NAME (BIO
*pBIO
, char *buf
, int len
);
35 int ACE_ASYNCH_BIO_PUTS_NAME (BIO
*pBIO
, const char *str
);
36 long ACE_ASYNCH_BIO_CTRL_NAME (BIO
*pBIO
, int cmd
, long arg1
, void *arg2
);
37 int ACE_ASYNCH_BIO_NEW_NAME (BIO
*pBIO
);
38 int ACE_ASYNCH_BIO_FREE_NAME (BIO
*pBIO
);
42 #define BIO_TYPE_ACE ( 21 | BIO_TYPE_SOURCE_SINK )
44 #if OPENSSL_VERSION_NUMBER < 0x10100000L || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x30500000L)
45 static BIO_METHOD methods_ACE
=
47 BIO_TYPE_ACE
, // BIO_TYPE_PROXY_SERVER,
49 ACE_ASYNCH_BIO_WRITE_NAME
,
50 ACE_ASYNCH_BIO_READ_NAME
,
51 ACE_ASYNCH_BIO_PUTS_NAME
,
52 0, /* ACE_ASYNCH_BIO_GETS_NAME, */
53 ACE_ASYNCH_BIO_CTRL_NAME
,
54 ACE_ASYNCH_BIO_NEW_NAME
,
55 ACE_ASYNCH_BIO_FREE_NAME
,
58 # define BIO_set_init(b, val) b->init = val
59 # define BIO_set_data(b, val) b->ptr = val
60 # define BIO_set_num(b, val) b->num = val
61 # if !defined (BIO_set_flags)
62 # define BIO_set_flags(b, val) b->flags = val
63 # endif /* !BIO_set_flags */
64 # define BIO_set_shutdown(b, val) b->shutdown = val
65 # define BIO_get_init(b) b->init
66 # define BIO_get_data(b) b->ptr
67 # define BIO_get_shutdown(b) b->shutdown
69 static BIO_METHOD
* methods_ACE
;
70 # define BIO_set_num(b, val)
71 #endif /* OPENSSL_VERSION_NUMBER < 0x10100000L || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x30500000L) */
73 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
76 ACE_SSL_make_BIO (void * ssl_asynch_stream
)
78 #if OPENSSL_VERSION_NUMBER < 0x10100000L || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x30500000L)
79 BIO
* const pBIO
= BIO_new (&methods_ACE
);
83 methods_ACE
= BIO_meth_new(BIO_TYPE_ACE
, "ACE_Asynch_BIO");
86 BIO_meth_set_write(methods_ACE
, ACE_ASYNCH_BIO_WRITE_NAME
);
87 BIO_meth_set_read(methods_ACE
, ACE_ASYNCH_BIO_READ_NAME
);
88 BIO_meth_set_puts(methods_ACE
, ACE_ASYNCH_BIO_PUTS_NAME
);
89 BIO_meth_set_ctrl(methods_ACE
, ACE_ASYNCH_BIO_CTRL_NAME
);
90 BIO_meth_set_create(methods_ACE
, ACE_ASYNCH_BIO_NEW_NAME
);
91 BIO_meth_set_destroy(methods_ACE
, ACE_ASYNCH_BIO_FREE_NAME
);
94 BIO
* const pBIO
= BIO_new (methods_ACE
);
107 * @struct @c ACE_SSL_Asynch_Stream_Accessor
109 * @brief Privileged @c ACE_SSL_Asynch_Stream accessor.
111 * This structure is a @c friend to the @c ACE_SSL_Asynch_Stream
112 * class so that it can gain access to the protected
113 * ssl_bio_{read,write}() methods in that class. It is full declared
114 * in this implementation file to hide its interface from users to
115 * prevent potential abuse of the friend relationship between it and
116 * the @c ACE_SSL_Asynch_Stream class.
118 struct ACE_SSL_Asynch_Stream_Accessor
120 static int read (ACE_SSL_Asynch_Stream
* stream
,
125 return stream
->ssl_bio_read (buf
, len
, errval
);
128 static int write (ACE_SSL_Asynch_Stream
* stream
,
133 return stream
->ssl_bio_write (buf
, len
, errval
);
137 ACE_END_VERSIONED_NAMESPACE_DECL
140 ACE_ASYNCH_BIO_NEW_NAME (BIO
* pBIO
)
142 BIO_set_init(pBIO
, 0); // not initialized
143 BIO_set_num(pBIO
, 0); // still zero ( we can use it )
144 BIO_set_data(pBIO
, 0); // will be pointer to ACE_SSL_Asynch_Stream
145 BIO_set_flags(pBIO
, 0);
151 ACE_ASYNCH_BIO_FREE_NAME (BIO
* pBIO
)
153 if (pBIO
&& BIO_get_shutdown(pBIO
))
155 BIO_set_data(pBIO
, 0);
156 BIO_set_init(pBIO
, 0);
157 BIO_set_num(pBIO
, 0);
158 BIO_set_flags(pBIO
, 0);
167 ACE_ASYNCH_BIO_READ_NAME (BIO
* pBIO
, char * buf
, int len
)
169 BIO_clear_retry_flags (pBIO
);
171 ACE_SSL_Asynch_Stream
* const p_stream
=
172 static_cast<ACE_SSL_Asynch_Stream
*> (BIO_get_data(pBIO
));
174 if (BIO_get_init(pBIO
) == 0 || p_stream
== 0 || buf
== 0 || len
<= 0)
177 BIO_clear_retry_flags (pBIO
);
182 ACE_SSL_Asynch_Stream_Accessor::read (p_stream
,
190 if (errval
== EINPROGRESS
)
191 BIO_set_retry_read (pBIO
);
197 ACE_ASYNCH_BIO_WRITE_NAME (BIO
* pBIO
, const char * buf
, int len
)
199 BIO_clear_retry_flags (pBIO
);
201 ACE_SSL_Asynch_Stream
* p_stream
=
202 static_cast<ACE_SSL_Asynch_Stream
*> (BIO_get_data(pBIO
));
204 if (BIO_get_init(pBIO
) == 0 || p_stream
== 0 || buf
== 0 || len
<= 0)
207 BIO_clear_retry_flags (pBIO
);
212 ACE_SSL_Asynch_Stream_Accessor::write (p_stream
,
220 if (errval
== EINPROGRESS
)
221 BIO_set_retry_write (pBIO
);
227 ACE_ASYNCH_BIO_CTRL_NAME (BIO
* pBIO
, int cmd
, long num
, void *ptr
)
233 case BIO_C_SET_FILE_PTR
:
234 BIO_set_shutdown(pBIO
, static_cast<int> (num
));
235 BIO_set_data(pBIO
, ptr
);
236 BIO_set_init(pBIO
, 1);
243 case BIO_CTRL_GET_CLOSE
:
244 ret
= BIO_get_shutdown(pBIO
);
247 case BIO_CTRL_SET_CLOSE
:
248 BIO_set_shutdown(pBIO
, static_cast<int> (num
));
251 case BIO_CTRL_PENDING
:
252 case BIO_CTRL_WPENDING
:
270 ACE_ASYNCH_BIO_PUTS_NAME (BIO
*pBIO
, const char *str
)
272 size_t const n
= ACE_OS::strlen (str
);
274 return ACE_ASYNCH_BIO_WRITE_NAME (pBIO
,
276 ACE_Utils::truncate_cast
<int> (n
));
279 #endif /* OPENSSL_VERSION_NUMBER > 0x0090581fL && (ACE_WIN32 || ACE_HAS_AIO_CALLS) */