Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: MiniTLS-HTTPS-Example
TLSSocket.cpp
00001 /* 00002 MiniTLS - A super trimmed down TLS/SSL Library for embedded devices 00003 Author: Donatien Garnier 00004 Copyright (C) 2013-2014 AppNearMe Ltd 00005 00006 This program is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU General Public License 00008 as published by the Free Software Foundation; either version 2 00009 of the License, or (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 *//** 00020 * \file TLSSocket.cpp 00021 * \copyright Copyright (c) AppNearMe Ltd 2013 00022 * \author Donatien Garnier 00023 */ 00024 00025 #define __DEBUG__ 0//4 00026 #ifndef __MODULE__ 00027 #define __MODULE__ "TLSSocket.cpp" 00028 #endif 00029 00030 #include "core/fwk.h" 00031 #include "TLSSocket.h" 00032 00033 00034 #include "tls/minitls.h" 00035 #include "tls/tls_socket.h" 00036 00037 /** Instantiate a TLS socket 00038 * 00039 */ 00040 TLSSocket::TLSSocket(MiniTLS* pMiniTLS) : m_pMiniTLS(pMiniTLS) 00041 { 00042 00043 } 00044 00045 TLSSocket::~TLSSocket() 00046 { 00047 00048 } 00049 00050 /** Initialize socket 00051 * \return MINITLS_OK on success, MINITLS_ERR_* error code otherwise 00052 */ 00053 minitls_err_t TLSSocket::init() 00054 { 00055 int ret = tls_socket_init(&m_sock, &m_pMiniTLS->m_minitls, m_writeBuf, sizeof(m_writeBuf), m_readBuf, sizeof(m_readBuf)); 00056 if(ret) 00057 { 00058 ERR("Could not init socket: error %d", ret); 00059 return ret; 00060 } 00061 return MINITLS_OK; 00062 } 00063 00064 00065 /** Connect to server 00066 * \param hostname server to connect to 00067 * \param port port to connect to 00068 * \param timeout timeout in ms 00069 * \return MINITLS_OK on success, MINITLS_ERR_* error code otherwise 00070 */ 00071 minitls_err_t TLSSocket::connect(const char* hostname, uint16_t port, int timeout) 00072 { 00073 minitls_err_t ret = tls_socket_connect(&m_sock, hostname, port, timeout); 00074 if(ret) 00075 { 00076 return ret; 00077 } 00078 return MINITLS_OK; 00079 } 00080 00081 /** Read data from server 00082 * \param buf buffer to read bytes to 00083 * \param minLength minimum number of bytes to read (will block before this number of bytes are read) 00084 * \param maxLength maximum number of bytes to read 00085 * \param readLength will receive actual number of bytes read 00086 * \param timeout timeout in ms 00087 * \return MINITLS_OK on success, MINITLS_ERR_* error code otherwise 00088 */ 00089 minitls_err_t TLSSocket::read(uint8_t* buf, size_t minLength, size_t maxLength, size_t* readLength, int timeout) 00090 { 00091 if(minLength > maxLength) 00092 { 00093 return MINITLS_ERR_PARAMETERS; 00094 } 00095 00096 *readLength = 0; 00097 00098 minitls_err_t ret; 00099 while( *readLength < minLength ) 00100 { 00101 ret = tls_socket_flush_read(&m_sock, timeout); 00102 if(ret == MINITLS_ERR_SOCKET_CLOSED) 00103 { 00104 return MINITLS_OK; 00105 } 00106 else if(ret) 00107 { 00108 return ret; 00109 } 00110 00111 size_t bytesRead = 0; 00112 ret = tls_socket_read(&m_sock, buf + *readLength, maxLength - *readLength, &bytesRead); 00113 if(ret) 00114 { 00115 return ret; 00116 } 00117 00118 *readLength += bytesRead; 00119 } 00120 00121 return MINITLS_OK; 00122 } 00123 00124 /** Write data to server 00125 * \param buf buffer to write bytes from 00126 * \param length number of bytes to write 00127 * \param writtenLength will receive actual number of bytes written 00128 * \param timeout timeout in ms 00129 * \return MINITLS_OK on success, MINITLS_ERR_* error code otherwise 00130 */ 00131 minitls_err_t TLSSocket::write(uint8_t* buf, size_t length, size_t* writtenLength, int timeout) 00132 { 00133 *writtenLength = 0; 00134 00135 minitls_err_t ret; 00136 while( *writtenLength < length ) 00137 { 00138 size_t bytesToWrite = length - *writtenLength; 00139 size_t bytesWritten = 0; 00140 ret = tls_socket_write(&m_sock, buf + *writtenLength, bytesToWrite, &bytesWritten); 00141 if(ret == MINITLS_ERR_SOCKET_CLOSED) 00142 { 00143 return MINITLS_OK; 00144 } 00145 else if(ret) 00146 { 00147 return ret; 00148 } 00149 00150 *writtenLength += bytesWritten; 00151 00152 //Flush only if needed 00153 if( bytesWritten < bytesToWrite ) 00154 { 00155 ret = tls_socket_flush_write(&m_sock, timeout); 00156 if(ret) 00157 { 00158 return ret; 00159 } 00160 } 00161 } 00162 00163 return MINITLS_OK; 00164 } 00165 00166 /** Call to ensure transmission of bytes written using write 00167 * \param timeout timeout in ms 00168 * \return MINITLS_OK on success, MINITLS_ERR_* error code otherwise 00169 */ 00170 minitls_err_t TLSSocket::flush(int timeout) 00171 { 00172 minitls_err_t ret = tls_socket_flush_write(&m_sock, timeout); 00173 if(ret) 00174 { 00175 return ret; 00176 } 00177 return MINITLS_OK; 00178 } 00179 00180 /** Tear down TLS connection and close socket 00181 * \return MINITLS_OK on success, MINITLS_ERR_* error code otherwise 00182 */ 00183 minitls_err_t TLSSocket::close() 00184 { 00185 minitls_err_t ret = tls_socket_close(&m_sock); 00186 if(ret) 00187 { 00188 return ret; 00189 } 00190 return MINITLS_OK; 00191 } 00192
Generated on Wed Jul 13 2022 00:22:55 by
1.7.2