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 // TODO consider changing this
return this->sock_open; return this->sock_open;
} }
inline bool IsSecure() const {
return this->ssl != nullptr;
}
inline net::IpAddress GetIpAddress() const { inline net::IpAddress GetIpAddress() const {
return this->ip; return this->ip;

View file

@ -55,7 +55,7 @@ bool sosc::TcpClient::Open(std::string host, std::uint16_t port, bool secure) {
else { else {
_ssl_ctx.client_mtx.lock(); _ssl_ctx.client_mtx.lock();
this->ssl = SSL_new(_ssl_ctx.client); this->ssl = SSL_new(_ssl_ctx.client);
_ssl_ctx.client_mtx.lock(); _ssl_ctx.client_mtx.unlock();
SSL_set_fd(this->ssl, this->sock); SSL_set_fd(this->ssl, this->sock);
if(SSL_connect(this->ssl) != 1) { if(SSL_connect(this->ssl) != 1) {
@ -76,8 +76,20 @@ void sosc::TcpClient::Open
this->sock = sock; this->sock = sock;
this->sock_open = true; this->sock_open = true;
if(!secure) if(!secure)
this->ssl = nullptr; 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 = addr;
this->addr_len = addr_len; 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()) while(block ? (first_recv ? true : this->IsDataReady())
: 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) { if(length <= 0) {
this->Close(); this->Close();
return -1; return -1;
@ -123,12 +138,17 @@ bool sosc::TcpClient::Send(const std::string& str) {
std::string::size_type total_sent = 0; std::string::size_type total_sent = 0;
while(total_sent < str.length()) { while(total_sent < str.length()) {
int sent = total_sent == 0 int sent = (total_sent == 0)
? send(this->sock, str.c_str(), str.length(), 0) ? (this->ssl == nullptr)
: send(this->sock, str.substr(total_sent).c_str(), ? (int)send(this->sock, str.c_str(), str.length(), 0)
str.length() - total_sent, 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(); this->Close();
return false; return false;
} else } else
@ -175,6 +195,9 @@ void sosc::TcpClient::Close() {
this->sock_open = false; this->sock_open = false;
shutdown(this->sock, SHUT_RDWR); shutdown(this->sock, SHUT_RDWR);
close(this->sock); 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->ip = net::IpAddress();
this->sock_open = true; 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; return true;
} }
void sosc::TcpClient::Open 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) if(this->sock_open)
return; return;
this->sock = sock; this->sock = sock;
this->sock_open = true; 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 = addr;
this->addr_len = addr_len; 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()) while(block ? (first_recv ? true : this->IsDataReady())
: 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) { if(length <= 0) {
this->Close(); this->Close();
return -1; return -1;
@ -120,10 +155,15 @@ bool sosc::TcpClient::Send(const std::string& str) {
std::string::size_type total_sent = 0; std::string::size_type total_sent = 0;
while(total_sent < str.length()) { while(total_sent < str.length()) {
int sent = total_sent == 0 int sent = (total_sent == 0)
? send(this->sock, str.c_str(), str.length(), 0) ? (this->ssl == nullptr)
: send(this->sock, str.substr(total_sent).c_str(), ? (int)send(this->sock, str.c_str(), str.length(), 0)
str.length() - total_sent, 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) { if(sent == SOCKET_ERROR) {
this->Close(); this->Close();