GARFIELD YOU LAZY CAT WHY ARE YOU SO BIG AND FAT

This commit is contained in:
MallocNull 2018-09-18 16:46:23 -05:00
parent 6c20ba38d9
commit 94b5783d3b
5 changed files with 63 additions and 8 deletions

1
.gitignore vendored
View file

@ -1,6 +1,7 @@
# additions by alec (tm) # additions by alec (tm)
build/ build/
connectionStrings.config connectionStrings.config
*.pem
*.kdev4 *.kdev4
.kdev4/ .kdev4/
.idea/ .idea/

View file

@ -65,10 +65,14 @@ else()
"src/server/*.h" "src/server/*.h"
) )
find_package(OpenSSL)
add_executable(server ${server_src}) add_executable(server ${server_src})
target_include_directories(server target_include_directories(server
PRIVATE ${PROJECT_SOURCE_DIR}/src/common PRIVATE ${PROJECT_SOURCE_DIR}/src/common
PRIVATE ${PROJECT_SOURCE_DIR}/src/server) PRIVATE ${PROJECT_SOURCE_DIR}/src/server
PRIVATE ${OPENSSL_INCLUDE_DIR})
target_link_libraries(server ${OPENSSL_LIBRARIES})
install(TARGETS server RUNTIME DESTINATION bin/server) install(TARGETS server RUNTIME DESTINATION bin/server)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")

14
src/server/common.hpp Normal file
View file

@ -0,0 +1,14 @@
#ifndef SOSC_SERVER_COMMON_H
#define SOSC_SERVER_COMMON_H
#include <string>
#ifdef SOSC_DEBUG
#define SOSC_RESOURCE_PATH (std::string("../resources/client/"))
#else
#define SOSC_RESOURCE_PATH (std::string("resources/"))
#endif
#define SOSC_RESC(X) (SOSC_RESOURCE_PATH + std::string(X))
#endif

View file

@ -31,9 +31,13 @@
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <mutex>
#include "utils/net.hpp" #include "utils/net.hpp"
#include "utils/string.hpp" #include "utils/string.hpp"
#include <openssl/ssl.h>
#include <openssl/err.h>
#define SOSC_TCP_BUFLEN 2048 #define SOSC_TCP_BUFLEN 2048
#define SOSC_TCP_APPEND 1 #define SOSC_TCP_APPEND 1
@ -43,7 +47,7 @@ namespace sosc {
class TcpClient { class TcpClient {
public: public:
TcpClient(); TcpClient();
bool Open(std::string host, uint16_t port); bool Open(std::string host, uint16_t port, bool secure = false);
int Receive(std::string* str, int flags = 0); int Receive(std::string* str, int flags = 0);
bool Send(const std::string& str); bool Send(const std::string& str);
@ -60,11 +64,13 @@ public:
void Close(); void Close();
private: private:
void Open(SOSC_SOCK_T sock, SOSC_ADDR_T addr, int addr_len); void Open(SOSC_SOCK_T sock, SOSC_ADDR_T addr,
int addr_len, bool secure = false);
void SetBlocking(bool will_block); void SetBlocking(bool will_block);
SOSC_SOCK_T sock; SOSC_SOCK_T sock;
bool sock_open; bool sock_open;
SSL* ssl;
net::IpAddress ip; net::IpAddress ip;
SOSC_ADDR_T addr; SOSC_ADDR_T addr;
@ -78,7 +84,7 @@ private:
class TcpServer { class TcpServer {
public: public:
TcpServer(); TcpServer();
bool Listen(uint16_t port); bool Listen(uint16_t port, bool secure = false);
bool Accept(TcpClient* client); bool Accept(TcpClient* client);
@ -86,6 +92,7 @@ public:
private: private:
SOSC_SOCK_T sock; SOSC_SOCK_T sock;
bool sock_open; bool sock_open;
bool secure;
}; };
} }

View file

@ -1,6 +1,32 @@
#include "tcpsock.hpp" #include "tcpsock.hpp"
#ifndef _WIN32 #ifndef _WIN32
static struct {
SSL_CTX* ssl_server;
SSL_CTX* ssl_client;
std::mutex ssl_mtx;
} _ssl_ctx;
static bool ssl_init() {
static bool is_inited = false;
if(is_inited) return true;
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
_ssl_ctx.ssl_server = SSL_CTX_new(SSLv23_server_method());
if(!_ssl_ctx.ssl_server)
return false;
_ssl_ctx.ssl_client = SSL_CTX_new(SSLv23_client_method());
if(!_ssl_ctx.ssl_client)
return false;
is_inited = true;
return true;
}
/****************************/ /****************************/
/* BEGIN TCPCLIENT CODE */ /* BEGIN TCPCLIENT CODE */
/****************************/ /****************************/
@ -10,7 +36,7 @@ sosc::TcpClient::TcpClient() {
this->addr_len = -1; this->addr_len = -1;
} }
bool sosc::TcpClient::Open(std::string host, std::uint16_t port) { bool sosc::TcpClient::Open(std::string host, std::uint16_t port, bool secure) {
if(this->sock_open) if(this->sock_open)
return false; return false;
@ -164,7 +190,9 @@ sosc::TcpServer::TcpServer() {
this->sock_open = false; this->sock_open = false;
} }
bool sosc::TcpServer::Listen(uint16_t port) { bool sosc::TcpServer::Listen(uint16_t port, bool secure) {
if(secure && !ssl_init())
return false;
if(this->sock_open) if(this->sock_open)
return false; return false;
@ -197,8 +225,9 @@ bool sosc::TcpServer::Listen(uint16_t port) {
close(this->sock); close(this->sock);
return false; return false;
} }
this->sock_open = true; this->sock_open = true;
this->secure = secure;
return true; return true;
} }
@ -217,7 +246,7 @@ bool sosc::TcpServer::Accept(TcpClient* client) {
} }
client->Close(); client->Close();
client->Open(sock, addr, addr_len); client->Open(sock, addr, addr_len, this->secure);
return true; return true;
} }