diff options
Diffstat (limited to '')
| -rwxr-xr-x | build.sh | 2 | ||||
| -rwxr-xr-x | build_static.sh | 9 | ||||
| -rw-r--r-- | client.cpp | 14 | ||||
| -rw-r--r-- | core.h | 16 | ||||
| -rw-r--r-- | main.cpp | 5 | ||||
| -rw-r--r-- | netcore.cpp | 21 | ||||
| -rw-r--r-- | server.cpp | 5 | ||||
| -rw-r--r-- | socketcommon.cpp | 31 | 
8 files changed, 88 insertions, 15 deletions
| @@ -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 + @@ -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! @@ -1,6 +1,9 @@  #ifndef CORE_H  #define CORE_H  +// Set in build.sh +//#define USE_GNUTLS +  #include <string.h>  #include <stdint.h>  #include <stdlib.h> @@ -13,13 +16,16 @@  #include <sys/socket.h>  #include <sys/select.h>  #include <netinet/in.h> -#include <gnutls/gnutls.h>  #include <list>  #include <map>  #include <string>  #include "buffer.h" +#ifdef USE_GNUTLS +#include <gnutls/gnutls.h> +#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 */ @@ -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(); +				}  			}  		} @@ -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();  		} | 
