Document return values
[ACE_TAO.git] / ACE / ace / SSL / SSL_Asynch_BIO.cpp
blob0317ccbd12c958174daab3e89f007fdc44a32881
1 // -*- C++ -*-
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)
17 #else
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 */
26 /**
27 * @name OpenSSL BIO Helper Methods for use with ACE's Asynchronous
28 * SSL I/O support.
30 //@{
31 extern "C"
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);
40 //@}
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,
48 "ACE_Asynch_BIO",
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
68 #else
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
75 BIO *
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);
80 #else
81 if (!methods_ACE)
83 methods_ACE = BIO_meth_new(BIO_TYPE_ACE, "ACE_Asynch_BIO");
84 if (methods_ACE)
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);
95 #endif
97 if (pBIO)
98 BIO_ctrl (pBIO,
99 BIO_C_SET_FILE_PTR,
101 ssl_asynch_stream);
103 return pBIO;
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,
121 char * buf,
122 size_t len,
123 int & errval)
125 return stream->ssl_bio_read (buf, len, errval);
128 static int write (ACE_SSL_Asynch_Stream * stream,
129 const char * buf,
130 size_t len,
131 int & errval)
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);
147 return 1;
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);
160 return 1;
163 return 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)
175 return -1;
177 BIO_clear_retry_flags (pBIO);
179 int errval = 0;
181 int retval =
182 ACE_SSL_Asynch_Stream_Accessor::read (p_stream,
183 buf,
184 len,
185 errval);
187 if (retval >= 0)
188 return retval;
190 if (errval == EINPROGRESS)
191 BIO_set_retry_read (pBIO);
193 return -1;
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)
205 return -1;
207 BIO_clear_retry_flags (pBIO);
209 int errval = 0;
211 int retval =
212 ACE_SSL_Asynch_Stream_Accessor::write (p_stream,
213 buf,
214 len,
215 errval);
217 if (retval >= 0)
218 return retval;
220 if (errval == EINPROGRESS)
221 BIO_set_retry_write (pBIO);
223 return -1;
226 long
227 ACE_ASYNCH_BIO_CTRL_NAME (BIO * pBIO, int cmd, long num, void *ptr)
229 long ret = 1;
231 switch (cmd)
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);
237 break;
239 case BIO_CTRL_INFO:
240 ret = 0;
241 break;
243 case BIO_CTRL_GET_CLOSE:
244 ret = BIO_get_shutdown(pBIO);
245 break;
247 case BIO_CTRL_SET_CLOSE:
248 BIO_set_shutdown(pBIO, static_cast<int> (num));
249 break;
251 case BIO_CTRL_PENDING:
252 case BIO_CTRL_WPENDING:
253 ret = 0;
254 break;
256 case BIO_CTRL_DUP:
257 case BIO_CTRL_FLUSH:
258 ret = 1;
259 break;
261 default:
262 ret = 0;
263 break;
266 return ret;
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,
275 str,
276 ACE_Utils::truncate_cast<int> (n));
279 #endif /* OPENSSL_VERSION_NUMBER > 0x0090581fL && (ACE_WIN32 || ACE_HAS_AIO_CALLS) */