diff --git a/Makefile b/Makefile index 76c0b18..c338512 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,9 @@ DEFAULT_TCPPORT=6131 DEFAULT_UDPPORT=6131 DEFAULT_SERVER=localhost -OBJLIB=network.o udp.o tcp.o unix.o ssl.o json.o -INCLIB=UDPTCPNetwork.h +CFLAGS=-I./ +OBJLIB=network.o udp.o tcp.o unix.o ssl.o json.o webutils.o webserver.o +INCLIB=UDPTCPNetwork.h UDPTCPNetwork-json.h UDPTCPNetwork-webutils.h DISTNAME=libUDPTCPNetwork-$(VERSION) ifeq ($(TARGET),) @@ -17,7 +18,9 @@ noconfig: help endif -all: dep $(TARGET) test-udp test-tcpserver test-tcpclient test-ssl test-json +all: dep $(TARGET) test-udp test-tcpserver test-tcpclient test-ssl test-json test-webserver + +-include .depend help: echo "set up configuration" @@ -31,6 +34,9 @@ configlinux: clean configwindows: clean cp -f Makefile.rules.windows Makefile.rules +test-webserver: $(TARGET) test-webserver.o + $(CXX) test-webserver.o -o $@ -lUDPTCPNetwork -L./ -I./ $(LDFLAGS) + test-tcpserver: $(TARGET) test-tcpserver.o $(CXX) test-tcpserver.o -o $@ -lUDPTCPNetwork -L./ -I./ $(LDFLAGS) @@ -52,17 +58,17 @@ keygen: install: $(TARGET) cp -f $(TARGET) $(PREFIX)/lib/ - ln -s $(PREFIX)/lib/$(TARGET) $(PREFIX)/lib/lib$(OBJLIB_NAME).so - cp -f UDPTCPNetwork.h $(PREFIX)/include/ + ln -sf $(PREFIX)/lib/$(TARGET) $(PREFIX)/lib/lib$(OBJLIB_NAME).so + cp -f UDPTCPNetwork*.h $(PREFIX)/include/ uninstall: rm -f $(PREFIX)/lib/$(TARGET) rm -f $(PREFIX)/lib/lib$(OBJLIB_NAME).so - rm -f $(PREFIX)/include/UDPTCPNetwork.h + rm -f $(PREFIX)/include/UDPTCPNetwork*.h rebuild: clean all -$(TARGET): $(OBJLIB) $(INCLIB) +$(TARGET): $(OBJLIB) $(CXX) -shared $(LINKPARAMS) -o $(TARGET) $^ $(LDFLAGS) if test -f "lib$(OBJLIB_NAME).so.$(VERSION)"; then ln -sf $(TARGET) lib$(OBJLIB_NAME).so; fi ar rcs lib$(OBJLIB_NAME).a $(OBJLIB) @@ -74,6 +80,7 @@ clean: rm Makefile.rules -rf rm test-tcpserver -rf rm test-tcpclient -rf + rm test-webserver -rf rm test-json -rf rm test-udp -rf rm test-ssl -rf diff --git a/UDPTCPNetwork.h b/UDPTCPNetwork.h index 517b169..9283c26 100644 --- a/UDPTCPNetwork.h +++ b/UDPTCPNetwork.h @@ -73,12 +73,17 @@ std::string string_to_lower(std::string s); std::string string_to_upper(std::string s); int file_is_readable (const char *fname); + +#ifndef UDPTCPNETWORK_DISABLE_DEBUG +void debug_set_logfile (std::string fname); void _debug (char *srcfile, int srcline, char *srcfunc, char *fmt,...); void _errorexit (char *srcfile, int srcline, char *srcfunc, char *fmt,...); #define debug(...) _debug ((char*)__FILE__, __LINE__, (char*)__FUNCTION__, (char*)__VA_ARGS__) #define errorexit(...) _errorexit ((char*)__FILE__, __LINE__, (char*)__FUNCTION__, (char*)__VA_ARGS__) +#endif + /************************************************************************ * * udp related functions @@ -228,7 +233,8 @@ public: }; #endif -#include "UDPTCPNetwork-json.h" +#include +#include #endif diff --git a/network.cc b/network.cc index 0931b44..26472ac 100644 --- a/network.cc +++ b/network.cc @@ -13,6 +13,7 @@ using namespace std; char dnsip[NET_HOSTLEN]; int UDPTCPNetwork_init = 0; +std::string debug_file = ""; // // convert host and port to sockaddr_in6 @@ -95,8 +96,13 @@ std::string string_to_lower(std::string s) { }; -#define LEN_TEXT 128 -#define DEBUG_TEXT1LEN 2048 +void debug_set_logfile(std::string fname) { + debug_file = fname; +}; + + +#define LEN_TEXT 256 +#define DEBUG_TEXT1LEN 4096 #define DEBUG_TEXT2LEN (DEBUG_TEXT1LEN+LEN_TEXT) void _debug (char *srcfile, int srcline, char *srcfunc, char *fmt,...) { va_list args; @@ -121,20 +127,19 @@ void _debug (char *srcfile, int srcline, char *srcfunc, char *fmt,...) { if (tmptr != NULL) strftime(texttime, LEN_TEXT, "%H:%M:%S", &tmp); - snprintf (text2, DEBUG_TEXT2LEN-1, "%s.%03d %d %s:%d %s %s", texttime, tv.tv_usec/1000, pid, + snprintf (text2, DEBUG_TEXT2LEN-1, "%s.%03d %d %s:%d %s %s\n", texttime, tv.tv_usec/1000, pid, srcfile, srcline, srcfunc, text1); printf ("%s", text2); -/* - if (type == 0 || (type & _debuglevel) != 0) { + + if (debug_file.length() > 0) { FILE *f; printf ("%s\n", text2); - f= fopen(DEBUG_FILE, "a"); + f= fopen(debug_file.c_str(), "a"); if (f) { fprintf (f, "%s\n", text2); fclose (f); } } -*/ }; diff --git a/ssl.cc b/ssl.cc index aff56e5..68be5f2 100644 --- a/ssl.cc +++ b/ssl.cc @@ -113,7 +113,10 @@ int SSLSocket::Connect (int sockfd, int block_timeout) { TimeoutReset(); sslerror = SSL_ERROR_NONE; - NewClientCTX(); + if (NewServerCTX() == 0) { + debug ("error on NewServerCTX()\n"); + return 0; + } timeout = block_timeout; if (sockfd > 0 && block_timeout > 0) { @@ -127,6 +130,9 @@ int SSLSocket::Connect (int sockfd, int block_timeout) { } ssl = SSL_new(ctx); + if (ssl == NULL) { + debug ("SSL_new failed\n"); + } SSL_set_fd (ssl, sockfd); do { @@ -141,13 +147,20 @@ int SSLSocket::Connect (int sockfd, int block_timeout) { +/// @brief +/// @param sockfd +/// @param block_timeout timeout in ms +/// @return 1 on success, 0 on error int SSLSocket::Accept (int sockfd, int block_timeout) { int flags, res; TimeoutReset(); sslerror = SSL_ERROR_NONE; timeout = block_timeout; - NewServerCTX(); + if (NewServerCTX() == 0) { + debug ("error on NewServerCTX()\n"); + return 0; + } if (sockfd > 0 && block_timeout > 0) { #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) @@ -160,6 +173,9 @@ int SSLSocket::Accept (int sockfd, int block_timeout) { } ssl = SSL_new(ctx); + if (ssl == NULL) { + debug ("SSL_new failed\n"); + } SSL_set_fd (ssl, sockfd); do { res = SSL_accept(ssl); @@ -264,6 +280,9 @@ long int SSLSocket::Read (char *buffer, long int len) { if (ret == -1) sslerror = SSL_get_error(ssl, -1); } while (ret == -1 && TimeoutTime() < timeout && (sslerror == SSL_ERROR_WANT_READ || sslerror == SSL_ERROR_WANT_WRITE)); + if (ret == -1 && + (sslerror == SSL_ERROR_WANT_READ || sslerror == SSL_ERROR_WANT_WRITE)) + ret = 0; return ret; }; diff --git a/tcp.cc b/tcp.cc index 9ad1a2a..4e89be6 100644 --- a/tcp.cc +++ b/tcp.cc @@ -65,6 +65,9 @@ TCP::TCP(string h, string p) { }; +/// @brief Listen on a specific TCP port for incomming connections. +/// @param port TCP port to listen on +/// @return 1 on success and 0 on error int TCP::Listen(int port) { char buffer[NET_BUFFERSIZE]; int err, i; @@ -122,6 +125,8 @@ int TCP::Listen(int port) { }; +/// @brief check for a new connection +/// @return NULL if there is noone connected or a pointer to the TCP connection. TCP* TCP::Accept() { fd_set rfds; struct timeval tv;