Add support Windows x64 OS
This commit is contained in:
		
							parent
							
								
									915975fba8
								
							
						
					
					
						commit
						2118270646
					
				@ -4,9 +4,16 @@
 | 
				
			|||||||
#include <iostream>
 | 
					#include <iostream>
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if __linux__
 | 
				
			||||||
    #include <arpa/inet.h>
 | 
					    #include <arpa/inet.h>
 | 
				
			||||||
    #include <netdb.h>
 | 
					    #include <netdb.h>
 | 
				
			||||||
    #include <unistd.h>
 | 
					    #include <unistd.h>
 | 
				
			||||||
 | 
					#elif _WIN32
 | 
				
			||||||
 | 
					    // #include <sstream>
 | 
				
			||||||
 | 
					    #include <WinSock.h>
 | 
				
			||||||
 | 
					    #include <ws2tcpip.h>
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
using namespace std;
 | 
					using namespace std;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -13,9 +13,11 @@
 | 
				
			|||||||
    #include <netdb.h>
 | 
					    #include <netdb.h>
 | 
				
			||||||
    #include <unistd.h>
 | 
					    #include <unistd.h>
 | 
				
			||||||
#elif _WIN32
 | 
					#elif _WIN32
 | 
				
			||||||
    // #include <sstream>
 | 
					 | 
				
			||||||
    #include <WinSock.h>
 | 
					    #include <WinSock.h>
 | 
				
			||||||
    #include <ws2tcpip.h>
 | 
					    #include <ws2tcpip.h>
 | 
				
			||||||
 | 
					    #pragma comment(lib,"ws2_32.lib")
 | 
				
			||||||
 | 
					    #define ushort u_short
 | 
				
			||||||
 | 
					    #define uint u_int
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "ip.hpp"
 | 
					#include "ip.hpp"
 | 
				
			||||||
@ -34,7 +36,11 @@ class client;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class server {
 | 
					class server {
 | 
				
			||||||
    public:
 | 
					    public:
 | 
				
			||||||
 | 
					    #if __linux__
 | 
				
			||||||
        int sock;
 | 
					        int sock;
 | 
				
			||||||
 | 
					    #elif _WIN32
 | 
				
			||||||
 | 
					        SOCKET sock;
 | 
				
			||||||
 | 
					    #endif
 | 
				
			||||||
    struct sockaddr_in addr;
 | 
					    struct sockaddr_in addr;
 | 
				
			||||||
    SSL_CTX* securefds = NULL;
 | 
					    SSL_CTX* securefds = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -70,7 +76,11 @@ class secure {
 | 
				
			|||||||
class client {
 | 
					class client {
 | 
				
			||||||
    public:
 | 
					    public:
 | 
				
			||||||
    // zajedničke
 | 
					    // zajedničke
 | 
				
			||||||
 | 
					    #if __linux__
 | 
				
			||||||
        int conn; // mijenja sock
 | 
					        int conn; // mijenja sock
 | 
				
			||||||
 | 
					    #elif _WIN32
 | 
				
			||||||
 | 
					        SOCKET conn; // mijenja sock
 | 
				
			||||||
 | 
					    #endif
 | 
				
			||||||
    struct sockaddr_in addr;
 | 
					    struct sockaddr_in addr;
 | 
				
			||||||
    SSL* ssl = NULL;
 | 
					    SSL* ssl = NULL;
 | 
				
			||||||
    // server s klijentima
 | 
					    // server s klijentima
 | 
				
			||||||
 | 
				
			|||||||
@ -11,6 +11,13 @@ server::server (const ushort port, const uint limit, SSL_CTX* _securefds) {
 | 
				
			|||||||
    addr.sin_addr.s_addr = INADDR_ANY;
 | 
					    addr.sin_addr.s_addr = INADDR_ANY;
 | 
				
			||||||
    addr.sin_port = htons(port);
 | 
					    addr.sin_port = htons(port);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #if _WIN32
 | 
				
			||||||
 | 
					        WSADATA wsa;
 | 
				
			||||||
 | 
					        if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) {
 | 
				
			||||||
 | 
					            throw string("[ERROR] WSA Startup. Detail: " + to_string(WSAGetLastError()));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    #endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    sock = socket(AF_INET, SOCK_STREAM, 0); 
 | 
					    sock = socket(AF_INET, SOCK_STREAM, 0); 
 | 
				
			||||||
    if (sock <= 0) {  
 | 
					    if (sock <= 0) {  
 | 
				
			||||||
        throw string("[ERROR] Unable to open TCP socket ");
 | 
					        throw string("[ERROR] Unable to open TCP socket ");
 | 
				
			||||||
@ -18,9 +25,16 @@ server::server (const ushort port, const uint limit, SSL_CTX* _securefds) {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    int opt=1;
 | 
					    int opt=1;
 | 
				
			||||||
 | 
					    #if __linux__
 | 
				
			||||||
        if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
 | 
					        if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
 | 
				
			||||||
            throw string("[ERROR] Unable to set REUSEADDR or REUSEPORT on socket ");
 | 
					            throw string("[ERROR] Unable to set REUSEADDR or REUSEPORT on socket ");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					    #elif _WIN32
 | 
				
			||||||
 | 
					        if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt))) {
 | 
				
			||||||
 | 
					            throw string("[ERROR] Unable to set REUSEADDR or REUSEPORT on socket ");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    #endif
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
 | 
					    if (bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
 | 
				
			||||||
        throw string("[ERROR] Unable to bind socket ");
 | 
					        throw string("[ERROR] Unable to bind socket ");
 | 
				
			||||||
@ -50,10 +64,21 @@ server::~server () {
 | 
				
			|||||||
        throw string("[ERROR] The socket is already closed "); 
 | 
					        throw string("[ERROR] The socket is already closed "); 
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    else if (close(sock) != 0) {
 | 
					    else {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        #if __linux__
 | 
				
			||||||
 | 
					            if (close(sock) != 0) {
 | 
				
			||||||
                throw string("[ERROR] Unable to close socket ");
 | 
					                throw string("[ERROR] Unable to close socket ");
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					        #elif _WIN32
 | 
				
			||||||
 | 
					            if (closesocket(sock) != 0) {
 | 
				
			||||||
 | 
					                throw string("[ERROR] Unable to close socket ");
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            WSACleanup();
 | 
				
			||||||
 | 
					        #endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
@ -120,9 +145,7 @@ client::client(const string address, const ushort port, const uint timeout, SSL_
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    #if _WIN32
 | 
					    #if _WIN32
 | 
				
			||||||
        WSADATA wsa;
 | 
					        WSADATA wsa;
 | 
				
			||||||
        SOCKET s;
 | 
					 | 
				
			||||||
        if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) {
 | 
					        if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) {
 | 
				
			||||||
            //printf("Failed. Error Code : %d",WSAGetLastError());
 | 
					 | 
				
			||||||
            throw string("[ERROR] Unable to set WinSock " + to_string(WSAGetLastError()));
 | 
					            throw string("[ERROR] Unable to set WinSock " + to_string(WSAGetLastError()));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    #endif
 | 
					    #endif
 | 
				
			||||||
@ -142,6 +165,7 @@ client::client(const string address, const ushort port, const uint timeout, SSL_
 | 
				
			|||||||
        throw string("Unable to connect to server ");
 | 
					        throw string("Unable to connect to server ");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #if __linux__
 | 
				
			||||||
        struct timeval tv;
 | 
					        struct timeval tv;
 | 
				
			||||||
        tv.tv_sec = timeout/1000;
 | 
					        tv.tv_sec = timeout/1000;
 | 
				
			||||||
        tv.tv_usec = (timeout%1000)*1000;
 | 
					        tv.tv_usec = (timeout%1000)*1000;
 | 
				
			||||||
@ -149,6 +173,15 @@ client::client(const string address, const ushort port, const uint timeout, SSL_
 | 
				
			|||||||
        if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
 | 
					        if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
 | 
				
			||||||
            throw string("[ERROR] Unable to set timeout ");
 | 
					            throw string("[ERROR] Unable to set timeout ");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					    #elif _WIN32
 | 
				
			||||||
 | 
					        DWORD tv = timeout;
 | 
				
			||||||
 | 
					        if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv))) {
 | 
				
			||||||
 | 
					            throw string("[ERROR] Unable to set timeout ");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    #endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (securefds) {
 | 
					    if (securefds) {
 | 
				
			||||||
        ssl = SSL_new(securefds);
 | 
					        ssl = SSL_new(securefds);
 | 
				
			||||||
@ -183,6 +216,7 @@ client::client(const server *_srv, const uint timeout, SSL_CTX* securefds) {
 | 
				
			|||||||
        throw string("[ERROR] Unable to accept client connection ");
 | 
					        throw string("[ERROR] Unable to accept client connection ");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #if __linux__
 | 
				
			||||||
        struct timeval tv;
 | 
					        struct timeval tv;
 | 
				
			||||||
        tv.tv_sec = timeout/1000;
 | 
					        tv.tv_sec = timeout/1000;
 | 
				
			||||||
        tv.tv_usec = (timeout%1000)*1000;
 | 
					        tv.tv_usec = (timeout%1000)*1000;
 | 
				
			||||||
@ -190,6 +224,15 @@ client::client(const server *_srv, const uint timeout, SSL_CTX* securefds) {
 | 
				
			|||||||
        if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
 | 
					        if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
 | 
				
			||||||
            throw string("[ERROR] Unable to set timeout ");
 | 
					            throw string("[ERROR] Unable to set timeout ");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					    #elif _WIN32
 | 
				
			||||||
 | 
					        DWORD tv = timeout;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv))) {
 | 
				
			||||||
 | 
					            throw string("[ERROR] Unable to set timeout ");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    #endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (securefds) {
 | 
					    if (securefds) {
 | 
				
			||||||
@ -224,10 +267,6 @@ client::client(const server *_srv, const uint timeout, SSL_CTX* securefds) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
client::~client () {
 | 
					client::~client () {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    #if _WIN32
 | 
					 | 
				
			||||||
        WSACleanup();
 | 
					 | 
				
			||||||
    #endif
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (ssl) {
 | 
					    if (ssl) {
 | 
				
			||||||
        SSL_shutdown(ssl);
 | 
					        SSL_shutdown(ssl);
 | 
				
			||||||
        SSL_free(ssl);
 | 
					        SSL_free(ssl);
 | 
				
			||||||
@ -237,9 +276,18 @@ client::~client () {
 | 
				
			|||||||
        throw string("[ERROR] The socket is already closed "); 
 | 
					        throw string("[ERROR] The socket is already closed "); 
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    else if (close(conn) != 0) {
 | 
					    else {
 | 
				
			||||||
 | 
					        #if __linux__
 | 
				
			||||||
 | 
					            if (close(conn) != 0) {
 | 
				
			||||||
                throw string("[ERROR] Unable to close socket ");
 | 
					                throw string("[ERROR] Unable to close socket ");
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					        #elif _WIN32
 | 
				
			||||||
 | 
					            if (closesocket(conn) != 0) {
 | 
				
			||||||
 | 
					                throw string("[ERROR] Unable to close socket ");
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            //WSACleanup();
 | 
				
			||||||
 | 
					        #endif
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -256,7 +304,7 @@ bool client::push (const string msg) {
 | 
				
			|||||||
       sended = SSL_write(ssl, msg.c_str(), msg.length());
 | 
					       sended = SSL_write(ssl, msg.c_str(), msg.length());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else {
 | 
					    else {
 | 
				
			||||||
        sended = write(conn, msg.c_str(), msg.length());
 | 
					        sended = send(conn, msg.c_str(), msg.length(), 0);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return sended == msg.length();
 | 
					    return sended == msg.length();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -274,7 +322,7 @@ string client::pull (size_t byte_limit) {
 | 
				
			|||||||
        SSL_read(ssl, res, byte_limit);
 | 
					        SSL_read(ssl, res, byte_limit);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else {
 | 
					    else {
 | 
				
			||||||
        read(conn , res, byte_limit);
 | 
					        recv(conn , res, byte_limit, 0);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return string(res);
 | 
					    return string(res);
 | 
				
			||||||
 | 
				
			|||||||
@ -11,7 +11,7 @@ int main() {
 | 
				
			|||||||
        secure crypto;
 | 
					        secure crypto;
 | 
				
			||||||
        cout << "init cert " << endl;
 | 
					        cout << "init cert " << endl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        client myserver("127.0.0.1", 5000, 5000, crypto.fds);
 | 
					        client myserver("localhost", 8000, 5000, crypto.fds);
 | 
				
			||||||
        // client myserver("localhost", 5000);
 | 
					        // client myserver("localhost", 5000);
 | 
				
			||||||
        cout << "init client " << endl;
 | 
					        cout << "init client " << endl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										
											BIN
										
									
								
								test/client.exe
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								test/client.exe
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										1
									
								
								test/compile-client.ps1
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								test/compile-client.ps1
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					g++ client.cpp ../src/* -o client.exe -lssl -lcrypto -lws2_32
 | 
				
			||||||
							
								
								
									
										1
									
								
								test/compile-server.ps1
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								test/compile-server.ps1
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					g++ server.cpp ../src/* -o server.exe -lssl -lcrypto -lws2_32
 | 
				
			||||||
@ -9,7 +9,7 @@ int main() {
 | 
				
			|||||||
        cout << "init cert " << endl;
 | 
					        cout << "init cert " << endl;
 | 
				
			||||||
        secure crypto ("../example/cert.pem", "../example/privkey.pem");
 | 
					        secure crypto ("../example/cert.pem", "../example/privkey.pem");
 | 
				
			||||||
        cout << "init server " << endl;
 | 
					        cout << "init server " << endl;
 | 
				
			||||||
        server myserver(5000, 100, crypto.fds);
 | 
					        server myserver(8000, 100, crypto.fds);
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        cout << "init client " << endl;
 | 
					        cout << "init client " << endl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -23,7 +23,7 @@ int main() {
 | 
				
			|||||||
        // string fromclient =  myclient.pull();
 | 
					        // string fromclient =  myclient.pull();
 | 
				
			||||||
        cout << "tell client " << fromclient << endl;
 | 
					        cout << "tell client " << fromclient << endl;
 | 
				
			||||||
        // usleep(600*1000);
 | 
					        // usleep(600*1000);
 | 
				
			||||||
        sleep(5);
 | 
					        //sleep(5);
 | 
				
			||||||
        myserver.cli->push(fromclient);
 | 
					        myserver.cli->push(fromclient);
 | 
				
			||||||
        // myclient.push(fromclient);
 | 
					        // myclient.push(fromclient);
 | 
				
			||||||
        // myclient.~comming();
 | 
					        // myclient.~comming();
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										
											BIN
										
									
								
								test/server.exe
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								test/server.exe
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user