added windows support

origin
steffen 5 years ago
parent df22906ada
commit 3b10916f8f

@ -1,3 +1,6 @@
2020-10-18:
- added support for windows. Everything should work but UNIX sockets.
2020-10-15:
- added: TCP::isListen function

@ -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

@ -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)

@ -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 <winsock2.h>
#include <io.h>
#include <Ws2tcpip.h>
#include <string.h>
#ifndef bzero
#define bzero(__z__, __x__) memset (__z__, 0x0, __x__)
#endif
#ifndef MSG_NOSIGNAL
# define MSG_NOSIGNAL 0
#endif
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/un.h>
#endif
#include <openssl/ssl.h>
#include <openssl/err.h>
@ -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

@ -1,11 +1,6 @@
#include "UDPTCPNetwork.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h> /* close() */
#include <string.h> /* 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;

@ -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;

@ -2,17 +2,11 @@
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h> /* close() */
#include <stdlib.h>
#include <string.h> /* memset() */
#include <errno.h>
#include <string.h>
#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) {

@ -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;
};

@ -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;
};

@ -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;
};

@ -2,12 +2,7 @@
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h> /* close() */
#include <stdlib.h>
@ -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
}

@ -2,12 +2,6 @@
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h> /* close() */
#include <stdlib.h>
@ -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

Loading…
Cancel
Save