00001 #include "headers.h"
00009
00010
00011 Ssl::Ssl()
00012 {
00013 m_ssl = NULL;
00014 m_sock = OS_SPEC_INVALID_SOCKET;
00015 m_timeout_infinite = true;
00016 m_timeout.tv_sec = 0;
00017 m_timeout.tv_usec = 0;
00018 }
00019
00020
00021 Ssl::~Ssl()
00022 {
00023 shutdown();
00024 }
00025
00026
00036 void
00037 Ssl::setTimeOut(bool infinite, int tv_sec, int tv_usec)
00038 {
00039 m_timeout_infinite = infinite;
00040 m_timeout.tv_sec = tv_sec;
00041 m_timeout.tv_usec = tv_usec;
00042 }
00043
00044
00053 bool
00054 Ssl::open(bool server_mode, SslCtx &ssl_ctx, OS_SPEC_SOCKET_TYPE sock)
00055 {
00056 bool returnValue = false;
00057
00058 SSL* ssl = NULL;
00059
00060 int err;
00061
00062 ssl = SSL_new(ssl_ctx.getSslCtx());
00063 if (!ssl) {
00064 returnValue = false;
00065 goto finalize;
00066 }
00067
00068 SSL_set_fd(ssl, (int)sock);
00069
00070 if (server_mode) {
00071 err = SSL_accept(ssl);
00072 } else {
00073 err = SSL_connect(ssl);
00074 }
00075 if (err <= 0) {
00076 returnValue = false;
00077 goto finalize;
00078 }
00079
00080 m_sock = sock;
00081 m_ssl = ssl;
00082
00083 returnValue = true;
00084 goto finalize;
00085
00086 finalize:
00087 if (!returnValue) {
00088 debug(DEBUG_CONN, "Error opening SSL connection");
00089
00090 if (ssl) {
00091 SSL_free (ssl);
00092 }
00093 }
00094
00095 return returnValue;
00096 }
00097
00098
00102 void
00103 Ssl::shutdown(void)
00104 {
00105 if (m_ssl) {
00106 SSL_shutdown(m_ssl);
00107 SSL_free(m_ssl);
00108 m_ssl = NULL;
00109 }
00110 }
00111
00112
00127 int
00128 Ssl::read(void* buff, int count)
00129 {
00130 int rn = -1;
00131 fd_set fdset;
00132
00133 FD_ZERO(&fdset);
00134 OS_SPEC_FD_SET(m_sock, &fdset);
00135 if (select(m_sock+1, &fdset, NULL, NULL,
00136 m_timeout_infinite ? NULL : &m_timeout) <= 0) {
00137 return -10;
00138 }
00139
00140 if (!OS_SPEC_FD_ISSET(m_sock, &fdset)) {
00141 return -10;
00142 }
00143
00144 Guard guard(&m_lock);
00145 rn = SSL_read (m_ssl, (char*)buff, count);
00146
00147 return rn;
00148 }
00149
00150
00166 int
00167 Ssl::write(void* buff, int count)
00168 {
00169 int wn = -1;
00170 fd_set fdset;
00171
00172 FD_ZERO(&fdset);
00173 OS_SPEC_FD_SET(m_sock, &fdset);
00174 if (select(m_sock+1, NULL, &fdset, NULL,
00175 m_timeout_infinite ? NULL : &m_timeout) <= 0) {
00176 return -10;
00177 }
00178
00179 if (!OS_SPEC_FD_ISSET(m_sock, &fdset)) {
00180 return -10;
00181 }
00182
00183 Guard guard(&m_lock);
00184 wn = SSL_write (m_ssl, (char*)buff, count);
00185
00186 return wn;
00187 }
00188
00189
00193 SSL*
00194 Ssl::getSsl(void)
00195 {
00196 return m_ssl;
00197 }
00198
00199
00203 OS_SPEC_SOCKET_TYPE
00204 Ssl::getStream(void) {
00205 return m_sock;
00206 }
00207