|
@@ -12,30 +12,29 @@
|
|
|
|
|
|
|
|
using namespace Network;
|
|
using namespace Network;
|
|
|
|
|
|
|
|
-// Inhalt der Server Klasse aus Server.h
|
|
|
|
|
-// Konstruktor
|
|
|
|
|
|
|
+// Content of the Server class from Server.h
|
|
|
|
|
+// Constructor
|
|
|
Server::Server()
|
|
Server::Server()
|
|
|
: ReferenceCounter()
|
|
: ReferenceCounter()
|
|
|
{
|
|
{
|
|
|
sock = 0;
|
|
sock = 0;
|
|
|
- memset(&addresse, 0, sizeof(addresse)); // Adresse setzen
|
|
|
|
|
- addresse.sin_family = AF_INET;
|
|
|
|
|
- addresse.sin_addr.s_addr = ADDR_ANY;
|
|
|
|
|
- klients = 0;
|
|
|
|
|
|
|
+ memset(&address, 0, sizeof(address)); // Set address
|
|
|
|
|
+ address.sin_family = AF_INET;
|
|
|
|
|
+ address.sin_addr.s_addr = ADDR_ANY;
|
|
|
|
|
+ clients = 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Destruktor
|
|
|
|
|
|
|
+// Destructor
|
|
|
Server::~Server()
|
|
Server::~Server()
|
|
|
{
|
|
{
|
|
|
- trenne();
|
|
|
|
|
|
|
+ disconnect();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// nicht constant
|
|
|
|
|
-bool Server::verbinde(
|
|
|
|
|
- unsigned short port, int warteschlangenLen) // Öffnet das Socket
|
|
|
|
|
|
|
+// non-constant
|
|
|
|
|
+bool Server::connect(unsigned short port, int queueLength) // Opens the socket
|
|
|
{
|
|
{
|
|
|
- sock = socket(AF_INET, SOCK_STREAM, 0); // Socket erstellen
|
|
|
|
|
- addresse.sin_port = htons(port); // port setzen
|
|
|
|
|
|
|
+ sock = socket(AF_INET, SOCK_STREAM, 0); // Create socket
|
|
|
|
|
+ address.sin_port = htons(port); // set port
|
|
|
if (sock < 0)
|
|
if (sock < 0)
|
|
|
{
|
|
{
|
|
|
sock = 0;
|
|
sock = 0;
|
|
@@ -48,25 +47,25 @@ bool Server::verbinde(
|
|
|
int reuseSocket = 1;
|
|
int reuseSocket = 1;
|
|
|
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseSocket, sizeof(int));
|
|
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseSocket, sizeof(int));
|
|
|
#endif
|
|
#endif
|
|
|
- if (bind(sock, (struct sockaddr*)&addresse, sizeof(addresse))
|
|
|
|
|
- == -1) // socket öffnen
|
|
|
|
|
|
|
+ if (bind(sock, (struct sockaddr*)&address, sizeof(address))
|
|
|
|
|
+ == -1) // open socket
|
|
|
{
|
|
{
|
|
|
- trenne();
|
|
|
|
|
- return 0; // Fehler
|
|
|
|
|
|
|
+ disconnect();
|
|
|
|
|
+ return 0; // Error
|
|
|
}
|
|
}
|
|
|
- if (listen(sock, warteschlangenLen) == -1) // Klients annehmen
|
|
|
|
|
|
|
+ if (listen(sock, queueLength) == -1) // accept clients
|
|
|
{
|
|
{
|
|
|
- trenne();
|
|
|
|
|
- return 0; // Fehler
|
|
|
|
|
|
|
+ disconnect();
|
|
|
|
|
+ return 0; // Error
|
|
|
}
|
|
}
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-SKlient* Server::getKlient() // nimmt Klient an
|
|
|
|
|
|
|
+SClient* Server::getClient() // accepts a client
|
|
|
{
|
|
{
|
|
|
if (!sock) return 0;
|
|
if (!sock) return 0;
|
|
|
sockaddr_in client;
|
|
sockaddr_in client;
|
|
|
- int len = sizeof(addresse);
|
|
|
|
|
|
|
+ int len = sizeof(address);
|
|
|
fd_set set;
|
|
fd_set set;
|
|
|
int rv = 0;
|
|
int rv = 0;
|
|
|
struct timeval timeout;
|
|
struct timeval timeout;
|
|
@@ -81,78 +80,78 @@ SKlient* Server::getKlient() // nimmt Klient an
|
|
|
}
|
|
}
|
|
|
if (!sock) return 0;
|
|
if (!sock) return 0;
|
|
|
#ifdef WIN32
|
|
#ifdef WIN32
|
|
|
- SOCKET cls = accept(sock, (sockaddr*)&client, &len); // Klient empfangen
|
|
|
|
|
|
|
+ SOCKET cls = accept(sock, (sockaddr*)&client, &len); // receive client
|
|
|
if (cls == INVALID_SOCKET)
|
|
if (cls == INVALID_SOCKET)
|
|
|
{
|
|
{
|
|
|
- trenne();
|
|
|
|
|
|
|
+ disconnect();
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
#else
|
|
#else
|
|
|
SOCKET cls = accept(
|
|
SOCKET cls = accept(
|
|
|
- sock, (sockaddr*)&client, (socklen_t*)&len); // Klient empfangen
|
|
|
|
|
|
|
+ sock, (sockaddr*)&client, (socklen_t*)&len); // receive client
|
|
|
if (!cls)
|
|
if (!cls)
|
|
|
{
|
|
{
|
|
|
- if (errno == ECONNABORTED || errno == EBADF) trenne();
|
|
|
|
|
|
|
+ if (errno == ECONNABORTED || errno == EBADF) disconnect();
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
#endif
|
|
#endif
|
|
|
- client.sin_port = addresse.sin_port;
|
|
|
|
|
- klients++;
|
|
|
|
|
- return new SKlient(client, cls); // Klient Handle Klasse zurückgeben
|
|
|
|
|
|
|
+ client.sin_port = address.sin_port;
|
|
|
|
|
+ clients++;
|
|
|
|
|
+ return new SClient(client, cls); // return client handle class
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-int Server::getKlients(bool reset) // gibt die Anzahl der Klients zurück
|
|
|
|
|
|
|
+int Server::getClients(bool reset) // returns the number of clients
|
|
|
{
|
|
{
|
|
|
- int ret = klients;
|
|
|
|
|
- if (reset) klients = 0;
|
|
|
|
|
|
|
+ int ret = clients;
|
|
|
|
|
+ if (reset) clients = 0;
|
|
|
return ret;
|
|
return ret;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-bool Server::trenne() // beendet den Server
|
|
|
|
|
|
|
+bool Server::disconnect() // stops the server
|
|
|
{
|
|
{
|
|
|
if (!sock) return 1;
|
|
if (!sock) return 1;
|
|
|
- if (closesocket(sock) < 0) // socket schließen
|
|
|
|
|
|
|
+ if (closesocket(sock) < 0) // close socket
|
|
|
return 0;
|
|
return 0;
|
|
|
sock = 0;
|
|
sock = 0;
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// constant
|
|
// constant
|
|
|
-unsigned short Server::getPort() const // gibt den Port zurück
|
|
|
|
|
|
|
+unsigned short Server::getPort() const // returns the port
|
|
|
{
|
|
{
|
|
|
- return htons(addresse.sin_port);
|
|
|
|
|
|
|
+ return htons(address.sin_port);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
bool Server::isConnected()
|
|
bool Server::isConnected()
|
|
|
- const // giebt 1 zurück, falls der Server verbunden ist
|
|
|
|
|
|
|
+ const // returns 1 if the server is connected
|
|
|
{
|
|
{
|
|
|
return sock != 0;
|
|
return sock != 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Inhalt der SKlient Klasse aus Server.h
|
|
|
|
|
-// Konstruktor
|
|
|
|
|
-SKlient::SKlient(sockaddr_in addresse, SOCKET sock)
|
|
|
|
|
|
|
+// Content of the SClient class from Server.h
|
|
|
|
|
+// Constructor
|
|
|
|
|
+SClient::SClient(sockaddr_in address, SOCKET sock)
|
|
|
: ReferenceCounter()
|
|
: ReferenceCounter()
|
|
|
{
|
|
{
|
|
|
- clientAddr = addresse;
|
|
|
|
|
|
|
+ clientAddr = address;
|
|
|
this->sock = sock;
|
|
this->sock = sock;
|
|
|
downStreamBytes = 0;
|
|
downStreamBytes = 0;
|
|
|
upStreamBytes = 0;
|
|
upStreamBytes = 0;
|
|
|
- sendeKey = 0;
|
|
|
|
|
- empfangKey = 0;
|
|
|
|
|
|
|
+ sendKey = 0;
|
|
|
|
|
+ receiveKey = 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Destruktor
|
|
|
|
|
-SKlient::~SKlient()
|
|
|
|
|
|
|
+// Destructor
|
|
|
|
|
+SClient::~SClient()
|
|
|
{
|
|
{
|
|
|
- trenne();
|
|
|
|
|
- if (sendeKey) sendeKey->release();
|
|
|
|
|
- if (empfangKey) empfangKey->release();
|
|
|
|
|
|
|
+ disconnect();
|
|
|
|
|
+ if (sendKey) sendKey->release();
|
|
|
|
|
+ if (receiveKey) receiveKey->release();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// nicht constant
|
|
|
|
|
-void SKlient::setEmpfangTimeout(
|
|
|
|
|
- int miliseconds) // Setzt ein timeout fürs empfangen von daten
|
|
|
|
|
|
|
+// non-constant
|
|
|
|
|
+void SClient::setReceiveTimeout(
|
|
|
|
|
+ int miliseconds) // Sets a timeout for receiving data
|
|
|
{
|
|
{
|
|
|
#ifdef WIN32
|
|
#ifdef WIN32
|
|
|
DWORD timeout = miliseconds;
|
|
DWORD timeout = miliseconds;
|
|
@@ -166,42 +165,42 @@ void SKlient::setEmpfangTimeout(
|
|
|
#endif
|
|
#endif
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void SKlient::setSendeKeyZ(Encryption::Key* key) // Setzt den Key fürs Senden
|
|
|
|
|
|
|
+void SClient::setSendKeyZ(Encryption::Key* key) // Sets the key for sending
|
|
|
{
|
|
{
|
|
|
- if (sendeKey) sendeKey->release();
|
|
|
|
|
- sendeKey = key;
|
|
|
|
|
|
|
+ if (sendKey) sendKey->release();
|
|
|
|
|
+ sendKey = key;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void SKlient::setEmpfangKeyZ(
|
|
|
|
|
- Encryption::Key* key) // Setzt den Key fürs Empfangen
|
|
|
|
|
|
|
+void SClient::setReceiveKeyZ(
|
|
|
|
|
+ Encryption::Key* key) // Sets the key for receiving
|
|
|
{
|
|
{
|
|
|
- if (empfangKey) empfangKey->release();
|
|
|
|
|
- empfangKey = key;
|
|
|
|
|
|
|
+ if (receiveKey) receiveKey->release();
|
|
|
|
|
+ receiveKey = key;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void SKlient::setSendeKey(const char* key, int len) // Setzt den Key fürs Senden
|
|
|
|
|
|
|
+void SClient::setSendKey(const char* key, int len) // Sets the key for sending
|
|
|
{
|
|
{
|
|
|
- if (!sendeKey) sendeKey = new Encryption::Key();
|
|
|
|
|
- sendeKey->setKey(key, len);
|
|
|
|
|
|
|
+ if (!sendKey) sendKey = new Encryption::Key();
|
|
|
|
|
+ sendKey->setKey(key, len);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void SKlient::setEmpfangKey(
|
|
|
|
|
- const char* key, int len) // Setzt den Key fürs Empfangen
|
|
|
|
|
|
|
+void SClient::setReceiveKey(
|
|
|
|
|
+ const char* key, int len) // Sets the key for receiving
|
|
|
{
|
|
{
|
|
|
- if (!empfangKey) empfangKey = new Encryption::Key();
|
|
|
|
|
- empfangKey->setKey(key, len);
|
|
|
|
|
|
|
+ if (!receiveKey) receiveKey = new Encryption::Key();
|
|
|
|
|
+ receiveKey->setKey(key, len);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-bool SKlient::sende(const char* nachricht, int len) // sendet zum Klient
|
|
|
|
|
|
|
+bool SClient::send(const char* message, int len) // sends to client
|
|
|
{
|
|
{
|
|
|
if (!sock) return 0;
|
|
if (!sock) return 0;
|
|
|
int ll = 0;
|
|
int ll = 0;
|
|
|
while (len > 0)
|
|
while (len > 0)
|
|
|
{
|
|
{
|
|
|
#ifdef WIN32
|
|
#ifdef WIN32
|
|
|
- int l = send(sock, nachricht + ll, len, 0);
|
|
|
|
|
|
|
+ int l = send(sock, message + ll, len, 0);
|
|
|
#else
|
|
#else
|
|
|
- int l = (int)send(sock, nachricht + ll, len, MSG_NOSIGNAL);
|
|
|
|
|
|
|
+ int l = (int)send(sock, message + ll, len, MSG_NOSIGNAL);
|
|
|
#endif
|
|
#endif
|
|
|
if (l <= 0)
|
|
if (l <= 0)
|
|
|
{
|
|
{
|
|
@@ -211,7 +210,7 @@ bool SKlient::sende(const char* nachricht, int len) // sendet zum Klient
|
|
|
<< "send: " << l << " Error: " << WSAGetLastError();
|
|
<< "send: " << l << " Error: " << WSAGetLastError();
|
|
|
# endif
|
|
# endif
|
|
|
#endif
|
|
#endif
|
|
|
- return 0; // Fehler
|
|
|
|
|
|
|
+ return 0; // Error
|
|
|
}
|
|
}
|
|
|
len -= l;
|
|
len -= l;
|
|
|
ll += l;
|
|
ll += l;
|
|
@@ -220,14 +219,13 @@ bool SKlient::sende(const char* nachricht, int len) // sendet zum Klient
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-bool SKlient::getNachricht(
|
|
|
|
|
- char* nachricht, int len) // empfängt Nachricht von Klient
|
|
|
|
|
|
|
+bool SClient::getMessage(char* message, int len) // receives message from client
|
|
|
{
|
|
{
|
|
|
if (!sock) return 0;
|
|
if (!sock) return 0;
|
|
|
int ll = 0;
|
|
int ll = 0;
|
|
|
while (len > 0)
|
|
while (len > 0)
|
|
|
{
|
|
{
|
|
|
- int l = (int)recv(sock, nachricht + ll, len, MSG_WAITALL);
|
|
|
|
|
|
|
+ int l = (int)recv(sock, message + ll, len, MSG_WAITALL);
|
|
|
if (l <= 0)
|
|
if (l <= 0)
|
|
|
{
|
|
{
|
|
|
#ifdef WIN32
|
|
#ifdef WIN32
|
|
@@ -236,7 +234,7 @@ bool SKlient::getNachricht(
|
|
|
<< "recv: " << l << " Error: " << WSAGetLastError();
|
|
<< "recv: " << l << " Error: " << WSAGetLastError();
|
|
|
# endif
|
|
# endif
|
|
|
#endif
|
|
#endif
|
|
|
- return 0; // Fehler
|
|
|
|
|
|
|
+ return 0; // Error
|
|
|
}
|
|
}
|
|
|
len -= l;
|
|
len -= l;
|
|
|
ll += l;
|
|
ll += l;
|
|
@@ -245,12 +243,11 @@ bool SKlient::getNachricht(
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-bool SKlient::sendeEncrypted(
|
|
|
|
|
- const char* nachricht, int len) // sendet zum Server
|
|
|
|
|
|
|
+bool SClient::sendEncrypted(const char* message, int len) // sends to server
|
|
|
{
|
|
{
|
|
|
- if (!sendeKey) return sende(nachricht, len);
|
|
|
|
|
- Encryption::Bytes* n = new Encryption::Bytes(nachricht, len);
|
|
|
|
|
- sendeKey->codieren(dynamic_cast<Encryption::Bytes*>(n->getThis()));
|
|
|
|
|
|
|
+ if (!sendKey) return send(message, len);
|
|
|
|
|
+ Encryption::Bytes* n = new Encryption::Bytes(message, len);
|
|
|
|
|
+ sendKey->codieren(dynamic_cast<Encryption::Bytes*>(n->getThis()));
|
|
|
int ll = 0;
|
|
int ll = 0;
|
|
|
while (len > 0)
|
|
while (len > 0)
|
|
|
{
|
|
{
|
|
@@ -268,7 +265,7 @@ bool SKlient::sendeEncrypted(
|
|
|
# endif
|
|
# endif
|
|
|
#endif
|
|
#endif
|
|
|
n->release();
|
|
n->release();
|
|
|
- return 0; // Fehler
|
|
|
|
|
|
|
+ return 0; // Error
|
|
|
}
|
|
}
|
|
|
len -= l;
|
|
len -= l;
|
|
|
ll += l;
|
|
ll += l;
|
|
@@ -278,14 +275,13 @@ bool SKlient::sendeEncrypted(
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-bool SKlient::getNachrichtEncrypted(
|
|
|
|
|
- char* nachricht, int len) // empfängt Nachricht
|
|
|
|
|
|
|
+bool SClient::getMessageEncrypted(char* message, int len) // receives message
|
|
|
{
|
|
{
|
|
|
- if (!empfangKey) return getNachricht(nachricht, len);
|
|
|
|
|
|
|
+ if (!receiveKey) return getMessage(message, len);
|
|
|
int ll = 0;
|
|
int ll = 0;
|
|
|
while (len > 0)
|
|
while (len > 0)
|
|
|
{
|
|
{
|
|
|
- int l = (int)recv(sock, nachricht + ll, len, MSG_WAITALL);
|
|
|
|
|
|
|
+ int l = (int)recv(sock, message + ll, len, MSG_WAITALL);
|
|
|
if (l <= 0)
|
|
if (l <= 0)
|
|
|
{
|
|
{
|
|
|
#ifdef WIN32
|
|
#ifdef WIN32
|
|
@@ -294,51 +290,51 @@ bool SKlient::getNachrichtEncrypted(
|
|
|
<< "recv: " << l << " Error: " << WSAGetLastError();
|
|
<< "recv: " << l << " Error: " << WSAGetLastError();
|
|
|
# endif
|
|
# endif
|
|
|
#endif
|
|
#endif
|
|
|
- return 0; // Fehler
|
|
|
|
|
|
|
+ return 0; // Error
|
|
|
}
|
|
}
|
|
|
len -= l;
|
|
len -= l;
|
|
|
ll += l;
|
|
ll += l;
|
|
|
}
|
|
}
|
|
|
Encryption::Bytes* n = new Encryption::Bytes();
|
|
Encryption::Bytes* n = new Encryption::Bytes();
|
|
|
- n->setBytesZ(nachricht, ll);
|
|
|
|
|
- empfangKey->decodieren(n);
|
|
|
|
|
|
|
+ n->setBytesZ(message, ll);
|
|
|
|
|
+ receiveKey->decodieren(n);
|
|
|
downStreamBytes += ll;
|
|
downStreamBytes += ll;
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-int SKlient::getDownloadBytes(
|
|
|
|
|
- bool reset) // gibt die anzahl von empfangen bytes zurück
|
|
|
|
|
|
|
+int SClient::getDownloadBytes(
|
|
|
|
|
+ bool reset) // returns the number of received bytes
|
|
|
{
|
|
{
|
|
|
int ret = downStreamBytes;
|
|
int ret = downStreamBytes;
|
|
|
if (reset) downStreamBytes = 0;
|
|
if (reset) downStreamBytes = 0;
|
|
|
return ret;
|
|
return ret;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-int SKlient::getUploadBytes(
|
|
|
|
|
- bool reset) // gibt die anzahl von versendeter bytes zurück
|
|
|
|
|
|
|
+int SClient::getUploadBytes(
|
|
|
|
|
+ bool reset) // returns the number of sent bytes
|
|
|
{
|
|
{
|
|
|
int ret = upStreamBytes;
|
|
int ret = upStreamBytes;
|
|
|
if (reset) upStreamBytes = 0;
|
|
if (reset) upStreamBytes = 0;
|
|
|
return ret;
|
|
return ret;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-bool SKlient::trenne() // trennt die Verbindung zum Klient
|
|
|
|
|
|
|
+bool SClient::disconnect() // disconnects from client
|
|
|
{
|
|
{
|
|
|
if (!sock) return 0;
|
|
if (!sock) return 0;
|
|
|
- if (closesocket(sock) < 0) // trennen
|
|
|
|
|
|
|
+ if (closesocket(sock) < 0) // disconnect
|
|
|
return 0;
|
|
return 0;
|
|
|
sock = 0;
|
|
sock = 0;
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// constant
|
|
// constant
|
|
|
-bool SKlient::hatNachricht(
|
|
|
|
|
- int zeit) const // Wartet eine Zeit Lang auf eine Nachricht
|
|
|
|
|
|
|
+bool SClient::hasMessage(
|
|
|
|
|
+ int time) const // Waits for a message for a given time
|
|
|
{
|
|
{
|
|
|
fd_set set;
|
|
fd_set set;
|
|
|
FD_ZERO(&set);
|
|
FD_ZERO(&set);
|
|
|
FD_SET(sock, &set);
|
|
FD_SET(sock, &set);
|
|
|
- timeval time = {zeit / 1000, zeit};
|
|
|
|
|
|
|
+ timeval time = {time / 1000, time};
|
|
|
int result = select(0, &set, 0, 0, &time);
|
|
int result = select(0, &set, 0, 0, &time);
|
|
|
if (result < 0)
|
|
if (result < 0)
|
|
|
{
|
|
{
|
|
@@ -352,20 +348,20 @@ bool SKlient::hatNachricht(
|
|
|
return result > 0;
|
|
return result > 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-unsigned short SKlient::getPort() const // gibt den Port zurück
|
|
|
|
|
|
|
+unsigned short SClient::getPort() const // returns the port
|
|
|
{
|
|
{
|
|
|
return htons(clientAddr.sin_port);
|
|
return htons(clientAddr.sin_port);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-const char* SKlient::getIp() const // gibt die Ip des Klients zurück
|
|
|
|
|
|
|
+const char* SClient::getIp() const // returns the client's IP
|
|
|
{
|
|
{
|
|
|
return inet_ntoa(clientAddr.sin_addr);
|
|
return inet_ntoa(clientAddr.sin_addr);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int pem_passwd_cb(char* buf, int size, int rwflag, void* userdata)
|
|
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));
|
|
|
|
|
|
|
+ const char* pw = ((Text*)userdata)->getText();
|
|
|
|
|
+ memcpy(buf, pw, MIN((unsigned int)size, strlen(pw) + 1));
|
|
|
return (int)strlen(buf);
|
|
return (int)strlen(buf);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -395,8 +391,8 @@ bool SSLErrorCheck(__int64 result, const char* action)
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-bool SKlient::waitForNextMessage()
|
|
|
|
|
- const // wartet bis es etwas zu empfangen gibt
|
|
|
|
|
|
|
+bool SClient::waitForNextMessage()
|
|
|
|
|
+ const // waits until there is something to receive
|
|
|
{
|
|
{
|
|
|
fd_set set;
|
|
fd_set set;
|
|
|
int rv = 0;
|
|
int rv = 0;
|
|
@@ -435,13 +431,13 @@ bool SKlient::waitForNextMessage()
|
|
|
<< "recv: " << l << " Error: " << WSAGetLastError();
|
|
<< "recv: " << l << " Error: " << WSAGetLastError();
|
|
|
# endif
|
|
# endif
|
|
|
#endif
|
|
#endif
|
|
|
- return 0; // Fehler
|
|
|
|
|
|
|
+ return 0; // Error
|
|
|
}
|
|
}
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Inhalt der SSLServer Klasse
|
|
|
|
|
-// Konstruktor
|
|
|
|
|
|
|
+// Content of the SSLServer class
|
|
|
|
|
+// Constructor
|
|
|
SSLServer::SSLServer()
|
|
SSLServer::SSLServer()
|
|
|
: ReferenceCounter()
|
|
: ReferenceCounter()
|
|
|
{
|
|
{
|
|
@@ -454,26 +450,26 @@ SSLServer::SSLServer()
|
|
|
"SSL_CTX_set_max_proto_version");
|
|
"SSL_CTX_set_max_proto_version");
|
|
|
SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
|
|
SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
|
|
|
SSL_CTX_set_default_passwd_cb(ctx, pem_passwd_cb);
|
|
SSL_CTX_set_default_passwd_cb(ctx, pem_passwd_cb);
|
|
|
- passw = new Text();
|
|
|
|
|
- SSL_CTX_set_default_passwd_cb_userdata(ctx, passw);
|
|
|
|
|
|
|
+ password = new Text();
|
|
|
|
|
+ SSL_CTX_set_default_passwd_cb_userdata(ctx, password);
|
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_family = AF_INET;
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
- klients = 0;
|
|
|
|
|
|
|
+ clients = 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Destruktor
|
|
|
|
|
|
|
+// Destructor
|
|
|
SSLServer::~SSLServer()
|
|
SSLServer::~SSLServer()
|
|
|
{
|
|
{
|
|
|
- trenne();
|
|
|
|
|
|
|
+ disconnect();
|
|
|
SSL_CTX_free(ctx);
|
|
SSL_CTX_free(ctx);
|
|
|
- passw->release();
|
|
|
|
|
|
|
+ password->release();
|
|
|
#ifdef WIN32
|
|
#ifdef WIN32
|
|
|
OPENSSL_thread_stop();
|
|
OPENSSL_thread_stop();
|
|
|
#endif
|
|
#endif
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// nicht constant
|
|
|
|
|
-// Setzt den Pfad zur Datei, in dem das Certifikat gespeichert ist
|
|
|
|
|
|
|
+// non-constant
|
|
|
|
|
+// Sets the path to the file where the certificate is stored
|
|
|
bool SSLServer::setCertificateFile(const char* file)
|
|
bool SSLServer::setCertificateFile(const char* file)
|
|
|
{
|
|
{
|
|
|
return SSLErrorCheck(
|
|
return SSLErrorCheck(
|
|
@@ -481,7 +477,7 @@ bool SSLServer::setCertificateFile(const char* file)
|
|
|
"SSL_CTX_use_certificate_file");
|
|
"SSL_CTX_use_certificate_file");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Setzt den Pfad zur Datei, in dem der private Schlüssel gespeichert ist
|
|
|
|
|
|
|
+// Sets the path to the file where the private key is stored
|
|
|
bool SSLServer::setPrivateKeyFile(const char* file)
|
|
bool SSLServer::setPrivateKeyFile(const char* file)
|
|
|
{
|
|
{
|
|
|
return SSLErrorCheck(
|
|
return SSLErrorCheck(
|
|
@@ -489,15 +485,15 @@ bool SSLServer::setPrivateKeyFile(const char* file)
|
|
|
"SSL_CTX_use_PrivateKey_file");
|
|
"SSL_CTX_use_PrivateKey_file");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// setzt das passwort des private keys (muss vor setPrivateKeyFile aufgerufen
|
|
|
|
|
-// werden)
|
|
|
|
|
|
|
+// sets the password of the private key (must be called before
|
|
|
|
|
+// setPrivateKeyFile)
|
|
|
void SSLServer::setPrivateKeyPassword(const char* password)
|
|
void SSLServer::setPrivateKeyPassword(const char* password)
|
|
|
{
|
|
{
|
|
|
- passw->setText(password);
|
|
|
|
|
|
|
+ this->password->setText(password);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Öffnet das Socket
|
|
|
|
|
-bool SSLServer::verbinde(unsigned short port, int warteschlangenLen)
|
|
|
|
|
|
|
+// Opens the socket
|
|
|
|
|
+bool SSLServer::connect(unsigned short port, int queueLength)
|
|
|
{
|
|
{
|
|
|
addr.sin_port = htons(port);
|
|
addr.sin_port = htons(port);
|
|
|
s = socket(AF_INET, SOCK_STREAM, 0);
|
|
s = socket(AF_INET, SOCK_STREAM, 0);
|
|
@@ -515,19 +511,19 @@ bool SSLServer::verbinde(unsigned short port, int warteschlangenLen)
|
|
|
#endif
|
|
#endif
|
|
|
if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0)
|
|
if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0)
|
|
|
{
|
|
{
|
|
|
- trenne();
|
|
|
|
|
|
|
+ disconnect();
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
- if (listen(s, warteschlangenLen) < 0)
|
|
|
|
|
|
|
+ if (listen(s, queueLength) < 0)
|
|
|
{
|
|
{
|
|
|
- trenne();
|
|
|
|
|
|
|
+ disconnect();
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// nimmt Klient an
|
|
|
|
|
-SSLSKlient* SSLServer::getKlient()
|
|
|
|
|
|
|
+// accepts a client
|
|
|
|
|
+SSLSClient* SSLServer::getClient()
|
|
|
{
|
|
{
|
|
|
if (!s) return 0;
|
|
if (!s) return 0;
|
|
|
int len = sizeof(addr);
|
|
int len = sizeof(addr);
|
|
@@ -549,15 +545,15 @@ SSLSKlient* SSLServer::getKlient()
|
|
|
SOCKET client = accept(s, (struct sockaddr*)&addr, &len);
|
|
SOCKET client = accept(s, (struct sockaddr*)&addr, &len);
|
|
|
if (client == INVALID_SOCKET)
|
|
if (client == INVALID_SOCKET)
|
|
|
{
|
|
{
|
|
|
- trenne();
|
|
|
|
|
|
|
+ disconnect();
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
#else
|
|
#else
|
|
|
SOCKET client
|
|
SOCKET client
|
|
|
- = accept(s, (sockaddr*)&addr, (socklen_t*)&len); // Klient empfangen
|
|
|
|
|
|
|
+ = accept(s, (sockaddr*)&addr, (socklen_t*)&len); // receive client
|
|
|
if (!client)
|
|
if (!client)
|
|
|
{
|
|
{
|
|
|
- if (errno == ECONNABORTED || errno == EBADF) trenne();
|
|
|
|
|
|
|
+ if (errno == ECONNABORTED || errno == EBADF) disconnect();
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
#endif
|
|
#endif
|
|
@@ -580,44 +576,44 @@ SSLSKlient* SSLServer::getKlient()
|
|
|
closesocket(client);
|
|
closesocket(client);
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
- klients++;
|
|
|
|
|
- return new SSLSKlient(addr, ssl, client);
|
|
|
|
|
|
|
+ clients++;
|
|
|
|
|
+ return new SSLSClient(addr, ssl, client);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// gibt die Anzahl der Klients zurück
|
|
|
|
|
-int SSLServer::getKlients(bool reset)
|
|
|
|
|
|
|
+// returns the number of clients
|
|
|
|
|
+int SSLServer::getClients(bool reset)
|
|
|
{
|
|
{
|
|
|
- int ret = klients;
|
|
|
|
|
- if (reset) klients = 0;
|
|
|
|
|
|
|
+ int ret = clients;
|
|
|
|
|
+ if (reset) clients = 0;
|
|
|
return ret;
|
|
return ret;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// beendet den Server
|
|
|
|
|
-bool SSLServer::trenne()
|
|
|
|
|
|
|
+// stops the server
|
|
|
|
|
+bool SSLServer::disconnect()
|
|
|
{
|
|
{
|
|
|
if (!s) return 1;
|
|
if (!s) return 1;
|
|
|
- if (closesocket(s) < 0) // socket schließen
|
|
|
|
|
|
|
+ if (closesocket(s) < 0) // close socket
|
|
|
return 0;
|
|
return 0;
|
|
|
s = 0;
|
|
s = 0;
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// constant
|
|
// constant
|
|
|
-// gibt den Port zurück
|
|
|
|
|
|
|
+// returns the port
|
|
|
unsigned short SSLServer::getPort() const
|
|
unsigned short SSLServer::getPort() const
|
|
|
{
|
|
{
|
|
|
return htons(addr.sin_port);
|
|
return htons(addr.sin_port);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// giebt 1 zurück, falls der Server verbunden ist
|
|
|
|
|
|
|
+// returns 1 if the server is connected
|
|
|
bool SSLServer::isConnected() const
|
|
bool SSLServer::isConnected() const
|
|
|
{
|
|
{
|
|
|
return s != 0;
|
|
return s != 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Inhalt der SSLSKlient Klasse
|
|
|
|
|
-// Konstruktor
|
|
|
|
|
-SSLSKlient::SSLSKlient(sockaddr_in client, SSL* ssl, SOCKET s)
|
|
|
|
|
|
|
+// Content of the SSLSClient class
|
|
|
|
|
+// Constructor
|
|
|
|
|
+SSLSClient::SSLSClient(sockaddr_in client, SSL* ssl, SOCKET s)
|
|
|
: ReferenceCounter()
|
|
: ReferenceCounter()
|
|
|
{
|
|
{
|
|
|
this->s = s;
|
|
this->s = s;
|
|
@@ -627,18 +623,18 @@ SSLSKlient::SSLSKlient(sockaddr_in client, SSL* ssl, SOCKET s)
|
|
|
upStreamBytes = 0;
|
|
upStreamBytes = 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Destruktor
|
|
|
|
|
-SSLSKlient::~SSLSKlient()
|
|
|
|
|
|
|
+// Destructor
|
|
|
|
|
+SSLSClient::~SSLSClient()
|
|
|
{
|
|
{
|
|
|
- trenne();
|
|
|
|
|
|
|
+ disconnect();
|
|
|
#ifdef WIN32
|
|
#ifdef WIN32
|
|
|
OPENSSL_thread_stop();
|
|
OPENSSL_thread_stop();
|
|
|
#endif
|
|
#endif
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// nicht constant
|
|
|
|
|
-void SSLSKlient::setEmpfangTimeout(
|
|
|
|
|
- int miliseconds) // Setzt ein timeout fürs empfangen von daten
|
|
|
|
|
|
|
+// non-constant
|
|
|
|
|
+void SSLSClient::setReceiveTimeout(
|
|
|
|
|
+ int miliseconds) // Sets a timeout for receiving data
|
|
|
{
|
|
{
|
|
|
#ifdef WIN32
|
|
#ifdef WIN32
|
|
|
DWORD timeout = miliseconds;
|
|
DWORD timeout = miliseconds;
|
|
@@ -652,20 +648,20 @@ void SSLSKlient::setEmpfangTimeout(
|
|
|
#endif
|
|
#endif
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-bool SSLSKlient::sende(const char* nachricht, int len) // sendet zum Klient
|
|
|
|
|
|
|
+bool SSLSClient::send(const char* message, int len) // sends to client
|
|
|
{
|
|
{
|
|
|
if (!ssl) return 0;
|
|
if (!ssl) return 0;
|
|
|
int ll = 0;
|
|
int ll = 0;
|
|
|
while (len > 0)
|
|
while (len > 0)
|
|
|
{
|
|
{
|
|
|
- int l = SSL_write(ssl, nachricht + ll, len);
|
|
|
|
|
|
|
+ int l = SSL_write(ssl, message + ll, len);
|
|
|
if (l <= 0)
|
|
if (l <= 0)
|
|
|
{
|
|
{
|
|
|
#ifdef _DEBUG
|
|
#ifdef _DEBUG
|
|
|
Framework::Logging::warning()
|
|
Framework::Logging::warning()
|
|
|
<< "SSL_write: " << l << " Error: " << SSL_get_error(ssl, l);
|
|
<< "SSL_write: " << l << " Error: " << SSL_get_error(ssl, l);
|
|
|
#endif
|
|
#endif
|
|
|
- return 0; // Fehler
|
|
|
|
|
|
|
+ return 0; // Error
|
|
|
}
|
|
}
|
|
|
len -= l;
|
|
len -= l;
|
|
|
ll += l;
|
|
ll += l;
|
|
@@ -674,21 +670,20 @@ bool SSLSKlient::sende(const char* nachricht, int len) // sendet zum Klient
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-bool SSLSKlient::getNachricht(
|
|
|
|
|
- char* nachricht, int len) // empfängt Nachricht von Klient
|
|
|
|
|
|
|
+bool SSLSClient::getMessage(char* message, int len) // receives message from client
|
|
|
{
|
|
{
|
|
|
if (!ssl) return 0;
|
|
if (!ssl) return 0;
|
|
|
int ll = 0;
|
|
int ll = 0;
|
|
|
while (len > 0)
|
|
while (len > 0)
|
|
|
{
|
|
{
|
|
|
- int l = (int)SSL_read(ssl, nachricht + ll, len);
|
|
|
|
|
|
|
+ int l = (int)SSL_read(ssl, message + ll, len);
|
|
|
if (l <= 0)
|
|
if (l <= 0)
|
|
|
{
|
|
{
|
|
|
#ifdef _DEBUG
|
|
#ifdef _DEBUG
|
|
|
Framework::Logging::warning()
|
|
Framework::Logging::warning()
|
|
|
<< "SSL_read: " << l << " Error: " << SSL_get_error(ssl, l);
|
|
<< "SSL_read: " << l << " Error: " << SSL_get_error(ssl, l);
|
|
|
#endif
|
|
#endif
|
|
|
- return 0; // Fehler
|
|
|
|
|
|
|
+ return 0; // Error
|
|
|
}
|
|
}
|
|
|
len -= l;
|
|
len -= l;
|
|
|
ll += l;
|
|
ll += l;
|
|
@@ -697,27 +692,27 @@ bool SSLSKlient::getNachricht(
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-int SSLSKlient::getDownloadBytes(
|
|
|
|
|
- bool reset) // gibt die anzahl von empfangen bytes zurück
|
|
|
|
|
|
|
+int SSLSClient::getDownloadBytes(
|
|
|
|
|
+ bool reset) // returns the number of received bytes
|
|
|
{
|
|
{
|
|
|
int ret = downStreamBytes;
|
|
int ret = downStreamBytes;
|
|
|
if (reset) downStreamBytes = 0;
|
|
if (reset) downStreamBytes = 0;
|
|
|
return ret;
|
|
return ret;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-int SSLSKlient::getUploadBytes(
|
|
|
|
|
- bool reset) // gibt die anzahl von versendeter bytes zurück
|
|
|
|
|
|
|
+int SSLSClient::getUploadBytes(
|
|
|
|
|
+ bool reset) // returns the number of sent bytes
|
|
|
{
|
|
{
|
|
|
int ret = upStreamBytes;
|
|
int ret = upStreamBytes;
|
|
|
if (reset) upStreamBytes = 0;
|
|
if (reset) upStreamBytes = 0;
|
|
|
return ret;
|
|
return ret;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-bool SSLSKlient::trenne() // trennt die Verbindung zum Klient
|
|
|
|
|
|
|
+bool SSLSClient::disconnect() // disconnects from client
|
|
|
{
|
|
{
|
|
|
if (!ssl) return 0;
|
|
if (!ssl) return 0;
|
|
|
SSL_free(ssl);
|
|
SSL_free(ssl);
|
|
|
- if (closesocket(s) < 0) // trennen
|
|
|
|
|
|
|
+ if (closesocket(s) < 0) // disconnect
|
|
|
return 0;
|
|
return 0;
|
|
|
ssl = 0;
|
|
ssl = 0;
|
|
|
s = 0;
|
|
s = 0;
|
|
@@ -725,22 +720,22 @@ bool SSLSKlient::trenne() // trennt die Verbindung zum Klient
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// constant
|
|
// constant
|
|
|
-bool SSLSKlient::hatNachricht(
|
|
|
|
|
- int zeit) const // Wartet eine Zeit Lang auf eine Nachricht
|
|
|
|
|
|
|
+bool SSLSClient::hasMessage(
|
|
|
|
|
+ int time) const // Waits for a message for a given time
|
|
|
{
|
|
{
|
|
|
fd_set set;
|
|
fd_set set;
|
|
|
FD_ZERO(&set);
|
|
FD_ZERO(&set);
|
|
|
FD_SET(SSL_get_rfd(ssl), &set);
|
|
FD_SET(SSL_get_rfd(ssl), &set);
|
|
|
- timeval time = {zeit / 1000, zeit};
|
|
|
|
|
|
|
+ timeval time = {time / 1000, time};
|
|
|
return SSL_pending(ssl) > 0 || select(0, &set, 0, 0, &time) == 1;
|
|
return SSL_pending(ssl) > 0 || select(0, &set, 0, 0, &time) == 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-unsigned short SSLSKlient::getPort() const // gibt den Port zurück
|
|
|
|
|
|
|
+unsigned short SSLSClient::getPort() const // returns the port
|
|
|
{
|
|
{
|
|
|
return htons(clientAddr.sin_port);
|
|
return htons(clientAddr.sin_port);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-const char* SSLSKlient::getIp() const // gibt die Ip des Klients zurück
|
|
|
|
|
|
|
+const char* SSLSClient::getIp() const // returns the client's IP
|
|
|
{
|
|
{
|
|
|
return inet_ntoa(clientAddr.sin_addr);
|
|
return inet_ntoa(clientAddr.sin_addr);
|
|
|
}
|
|
}
|