|
@@ -1,56 +1,70 @@
|
|
|
-#include <openssl/ssl.h>
|
|
|
-#include <openssl/err.h>
|
|
|
#include "Server.h"
|
|
|
+
|
|
|
+#include <openssl/err.h>
|
|
|
+#include <openssl/ssl.h>
|
|
|
#ifndef WIN32
|
|
|
-#include <string.h>
|
|
|
+# include <string.h>
|
|
|
#endif
|
|
|
+#include <iostream>
|
|
|
#include <Key.h>
|
|
|
#include <Text.h>
|
|
|
-#include <iostream>
|
|
|
|
|
|
using namespace Network;
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
Server::Server()
|
|
|
- : ReferenceCounter()
|
|
|
+ : ReferenceCounter()
|
|
|
{
|
|
|
- sock = 0;
|
|
|
- memset(&addresse, 0, sizeof(addresse));
|
|
|
- addresse.sin_family = AF_INET;
|
|
|
- addresse.sin_addr.s_addr = ADDR_ANY;
|
|
|
- klients = 0;
|
|
|
+ sock = 0;
|
|
|
+ memset(&addresse, 0, sizeof(addresse));
|
|
|
+ addresse.sin_family = AF_INET;
|
|
|
+ addresse.sin_addr.s_addr = ADDR_ANY;
|
|
|
+ klients = 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
Server::~Server()
|
|
|
{
|
|
|
- trenne();
|
|
|
+ trenne();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-bool Server::verbinde(unsigned short port, int warteschlangenLen)
|
|
|
+
|
|
|
+bool Server::verbinde(
|
|
|
+ unsigned short port, int warteschlangenLen)
|
|
|
{
|
|
|
- sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
- addresse.sin_port = htons(port);
|
|
|
- if (bind(sock, (struct sockaddr*)&addresse, sizeof(addresse)) == -1)
|
|
|
- {
|
|
|
- trenne();
|
|
|
- return 0;
|
|
|
- }
|
|
|
- if (listen(sock, warteschlangenLen) == -1)
|
|
|
- {
|
|
|
- trenne();
|
|
|
- return 0;
|
|
|
- }
|
|
|
- return 1;
|
|
|
+ sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
+ addresse.sin_port = htons(port);
|
|
|
+ if (sock < 0)
|
|
|
+ {
|
|
|
+ sock = 0;
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+#ifdef WIN32
|
|
|
+ char reuseSocket = 1;
|
|
|
+ setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseSocket, sizeof(char));
|
|
|
+#else
|
|
|
+ int reuseSocket = 1;
|
|
|
+ setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseSocket, sizeof(int));
|
|
|
+#endif
|
|
|
+ if (bind(sock, (struct sockaddr*)&addresse, sizeof(addresse))
|
|
|
+ == -1)
|
|
|
+ {
|
|
|
+ trenne();
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (listen(sock, warteschlangenLen) == -1)
|
|
|
+ {
|
|
|
+ trenne();
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
SKlient* Server::getKlient()
|
|
|
{
|
|
|
- if (!sock)
|
|
|
- return 0;
|
|
|
- sockaddr_in client;
|
|
|
+ if (!sock) return 0;
|
|
|
+ sockaddr_in client;
|
|
|
int len = sizeof(addresse);
|
|
|
fd_set set;
|
|
|
int rv = 0;
|
|
@@ -66,281 +80,280 @@ SKlient* Server::getKlient()
|
|
|
}
|
|
|
if (!sock) return 0;
|
|
|
#ifdef WIN32
|
|
|
- SOCKET cls = accept(sock, (sockaddr*)&client, &len);
|
|
|
- if (cls == INVALID_SOCKET)
|
|
|
- {
|
|
|
- trenne();
|
|
|
- return 0;
|
|
|
- }
|
|
|
+ SOCKET cls = accept(sock, (sockaddr*)&client, &len);
|
|
|
+ if (cls == INVALID_SOCKET)
|
|
|
+ {
|
|
|
+ trenne();
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
#else
|
|
|
- SOCKET cls = accept(sock, (sockaddr*)&client, (socklen_t*)&len);
|
|
|
- if (!cls)
|
|
|
- {
|
|
|
- if (errno == ECONNABORTED || errno == EBADF)
|
|
|
- trenne();
|
|
|
- return 0;
|
|
|
- }
|
|
|
+ SOCKET cls = accept(
|
|
|
+ sock, (sockaddr*)&client, (socklen_t*)&len);
|
|
|
+ if (!cls)
|
|
|
+ {
|
|
|
+ if (errno == ECONNABORTED || errno == EBADF) trenne();
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
#endif
|
|
|
- client.sin_port = addresse.sin_port;
|
|
|
- klients++;
|
|
|
- return new SKlient(client, cls);
|
|
|
+ client.sin_port = addresse.sin_port;
|
|
|
+ klients++;
|
|
|
+ return new SKlient(client, cls);
|
|
|
}
|
|
|
|
|
|
int Server::getKlients(bool reset)
|
|
|
{
|
|
|
- int ret = klients;
|
|
|
- if (reset)
|
|
|
- klients = 0;
|
|
|
- return ret;
|
|
|
+ int ret = klients;
|
|
|
+ if (reset) klients = 0;
|
|
|
+ return ret;
|
|
|
}
|
|
|
|
|
|
bool Server::trenne()
|
|
|
{
|
|
|
- if (!sock)
|
|
|
- return 1;
|
|
|
- if (closesocket(sock) < 0)
|
|
|
- return 0;
|
|
|
- sock = 0;
|
|
|
- return 1;
|
|
|
+ if (!sock) return 1;
|
|
|
+ if (closesocket(sock) < 0)
|
|
|
+ return 0;
|
|
|
+ sock = 0;
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
|
|
|
unsigned short Server::getPort() const
|
|
|
{
|
|
|
- return htons(addresse.sin_port);
|
|
|
+ return htons(addresse.sin_port);
|
|
|
}
|
|
|
|
|
|
-bool Server::isConnected() const
|
|
|
+bool Server::isConnected()
|
|
|
+ const
|
|
|
{
|
|
|
- return sock != 0;
|
|
|
+ return sock != 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
SKlient::SKlient(sockaddr_in addresse, SOCKET sock)
|
|
|
- : ReferenceCounter()
|
|
|
+ : ReferenceCounter()
|
|
|
{
|
|
|
- clientAddr = addresse;
|
|
|
- this->sock = sock;
|
|
|
- downStreamBytes = 0;
|
|
|
- upStreamBytes = 0;
|
|
|
- sendeKey = 0;
|
|
|
- empfangKey = 0;
|
|
|
+ clientAddr = addresse;
|
|
|
+ this->sock = sock;
|
|
|
+ downStreamBytes = 0;
|
|
|
+ upStreamBytes = 0;
|
|
|
+ sendeKey = 0;
|
|
|
+ empfangKey = 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
SKlient::~SKlient()
|
|
|
{
|
|
|
- trenne();
|
|
|
- if (sendeKey)
|
|
|
- sendeKey->release();
|
|
|
- if (empfangKey)
|
|
|
- empfangKey->release();
|
|
|
+ trenne();
|
|
|
+ if (sendeKey) sendeKey->release();
|
|
|
+ if (empfangKey) empfangKey->release();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-void SKlient::setEmpfangTimeout(int miliseconds)
|
|
|
+
|
|
|
+void SKlient::setEmpfangTimeout(
|
|
|
+ int miliseconds)
|
|
|
{
|
|
|
#ifdef WIN32
|
|
|
- DWORD timeout = miliseconds;
|
|
|
- setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof timeout);
|
|
|
+ DWORD timeout = miliseconds;
|
|
|
+ setsockopt(
|
|
|
+ sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof timeout);
|
|
|
#else
|
|
|
- struct timeval tv;
|
|
|
- tv.tv_sec = miliseconds / 1000;
|
|
|
- tv.tv_usec = (miliseconds % 1000) * 1000;
|
|
|
- setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
|
|
|
+ struct timeval tv;
|
|
|
+ tv.tv_sec = miliseconds / 1000;
|
|
|
+ tv.tv_usec = (miliseconds % 1000) * 1000;
|
|
|
+ setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
void SKlient::setSendeKeyZ(Encryption::Key* key)
|
|
|
{
|
|
|
- if (sendeKey)
|
|
|
- sendeKey->release();
|
|
|
- sendeKey = key;
|
|
|
+ if (sendeKey) sendeKey->release();
|
|
|
+ sendeKey = key;
|
|
|
}
|
|
|
|
|
|
-void SKlient::setEmpfangKeyZ(Encryption::Key* key)
|
|
|
+void SKlient::setEmpfangKeyZ(
|
|
|
+ Encryption::Key* key)
|
|
|
{
|
|
|
- if (empfangKey)
|
|
|
- empfangKey->release();
|
|
|
- empfangKey = key;
|
|
|
+ if (empfangKey) empfangKey->release();
|
|
|
+ empfangKey = key;
|
|
|
}
|
|
|
|
|
|
void SKlient::setSendeKey(const char* key, int len)
|
|
|
{
|
|
|
- if (!sendeKey)
|
|
|
- sendeKey = new Encryption::Key();
|
|
|
- sendeKey->setKey(key, len);
|
|
|
+ if (!sendeKey) sendeKey = new Encryption::Key();
|
|
|
+ sendeKey->setKey(key, len);
|
|
|
}
|
|
|
|
|
|
-void SKlient::setEmpfangKey(const char* key, int len)
|
|
|
+void SKlient::setEmpfangKey(
|
|
|
+ const char* key, int len)
|
|
|
{
|
|
|
- if (!empfangKey)
|
|
|
- empfangKey = new Encryption::Key();
|
|
|
- empfangKey->setKey(key, len);
|
|
|
+ if (!empfangKey) empfangKey = new Encryption::Key();
|
|
|
+ empfangKey->setKey(key, len);
|
|
|
}
|
|
|
|
|
|
bool SKlient::sende(const char* nachricht, int len)
|
|
|
{
|
|
|
- if (!sock)
|
|
|
- return 0;
|
|
|
- int ll = 0;
|
|
|
- while (len > 0)
|
|
|
- {
|
|
|
+ if (!sock) return 0;
|
|
|
+ int ll = 0;
|
|
|
+ while (len > 0)
|
|
|
+ {
|
|
|
#ifdef WIN32
|
|
|
- int l = send(sock, nachricht + ll, len, 0);
|
|
|
+ int l = send(sock, nachricht + ll, len, 0);
|
|
|
#else
|
|
|
- int l = (int)send(sock, nachricht + ll, len, MSG_NOSIGNAL);
|
|
|
+ int l = (int)send(sock, nachricht + ll, len, MSG_NOSIGNAL);
|
|
|
#endif
|
|
|
- if (l <= 0)
|
|
|
- return 0;
|
|
|
- len -= l;
|
|
|
- ll += l;
|
|
|
- }
|
|
|
- upStreamBytes += ll;
|
|
|
- return 1;
|
|
|
-}
|
|
|
-
|
|
|
-bool SKlient::getNachricht(char* nachricht, int len)
|
|
|
-{
|
|
|
- if (!sock)
|
|
|
- return 0;
|
|
|
- int ll = 0;
|
|
|
- while (len > 0)
|
|
|
- {
|
|
|
- int l = (int)recv(sock, nachricht + ll, len, MSG_WAITALL);
|
|
|
- if (l <= 0)
|
|
|
- return 0;
|
|
|
- len -= l;
|
|
|
- ll += l;
|
|
|
- }
|
|
|
- downStreamBytes += ll;
|
|
|
- return 1;
|
|
|
-}
|
|
|
-
|
|
|
-bool SKlient::sendeEncrypted(const char* nachricht, int len)
|
|
|
-{
|
|
|
- if (!sendeKey)
|
|
|
- return sende(nachricht, len);
|
|
|
- Encryption::Bytes* n = new Encryption::Bytes(nachricht, len);
|
|
|
- sendeKey->codieren(dynamic_cast<Encryption::Bytes*>(n->getThis())); int ll = 0;
|
|
|
- while (len > 0)
|
|
|
- {
|
|
|
+ if (l <= 0) return 0;
|
|
|
+ len -= l;
|
|
|
+ ll += l;
|
|
|
+ }
|
|
|
+ upStreamBytes += ll;
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+bool SKlient::getNachricht(
|
|
|
+ char* nachricht, int len)
|
|
|
+{
|
|
|
+ if (!sock) return 0;
|
|
|
+ int ll = 0;
|
|
|
+ while (len > 0)
|
|
|
+ {
|
|
|
+ int l = (int)recv(sock, nachricht + ll, len, MSG_WAITALL);
|
|
|
+ if (l <= 0) return 0;
|
|
|
+ len -= l;
|
|
|
+ ll += l;
|
|
|
+ }
|
|
|
+ downStreamBytes += ll;
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+bool SKlient::sendeEncrypted(
|
|
|
+ const char* nachricht, int len)
|
|
|
+{
|
|
|
+ if (!sendeKey) return sende(nachricht, len);
|
|
|
+ Encryption::Bytes* n = new Encryption::Bytes(nachricht, len);
|
|
|
+ sendeKey->codieren(dynamic_cast<Encryption::Bytes*>(n->getThis()));
|
|
|
+ int ll = 0;
|
|
|
+ while (len > 0)
|
|
|
+ {
|
|
|
#ifdef WIN32
|
|
|
- int l = send(sock, n->getBytes() + ll, len, 0);
|
|
|
+ int l = send(sock, n->getBytes() + ll, len, 0);
|
|
|
#else
|
|
|
- int l = (int)send(sock, n->getBytes() + ll, len, MSG_NOSIGNAL);
|
|
|
+ int l = (int)send(sock, n->getBytes() + ll, len, MSG_NOSIGNAL);
|
|
|
#endif
|
|
|
- if (l <= 0)
|
|
|
- {
|
|
|
- n->release();
|
|
|
- return 0;
|
|
|
- }
|
|
|
- len -= l;
|
|
|
- ll += l;
|
|
|
- }
|
|
|
- upStreamBytes += ll;
|
|
|
- n->release();
|
|
|
- return 1;
|
|
|
-}
|
|
|
-
|
|
|
-bool SKlient::getNachrichtEncrypted(char* nachricht, int len)
|
|
|
-{
|
|
|
- if (!empfangKey)
|
|
|
- return getNachricht(nachricht, len);
|
|
|
- int ll = 0;
|
|
|
- while (len > 0)
|
|
|
- {
|
|
|
- int l = (int)recv(sock, nachricht + ll, len, MSG_WAITALL);
|
|
|
- if (l <= 0)
|
|
|
- return 0;
|
|
|
- len -= l;
|
|
|
- ll += l;
|
|
|
- }
|
|
|
- Encryption::Bytes* n = new Encryption::Bytes();
|
|
|
- n->setBytesZ(nachricht, ll);
|
|
|
- empfangKey->decodieren(n);
|
|
|
- downStreamBytes += ll;
|
|
|
- return 1;
|
|
|
-}
|
|
|
-
|
|
|
-int SKlient::getDownloadBytes(bool reset)
|
|
|
-{
|
|
|
- int ret = downStreamBytes;
|
|
|
- if (reset)
|
|
|
- downStreamBytes = 0;
|
|
|
- return ret;
|
|
|
-}
|
|
|
-
|
|
|
-int SKlient::getUploadBytes(bool reset)
|
|
|
-{
|
|
|
- int ret = upStreamBytes;
|
|
|
- if (reset)
|
|
|
- upStreamBytes = 0;
|
|
|
- return ret;
|
|
|
+ if (l <= 0)
|
|
|
+ {
|
|
|
+ n->release();
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ len -= l;
|
|
|
+ ll += l;
|
|
|
+ }
|
|
|
+ upStreamBytes += ll;
|
|
|
+ n->release();
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+bool SKlient::getNachrichtEncrypted(
|
|
|
+ char* nachricht, int len)
|
|
|
+{
|
|
|
+ if (!empfangKey) return getNachricht(nachricht, len);
|
|
|
+ int ll = 0;
|
|
|
+ while (len > 0)
|
|
|
+ {
|
|
|
+ int l = (int)recv(sock, nachricht + ll, len, MSG_WAITALL);
|
|
|
+ if (l <= 0) return 0;
|
|
|
+ len -= l;
|
|
|
+ ll += l;
|
|
|
+ }
|
|
|
+ Encryption::Bytes* n = new Encryption::Bytes();
|
|
|
+ n->setBytesZ(nachricht, ll);
|
|
|
+ empfangKey->decodieren(n);
|
|
|
+ downStreamBytes += ll;
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+int SKlient::getDownloadBytes(
|
|
|
+ bool reset)
|
|
|
+{
|
|
|
+ int ret = downStreamBytes;
|
|
|
+ if (reset) downStreamBytes = 0;
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+int SKlient::getUploadBytes(
|
|
|
+ bool reset)
|
|
|
+{
|
|
|
+ int ret = upStreamBytes;
|
|
|
+ if (reset) upStreamBytes = 0;
|
|
|
+ return ret;
|
|
|
}
|
|
|
|
|
|
bool SKlient::trenne()
|
|
|
{
|
|
|
- if (!sock)
|
|
|
- return 0;
|
|
|
- if (closesocket(sock) < 0)
|
|
|
- return 0;
|
|
|
- sock = 0;
|
|
|
- return 1;
|
|
|
+ if (!sock) return 0;
|
|
|
+ if (closesocket(sock) < 0)
|
|
|
+ return 0;
|
|
|
+ sock = 0;
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-bool SKlient::hatNachricht(int zeit) const
|
|
|
+
|
|
|
+bool SKlient::hatNachricht(
|
|
|
+ int zeit) const
|
|
|
{
|
|
|
- fd_set set;
|
|
|
- FD_ZERO(&set);
|
|
|
- FD_SET(sock, &set);
|
|
|
- timeval time = { zeit / 1000, zeit };
|
|
|
- return select(0, &set, 0, 0, &time) == 1;
|
|
|
+ fd_set set;
|
|
|
+ FD_ZERO(&set);
|
|
|
+ FD_SET(sock, &set);
|
|
|
+ timeval time = {zeit / 1000, zeit};
|
|
|
+ return select(0, &set, 0, 0, &time) == 1;
|
|
|
}
|
|
|
|
|
|
unsigned short SKlient::getPort() const
|
|
|
{
|
|
|
- return htons(clientAddr.sin_port);
|
|
|
+ return htons(clientAddr.sin_port);
|
|
|
}
|
|
|
|
|
|
const char* SKlient::getIp() const
|
|
|
{
|
|
|
- return inet_ntoa(clientAddr.sin_addr);
|
|
|
+ return inet_ntoa(clientAddr.sin_addr);
|
|
|
}
|
|
|
|
|
|
int pem_passwd_cb(char* buf, int size, int rwflag, void* userdata)
|
|
|
{
|
|
|
- const char* passw = ((Text*)userdata)->getText();
|
|
|
- memcpy(buf, passw, MIN((unsigned int)size, strlen(passw) + 1));
|
|
|
- return (int)strlen(buf);
|
|
|
+ const char* passw = ((Text*)userdata)->getText();
|
|
|
+ memcpy(buf, passw, MIN((unsigned int)size, strlen(passw) + 1));
|
|
|
+ return (int)strlen(buf);
|
|
|
}
|
|
|
|
|
|
bool SSLErrorCheck(int result, SSL* ssl, const char* action)
|
|
|
{
|
|
|
- if (result <= 0)
|
|
|
- {
|
|
|
- std::cout << "ERROR: '" << action << "' returned error code: " << SSL_get_error(ssl, result) << "\n";
|
|
|
- std::cout.flush();
|
|
|
- return 0;
|
|
|
- }
|
|
|
- return 1;
|
|
|
+ if (result <= 0)
|
|
|
+ {
|
|
|
+ std::cout << "ERROR: '" << action
|
|
|
+ << "' returned error code: " << SSL_get_error(ssl, result)
|
|
|
+ << "\n";
|
|
|
+ std::cout.flush();
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
bool SSLErrorCheck(__int64 result, const char* action)
|
|
|
{
|
|
|
- if (result <= 0)
|
|
|
- {
|
|
|
- unsigned long error = ERR_get_error();
|
|
|
- std::cout << "ERROR: '" << action << "' returned " << result << " error code: " << error << "(" << ERR_reason_error_string(error) << ")\n";
|
|
|
- std::cout.flush();
|
|
|
- return 0;
|
|
|
- }
|
|
|
- return 1;
|
|
|
+ if (result <= 0)
|
|
|
+ {
|
|
|
+ unsigned long error = ERR_get_error();
|
|
|
+ std::cout << "ERROR: '" << action << "' returned " << result
|
|
|
+ << " error code: " << error << "("
|
|
|
+ << ERR_reason_error_string(error) << ")\n";
|
|
|
+ std::cout.flush();
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
-bool SKlient::waitForNextMessage() const
|
|
|
+bool SKlient::waitForNextMessage()
|
|
|
+ const
|
|
|
{
|
|
|
fd_set set;
|
|
|
int rv = 0;
|
|
@@ -355,93 +368,104 @@ bool SKlient::waitForNextMessage() const
|
|
|
if (rv == -1) return 0;
|
|
|
}
|
|
|
if (!sock) return 0;
|
|
|
- char c;
|
|
|
- int l = (int)recv(sock, &c, 1, MSG_WAITALL | MSG_PEEK);
|
|
|
- if (l <= 0)
|
|
|
- return 0;
|
|
|
- return 1;
|
|
|
+ char c;
|
|
|
+ int l = (int)recv(sock, &c, 1, MSG_WAITALL | MSG_PEEK);
|
|
|
+ if (l <= 0) return 0;
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
-
|
|
|
+
|
|
|
SSLServer::SSLServer()
|
|
|
- : ReferenceCounter()
|
|
|
-{
|
|
|
- s = 0;
|
|
|
- const SSL_METHOD* method = TLS_server_method();
|
|
|
- ctx = SSL_CTX_new(method);
|
|
|
- SSLErrorCheck(SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), "SSL_CTX_set_min_proto_version");
|
|
|
- SSLErrorCheck(SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION), "SSL_CTX_set_max_proto_version");
|
|
|
- SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
|
|
|
- SSL_CTX_set_default_passwd_cb(ctx, pem_passwd_cb);
|
|
|
- passw = new Text();
|
|
|
- SSL_CTX_set_default_passwd_cb_userdata(ctx, passw);
|
|
|
- addr.sin_family = AF_INET;
|
|
|
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
- klients = 0;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
+ : ReferenceCounter()
|
|
|
+{
|
|
|
+ s = 0;
|
|
|
+ const SSL_METHOD* method = TLS_server_method();
|
|
|
+ ctx = SSL_CTX_new(method);
|
|
|
+ SSLErrorCheck(SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
|
|
|
+ "SSL_CTX_set_min_proto_version");
|
|
|
+ SSLErrorCheck(SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION),
|
|
|
+ "SSL_CTX_set_max_proto_version");
|
|
|
+ SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
|
|
|
+ SSL_CTX_set_default_passwd_cb(ctx, pem_passwd_cb);
|
|
|
+ passw = new Text();
|
|
|
+ SSL_CTX_set_default_passwd_cb_userdata(ctx, passw);
|
|
|
+ addr.sin_family = AF_INET;
|
|
|
+ addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
+ klients = 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
SSLServer::~SSLServer()
|
|
|
{
|
|
|
- trenne();
|
|
|
- SSL_CTX_free(ctx);
|
|
|
- passw->release();
|
|
|
+ trenne();
|
|
|
+ SSL_CTX_free(ctx);
|
|
|
+ passw->release();
|
|
|
#ifdef WIN32
|
|
|
- OPENSSL_thread_stop();
|
|
|
+ OPENSSL_thread_stop();
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
bool SSLServer::setCertificateFile(const char* file)
|
|
|
{
|
|
|
- return SSLErrorCheck(SSL_CTX_use_certificate_file(ctx, file, SSL_FILETYPE_PEM), "SSL_CTX_use_certificate_file");
|
|
|
+ return SSLErrorCheck(
|
|
|
+ SSL_CTX_use_certificate_file(ctx, file, SSL_FILETYPE_PEM),
|
|
|
+ "SSL_CTX_use_certificate_file");
|
|
|
}
|
|
|
|
|
|
|
|
|
bool SSLServer::setPrivateKeyFile(const char* file)
|
|
|
{
|
|
|
- return SSLErrorCheck(SSL_CTX_use_PrivateKey_file(ctx, file, SSL_FILETYPE_PEM), "SSL_CTX_use_PrivateKey_file");
|
|
|
+ return SSLErrorCheck(
|
|
|
+ SSL_CTX_use_PrivateKey_file(ctx, file, SSL_FILETYPE_PEM),
|
|
|
+ "SSL_CTX_use_PrivateKey_file");
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
void SSLServer::setPrivateKeyPassword(const char* password)
|
|
|
{
|
|
|
- passw->setText(password);
|
|
|
+ passw->setText(password);
|
|
|
}
|
|
|
|
|
|
|
|
|
bool SSLServer::verbinde(unsigned short port, int warteschlangenLen)
|
|
|
{
|
|
|
- addr.sin_port = htons(port);
|
|
|
- s = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
- if (s < 0)
|
|
|
- {
|
|
|
- s = 0;
|
|
|
- return 0;
|
|
|
- }
|
|
|
- if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0)
|
|
|
- {
|
|
|
- trenne();
|
|
|
- return 0;
|
|
|
- }
|
|
|
- if (listen(s, warteschlangenLen) < 0)
|
|
|
- {
|
|
|
- trenne();
|
|
|
- return 0;
|
|
|
- }
|
|
|
- return 1;
|
|
|
+ addr.sin_port = htons(port);
|
|
|
+ s = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
+ if (s < 0)
|
|
|
+ {
|
|
|
+ s = 0;
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+#ifdef WIN32
|
|
|
+ char reuseSocket = 1;
|
|
|
+ setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &reuseSocket, sizeof(char));
|
|
|
+#else
|
|
|
+ int reuseSocket = 1;
|
|
|
+ setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &reuseSocket, sizeof(int));
|
|
|
+#endif
|
|
|
+ if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0)
|
|
|
+ {
|
|
|
+ trenne();
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (listen(s, warteschlangenLen) < 0)
|
|
|
+ {
|
|
|
+ trenne();
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
|
|
|
SSLSKlient* SSLServer::getKlient()
|
|
|
{
|
|
|
- if (!s)
|
|
|
- return 0;
|
|
|
- int len = sizeof(addr);
|
|
|
- struct sockaddr_in addr;
|
|
|
+ if (!s) return 0;
|
|
|
+ int len = sizeof(addr);
|
|
|
+ struct sockaddr_in addr;
|
|
|
fd_set set;
|
|
|
int rv = 0;
|
|
|
struct timeval timeout;
|
|
@@ -456,191 +480,187 @@ SSLSKlient* SSLServer::getKlient()
|
|
|
}
|
|
|
if (!s) return 0;
|
|
|
#ifdef WIN32
|
|
|
- SOCKET client = accept(s, (struct sockaddr*)&addr, &len);
|
|
|
- if (client == INVALID_SOCKET)
|
|
|
- {
|
|
|
- trenne();
|
|
|
- return 0;
|
|
|
- }
|
|
|
+ SOCKET client = accept(s, (struct sockaddr*)&addr, &len);
|
|
|
+ if (client == INVALID_SOCKET)
|
|
|
+ {
|
|
|
+ trenne();
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
#else
|
|
|
- SOCKET client = accept(s, (sockaddr*)&addr, (socklen_t*)&len);
|
|
|
- if (!client)
|
|
|
- {
|
|
|
- if (errno == ECONNABORTED || errno == EBADF)
|
|
|
- trenne();
|
|
|
- return 0;
|
|
|
- }
|
|
|
+ SOCKET client
|
|
|
+ = accept(s, (sockaddr*)&addr, (socklen_t*)&len);
|
|
|
+ if (!client)
|
|
|
+ {
|
|
|
+ if (errno == ECONNABORTED || errno == EBADF) trenne();
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
#endif
|
|
|
- addr.sin_port = this->addr.sin_port;
|
|
|
- SSL* ssl = SSL_new(ctx);
|
|
|
- if (ssl == 0 && !SSLErrorCheck(0, "SSL_new"))
|
|
|
- {
|
|
|
- closesocket(client);
|
|
|
- return 0;
|
|
|
- }
|
|
|
- if (!SSLErrorCheck(SSL_set_fd(ssl, (int)client), ssl, "SSL_set_fd"))
|
|
|
- {
|
|
|
- SSL_free(ssl);
|
|
|
- closesocket(client);
|
|
|
- return 0;
|
|
|
- }
|
|
|
- if (!SSLErrorCheck(SSL_accept(ssl), ssl, "SSL_accept"))
|
|
|
- {
|
|
|
- SSL_free(ssl);
|
|
|
- closesocket(client);
|
|
|
- return 0;
|
|
|
- }
|
|
|
- klients++;
|
|
|
- return new SSLSKlient(addr, ssl, client);
|
|
|
+ addr.sin_port = this->addr.sin_port;
|
|
|
+ SSL* ssl = SSL_new(ctx);
|
|
|
+ if (ssl == 0 && !SSLErrorCheck(0, "SSL_new"))
|
|
|
+ {
|
|
|
+ closesocket(client);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (!SSLErrorCheck(SSL_set_fd(ssl, (int)client), ssl, "SSL_set_fd"))
|
|
|
+ {
|
|
|
+ SSL_free(ssl);
|
|
|
+ closesocket(client);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (!SSLErrorCheck(SSL_accept(ssl), ssl, "SSL_accept"))
|
|
|
+ {
|
|
|
+ SSL_free(ssl);
|
|
|
+ closesocket(client);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ klients++;
|
|
|
+ return new SSLSKlient(addr, ssl, client);
|
|
|
}
|
|
|
|
|
|
|
|
|
int SSLServer::getKlients(bool reset)
|
|
|
{
|
|
|
- int ret = klients;
|
|
|
- if (reset)
|
|
|
- klients = 0;
|
|
|
- return ret;
|
|
|
+ int ret = klients;
|
|
|
+ if (reset) klients = 0;
|
|
|
+ return ret;
|
|
|
}
|
|
|
|
|
|
|
|
|
bool SSLServer::trenne()
|
|
|
{
|
|
|
- if (!s)
|
|
|
- return 1;
|
|
|
- if (closesocket(s) < 0)
|
|
|
- return 0;
|
|
|
- s = 0;
|
|
|
- return 1;
|
|
|
+ if (!s) return 1;
|
|
|
+ if (closesocket(s) < 0)
|
|
|
+ return 0;
|
|
|
+ s = 0;
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned short SSLServer::getPort() const
|
|
|
{
|
|
|
- return htons(addr.sin_port);
|
|
|
+ return htons(addr.sin_port);
|
|
|
}
|
|
|
|
|
|
|
|
|
bool SSLServer::isConnected() const
|
|
|
{
|
|
|
- return s != 0;
|
|
|
+ return s != 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
-
|
|
|
+
|
|
|
SSLSKlient::SSLSKlient(sockaddr_in client, SSL* ssl, SOCKET s)
|
|
|
- : ReferenceCounter()
|
|
|
+ : ReferenceCounter()
|
|
|
{
|
|
|
- this->s = s;
|
|
|
- clientAddr = client;
|
|
|
- this->ssl = ssl;
|
|
|
- downStreamBytes = 0;
|
|
|
- upStreamBytes = 0;
|
|
|
+ this->s = s;
|
|
|
+ clientAddr = client;
|
|
|
+ this->ssl = ssl;
|
|
|
+ downStreamBytes = 0;
|
|
|
+ upStreamBytes = 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
SSLSKlient::~SSLSKlient()
|
|
|
{
|
|
|
- trenne();
|
|
|
+ trenne();
|
|
|
#ifdef WIN32
|
|
|
- OPENSSL_thread_stop();
|
|
|
+ OPENSSL_thread_stop();
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-void SSLSKlient::setEmpfangTimeout(int miliseconds)
|
|
|
+
|
|
|
+void SSLSKlient::setEmpfangTimeout(
|
|
|
+ int miliseconds)
|
|
|
{
|
|
|
#ifdef WIN32
|
|
|
- DWORD timeout = miliseconds;
|
|
|
- setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof timeout);
|
|
|
+ DWORD timeout = miliseconds;
|
|
|
+ setsockopt(
|
|
|
+ s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof timeout);
|
|
|
#else
|
|
|
- struct timeval tv;
|
|
|
- tv.tv_sec = miliseconds / 1000;
|
|
|
- tv.tv_usec = (miliseconds % 1000) * 1000;
|
|
|
- setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
|
|
|
+ struct timeval tv;
|
|
|
+ tv.tv_sec = miliseconds / 1000;
|
|
|
+ tv.tv_usec = (miliseconds % 1000) * 1000;
|
|
|
+ setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
bool SSLSKlient::sende(const char* nachricht, int len)
|
|
|
{
|
|
|
- if (!ssl)
|
|
|
- return 0;
|
|
|
- int ll = 0;
|
|
|
- while (len > 0)
|
|
|
- {
|
|
|
- int l = SSL_write(ssl, nachricht + ll, len);
|
|
|
- if (l <= 0)
|
|
|
- return 0;
|
|
|
- len -= l;
|
|
|
- ll += l;
|
|
|
- }
|
|
|
- upStreamBytes += ll;
|
|
|
- return 1;
|
|
|
+ if (!ssl) return 0;
|
|
|
+ int ll = 0;
|
|
|
+ while (len > 0)
|
|
|
+ {
|
|
|
+ int l = SSL_write(ssl, nachricht + ll, len);
|
|
|
+ if (l <= 0) return 0;
|
|
|
+ len -= l;
|
|
|
+ ll += l;
|
|
|
+ }
|
|
|
+ upStreamBytes += ll;
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
-bool SSLSKlient::getNachricht(char* nachricht, int len)
|
|
|
+bool SSLSKlient::getNachricht(
|
|
|
+ char* nachricht, int len)
|
|
|
{
|
|
|
- if (!ssl)
|
|
|
- return 0;
|
|
|
- int ll = 0;
|
|
|
- while (len > 0)
|
|
|
- {
|
|
|
- int l = (int)SSL_read(ssl, nachricht + ll, len);
|
|
|
- if (l <= 0)
|
|
|
- return 0;
|
|
|
- len -= l;
|
|
|
- ll += l;
|
|
|
- }
|
|
|
- downStreamBytes += ll;
|
|
|
- return 1;
|
|
|
+ if (!ssl) return 0;
|
|
|
+ int ll = 0;
|
|
|
+ while (len > 0)
|
|
|
+ {
|
|
|
+ int l = (int)SSL_read(ssl, nachricht + ll, len);
|
|
|
+ if (l <= 0) return 0;
|
|
|
+ len -= l;
|
|
|
+ ll += l;
|
|
|
+ }
|
|
|
+ downStreamBytes += ll;
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
-int SSLSKlient::getDownloadBytes(bool reset)
|
|
|
+int SSLSKlient::getDownloadBytes(
|
|
|
+ bool reset)
|
|
|
{
|
|
|
- int ret = downStreamBytes;
|
|
|
- if (reset)
|
|
|
- downStreamBytes = 0;
|
|
|
- return ret;
|
|
|
+ int ret = downStreamBytes;
|
|
|
+ if (reset) downStreamBytes = 0;
|
|
|
+ return ret;
|
|
|
}
|
|
|
|
|
|
-int SSLSKlient::getUploadBytes(bool reset)
|
|
|
+int SSLSKlient::getUploadBytes(
|
|
|
+ bool reset)
|
|
|
{
|
|
|
- int ret = upStreamBytes;
|
|
|
- if (reset)
|
|
|
- upStreamBytes = 0;
|
|
|
- return ret;
|
|
|
+ int ret = upStreamBytes;
|
|
|
+ if (reset) upStreamBytes = 0;
|
|
|
+ return ret;
|
|
|
}
|
|
|
|
|
|
bool SSLSKlient::trenne()
|
|
|
{
|
|
|
- if (!ssl)
|
|
|
- return 0;
|
|
|
- SSL_free(ssl);
|
|
|
- if (closesocket(s) < 0)
|
|
|
- return 0;
|
|
|
- ssl = 0;
|
|
|
- s = 0;
|
|
|
- return 1;
|
|
|
+ if (!ssl) return 0;
|
|
|
+ SSL_free(ssl);
|
|
|
+ if (closesocket(s) < 0)
|
|
|
+ return 0;
|
|
|
+ ssl = 0;
|
|
|
+ s = 0;
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-bool SSLSKlient::hatNachricht(int zeit) const
|
|
|
+
|
|
|
+bool SSLSKlient::hatNachricht(
|
|
|
+ int zeit) const
|
|
|
{
|
|
|
- fd_set set;
|
|
|
- FD_ZERO(&set);
|
|
|
- FD_SET(SSL_get_rfd(ssl), &set);
|
|
|
- timeval time = { zeit / 1000, zeit };
|
|
|
- return SSL_pending(ssl) > 0 || select(0, &set, 0, 0, &time) == 1;
|
|
|
+ fd_set set;
|
|
|
+ FD_ZERO(&set);
|
|
|
+ FD_SET(SSL_get_rfd(ssl), &set);
|
|
|
+ timeval time = {zeit / 1000, zeit};
|
|
|
+ return SSL_pending(ssl) > 0 || select(0, &set, 0, 0, &time) == 1;
|
|
|
}
|
|
|
|
|
|
unsigned short SSLSKlient::getPort() const
|
|
|
{
|
|
|
- return htons(clientAddr.sin_port);
|
|
|
+ return htons(clientAddr.sin_port);
|
|
|
}
|
|
|
|
|
|
const char* SSLSKlient::getIp() const
|
|
|
{
|
|
|
- return inet_ntoa(clientAddr.sin_addr);
|
|
|
+ return inet_ntoa(clientAddr.sin_addr);
|
|
|
}
|