summaryrefslogtreecommitdiff
path: root/ace/SSL/SSL_Asynch_BIO.cpp
blob: 972a20e441b31818c8807982318c13c91bd26b62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// -*- C++ -*-

#include "SSL_Asynch_BIO.h"

#if OPENSSL_VERSION_NUMBER > 0x0090581fL && ((defined (ACE_WIN32) && !defined (ACE_HAS_WINCE)) || (defined (ACE_HAS_AIO_CALLS)))

#include "SSL_Asynch_Stream.h"
#include "ace/OS_NS_string.h"


ACE_RCSID (ACE_SSL,
           SSL_Asynch_BIO,
           "$Id$")


#if (defined (ACE_HAS_VERSIONED_NAMESPACE) && ACE_HAS_VERSIONED_NAMESPACE == 1)
# define ACE_ASYNCH_BIO_WRITE_NAME ACE_PREPROC_CONCATENATE(ACE_VERSIONED_NAMESPACE_NAME, _ACE_Asynch_BIO_write)
# define ACE_ASYNCH_BIO_READ_NAME  ACE_PREPROC_CONCATENATE(ACE_VERSIONED_NAMESPACE_NAME, _ACE_Asynch_BIO_read)
# define ACE_ASYNCH_BIO_PUTS_NAME  ACE_PREPROC_CONCATENATE(ACE_VERSIONED_NAMESPACE_NAME, _ACE_Asynch_BIO_puts)
# define ACE_ASYNCH_BIO_CTRL_NAME  ACE_PREPROC_CONCATENATE(ACE_VERSIONED_NAMESPACE_NAME, _ACE_Asynch_BIO_ctrl)
# define ACE_ASYNCH_BIO_NEW_NAME   ACE_PREPROC_CONCATENATE(ACE_VERSIONED_NAMESPACE_NAME, _ACE_Asynch_BIO_new)
# define ACE_ASYNCH_BIO_FREE_NAME  ACE_PREPROC_CONCATENATE(ACE_VERSIONED_NAMESPACE_NAME, _ACE_Asynch_BIO_free)
#endif  /* ACE_HAS_VERSIONED_NAMESPACE == 1 */


/**
 * @name OpenSSL BIO Helper Methods for use with ACE's Asynchronous
 *       SSL I/O support.
 */
//@{
extern "C"
{
  int  ACE_ASYNCH_BIO_WRITE_NAME (BIO *pBIO, const char *buf, int len);
  int  ACE_ASYNCH_BIO_READ_NAME  (BIO *pBIO, char *buf, int len);
  int  ACE_ASYNCH_BIO_PUTS_NAME  (BIO *pBIO, const char *str);
  long ACE_ASYNCH_BIO_CTRL_NAME  (BIO *pBIO, int cmd, long arg1, void *arg2);
  int  ACE_ASYNCH_BIO_NEW_NAME   (BIO *pBIO);
  int  ACE_ASYNCH_BIO_FREE_NAME  (BIO *pBIO);
}
//@}

#define BIO_TYPE_ACE  ( 21 | BIO_TYPE_SOURCE_SINK )

static BIO_METHOD methods_ACE =
  {
    BIO_TYPE_ACE, // BIO_TYPE_PROXY_SERVER,
    "ACE_Asynch_BIO",
    ACE_ASYNCH_BIO_WRITE_NAME,
    ACE_ASYNCH_BIO_READ_NAME,
    ACE_ASYNCH_BIO_PUTS_NAME,
    NULL, /* ACE_ASYNCH_BIO_GETS_NAME, */
    ACE_ASYNCH_BIO_CTRL_NAME,
    ACE_ASYNCH_BIO_NEW_NAME,
    ACE_ASYNCH_BIO_FREE_NAME,
    NULL
  };

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

BIO *
ACE_SSL_make_BIO (void * ssl_asynch_stream)
{
  BIO * const pBIO = BIO_new (&methods_ACE);

  if (pBIO)
    BIO_ctrl (pBIO,
              BIO_C_SET_FILE_PTR,
              0,
              ssl_asynch_stream);

  return pBIO;
}

/**
 * @struct @c ACE_SSL_Asynch_Stream_Accessor
 *
 * @brief Privileged @c ACE_SSL_Asynch_Stream accessor.
 *
 * This structure is a @c friend to the @c ACE_SSL_Asynch_Stream
 * class so that it can gain access to the protected
 * ssl_bio_{read,write}() methods in that class.  It is full declared
 * in this implementation file to hide its interface from users to
 * prevent potential abuse of the friend relationship between it and
 * the @c ACE_SSL_Asynch_Stream class.
 */
struct ACE_SSL_Asynch_Stream_Accessor
{
  static int read (ACE_SSL_Asynch_Stream * stream,
               char * buf,
               size_t len,
               int & errval)
  {
    return stream->ssl_bio_read (buf, len, errval);
  }

  static int write (ACE_SSL_Asynch_Stream * stream,
                    const char * buf,
                    size_t len,
                    int & errval)
  {
    return stream->ssl_bio_write (buf, len, errval);
  }
};

ACE_END_VERSIONED_NAMESPACE_DECL

int
ACE_ASYNCH_BIO_NEW_NAME (BIO * pBIO)
{
  pBIO->init  = 0;    // not initialized
  pBIO->num   = 0;    // still zero ( we can use it )
  pBIO->ptr   = 0;    // will be pointer to ACE_SSL_Asynch_Stream
  pBIO->flags = 0;    //

  return 1;
}

int
ACE_ASYNCH_BIO_FREE_NAME (BIO * pBIO)
{
  if (pBIO && pBIO->shutdown)
    {
      pBIO->ptr   = 0;
      pBIO->init  = 0;
      pBIO->num   = 0;
      pBIO->flags = 0;

      return 1;
    }

  return 0;
}

int
ACE_ASYNCH_BIO_READ_NAME (BIO * pBIO, char * buf, int len)
{
  BIO_clear_retry_flags (pBIO);

  ACE_SSL_Asynch_Stream * const p_stream =
    static_cast<ACE_SSL_Asynch_Stream *> (pBIO->ptr);

  if (pBIO->init == 0 || p_stream == 0 || buf == 0 || len <= 0)
    return -1;

  BIO_clear_retry_flags (pBIO);

  int errval = 0;

  int retval =
    ACE_SSL_Asynch_Stream_Accessor::read (p_stream,
                                          buf,
                                          len,
                                          errval);

  if (retval >= 0)
    return retval;

  if (errval == EINPROGRESS)
    BIO_set_retry_read (pBIO);

  return -1;
}

int
ACE_ASYNCH_BIO_WRITE_NAME (BIO * pBIO, const char * buf, int len)
{
  BIO_clear_retry_flags (pBIO);

  ACE_SSL_Asynch_Stream * p_stream =
    static_cast<ACE_SSL_Asynch_Stream *> (pBIO->ptr);

  if (pBIO->init == 0 || p_stream == 0 || buf == 0 || len <= 0)
    return -1;

  BIO_clear_retry_flags (pBIO);

  int errval = 0;

  int retval =
    ACE_SSL_Asynch_Stream_Accessor::write (p_stream,
                                           buf,
                                           len,
                                           errval);

  if (retval >= 0)
    return retval;

  if (errval == EINPROGRESS)
    BIO_set_retry_write (pBIO);

  return -1;
}

long
ACE_ASYNCH_BIO_CTRL_NAME (BIO * pBIO, int cmd, long num, void *ptr)
{
  long ret = 1;

  switch (cmd)
    {
    case BIO_C_SET_FILE_PTR:
      pBIO->shutdown = static_cast<int> (num);
      pBIO->ptr = ptr;
      pBIO->init = 1;
      break;

    case BIO_CTRL_INFO:
      ret = 0;
      break;

    case BIO_CTRL_GET_CLOSE:
      ret = pBIO->shutdown;
      break;

    case BIO_CTRL_SET_CLOSE:
      pBIO->shutdown = static_cast<int> (num);
      break;

    case BIO_CTRL_PENDING:
    case BIO_CTRL_WPENDING:
      ret = 0;
      break;

    case BIO_CTRL_DUP:
    case BIO_CTRL_FLUSH:
      ret = 1;
      break;

    default:
      ret = 0;
      break;
  }

  return ret;
}

int
ACE_ASYNCH_BIO_PUTS_NAME (BIO *pBIO, const char *str)
{
  size_t const n = ACE_OS::strlen (str);

  return ACE_ASYNCH_BIO_WRITE_NAME (pBIO, str, n);
}

#endif  /* OPENSSL_VERSION_NUMBER > 0x0090581fL && (ACE_WIN32 ||
           ACE_HAS_AIO_CALLS) */