diff --git a/Changelog b/Changelog index eaadd62..f6e656c 100644 --- a/Changelog +++ b/Changelog @@ -1,3 +1,6 @@ +2020-10-18: +- added support for windows. Everything should work but UNIX sockets. + 2020-10-15: - added: TCP::isListen function diff --git a/Makefile b/Makefile index 7de214c..63aab30 100644 --- a/Makefile +++ b/Makefile @@ -4,9 +4,12 @@ VERSION=0.1 PREFIX=/usr/local ETCPREFIX=/etc +# WINLIBS=-lwsock32 -lws2_32 + CXX=g++ CXXFLAGS= -ggdb -fPIC -pg -Wno-write-strings -I./ -std=c++11 -LDFLAGS= -lm -lc -pg -lssl -lcrypto +# LDFLAGS= -lm -lc -pg -lssl -lcrypto $(WINLIBS) +LDFLAGS= -lm -pg -lssl -lcrypto $(WINLIBS) DEFAULT_TCPPORT=6131 DEFAULT_UDPPORT=6131 @@ -23,13 +26,13 @@ DEPENDFILE=.depend all: dep $(TARGET) test-udp test-tcp test-ssl test-tcp: $(TARGET) test-tcp.o config.h - $(CXX) test-tcp.o -o $@ $(LDFLAGS) -lUDPTCPNetwork -L./ -I./ + $(CXX) test-tcp.o -o $@ -lUDPTCPNetwork -L./ -I./ $(LDFLAGS) test-ssl: $(TARGET) test-ssl.o config.h - $(CXX) test-ssl.o -o $@ $(LDFLAGS) -lUDPTCPNetwork -L./ -I./ + $(CXX) test-ssl.o -o $@ -lUDPTCPNetwork -L./ -I./ $(LDFLAGS) test-udp: $(TARGET) test-udp.o config.h - $(CXX) test-udp.o -o $@ $(LDFLAGS) -lUDPTCPNetwork -L./ -I./ + $(CXX) test-udp.o -o $@ -lUDPTCPNetwork -L./ -I./ $(LDFLAGS) keygen: # openssl req -nodes -new -newkey rsa:2048 -sha256 -out csr.pem -keyout privkey.pem @@ -48,7 +51,8 @@ uninstall: rebuild: clean all $(TARGET): $(OBJLIB) $(INCLIB) - $(CXX) -shared -Wl,-soname,lib$(OBJLIB_NAME).so -o $(TARGET) $^ -lc $(LDFLAGS) + # $(CXX) -shared -Wl,-soname,lib$(OBJLIB_NAME).so -o $(TARGET) $^ -lc $(LDFLAGS) + $(CXX) -shared -Wl,-soname,lib$(OBJLIB_NAME).so -o $(TARGET) $^ $(LDFLAGS) ln -sf $(TARGET) lib$(OBJLIB_NAME).so ar rcs lib$(OBJLIB_NAME).a $(OBJLIB) @@ -68,6 +72,7 @@ clean: rm -rf .depend rm -rf *.so rm -rf *.a + rm -rf *.dll rm -rf *.so.* rm -rf *.pem -rf diff --git a/Makefile.win b/Makefile.win new file mode 100644 index 0000000..d4ab5a7 --- /dev/null +++ b/Makefile.win @@ -0,0 +1,102 @@ +# .SILENT: + +VERSION=0.1 +PREFIX=/usr +ETCPREFIX=/etc + +WINLIBS=-lwsock32 -lws2_32 + +CXX=g++ +CXXFLAGS= -ggdb -fPIC -Wno-write-strings -I./ -std=c++11 +LDFLAGS= -lm -lssl -lcrypto $(WINLIBS) + +DEFAULT_TCPPORT=6131 +DEFAULT_UDPPORT=6131 +DEFAULT_SERVER=localhost + +OBJLIB=network.o udp.o tcp.o unix.o ssl.o +INCLIB=config.h UDPTCPNetwork.h +OBJLIB_NAME=UDPTCPNetwork +TARGET=UDPTCPNetwork.dll + +DISTNAME=libUDPTCPNetwork-$(VERSION) +DEPENDFILE=.depend + +all: dep $(TARGET) test-udp test-tcp test-ssl + +test-tcp: $(TARGET) test-tcp.o config.h + $(CXX) test-tcp.o -o $@ -lUDPTCPNetwork -L./ -I./ $(LDFLAGS) + +test-ssl: $(TARGET) test-ssl.o config.h + $(CXX) test-ssl.o -o $@ -lUDPTCPNetwork -L./ -I./ $(LDFLAGS) + +test-udp: $(TARGET) test-udp.o config.h + $(CXX) test-udp.o -o $@ -lUDPTCPNetwork -L./ -I./ $(LDFLAGS) + +keygen: + # openssl req -nodes -new -newkey rsa:2048 -sha256 -out csr.pem -keyout privkey.pem + openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout privkey.pem -out cert.pem + +install: $(TARGET) + cp -f $(TARGET) $(PREFIX)/lib/ + 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 + +rebuild: clean all + +$(TARGET): $(OBJLIB) $(INCLIB) + $(CXX) -shared -Wl,--out-implib,libUDPTCPNetwork.a -o $(TARGET) $^ $(LDFLAGS) + ln -sf $(TARGET) lib$(OBJLIB_NAME).so + ar rcs lib$(OBJLIB_NAME).a $(OBJLIB) + +dep: + $(CXX) -MM `ls *.cc` $(CXXFLAGS) > $(DEPENDFILE) + +clean: + rm test-tcp -rf + rm test-udp -rf + rm test-ssl -rf + rm -rf gmon.out + rm *.s -rf + rm *.a -rf + rm -rf *.dll + rm *.o -rf + rm *.oo -rf + rm *~ -rf + rm -rf config.h + rm -rf .depend + rm -rf *.so + rm -rf *.a + rm -rf *.so.* + rm -rf *.pem -rf + + +cleanall: clean + +source: cleanall + +config: + echo "#ifndef _CONFIG_H_" > config.h + echo "#define _CONFIG_H_" >> config.h + echo "" >> config.h + echo "#define UDPTCPNETWORK_VERSION \"$(VERSION)\"" >> config.h + echo "" >> config.h + echo "#define PREFIX \"$(PREFIX)\"" >> config.h + echo "#define ETCPREFIX \"$(ETCPREFIX)\"" >> config.h + echo "" >> config.h + echo "#endif" >> config.h + +dist: clean + mkdir -p $(DISTNAME) + cp -rf Makefile $(DISTNAME) + cp -rf *.h $(DISTNAME) + cp -rf *.cc $(DISTNAME) + tar cvzf $(DISTNAME).tgz --exclude=*/CVS/* --exclude=*/CVS/ $(DISTNAME) + rm -rf $(DISTNAME) + +-include $(DEPENDFILE) + diff --git a/UDPTCPNetwork.h b/UDPTCPNetwork.h index edb79ac..20fec16 100644 --- a/UDPTCPNetwork.h +++ b/UDPTCPNetwork.h @@ -2,9 +2,42 @@ #ifndef _UDPTCPNETWORK_H_ #define _UDPTCPNETWORK_H_ +#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) + +#define _NTDDI_VERSION_FROM_WIN32_WINNT2(ver) ver##0000 +#define _NTDDI_VERSION_FROM_WIN32_WINNT(ver) _NTDDI_VERSION_FROM_WIN32_WINNT2(ver) + +#ifndef _WIN32_WINNT +# define _WIN32_WINNT 0x501 +#endif +#ifndef NTDDI_VERSION +# define NTDDI_VERSION _NTDDI_VERSION_FROM_WIN32_WINNT(_WIN32_WINNT) +#endif + +#include +#include +#include +#include + +#ifndef bzero +#define bzero(__z__, __x__) memset (__z__, 0x0, __x__) +#endif + +#ifndef MSG_NOSIGNAL +# define MSG_NOSIGNAL 0 +#endif + +#else + #include #include #include +#include +#include +#include + +#endif + #include #include @@ -146,6 +179,8 @@ public: /************************************************************************ * unix socket related functions */ +#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) +#else class UNIX { private: int sock; @@ -172,7 +207,7 @@ public: int GetSocket() { return sock; }; }; - +#endif #endif diff --git a/network.cc b/network.cc index 7683211..9197d0b 100644 --- a/network.cc +++ b/network.cc @@ -1,11 +1,6 @@ #include "UDPTCPNetwork.h" -#include -#include -#include -#include -#include #include #include /* close() */ #include /* memset() */ @@ -14,12 +9,10 @@ using namespace std; char dnsip[NET_HOSTLEN]; - // // convert host and port to sockaddr_in6 // -int dns_filladdr (string host, string port, int ai_family, - struct sockaddr_storage *sAddr) { +int dns_filladdr (string host, string port, int ai_family, struct sockaddr_storage *sAddr) { struct addrinfo hints, *res; int err; diff --git a/ssl.cc b/ssl.cc index e6302c6..7a4cc41 100644 --- a/ssl.cc +++ b/ssl.cc @@ -114,8 +114,13 @@ int SSLSocket::Connect (int sockfd, int block_timeout) { timeout = block_timeout; if (sockfd > 0 && block_timeout > 0) { +#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) + u_long mode = 1; + ioctlsocket(sockfd, FIONBIO, &mode); +#else flags = fcntl(sockfd, F_GETFL, 0); fcntl(sockfd, F_SETFL, flags | O_NONBLOCK); +#endif } ssl = SSL_new(ctx); @@ -142,8 +147,13 @@ int SSLSocket::Accept (int sockfd, int block_timeout) { NewServerCTX(); if (sockfd > 0 && block_timeout > 0) { +#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) + u_long mode = 1; + ioctlsocket(sockfd, FIONBIO, &mode); +#else flags = fcntl(sockfd, F_GETFL, 0); fcntl(sockfd, F_SETFL, flags | O_NONBLOCK); +#endif } ssl = SSL_new(ctx); @@ -220,8 +230,13 @@ int SSLSocket::Close () { } if (sock > 0 && timeout > 0) { +#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) + u_long mode = 0; + ioctlsocket(sock, FIONBIO, &mode); +#else flags = fcntl(sock, F_GETFL, 0); fcntl(sock, F_SETFL, flags & ~(O_NONBLOCK)); +#endif } return sock; diff --git a/tcp.cc b/tcp.cc index f0d863b..c3dab00 100644 --- a/tcp.cc +++ b/tcp.cc @@ -2,17 +2,11 @@ * */ -#include -#include -#include -#include -#include #include #include /* close() */ #include #include /* memset() */ #include -#include #include "UDPTCPNetwork.h" @@ -66,7 +60,7 @@ int TCP::Listen(int port) { if (sock == -1) continue; i = 1; - if ((err = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof (i))) != 0) { + if ((err = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (char*)&i, sizeof (i))) != 0) { printf ("%s:%d setsockopt error\n", __FILE__, __LINE__); } @@ -75,7 +69,7 @@ int TCP::Listen(int port) { sock = -1; continue; } - if (listen (sock, 8) < 0) { + if (listen (sock, 8) < 0) { // maximum of 8 connections at the time close (sock); sock = -1; continue; @@ -122,7 +116,11 @@ TCP* TCP::Accept() { } else if (retval) { +#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) + newsock = accept (sock, (struct sockaddr *) &cli_addr, (int*) &cli_len); +#else newsock = accept (sock, (struct sockaddr *) &cli_addr, &cli_len); +#endif if (newsock < 0) return NULL; tcp = new TCP(); tcp->SetSocket(newsock, &cli_addr, cli_len); @@ -188,7 +186,7 @@ int TCP::Connect() { // setup timeout to max 2 secs timeout.tv_sec = 2; timeout.tv_usec = 0; - setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); + setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout)); if (s == -1) continue; if (connect(s, rp->ai_addr, rp->ai_addrlen) != -1) { diff --git a/test-ssl.cc b/test-ssl.cc index 20b345e..729aa15 100644 --- a/test-ssl.cc +++ b/test-ssl.cc @@ -6,6 +6,9 @@ #define DEFAULT_PORT 12345 +#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) +#else + void server () { TCP tcpserver; TCP *connection; @@ -126,11 +129,13 @@ void client () { tcpclient.Close(); }; - +#endif int main (int argc, char **argv) { pid_t pid; +#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) +#else pid = fork(); if (pid == 0) { // child process printf ("start client\n"); @@ -145,6 +150,7 @@ int main (int argc, char **argv) { else { // parent process server(); } +#endif return 0; }; diff --git a/test-tcp.cc b/test-tcp.cc index f9ab309..cb317b7 100644 --- a/test-tcp.cc +++ b/test-tcp.cc @@ -6,6 +6,8 @@ #define DEFAULT_PORT 12345 +#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) +#else void server () { TCP tcpserver; @@ -106,11 +108,13 @@ void client () { tcpclient.Close(); }; - +#endif int main (int argc, char **argv) { pid_t pid; +#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) +#else pid = fork(); if (pid == 0) { // child process client(); @@ -121,6 +125,7 @@ int main (int argc, char **argv) { else { // parent process server(); } +#endif return 0; }; diff --git a/test-udp.cc b/test-udp.cc index dd05bad..561627b 100644 --- a/test-udp.cc +++ b/test-udp.cc @@ -79,6 +79,8 @@ int main (int argc, char **argv) { pid_t pid; +#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) +#else pid = fork(); if (pid == 0) { // child process client.Loop (); @@ -86,7 +88,7 @@ int main (int argc, char **argv) { else { // parent process server.Loop (); } - +#endif return 0; }; diff --git a/udp.cc b/udp.cc index f0dfdcf..a40ab5a 100644 --- a/udp.cc +++ b/udp.cc @@ -2,12 +2,7 @@ * */ -#include -#include -#include -#include #include -#include #include #include /* close() */ #include @@ -95,12 +90,17 @@ long int UDP::Read(string *source, char *buffer, long int len) { int UDP::SetBlocked(int bl) { +#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) + u_long mode = bl; + return ioctlsocket(sock, FIONBIO, &mode); +#else int flags = fcntl(sock, F_GETFL, 0); if (bl) flags &= ~O_NONBLOCK; else flags |= O_NONBLOCK; return fcntl(sock, F_SETFL, flags | O_NONBLOCK); +#endif } diff --git a/unix.cc b/unix.cc index 39d5d77..783b222 100644 --- a/unix.cc +++ b/unix.cc @@ -2,12 +2,6 @@ * */ -#include -#include -#include -#include -#include -#include #include #include /* close() */ #include @@ -17,6 +11,11 @@ #include "UDPTCPNetwork.h" +// for now no support on windows +#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) +#warning UNIX socket are not supported on windows. +#else + UNIX::~UNIX() { Close(); } @@ -208,3 +207,4 @@ int UNIX::IsData(int timeout) { }; +#endif