securer boob

This commit is contained in:
malloc 2018-09-20 19:01:17 -05:00
parent 0e129180e3
commit e30c56041d
3 changed files with 80 additions and 13 deletions

View file

@ -59,6 +59,10 @@ public:
// TODO consider changing this
return this->sock_open;
}
inline bool IsSecure() const {
return this->ssl != nullptr;
}
inline net::IpAddress GetIpAddress() const {
return this->ip;

View file

@ -55,7 +55,7 @@ bool sosc::TcpClient::Open(std::string host, std::uint16_t port, bool secure) {
else {
_ssl_ctx.client_mtx.lock();
this->ssl = SSL_new(_ssl_ctx.client);
_ssl_ctx.client_mtx.lock();
_ssl_ctx.client_mtx.unlock();
SSL_set_fd(this->ssl, this->sock);
if(SSL_connect(this->ssl) != 1) {
@ -76,8 +76,20 @@ void sosc::TcpClient::Open
this->sock = sock;
this->sock_open = true;
if(!secure)
this->ssl = nullptr;
else {
_ssl_ctx.client_mtx.lock();
this->ssl = SSL_new(_ssl_ctx.server);
_ssl_ctx.client_mtx.unlock();
SSL_set_fd(this->ssl, this->sock);
if(SSL_accept(this->ssl) <= 0) {
this->Close();
return;
}
}
this->addr = addr;
this->addr_len = addr_len;
@ -99,7 +111,10 @@ int sosc::TcpClient::Receive(std::string* str, int flags) {
while(block ? (first_recv ? true : this->IsDataReady())
: this->IsDataReady())
{
int length = recv(this->sock, this->buffer, SOSC_TCP_BUFLEN, 0);
int length = (this->ssl == nullptr)
? (int)recv(this->sock, this->buffer, SOSC_TCP_BUFLEN, 0)
: (int)SSL_read(this->ssl, this->buffer, SOSC_TCP_BUFLEN);
if(length <= 0) {
this->Close();
return -1;
@ -123,12 +138,17 @@ bool sosc::TcpClient::Send(const std::string& str) {
std::string::size_type total_sent = 0;
while(total_sent < str.length()) {
int sent = total_sent == 0
? send(this->sock, str.c_str(), str.length(), 0)
: send(this->sock, str.substr(total_sent).c_str(),
str.length() - total_sent, 0);
int sent = (total_sent == 0)
? (this->ssl == nullptr)
? (int)send(this->sock, str.c_str(), str.length(), 0)
: (int)SSL_write(this->ssl, str.c_str(), str.length())
: (this->ssl == nullptr)
? (int)send(this->sock, str.c_str() + total_sent,
str.length() - total_sent, 0)
: (int)SSL_write(this->ssl, str.c_str() + total_sent,
str.length() - total_sent);
if(sent == -1) {
if(sent < 0) {
this->Close();
return false;
} else
@ -175,6 +195,9 @@ void sosc::TcpClient::Close() {
this->sock_open = false;
shutdown(this->sock, SHUT_RDWR);
close(this->sock);
if(this->ssl != nullptr)
SSL_free(this->ssl);
}
/****************************/

View file

@ -64,17 +64,49 @@ bool sosc::TcpClient::Open(std::string host, std::uint16_t port, bool secure) {
this->ip = net::IpAddress();
this->sock_open = true;
if(!secure)
this->ssl = nullptr;
else {
_ssl_ctx.client_mtx.lock();
this->ssl = SSL_new(_ssl_ctx.client);
_ssl_ctx.client_mtx.unlock();
SSL_set_fd(this->ssl, this->sock);
if(SSL_connect(this->ssl) != 1) {
SSL_free(this->ssl);
this->Close();
return false;
}
}
return true;
}
void sosc::TcpClient::Open
(SOSC_SOCK_T sock, SOSC_ADDR_T addr, int addr_len)
(SOSC_SOCK_T sock, SOSC_ADDR_T addr, int addr_len, bool secure)
{
if(secure && !ssl_init())
return false;
if(this->sock_open)
return;
this->sock = sock;
this->sock_open = true;
if(!secure)
this->ssl = nullptr;
else {
_ssl_ctx.client_mtx.lock();
this->ssl = SSL_new(_ssl_ctx.server);
_ssl_ctx.client_mtx.unlock();
SSL_set_fd(this->ssl, this->sock);
if(SSL_accept(this->ssl) <= 0) {
this->Close();
return;
}
}
this->addr = addr;
this->addr_len = addr_len;
@ -96,7 +128,10 @@ int sosc::TcpClient::Receive(std::string* str, int flags) {
while(block ? (first_recv ? true : this->IsDataReady())
: this->IsDataReady())
{
int length = recv(this->sock, this->buffer, SOSC_TCP_BUFLEN, 0);
int length = (this->ssl == nullptr)
? (int)recv(this->sock, this->buffer, SOSC_TCP_BUFLEN, 0)
: (int)SSL_read(this->ssl, this->buffer, SOSC_TCP_BUFLEN);
if(length <= 0) {
this->Close();
return -1;
@ -120,10 +155,15 @@ bool sosc::TcpClient::Send(const std::string& str) {
std::string::size_type total_sent = 0;
while(total_sent < str.length()) {
int sent = total_sent == 0
? send(this->sock, str.c_str(), str.length(), 0)
: send(this->sock, str.substr(total_sent).c_str(),
str.length() - total_sent, 0);
int sent = (total_sent == 0)
? (this->ssl == nullptr)
? (int)send(this->sock, str.c_str(), str.length(), 0)
: (int)SSL_write(this->ssl, str.c_str(), str.length())
: (this->ssl == nullptr)
? (int)send(this->sock, str.c_str() + total_sent,
str.length() - total_sent, 0)
: (int)SSL_write(this->ssl, str.c_str() + total_sent,
str.length() - total_sent);
if(sent == SOCKET_ERROR) {
this->Close();