Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 1 | // Copyright (C) 2010 David Sugar, Tycho Softworks. |
| 2 | // |
| 3 | // This file is part of GNU uCommon C++. |
| 4 | // |
| 5 | // GNU uCommon C++ is free software: you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU Lesser General Public License as published |
| 7 | // by the Free Software Foundation, either version 3 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // GNU uCommon C++ is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU Lesser General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU Lesser General Public License |
| 16 | // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | #include "local.h" |
| 19 | |
| 20 | #if defined(OLD_STDCPP) || defined(NEW_STDCPP) |
| 21 | |
| 22 | sstream::sstream(secure::client_t scontext) : |
| 23 | tcpstream() |
| 24 | { |
| 25 | context *ctx = (context *)scontext; |
| 26 | ssl = NULL; |
| 27 | bio = NULL; |
| 28 | server = false; |
| 29 | |
| 30 | if(ctx && ctx->ctx && ctx->err() == secure::OK) |
| 31 | ssl = SSL_new(ctx->ctx); |
| 32 | } |
| 33 | |
| 34 | sstream::sstream(const TCPServer *tcp, secure::server_t scontext, size_t size) : |
| 35 | tcpstream(tcp, size) |
| 36 | { |
| 37 | context *ctx = (context *)scontext; |
| 38 | ssl = NULL; |
| 39 | bio = NULL; |
| 40 | server = true; |
| 41 | |
| 42 | if(ctx && ctx->ctx && ctx->err() == secure::OK) |
| 43 | ssl = SSL_new(ctx->ctx); |
| 44 | |
| 45 | if(!is_open() || !ssl) |
| 46 | return; |
| 47 | |
| 48 | SSL_set_fd((SSL *)ssl, getsocket()); |
| 49 | |
| 50 | if(SSL_accept((SSL *)ssl) > 0) |
| 51 | bio = SSL_get_wbio((SSL *)ssl); |
| 52 | } |
| 53 | |
| 54 | sstream::~sstream() |
| 55 | { |
| 56 | release(); |
| 57 | } |
| 58 | |
| 59 | void sstream::open(const char *host, const char *service, size_t size) |
| 60 | { |
| 61 | if(server) |
| 62 | return; |
| 63 | |
| 64 | close(); |
| 65 | tcpstream::open(host, service, size); |
| 66 | |
| 67 | if(!is_open() || !ssl) |
| 68 | return; |
| 69 | |
| 70 | SSL_set_fd((SSL *)ssl, getsocket()); |
| 71 | |
| 72 | if(SSL_connect((SSL *)ssl) > 0) |
| 73 | bio = SSL_get_wbio((SSL *)ssl); |
| 74 | } |
| 75 | |
| 76 | void sstream::close(void) |
| 77 | { |
| 78 | if(server) |
| 79 | return; |
| 80 | |
| 81 | if(bio) { |
| 82 | SSL_shutdown((SSL *)ssl); |
| 83 | bio = NULL; |
| 84 | } |
| 85 | |
| 86 | tcpstream::close(); |
| 87 | } |
| 88 | |
| 89 | void sstream::release(void) |
| 90 | { |
| 91 | server = false; |
| 92 | close(); |
| 93 | |
| 94 | if(ssl) { |
| 95 | SSL_free((SSL *)ssl); |
| 96 | ssl = NULL; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | ssize_t sstream::_write(const char *address, size_t size) |
| 101 | { |
| 102 | if(!bio) |
| 103 | return tcpstream::_write(address, size); |
| 104 | |
| 105 | return SSL_write((SSL *)ssl, address, size); |
| 106 | } |
| 107 | |
| 108 | ssize_t sstream::_read(char *address, size_t size) |
| 109 | { |
| 110 | if(!bio) |
| 111 | return tcpstream::_read(address, size); |
| 112 | |
| 113 | return SSL_read((SSL *)ssl, address, size); |
| 114 | } |
| 115 | |
| 116 | bool sstream::_wait(void) |
| 117 | { |
| 118 | if(so == INVALID_SOCKET) |
| 119 | return false; |
| 120 | |
| 121 | if(ssl && SSL_pending((SSL *)ssl)) |
| 122 | return true; |
| 123 | |
| 124 | return tcpstream::_wait(); |
| 125 | } |
| 126 | |
| 127 | int sstream::sync() |
| 128 | { |
| 129 | int rtn = tcpstream::sync(); |
| 130 | if(bio) |
| 131 | rtn = BIO_flush((BIO *)bio); |
| 132 | |
| 133 | return rtn; |
| 134 | } |
| 135 | |
| 136 | #endif |