Go to the documentation of this file.00001
00002
00003
00004
00005
00006 #include "SSL_SOCK_Acceptor.h"
00007
00008 #include "ace/Handle_Set.h"
00009 #include "ace/OS_Errno.h"
00010 #include "ace/OS_NS_errno.h"
00011 #include "ace/Log_Msg.h"
00012 #include "ace/Time_Value.h"
00013 #include "ace/Countdown_Time.h"
00014 #include "ace/Truncate.h"
00015
00016 #if !defined (__ACE_INLINE__)
00017 #include "SSL_SOCK_Acceptor.inl"
00018 #endif
00019
00020 ACE_RCSID (ACE_SSL,
00021 SSL_SOCK_Acceptor,
00022 "$Id: SSL_SOCK_Acceptor.cpp 82723 2008-09-16 09:35:44Z johnnyw $")
00023
00024 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00025
00026 ACE_ALLOC_HOOK_DEFINE(ACE_SSL_SOCK_Acceptor)
00027
00028 ACE_SSL_SOCK_Acceptor::~ACE_SSL_SOCK_Acceptor (void)
00029 {
00030 ACE_TRACE ("ACE_SSL_SOCK_Acceptor::~ACE_SSL_SOCK_Acceptor");
00031 }
00032
00033
00034 int
00035 ACE_SSL_SOCK_Acceptor::ssl_accept (ACE_SSL_SOCK_Stream &new_stream,
00036 ACE_Time_Value *timeout) const
00037 {
00038 SSL *ssl = new_stream.ssl ();
00039
00040 if (SSL_is_init_finished (ssl))
00041 return 0;
00042
00043 if (!SSL_in_accept_init (ssl))
00044 ::SSL_set_accept_state (ssl);
00045
00046 ACE_HANDLE handle = new_stream.get_handle ();
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 int reset_blocking_mode = 0;
00057 if (timeout != 0)
00058 {
00059 reset_blocking_mode = ACE_BIT_DISABLED (ACE::get_flags (handle),
00060 ACE_NONBLOCK);
00061
00062
00063 if (reset_blocking_mode
00064 && ACE::set_flags (handle,
00065 ACE_NONBLOCK) == -1)
00066 return -1;
00067 }
00068
00069
00070 ACE_Countdown_Time countdown (timeout);
00071
00072 int status;
00073 do
00074 {
00075
00076
00077 ACE_Handle_Set rd_handle;
00078 ACE_Handle_Set wr_handle;
00079
00080 status = ::SSL_accept (ssl);
00081 switch (::SSL_get_error (ssl, status))
00082 {
00083 case SSL_ERROR_NONE:
00084 status = 0;
00085 break;
00086
00087 case SSL_ERROR_WANT_WRITE:
00088 wr_handle.set_bit (handle);
00089 status = 1;
00090 break;
00091
00092 case SSL_ERROR_WANT_READ:
00093 rd_handle.set_bit (handle);
00094 status = 1;
00095 break;
00096
00097 case SSL_ERROR_ZERO_RETURN:
00098
00099
00100
00101 status = -1;
00102 break;
00103
00104 case SSL_ERROR_SYSCALL:
00105
00106
00107
00108
00109
00110
00111
00112
00113 if (ACE_OS::set_errno_to_last_error () == EWOULDBLOCK &&
00114 status == -1)
00115 {
00116
00117
00118
00119 status = 1;
00120 if (SSL_want_write (ssl))
00121 wr_handle.set_bit (handle);
00122 else if (SSL_want_read (ssl))
00123 rd_handle.set_bit (handle);
00124 else
00125 status = -1;
00126 }
00127 else
00128 status = -1;
00129 break;
00130
00131 default:
00132 ACE_SSL_Context::report_error ();
00133 status = -1;
00134 break;
00135 }
00136
00137 if (status == 1)
00138 {
00139
00140 ACE_ASSERT (rd_handle.num_set() == 1 || wr_handle.num_set () == 1);
00141 status = ACE::select (int (handle) + 1,
00142 &rd_handle,
00143 &wr_handle,
00144 0,
00145 timeout);
00146
00147 (void) countdown.update ();
00148
00149
00150
00151
00152
00153 if (status >= 1)
00154 status = 1;
00155 else
00156 status = -1;
00157 }
00158
00159 } while (status == 1 && !SSL_is_init_finished (ssl));
00160
00161 if (reset_blocking_mode)
00162 {
00163 ACE_Errno_Guard eguard (errno);
00164 ACE::clr_flags (handle, ACE_NONBLOCK);
00165 }
00166
00167 return (status == -1 ? -1 : 0);
00168
00169 }
00170
00171
00172
00173
00174
00175 int
00176 ACE_SSL_SOCK_Acceptor::accept (ACE_SSL_SOCK_Stream &new_stream,
00177 ACE_Addr *remote_addr,
00178 ACE_Time_Value *timeout,
00179 bool restart,
00180 bool reset_new_handle) const
00181 {
00182 ACE_TRACE ("ACE_SSL_SOCK_Acceptor::accept");
00183
00184
00185
00186 ACE_Countdown_Time countdown (timeout);
00187
00188 ACE_SOCK_Stream temp_stream;
00189 if (-1 == this->acceptor_.accept (temp_stream,
00190 remote_addr,
00191 timeout,
00192 restart,
00193 reset_new_handle))
00194 return -1;
00195
00196 (void) countdown.update ();
00197
00198 new_stream.set_handle (temp_stream.get_handle ());
00199 temp_stream.set_handle (ACE_INVALID_HANDLE);
00200
00201 if (this->ssl_accept (new_stream, timeout) == -1)
00202 {
00203 new_stream.close ();
00204 new_stream.set_handle (ACE_INVALID_HANDLE);
00205 return -1;
00206 }
00207
00208 return 0;
00209
00210 }
00211
00212 int
00213 ACE_SSL_SOCK_Acceptor::accept (ACE_SSL_SOCK_Stream &new_stream,
00214 ACE_Accept_QoS_Params qos_params,
00215 ACE_Addr *remote_addr,
00216 ACE_Time_Value *timeout,
00217 bool restart,
00218 bool reset_new_handle) const
00219 {
00220 ACE_TRACE ("ACE_SSL_SOCK_Acceptor::accept");
00221
00222
00223
00224 ACE_Countdown_Time countdown (timeout);
00225
00226 ACE_SOCK_Stream temp_stream;
00227 if (-1 == this->acceptor_.accept (temp_stream,
00228 qos_params,
00229 remote_addr,
00230 timeout,
00231 restart,
00232 reset_new_handle))
00233 return -1;
00234
00235 (void) countdown.update ();
00236
00237 new_stream.set_handle (temp_stream.get_handle ());
00238 temp_stream.set_handle (ACE_INVALID_HANDLE);
00239
00240 if (this->ssl_accept (new_stream, timeout) == -1)
00241 {
00242 new_stream.close ();
00243 new_stream.set_handle (ACE_INVALID_HANDLE);
00244 return -1;
00245 }
00246
00247 return 0;
00248 }
00249
00250 ACE_END_VERSIONED_NAMESPACE_DECL