Server.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. #include "Server.h"
  2. #include <openssl/err.h>
  3. #include <openssl/ssl.h>
  4. #ifndef WIN32
  5. # include <string.h>
  6. #endif
  7. #include <iostream>
  8. #include <Key.h>
  9. #include <Text.h>
  10. using namespace Network;
  11. // Inhalt der Server Klasse aus Server.h
  12. // Konstruktor
  13. Server::Server()
  14. : ReferenceCounter()
  15. {
  16. sock = 0;
  17. memset(&addresse, 0, sizeof(addresse)); // Adresse setzen
  18. addresse.sin_family = AF_INET;
  19. addresse.sin_addr.s_addr = ADDR_ANY;
  20. klients = 0;
  21. }
  22. // Destruktor
  23. Server::~Server()
  24. {
  25. trenne();
  26. }
  27. // nicht constant
  28. bool Server::verbinde(
  29. unsigned short port, int warteschlangenLen) // Öffnet das Socket
  30. {
  31. sock = socket(AF_INET, SOCK_STREAM, 0); // Socket erstellen
  32. addresse.sin_port = htons(port); // port setzen
  33. if (sock < 0)
  34. {
  35. sock = 0;
  36. return 0;
  37. }
  38. #ifdef WIN32
  39. char reuseSocket = 1;
  40. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseSocket, sizeof(char));
  41. #else
  42. int reuseSocket = 1;
  43. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseSocket, sizeof(int));
  44. #endif
  45. if (bind(sock, (struct sockaddr*)&addresse, sizeof(addresse))
  46. == -1) // socket öffnen
  47. {
  48. trenne();
  49. return 0; // Fehler
  50. }
  51. if (listen(sock, warteschlangenLen) == -1) // Klients annehmen
  52. {
  53. trenne();
  54. return 0; // Fehler
  55. }
  56. return 1;
  57. }
  58. SKlient* Server::getKlient() // nimmt Klient an
  59. {
  60. if (!sock) return 0;
  61. sockaddr_in client;
  62. int len = sizeof(addresse);
  63. fd_set set;
  64. int rv = 0;
  65. struct timeval timeout;
  66. while (rv == 0 && sock)
  67. {
  68. FD_ZERO(&set); /* clear the set */
  69. FD_SET(sock, &set); /* add our file descriptor to the set */
  70. timeout.tv_sec = 10;
  71. timeout.tv_usec = 0;
  72. rv = select((int)sock + 1, &set, NULL, NULL, &timeout);
  73. if (rv == -1) return 0;
  74. }
  75. if (!sock) return 0;
  76. #ifdef WIN32
  77. SOCKET cls = accept(sock, (sockaddr*)&client, &len); // Klient empfangen
  78. if (cls == INVALID_SOCKET)
  79. {
  80. trenne();
  81. return 0;
  82. }
  83. #else
  84. SOCKET cls = accept(
  85. sock, (sockaddr*)&client, (socklen_t*)&len); // Klient empfangen
  86. if (!cls)
  87. {
  88. if (errno == ECONNABORTED || errno == EBADF) trenne();
  89. return 0;
  90. }
  91. #endif
  92. client.sin_port = addresse.sin_port;
  93. klients++;
  94. return new SKlient(client, cls); // Klient Handle Klasse zurückgeben
  95. }
  96. int Server::getKlients(bool reset) // gibt die Anzahl der Klients zurück
  97. {
  98. int ret = klients;
  99. if (reset) klients = 0;
  100. return ret;
  101. }
  102. bool Server::trenne() // beendet den Server
  103. {
  104. if (!sock) return 1;
  105. if (closesocket(sock) < 0) // socket schließen
  106. return 0;
  107. sock = 0;
  108. return 1;
  109. }
  110. // constant
  111. unsigned short Server::getPort() const // gibt den Port zurück
  112. {
  113. return htons(addresse.sin_port);
  114. }
  115. bool Server::isConnected()
  116. const // giebt 1 zurück, falls der Server verbunden ist
  117. {
  118. return sock != 0;
  119. }
  120. // Inhalt der SKlient Klasse aus Server.h
  121. // Konstruktor
  122. SKlient::SKlient(sockaddr_in addresse, SOCKET sock)
  123. : ReferenceCounter()
  124. {
  125. clientAddr = addresse;
  126. this->sock = sock;
  127. downStreamBytes = 0;
  128. upStreamBytes = 0;
  129. sendeKey = 0;
  130. empfangKey = 0;
  131. }
  132. // Destruktor
  133. SKlient::~SKlient()
  134. {
  135. trenne();
  136. if (sendeKey) sendeKey->release();
  137. if (empfangKey) empfangKey->release();
  138. }
  139. // nicht constant
  140. void SKlient::setEmpfangTimeout(
  141. int miliseconds) // Setzt ein timeout fürs empfangen von daten
  142. {
  143. #ifdef WIN32
  144. DWORD timeout = miliseconds;
  145. setsockopt(
  146. sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof timeout);
  147. #else
  148. struct timeval tv;
  149. tv.tv_sec = miliseconds / 1000;
  150. tv.tv_usec = (miliseconds % 1000) * 1000;
  151. setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
  152. #endif
  153. }
  154. void SKlient::setSendeKeyZ(Encryption::Key* key) // Setzt den Key fürs Senden
  155. {
  156. if (sendeKey) sendeKey->release();
  157. sendeKey = key;
  158. }
  159. void SKlient::setEmpfangKeyZ(
  160. Encryption::Key* key) // Setzt den Key fürs Empfangen
  161. {
  162. if (empfangKey) empfangKey->release();
  163. empfangKey = key;
  164. }
  165. void SKlient::setSendeKey(const char* key, int len) // Setzt den Key fürs Senden
  166. {
  167. if (!sendeKey) sendeKey = new Encryption::Key();
  168. sendeKey->setKey(key, len);
  169. }
  170. void SKlient::setEmpfangKey(
  171. const char* key, int len) // Setzt den Key fürs Empfangen
  172. {
  173. if (!empfangKey) empfangKey = new Encryption::Key();
  174. empfangKey->setKey(key, len);
  175. }
  176. bool SKlient::sende(const char* nachricht, int len) // sendet zum Klient
  177. {
  178. if (!sock) return 0;
  179. int ll = 0;
  180. while (len > 0)
  181. {
  182. #ifdef WIN32
  183. int l = send(sock, nachricht + ll, len, 0);
  184. #else
  185. int l = (int)send(sock, nachricht + ll, len, MSG_NOSIGNAL);
  186. #endif
  187. if (l <= 0)
  188. {
  189. #ifdef WIN32
  190. # ifdef _DEBUG
  191. std::cout << "send: " << l << " Error: " << WSAGetLastError()
  192. << std::endl;
  193. # endif
  194. #endif
  195. return 0; // Fehler
  196. }
  197. len -= l;
  198. ll += l;
  199. }
  200. upStreamBytes += ll;
  201. return 1;
  202. }
  203. bool SKlient::getNachricht(
  204. char* nachricht, int len) // empfängt Nachricht von Klient
  205. {
  206. if (!sock) return 0;
  207. int ll = 0;
  208. while (len > 0)
  209. {
  210. int l = (int)recv(sock, nachricht + ll, len, MSG_WAITALL);
  211. if (l <= 0)
  212. {
  213. #ifdef WIN32
  214. # ifdef _DEBUG
  215. std::cout << "recv: " << l << " Error: " << WSAGetLastError()
  216. << std::endl;
  217. # endif
  218. #endif
  219. return 0; // Fehler
  220. }
  221. len -= l;
  222. ll += l;
  223. }
  224. downStreamBytes += ll;
  225. return 1;
  226. }
  227. bool SKlient::sendeEncrypted(
  228. const char* nachricht, int len) // sendet zum Server
  229. {
  230. if (!sendeKey) return sende(nachricht, len);
  231. Encryption::Bytes* n = new Encryption::Bytes(nachricht, len);
  232. sendeKey->codieren(dynamic_cast<Encryption::Bytes*>(n->getThis()));
  233. int ll = 0;
  234. while (len > 0)
  235. {
  236. #ifdef WIN32
  237. int l = send(sock, n->getBytes() + ll, len, 0);
  238. #else
  239. int l = (int)send(sock, n->getBytes() + ll, len, MSG_NOSIGNAL);
  240. #endif
  241. if (l <= 0)
  242. {
  243. #ifdef WIN32
  244. # ifdef _DEBUG
  245. std::cout << "send: " << l << " Error: " << WSAGetLastError()
  246. << std::endl;
  247. # endif
  248. #endif
  249. n->release();
  250. return 0; // Fehler
  251. }
  252. len -= l;
  253. ll += l;
  254. }
  255. upStreamBytes += ll;
  256. n->release();
  257. return 1;
  258. }
  259. bool SKlient::getNachrichtEncrypted(
  260. char* nachricht, int len) // empfängt Nachricht
  261. {
  262. if (!empfangKey) return getNachricht(nachricht, len);
  263. int ll = 0;
  264. while (len > 0)
  265. {
  266. int l = (int)recv(sock, nachricht + ll, len, MSG_WAITALL);
  267. if (l <= 0)
  268. {
  269. #ifdef WIN32
  270. # ifdef _DEBUG
  271. std::cout << "recv: " << l << " Error: " << WSAGetLastError()
  272. << std::endl;
  273. # endif
  274. #endif
  275. return 0; // Fehler
  276. }
  277. len -= l;
  278. ll += l;
  279. }
  280. Encryption::Bytes* n = new Encryption::Bytes();
  281. n->setBytesZ(nachricht, ll);
  282. empfangKey->decodieren(n);
  283. downStreamBytes += ll;
  284. return 1;
  285. }
  286. int SKlient::getDownloadBytes(
  287. bool reset) // gibt die anzahl von empfangen bytes zurück
  288. {
  289. int ret = downStreamBytes;
  290. if (reset) downStreamBytes = 0;
  291. return ret;
  292. }
  293. int SKlient::getUploadBytes(
  294. bool reset) // gibt die anzahl von versendeter bytes zurück
  295. {
  296. int ret = upStreamBytes;
  297. if (reset) upStreamBytes = 0;
  298. return ret;
  299. }
  300. bool SKlient::trenne() // trennt die Verbindung zum Klient
  301. {
  302. if (!sock) return 0;
  303. if (closesocket(sock) < 0) // trennen
  304. return 0;
  305. sock = 0;
  306. return 1;
  307. }
  308. // constant
  309. bool SKlient::hatNachricht(
  310. int zeit) const // Wartet eine Zeit Lang auf eine Nachricht
  311. {
  312. fd_set set;
  313. FD_ZERO(&set);
  314. FD_SET(sock, &set);
  315. timeval time = {zeit / 1000, zeit};
  316. int result = select(0, &set, 0, 0, &time);
  317. if (result < 0)
  318. {
  319. #ifdef WIN32
  320. # ifdef _DEBUG
  321. std::cout << "select: " << result << " Error: " << WSAGetLastError()
  322. << std::endl;
  323. # endif
  324. #endif
  325. }
  326. return result > 0;
  327. }
  328. unsigned short SKlient::getPort() const // gibt den Port zurück
  329. {
  330. return htons(clientAddr.sin_port);
  331. }
  332. const char* SKlient::getIp() const // gibt die Ip des Klients zurück
  333. {
  334. return inet_ntoa(clientAddr.sin_addr);
  335. }
  336. int pem_passwd_cb(char* buf, int size, int rwflag, void* userdata)
  337. {
  338. const char* passw = ((Text*)userdata)->getText();
  339. memcpy(buf, passw, MIN((unsigned int)size, strlen(passw) + 1));
  340. return (int)strlen(buf);
  341. }
  342. bool SSLErrorCheck(int result, SSL* ssl, const char* action)
  343. {
  344. if (result <= 0)
  345. {
  346. std::cout << "ERROR: '" << action
  347. << "' returned error code: " << SSL_get_error(ssl, result)
  348. << "\n";
  349. std::cout.flush();
  350. return 0;
  351. }
  352. return 1;
  353. }
  354. bool SSLErrorCheck(__int64 result, const char* action)
  355. {
  356. if (result <= 0)
  357. {
  358. unsigned long error = ERR_get_error();
  359. std::cout << "ERROR: '" << action << "' returned " << result
  360. << " error code: " << error << "("
  361. << ERR_reason_error_string(error) << ")\n";
  362. std::cout.flush();
  363. return 0;
  364. }
  365. return 1;
  366. }
  367. bool SKlient::waitForNextMessage()
  368. const // wartet bis es etwas zu empfangen gibt
  369. {
  370. fd_set set;
  371. int rv = 0;
  372. struct timeval timeout;
  373. while (rv == 0 && sock)
  374. {
  375. FD_ZERO(&set); /* clear the set */
  376. FD_SET(sock, &set); /* add our file descriptor to the set */
  377. timeout.tv_sec = 10;
  378. timeout.tv_usec = 0;
  379. rv = select((int)sock + 1, &set, NULL, NULL, &timeout);
  380. if (rv == -1)
  381. {
  382. #ifdef WIN32
  383. # ifdef _DEBUG
  384. std::cout << "select: " << rv << " Error: " << WSAGetLastError()
  385. << std::endl;
  386. # endif
  387. #endif
  388. return 0;
  389. }
  390. }
  391. if (!sock) return 0;
  392. char c;
  393. #ifdef WIN32
  394. int l = (int)recv(sock, &c, 1, MSG_PEEK);
  395. #else
  396. int l = (int)recv(sock, &c, 1, MSG_WAITALL | MSG_PEEK);
  397. #endif
  398. if (l <= 0)
  399. {
  400. #ifdef WIN32
  401. # ifdef _DEBUG
  402. std::cout << "recv: " << l << " Error: " << WSAGetLastError()
  403. << std::endl;
  404. # endif
  405. #endif
  406. return 0; // Fehler
  407. }
  408. return 1;
  409. }
  410. // Inhalt der SSLServer Klasse
  411. // Konstruktor
  412. SSLServer::SSLServer()
  413. : ReferenceCounter()
  414. {
  415. s = 0;
  416. const SSL_METHOD* method = TLS_server_method();
  417. ctx = SSL_CTX_new(method);
  418. SSLErrorCheck(SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  419. "SSL_CTX_set_min_proto_version");
  420. SSLErrorCheck(SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION),
  421. "SSL_CTX_set_max_proto_version");
  422. SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
  423. SSL_CTX_set_default_passwd_cb(ctx, pem_passwd_cb);
  424. passw = new Text();
  425. SSL_CTX_set_default_passwd_cb_userdata(ctx, passw);
  426. addr.sin_family = AF_INET;
  427. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  428. klients = 0;
  429. }
  430. // Destruktor
  431. SSLServer::~SSLServer()
  432. {
  433. trenne();
  434. SSL_CTX_free(ctx);
  435. passw->release();
  436. #ifdef WIN32
  437. OPENSSL_thread_stop();
  438. #endif
  439. }
  440. // nicht constant
  441. // Setzt den Pfad zur Datei, in dem das Certifikat gespeichert ist
  442. bool SSLServer::setCertificateFile(const char* file)
  443. {
  444. return SSLErrorCheck(
  445. SSL_CTX_use_certificate_file(ctx, file, SSL_FILETYPE_PEM),
  446. "SSL_CTX_use_certificate_file");
  447. }
  448. // Setzt den Pfad zur Datei, in dem der private Schlüssel gespeichert ist
  449. bool SSLServer::setPrivateKeyFile(const char* file)
  450. {
  451. return SSLErrorCheck(
  452. SSL_CTX_use_PrivateKey_file(ctx, file, SSL_FILETYPE_PEM),
  453. "SSL_CTX_use_PrivateKey_file");
  454. }
  455. // setzt das passwort des private keys (muss vor setPrivateKeyFile aufgerufen
  456. // werden)
  457. void SSLServer::setPrivateKeyPassword(const char* password)
  458. {
  459. passw->setText(password);
  460. }
  461. // Öffnet das Socket
  462. bool SSLServer::verbinde(unsigned short port, int warteschlangenLen)
  463. {
  464. addr.sin_port = htons(port);
  465. s = socket(AF_INET, SOCK_STREAM, 0);
  466. if (s < 0)
  467. {
  468. s = 0;
  469. return 0;
  470. }
  471. #ifdef WIN32
  472. char reuseSocket = 1;
  473. setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &reuseSocket, sizeof(char));
  474. #else
  475. int reuseSocket = 1;
  476. setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &reuseSocket, sizeof(int));
  477. #endif
  478. if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0)
  479. {
  480. trenne();
  481. return 0;
  482. }
  483. if (listen(s, warteschlangenLen) < 0)
  484. {
  485. trenne();
  486. return 0;
  487. }
  488. return 1;
  489. }
  490. // nimmt Klient an
  491. SSLSKlient* SSLServer::getKlient()
  492. {
  493. if (!s) return 0;
  494. int len = sizeof(addr);
  495. struct sockaddr_in addr;
  496. fd_set set;
  497. int rv = 0;
  498. struct timeval timeout;
  499. while (rv == 0 && s)
  500. {
  501. FD_ZERO(&set); /* clear the set */
  502. FD_SET(s, &set); /* add our file descriptor to the set */
  503. timeout.tv_sec = 10;
  504. timeout.tv_usec = 0;
  505. rv = select((int)s + 1, &set, NULL, NULL, &timeout);
  506. if (rv == -1) return 0;
  507. }
  508. if (!s) return 0;
  509. #ifdef WIN32
  510. SOCKET client = accept(s, (struct sockaddr*)&addr, &len);
  511. if (client == INVALID_SOCKET)
  512. {
  513. trenne();
  514. return 0;
  515. }
  516. #else
  517. SOCKET client
  518. = accept(s, (sockaddr*)&addr, (socklen_t*)&len); // Klient empfangen
  519. if (!client)
  520. {
  521. if (errno == ECONNABORTED || errno == EBADF) trenne();
  522. return 0;
  523. }
  524. #endif
  525. addr.sin_port = this->addr.sin_port;
  526. SSL* ssl = SSL_new(ctx);
  527. if (ssl == 0 && !SSLErrorCheck(0, "SSL_new"))
  528. {
  529. closesocket(client);
  530. return 0;
  531. }
  532. if (!SSLErrorCheck(SSL_set_fd(ssl, (int)client), ssl, "SSL_set_fd"))
  533. {
  534. SSL_free(ssl);
  535. closesocket(client);
  536. return 0;
  537. }
  538. if (!SSLErrorCheck(SSL_accept(ssl), ssl, "SSL_accept"))
  539. {
  540. SSL_free(ssl);
  541. closesocket(client);
  542. return 0;
  543. }
  544. klients++;
  545. return new SSLSKlient(addr, ssl, client);
  546. }
  547. // gibt die Anzahl der Klients zurück
  548. int SSLServer::getKlients(bool reset)
  549. {
  550. int ret = klients;
  551. if (reset) klients = 0;
  552. return ret;
  553. }
  554. // beendet den Server
  555. bool SSLServer::trenne()
  556. {
  557. if (!s) return 1;
  558. if (closesocket(s) < 0) // socket schließen
  559. return 0;
  560. s = 0;
  561. return 1;
  562. }
  563. // constant
  564. // gibt den Port zurück
  565. unsigned short SSLServer::getPort() const
  566. {
  567. return htons(addr.sin_port);
  568. }
  569. // giebt 1 zurück, falls der Server verbunden ist
  570. bool SSLServer::isConnected() const
  571. {
  572. return s != 0;
  573. }
  574. // Inhalt der SSLSKlient Klasse
  575. // Konstruktor
  576. SSLSKlient::SSLSKlient(sockaddr_in client, SSL* ssl, SOCKET s)
  577. : ReferenceCounter()
  578. {
  579. this->s = s;
  580. clientAddr = client;
  581. this->ssl = ssl;
  582. downStreamBytes = 0;
  583. upStreamBytes = 0;
  584. }
  585. // Destruktor
  586. SSLSKlient::~SSLSKlient()
  587. {
  588. trenne();
  589. #ifdef WIN32
  590. OPENSSL_thread_stop();
  591. #endif
  592. }
  593. // nicht constant
  594. void SSLSKlient::setEmpfangTimeout(
  595. int miliseconds) // Setzt ein timeout fürs empfangen von daten
  596. {
  597. #ifdef WIN32
  598. DWORD timeout = miliseconds;
  599. setsockopt(
  600. s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof timeout);
  601. #else
  602. struct timeval tv;
  603. tv.tv_sec = miliseconds / 1000;
  604. tv.tv_usec = (miliseconds % 1000) * 1000;
  605. setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
  606. #endif
  607. }
  608. bool SSLSKlient::sende(const char* nachricht, int len) // sendet zum Klient
  609. {
  610. if (!ssl) return 0;
  611. int ll = 0;
  612. while (len > 0)
  613. {
  614. int l = SSL_write(ssl, nachricht + ll, len);
  615. if (l <= 0)
  616. {
  617. #ifdef _DEBUG
  618. std::cout << "SSL_write: " << l
  619. << " Error: " << SSL_get_error(ssl, l) << std::endl;
  620. #endif
  621. return 0; // Fehler
  622. }
  623. len -= l;
  624. ll += l;
  625. }
  626. upStreamBytes += ll;
  627. return 1;
  628. }
  629. bool SSLSKlient::getNachricht(
  630. char* nachricht, int len) // empfängt Nachricht von Klient
  631. {
  632. if (!ssl) return 0;
  633. int ll = 0;
  634. while (len > 0)
  635. {
  636. int l = (int)SSL_read(ssl, nachricht + ll, len);
  637. if (l <= 0)
  638. {
  639. #ifdef _DEBUG
  640. SSL_ERROR_WANT_ASYNC
  641. std::cout << "SSL_read: " << l
  642. << " Error: " << SSL_get_error(ssl, l) << std::endl;
  643. #endif
  644. return 0; // Fehler
  645. }
  646. len -= l;
  647. ll += l;
  648. }
  649. downStreamBytes += ll;
  650. return 1;
  651. }
  652. int SSLSKlient::getDownloadBytes(
  653. bool reset) // gibt die anzahl von empfangen bytes zurück
  654. {
  655. int ret = downStreamBytes;
  656. if (reset) downStreamBytes = 0;
  657. return ret;
  658. }
  659. int SSLSKlient::getUploadBytes(
  660. bool reset) // gibt die anzahl von versendeter bytes zurück
  661. {
  662. int ret = upStreamBytes;
  663. if (reset) upStreamBytes = 0;
  664. return ret;
  665. }
  666. bool SSLSKlient::trenne() // trennt die Verbindung zum Klient
  667. {
  668. if (!ssl) return 0;
  669. SSL_free(ssl);
  670. if (closesocket(s) < 0) // trennen
  671. return 0;
  672. ssl = 0;
  673. s = 0;
  674. return 1;
  675. }
  676. // constant
  677. bool SSLSKlient::hatNachricht(
  678. int zeit) const // Wartet eine Zeit Lang auf eine Nachricht
  679. {
  680. fd_set set;
  681. FD_ZERO(&set);
  682. FD_SET(SSL_get_rfd(ssl), &set);
  683. timeval time = {zeit / 1000, zeit};
  684. return SSL_pending(ssl) > 0 || select(0, &set, 0, 0, &time) == 1;
  685. }
  686. unsigned short SSLSKlient::getPort() const // gibt den Port zurück
  687. {
  688. return htons(clientAddr.sin_port);
  689. }
  690. const char* SSLSKlient::getIp() const // gibt die Ip des Klients zurück
  691. {
  692. return inet_ntoa(clientAddr.sin_addr);
  693. }