| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- #define INCLUDE_SSL
- #include "Client.h"
- #ifndef WIN32
- # include <netdb.h>
- # include <string.h>
- # include <sys/select.h>
- #endif
- #include <File.h>
- #include <Key.h>
- #include <Logging.h>
- using namespace Network;
- // Content of the Client class from Client.h
- // Constructor
- Client::Client()
- : ReferenceCounter(),
- errorOccured(0)
- {
- memset(&server, 0, sizeof(server));
- server.sin_family = AF_INET;
- downStreamBytes = 0;
- upStreamBytes = 0;
- sock = 0;
- sendKey = 0;
- receiveKey = 0;
- }
- // Destructor
- Client::~Client()
- {
- if (sock) disconnect();
- if (sendKey) sendKey->release();
- if (receiveKey) receiveKey->release();
- }
- // non-constant
- void Client::setSendKeyZ(Encryption::Key* key) // Sets the key for sending
- {
- if (sendKey) sendKey->release();
- sendKey = key;
- }
- void Client::setReceiveKeyZ(Encryption::Key* key) // Sets the key for receiving
- {
- if (receiveKey) receiveKey->release();
- receiveKey = key;
- }
- void Client::setSendKey(const char* key, int len) // Sets the key for sending
- {
- if (!sendKey) sendKey = new Encryption::Key();
- sendKey->setKey(key, len);
- }
- void Client::setReceiveKey(
- const char* key, int len) // Sets the key for receiving
- {
- if (!receiveKey) receiveKey = new Encryption::Key();
- receiveKey->setKey(key, len);
- }
- bool Client::connect(unsigned short port, const char* ip) // connects to server
- {
- if (sendKey) sendKey->setPos(0);
- if (receiveKey) receiveKey->setPos(0);
- if (sock) closesocket(sock);
- sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- long sIp = inet_addr(ip); // ip address
- if (sIp == INADDR_NONE)
- {
- struct hostent* pHostInfo = gethostbyname(ip);
- if (pHostInfo == 0) return 0;
- sIp = *(long*)pHostInfo->h_addr_list[0];
- }
- memcpy((char*)&server.sin_addr, &sIp, sizeof(sIp));
- server.sin_port = htons(port); // port
- if (::connect(sock, (struct sockaddr*)&server, sizeof(server))
- < 0) // connect
- {
- errorOccured = 1;
- return 0; // Error
- }
- errorOccured = 0;
- return 1;
- }
- bool Client::send(const char* message, int len) // sends to server
- {
- int ll = 0;
- while (len > 0)
- {
- #ifdef WIN32
- int l = ::send(sock, message + ll, len, 0);
- #else
- int l = (int)::send(sock, message + ll, len, MSG_NOSIGNAL);
- #endif
- if (l <= 0)
- {
- #ifdef WIN32
- # ifdef _DEBUG
- Framework::Logging::warning()
- << "send: " << l << " Error: " << WSAGetLastError();
- # endif
- #endif
- errorOccured = 1;
- return 0; // Error
- }
- len -= l;
- ll += l;
- }
- upStreamBytes += ll;
- return 1;
- }
- bool Client::getMessage(char* message, int len) // receives message
- {
- int ll = 0;
- while (len > 0)
- {
- int l = (int)recv(sock, message + ll, len, MSG_WAITALL);
- if (l <= 0)
- {
- #ifdef WIN32
- # ifdef _DEBUG
- Framework::Logging::warning()
- << "recv: " << l << " Error: " << WSAGetLastError();
- # endif
- #endif
- errorOccured = 1;
- return 0; // Error
- }
- len -= l;
- ll += l;
- }
- downStreamBytes += ll;
- return 1;
- }
- bool Client::sendEncrypted(const char* message, int len) // sends to server
- {
- if (!sendKey) return send(message, len);
- Encryption::Bytes* n = new Encryption::Bytes(message, len);
- sendKey->encode(dynamic_cast<Framework::Encryption::Bytes*>(n->getThis()));
- int ll = 0;
- while (len > 0)
- {
- #ifdef WIN32
- int l = ::send(sock, n->getBytes() + ll, len, 0);
- #else
- int l = (int)::send(sock, n->getBytes() + ll, len, MSG_NOSIGNAL);
- #endif
- if (l <= 0)
- {
- #ifdef WIN32
- # ifdef _DEBUG
- Framework::Logging::warning()
- << "send: " << l << " Error: " << WSAGetLastError();
- # endif
- #endif
- n->release();
- errorOccured = 1;
- return 0; // Error
- }
- len -= l;
- ll += l;
- }
- upStreamBytes += ll;
- n->release();
- return 1;
- }
- bool Client::getMessageEncrypted(char* message, int len) // receives message
- {
- if (!receiveKey) return getMessage(message, len);
- int ll = 0;
- while (len > 0)
- {
- int l = (int)recv(sock, message + ll, len, MSG_WAITALL);
- if (l <= 0)
- {
- #ifdef WIN32
- # ifdef _DEBUG
- Framework::Logging::warning()
- << "recv: " << l << " Error: " << WSAGetLastError();
- # endif
- #endif
- errorOccured = 1;
- return 0; // Error
- }
- len -= l;
- ll += l;
- }
- Encryption::Bytes* n = new Encryption::Bytes();
- n->setBytesZ(message, ll);
- receiveKey->decode(n);
- downStreamBytes += ll;
- return 1;
- }
- int Client::getDownloadBytes(bool reset) // returns the number of received bytes
- {
- int ret = downStreamBytes;
- if (reset) downStreamBytes = 0;
- return ret;
- }
- int Client::getUploadBytes(bool reset) // returns the number of sent bytes
- {
- int ret = upStreamBytes;
- if (reset) upStreamBytes = 0;
- return ret;
- }
- bool Client::disconnect() // Disconnects from server
- {
- if (!sock) return 1;
- if (sendKey) sendKey->setPos(0);
- if (receiveKey) receiveKey->setPos(0);
- if (closesocket(sock) < 0) // disconnect
- {
- errorOccured = 1;
- return 0; // Error
- }
- sock = 0;
- return 1;
- }
- // constant
- bool Client::hasMessage(int waitTime) // Waits for a message for a given time
- {
- fd_set set;
- FD_ZERO(&set);
- FD_SET(sock, &set);
- timeval time = {waitTime / 1000, waitTime};
- int result = select(0, &set, 0, 0, &time);
- if (result < 0)
- {
- #ifdef WIN32
- # ifdef _DEBUG
- Framework::Logging::warning()
- << "select: " << result << " Error: " << WSAGetLastError();
- # endif
- #endif
- }
- return result > 0;
- }
- unsigned short Client::getServerPort() const // returns the port
- {
- return htons(server.sin_port);
- }
- const char* Client::getServerIp() const // returns the IP
- {
- return inet_ntoa(server.sin_addr);
- }
- bool Client::waitForNextMessage()
- const // waits until there is something to receive
- {
- fd_set set;
- int rv = 0;
- struct timeval timeout;
- while (rv == 0 && sock)
- {
- FD_ZERO(&set); /* clear the set */
- FD_SET(sock, &set); /* add our file descriptor to the set */
- timeout.tv_sec = 10;
- timeout.tv_usec = 0;
- rv = select((int)sock + 1, &set, NULL, NULL, &timeout);
- if (rv == -1)
- {
- #ifdef WIN32
- # ifdef _DEBUG
- Framework::Logging::warning()
- << "select: " << rv << " Error: " << WSAGetLastError();
- # endif
- #endif
- return 0;
- }
- }
- if (!sock) return 0;
- char c;
- #ifdef WIN32
- int l = (int)recv(sock, &c, 1, MSG_PEEK);
- #else
- int l = (int)recv(sock, &c, 1, MSG_WAITALL | MSG_PEEK);
- #endif
- if (l <= 0)
- {
- #ifdef WIN32
- # ifdef _DEBUG
- Framework::Logging::warning()
- << "recv: " << l << " Error: " << WSAGetLastError();
- # endif
- #endif
- return 0; // Error
- }
- return 1;
- }
- bool Client::isConnected() const // returns true if a connection exists
- {
- return sock != 0 && !errorOccured;
- }
- // Content of the SSLClient class from Client.h
- // Constructor
- SSLClient::SSLClient()
- : ReferenceCounter()
- {
- ctx = SSL_CTX_new(TLS_client_method());
- SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
- SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
- ip = 0;
- port = 0;
- bio = BIO_new_ssl_connect(ctx);
- BIO_get_ssl(bio, &ssl);
- downStreamBytes = 0;
- upStreamBytes = 0;
- connected = 0;
- }
- // Destructor
- SSLClient::~SSLClient()
- {
- if (this->ip) this->ip->release();
- if (connected) disconnect();
- BIO_free_all(bio);
- SSL_CTX_free(ctx);
- #ifdef WIN32
- OPENSSL_thread_stop();
- #endif
- }
- bool SSLClient::connect(
- unsigned short port, const char* ip) // connects to server
- {
- this->port = port;
- if (this->ip) this->ip->release();
- this->ip = new Text(ip);
- Text adr = ip;
- adr += ":";
- adr += port;
- BIO_set_conn_hostname(bio, (const char*)adr);
- connected = BIO_do_connect(bio) > 0;
- if (connected && BIO_do_handshake(bio) <= 0) disconnect();
- return connected;
- }
- bool SSLClient::send(const char* message, int len) // sends to server
- {
- int ll = 0;
- while (len > 0)
- {
- int l = SSL_write(ssl, message + ll, len);
- if (l <= 0)
- {
- #ifdef _DEBUG
- Framework::Logging::warning()
- << "SSL_write: " << l << " Error: " << SSL_get_error(ssl, l);
- #endif
- return 0; // Error
- }
- len -= l;
- ll += l;
- }
- upStreamBytes += ll;
- return 1;
- }
- bool SSLClient::getMessage(char* message, int len) // receives message
- {
- if (!connected) return 0;
- int ll = 0;
- while (len > 0)
- {
- int l = SSL_read(ssl, message + ll, len);
- if (l <= 0)
- {
- #ifdef _DEBUG
- Framework::Logging::warning()
- << "SSL_read: " << l << " Error: " << SSL_get_error(ssl, l);
- #endif
- return 0; // Error
- }
- len -= l;
- ll += l;
- }
- downStreamBytes += ll;
- return 1;
- }
- int SSLClient::getDownloadBytes(
- bool reset) // returns the number of received bytes
- {
- int ret = downStreamBytes;
- if (reset) downStreamBytes = 0;
- return ret;
- }
- int SSLClient::getUploadBytes(bool reset) // returns the number of sent bytes
- {
- int ret = upStreamBytes;
- if (reset) upStreamBytes = 0;
- return ret;
- }
- bool SSLClient::disconnect() // Disconnects from server
- {
- BIO_ssl_shutdown(bio);
- connected = 0;
- return 1;
- }
- // constant
- bool SSLClient::hasMessage(int waitTime) // Waits for a message for a given time
- {
- fd_set set;
- FD_ZERO(&set);
- FD_SET(SSL_get_rfd(ssl), &set);
- timeval time = {waitTime / 1000, waitTime};
- return SSL_pending(ssl) > 0 || select(0, &set, 0, 0, &time) == 1;
- }
- unsigned short SSLClient::getServerPort() const // returns the server port
- {
- return port;
- }
- const char* SSLClient::getServerIp() const // returns the server IP
- {
- return ip->getText();
- }
|