From f923cd4beaf1c97f5873a9549b216c67520a8cca Mon Sep 17 00:00:00 2001 From: Treeki Date: Thu, 23 Jan 2014 23:42:54 +0100 Subject: add build_static.sh, make gnutls usage a define --- build.sh | 2 +- build_static.sh | 9 +++++++++ client.cpp | 14 +++++++++++--- core.h | 16 ++++++++++++++-- main.cpp | 5 ++++- netcore.cpp | 21 +++++++++++++++++++-- server.cpp | 5 ++++- socketcommon.cpp | 31 ++++++++++++++++++++++++++----- 8 files changed, 88 insertions(+), 15 deletions(-) create mode 100755 build_static.sh diff --git a/build.sh b/build.sh index 72a77af..935cae6 100755 --- a/build.sh +++ b/build.sh @@ -3,7 +3,7 @@ mkdir -p binary NETCODE="socketcommon.cpp client.cpp mobileclient.cpp server.cpp ircserver.cpp netcore.cpp" SOURCES="$NETCODE main.cpp window.cpp dns.cpp" -FLAGS="-std=c++11 -lgnutls -pthread -g" +FLAGS="-std=c++11 -DUSE_GNUTLS -lgnutls -pthread -g" g++ -o binary/nb4 $FLAGS $SOURCES diff --git a/build_static.sh b/build_static.sh new file mode 100755 index 0000000..6ed9183 --- /dev/null +++ b/build_static.sh @@ -0,0 +1,9 @@ +#!/bin/sh +mkdir -p binary + +NETCODE="socketcommon.cpp client.cpp mobileclient.cpp server.cpp ircserver.cpp netcore.cpp" +SOURCES="$NETCODE main.cpp window.cpp dns.cpp" +FLAGS="-static -static-libgcc -static-libstdc++ -std=c++11 -pthread" + +g++ -o binary/nb4_static $FLAGS $SOURCES + diff --git a/client.cpp b/client.cpp index 9cbb889..4fa0a12 100644 --- a/client.cpp +++ b/client.cpp @@ -39,6 +39,7 @@ void Client::startService(int _sock, bool withTls) { return; } +#ifdef USE_GNUTLS if (withTls) { int initRet = gnutls_init(&tls, GNUTLS_SERVER); if (initRet != GNUTLS_E_SUCCESS) { @@ -75,7 +76,9 @@ void Client::startService(int _sock, bool withTls) { state = CS_TLS_HANDSHAKE; printf("[fd=%d] preparing for TLS handshake\n", sock); - } else { + } else +#endif + { state = CS_CONNECTED; } } @@ -260,14 +263,19 @@ void Client::resumeSession(Client *other, int lastReceivedByClient) { outputBuf.append(other->outputBuf.data(), other->outputBuf.size()); sock = other->sock; + state = other->state; +#ifdef USE_GNUTLS tls = other->tls; tlsActive = other->tlsActive; - state = other->state; +#endif other->sock = -1; + other->state = CS_DISCONNECTED; +#ifdef USE_GNUTLS other->tls = 0; other->tlsActive = false; - other->state = CS_DISCONNECTED; +#endif + other->close(); // Now send them everything we've got! diff --git a/core.h b/core.h index 7d2f052..b5d3164 100644 --- a/core.h +++ b/core.h @@ -1,6 +1,9 @@ #ifndef CORE_H #define CORE_H +// Set in build.sh +//#define USE_GNUTLS + #include #include #include @@ -13,13 +16,16 @@ #include #include #include -#include #include #include #include #include "buffer.h" +#ifdef USE_GNUTLS +#include +#endif + #define CLIENT_LIMIT 100 #define SERVER_LIMIT 20 @@ -125,8 +131,10 @@ protected: ConnState state; int sock; +#ifdef USE_GNUTLS gnutls_session_t tls; bool tlsActive; +#endif public: SocketRWCommon(NetCore *_netCore); @@ -135,11 +143,13 @@ public: virtual void close(); private: +#ifdef USE_GNUTLS bool tryTLSHandshake(); + bool hasTlsPendingData() const; +#endif void readAction(); void writeAction(); - bool hasTlsPendingData() const; virtual void processReadBuffer() = 0; }; @@ -356,6 +366,8 @@ private: // This is ugly as crap, TODO FIXME etc etc +#ifdef USE_GNUTLS extern gnutls_certificate_credentials_t g_serverCreds, g_clientCreds; +#endif #endif /* CORE_H */ diff --git a/main.cpp b/main.cpp index d772d6c..5330310 100644 --- a/main.cpp +++ b/main.cpp @@ -1,10 +1,10 @@ #include "core.h" #include "dns.h" +#ifdef USE_GNUTLS static gnutls_dh_params_t dh_params; gnutls_certificate_credentials_t g_serverCreds, g_clientCreds; - bool initTLS() { int ret; ret = gnutls_global_init(); @@ -39,10 +39,13 @@ bool initTLS() { return true; } +#endif int main(int argc, char **argv) { +#ifdef USE_GNUTLS if (!initTLS()) return EXIT_FAILURE; +#endif DNS::start(); diff --git a/netcore.cpp b/netcore.cpp index 7082f9b..dee6ef7 100644 --- a/netcore.cpp +++ b/netcore.cpp @@ -93,8 +93,10 @@ int NetCore::execute() { time_t now = time(NULL); for (int i = 0; i < clientCount; i++) { +#ifdef USE_GNUTLS if (clients[i]->state == Client::CS_TLS_HANDSHAKE) clients[i]->tryTLSHandshake(); +#endif if (clients[i]->sock != -1) { if (clients[i]->sock > maxFD) @@ -134,10 +136,12 @@ int NetCore::execute() { for (int i = 0; i < serverCount; i++) { if (servers[i]->state == Server::CS_WAITING_DNS) servers[i]->tryConnectPhase(); +#ifdef USE_GNUTLS else if (servers[i]->state == Server::CS_TLS_HANDSHAKE) { if (servers[i]->tryTLSHandshake()) servers[i]->connectedEvent(); } +#endif if (servers[i]->sock != -1) { if (servers[i]->sock > maxFD) @@ -163,8 +167,15 @@ int NetCore::execute() { if (clients[i]->sock != -1) { if (FD_ISSET(clients[i]->sock, &writeSet)) clients[i]->writeAction(); - if (FD_ISSET(clients[i]->sock, &readSet) || clients[i]->hasTlsPendingData()) + + if (FD_ISSET(clients[i]->sock, &readSet) +#ifdef USE_GNUTLS + || clients[i]->hasTlsPendingData() +#endif + ) + { clients[i]->readAction(); + } } } @@ -203,8 +214,14 @@ int NetCore::execute() { } - if (FD_ISSET(servers[i]->sock, &readSet) || servers[i]->hasTlsPendingData()) + if (FD_ISSET(servers[i]->sock, &readSet) +#ifdef USE_GNUTLS + || servers[i]->hasTlsPendingData() +#endif + ) + { servers[i]->readAction(); + } } } diff --git a/server.cpp b/server.cpp index 820c579..16c754b 100644 --- a/server.cpp +++ b/server.cpp @@ -115,6 +115,7 @@ void Server::connectionSuccessful() { outputBuf.clear(); // Do we need to do any TLS junk? +#ifdef USE_GNUTLS if (useTls) { state = CS_TLS_HANDSHAKE; @@ -135,7 +136,9 @@ void Server::connectionSuccessful() { gnutls_transport_set_int(tls, sock); tlsActive = true; - } else { + } else +#endif + { connectedEvent(); } } diff --git a/socketcommon.cpp b/socketcommon.cpp index 897bc58..7bc55b6 100644 --- a/socketcommon.cpp +++ b/socketcommon.cpp @@ -19,12 +19,15 @@ SocketRWCommon::SocketRWCommon(NetCore *_netCore) { netCore = _netCore; sock = -1; state = CS_DISCONNECTED; +#ifdef USE_GNUTLS tlsActive = false; +#endif } SocketRWCommon::~SocketRWCommon() { close(); } +#ifdef USE_GNUTLS bool SocketRWCommon::hasTlsPendingData() const { if (tlsActive) return (gnutls_record_check_pending(tls) > 0); @@ -54,11 +57,14 @@ bool SocketRWCommon::tryTLSHandshake() { return false; } +#endif void SocketRWCommon::close() { if (sock != -1) { +#ifdef USE_GNUTLS if (tlsActive) gnutls_bye(tls, GNUTLS_SHUT_RDWR); +#endif shutdown(sock, SHUT_RDWR); ::close(sock); } @@ -68,10 +74,12 @@ void SocketRWCommon::close() { outputBuf.clear(); state = CS_DISCONNECTED; +#ifdef USE_GNUTLS if (tlsActive) { gnutls_deinit(tls); tlsActive = false; } +#endif } void SocketRWCommon::readAction() { @@ -83,12 +91,15 @@ void SocketRWCommon::readAction() { inputBuf.setCapacity(requiredSize); ssize_t amount; + +#ifdef USE_GNUTLS if (tlsActive) { amount = gnutls_record_recv(tls, &inputBuf.data()[bufSize], 0x200); - } else { - + } else +#endif + { amount = recv(sock, &inputBuf.data()[bufSize], 0x200, @@ -108,12 +119,15 @@ void SocketRWCommon::readAction() { close(); } else if (amount < 0) { +#ifdef USE_GNUTLS if (tlsActive) { if (gnutls_error_is_fatal(amount)) { printf("Error while reading [gnutls %d]!\n", amount); close(); } - } else { + } else +#endif + { perror("Error while reading!"); close(); } @@ -123,11 +137,15 @@ void SocketRWCommon::readAction() { void SocketRWCommon::writeAction() { // What can we get rid of...? ssize_t amount; + +#ifdef USE_GNUTLS if (tlsActive) { amount = gnutls_record_send(tls, outputBuf.data(), outputBuf.size()); - } else { + } else +#endif + { amount = send(sock, outputBuf.data(), outputBuf.size(), @@ -140,12 +158,15 @@ void SocketRWCommon::writeAction() { } else if (amount == 0) printf("Sent 0!\n"); else if (amount < 0) { +#ifdef USE_GNUTLS if (tlsActive) { if (gnutls_error_is_fatal(amount)) { printf("Error while sending [gnutls %d]!\n", amount); close(); } - } else { + } else +#endif + { perror("Error while sending!"); close(); } -- cgit v1.2.3