Klient.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #define INCLUDE_SSL
  2. #include "Klient.h"
  3. #ifndef WIN32
  4. # include <netdb.h>
  5. # include <string.h>
  6. # include <sys/select.h>
  7. #endif
  8. #include <Datei.h>
  9. #include <Key.h>
  10. #include <Logging.h>
  11. using namespace Network;
  12. // inhalt der Klient Klasse aus Klient.h
  13. // Konstruktor
  14. Klient::Klient()
  15. : ReferenceCounter(),
  16. errorOccured(0)
  17. {
  18. memset(&server, 0, sizeof(server));
  19. server.sin_family = AF_INET;
  20. downStreamBytes = 0;
  21. upStreamBytes = 0;
  22. sock = 0;
  23. sendeKey = 0;
  24. empfangKey = 0;
  25. }
  26. // Destruktoe
  27. Klient::~Klient()
  28. {
  29. if (sock) trenne();
  30. if (sendeKey) sendeKey->release();
  31. if (empfangKey) empfangKey->release();
  32. }
  33. // nicht constant
  34. void Klient::setSendeKeyZ(Encryption::Key* key) // Setzt den Key fürs Senden
  35. {
  36. if (sendeKey) sendeKey->release();
  37. sendeKey = key;
  38. }
  39. void Klient::setEmpfangKeyZ(
  40. Encryption::Key* key) // Setzt den Key fürs Empfangen
  41. {
  42. if (empfangKey) empfangKey->release();
  43. empfangKey = key;
  44. }
  45. void Klient::setSendeKey(const char* key, int len) // Setzt den Key fürs Senden
  46. {
  47. if (!sendeKey) sendeKey = new Encryption::Key();
  48. sendeKey->setKey(key, len);
  49. }
  50. void Klient::setEmpfangKey(
  51. const char* key, int len) // Setzt den Key fürs Empfangen
  52. {
  53. if (!empfangKey) empfangKey = new Encryption::Key();
  54. empfangKey->setKey(key, len);
  55. }
  56. bool Klient::verbinde(
  57. unsigned short port, const char* ip) // verbindet mit Server
  58. {
  59. if (sendeKey) sendeKey->setPos(0);
  60. if (empfangKey) empfangKey->setPos(0);
  61. if (sock) closesocket(sock);
  62. sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  63. long sIp = inet_addr(ip); // ip addresse
  64. if (sIp == INADDR_NONE)
  65. {
  66. struct hostent* pHostInfo = gethostbyname(ip);
  67. if (pHostInfo == 0) return 0;
  68. sIp = *(long*)pHostInfo->h_addr_list[0];
  69. }
  70. memcpy((char*)&server.sin_addr, &sIp, sizeof(sIp));
  71. server.sin_port = htons(port); // port
  72. if (connect(sock, (struct sockaddr*)&server, sizeof(server))
  73. < 0) // verbinden
  74. {
  75. errorOccured = 1;
  76. return 0; // Fehler
  77. }
  78. errorOccured = 0;
  79. return 1;
  80. }
  81. bool Klient::sende(const char* nachricht, int len) // sendet zum Server
  82. {
  83. int ll = 0;
  84. while (len > 0)
  85. {
  86. #ifdef WIN32
  87. int l = send(sock, nachricht + ll, len, 0);
  88. #else
  89. int l = (int)send(sock, nachricht + ll, len, MSG_NOSIGNAL);
  90. #endif
  91. if (l <= 0)
  92. {
  93. #ifdef WIN32
  94. # ifdef _DEBUG
  95. Framework::Logging::warning()
  96. << "send: " << l << " Error: " << WSAGetLastError();
  97. # endif
  98. #endif
  99. errorOccured = 1;
  100. return 0; // Fehler
  101. }
  102. len -= l;
  103. ll += l;
  104. }
  105. upStreamBytes += ll;
  106. return 1;
  107. }
  108. bool Klient::getNachricht(char* nachricht, int len) // empfängt Nachricht
  109. {
  110. int ll = 0;
  111. while (len > 0)
  112. {
  113. int l = (int)recv(sock, nachricht + ll, len, MSG_WAITALL);
  114. if (l <= 0)
  115. {
  116. #ifdef WIN32
  117. # ifdef _DEBUG
  118. Framework::Logging::warning()
  119. << "recv: " << l << " Error: " << WSAGetLastError();
  120. # endif
  121. #endif
  122. errorOccured = 1;
  123. return 0; // Fehler
  124. }
  125. len -= l;
  126. ll += l;
  127. }
  128. downStreamBytes += ll;
  129. return 1;
  130. }
  131. bool Klient::sendeEncrypted(const char* nachricht, int len) // sendet zum Server
  132. {
  133. if (!sendeKey) return sende(nachricht, len);
  134. Encryption::Bytes* n = new Encryption::Bytes(nachricht, len);
  135. sendeKey->codieren(
  136. dynamic_cast<Framework::Encryption::Bytes*>(n->getThis()));
  137. int ll = 0;
  138. while (len > 0)
  139. {
  140. #ifdef WIN32
  141. int l = send(sock, n->getBytes() + ll, len, 0);
  142. #else
  143. int l = (int)send(sock, n->getBytes() + ll, len, MSG_NOSIGNAL);
  144. #endif
  145. if (l <= 0)
  146. {
  147. #ifdef WIN32
  148. # ifdef _DEBUG
  149. Framework::Logging::warning()
  150. << "send: " << l << " Error: " << WSAGetLastError();
  151. # endif
  152. #endif
  153. n->release();
  154. errorOccured = 1;
  155. return 0; // Fehler
  156. }
  157. len -= l;
  158. ll += l;
  159. }
  160. upStreamBytes += ll;
  161. n->release();
  162. return 1;
  163. }
  164. bool Klient::getNachrichtEncrypted(
  165. char* nachricht, int len) // empfängt Nachricht
  166. {
  167. if (!empfangKey) return getNachricht(nachricht, len);
  168. int ll = 0;
  169. while (len > 0)
  170. {
  171. int l = (int)recv(sock, nachricht + ll, len, MSG_WAITALL);
  172. if (l <= 0)
  173. {
  174. #ifdef WIN32
  175. # ifdef _DEBUG
  176. Framework::Logging::warning()
  177. << "recv: " << l << " Error: " << WSAGetLastError();
  178. # endif
  179. #endif
  180. errorOccured = 1;
  181. return 0; // Fehler
  182. }
  183. len -= l;
  184. ll += l;
  185. }
  186. Encryption::Bytes* n = new Encryption::Bytes();
  187. n->setBytesZ(nachricht, ll);
  188. empfangKey->decodieren(n);
  189. downStreamBytes += ll;
  190. return 1;
  191. }
  192. int Klient::getDownloadBytes(
  193. bool reset) // gibt die anzahl von empfangen bytes zurück
  194. {
  195. int ret = downStreamBytes;
  196. if (reset) downStreamBytes = 0;
  197. return ret;
  198. }
  199. int Klient::getUploadBytes(
  200. bool reset) // gibt die anzahl von versendeter bytes zurück
  201. {
  202. int ret = upStreamBytes;
  203. if (reset) upStreamBytes = 0;
  204. return ret;
  205. }
  206. bool Klient::trenne() // Trennt die Verbindung zum Server
  207. {
  208. if (!sock) return 1;
  209. if (sendeKey) sendeKey->setPos(0);
  210. if (empfangKey) empfangKey->setPos(0);
  211. if (closesocket(sock) < 0) // verbindung Trennen
  212. {
  213. errorOccured = 1;
  214. return 0; // Fehler
  215. }
  216. sock = 0;
  217. return 1;
  218. }
  219. // constant
  220. bool Klient::hatNachricht(int zeit) // Wartet eine Zeit Lang auf eine Nachricht
  221. {
  222. fd_set set;
  223. FD_ZERO(&set);
  224. FD_SET(sock, &set);
  225. timeval time = {zeit / 1000, zeit};
  226. int result = select(0, &set, 0, 0, &time);
  227. if (result < 0)
  228. {
  229. #ifdef WIN32
  230. # ifdef _DEBUG
  231. Framework::Logging::warning()
  232. << "select: " << result << " Error: " << WSAGetLastError();
  233. # endif
  234. #endif
  235. }
  236. return result > 0;
  237. }
  238. unsigned short Klient::getServerPort() const // gibt den Port zurück
  239. {
  240. return htons(server.sin_port);
  241. }
  242. const char* Klient::getServerIp() const // gibt die Ip zurück
  243. {
  244. return inet_ntoa(server.sin_addr);
  245. }
  246. bool Klient::waitForNextMessage() const // wartet bis es etwas zu empfangen gibt
  247. {
  248. fd_set set;
  249. int rv = 0;
  250. struct timeval timeout;
  251. while (rv == 0 && sock)
  252. {
  253. FD_ZERO(&set); /* clear the set */
  254. FD_SET(sock, &set); /* add our file descriptor to the set */
  255. timeout.tv_sec = 10;
  256. timeout.tv_usec = 0;
  257. rv = select((int)sock + 1, &set, NULL, NULL, &timeout);
  258. if (rv == -1)
  259. {
  260. #ifdef WIN32
  261. # ifdef _DEBUG
  262. Framework::Logging::warning()
  263. << "select: " << rv << " Error: " << WSAGetLastError();
  264. # endif
  265. #endif
  266. return 0;
  267. }
  268. }
  269. if (!sock) return 0;
  270. char c;
  271. #ifdef WIN32
  272. int l = (int)recv(sock, &c, 1, MSG_PEEK);
  273. #else
  274. int l = (int)recv(sock, &c, 1, MSG_WAITALL | MSG_PEEK);
  275. #endif
  276. if (l <= 0)
  277. {
  278. #ifdef WIN32
  279. # ifdef _DEBUG
  280. Framework::Logging::warning()
  281. << "recv: " << l << " Error: " << WSAGetLastError();
  282. # endif
  283. #endif
  284. return 0; // Fehler
  285. }
  286. return 1;
  287. }
  288. bool Klient::isConnected()
  289. const // gibt true zurück, wenn eine Verbindung besteht
  290. {
  291. return sock != 0 && !errorOccured;
  292. }
  293. // Inhalt der SSLKlient Klasse aus Klient.h
  294. // Konstruktor
  295. SSLKlient::SSLKlient()
  296. : ReferenceCounter()
  297. {
  298. ctx = SSL_CTX_new(TLS_client_method());
  299. SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
  300. SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
  301. ip = 0;
  302. port = 0;
  303. bio = BIO_new_ssl_connect(ctx);
  304. BIO_get_ssl(bio, &ssl);
  305. downStreamBytes = 0;
  306. upStreamBytes = 0;
  307. connected = 0;
  308. }
  309. // Destruktor
  310. SSLKlient::~SSLKlient()
  311. {
  312. if (this->ip) this->ip->release();
  313. if (connected) trenne();
  314. BIO_free_all(bio);
  315. SSL_CTX_free(ctx);
  316. #ifdef WIN32
  317. OPENSSL_thread_stop();
  318. #endif
  319. }
  320. bool SSLKlient::verbinde(
  321. unsigned short port, const char* ip) // verbindet mit Server
  322. {
  323. this->port = port;
  324. if (this->ip) this->ip->release();
  325. this->ip = new Text(ip);
  326. Text adr = ip;
  327. adr += ":";
  328. adr += port;
  329. BIO_set_conn_hostname(bio, (const char*)adr);
  330. connected = BIO_do_connect(bio) > 0;
  331. if (connected && BIO_do_handshake(bio) <= 0) trenne();
  332. return connected;
  333. }
  334. bool SSLKlient::sende(const char* nachricht, int len) // sendet zum Server
  335. {
  336. int ll = 0;
  337. while (len > 0)
  338. {
  339. int l = SSL_write(ssl, nachricht + ll, len);
  340. if (l <= 0)
  341. {
  342. #ifdef _DEBUG
  343. Framework::Logging::warning()
  344. << "SSL_write: " << l << " Error: " << SSL_get_error(ssl, l);
  345. #endif
  346. return 0; // Fehler
  347. }
  348. len -= l;
  349. ll += l;
  350. }
  351. upStreamBytes += ll;
  352. return 1;
  353. }
  354. bool SSLKlient::getNachricht(char* nachricht, int len) // empfängt Nachricht
  355. {
  356. if (!connected) return 0;
  357. int ll = 0;
  358. while (len > 0)
  359. {
  360. int l = SSL_read(ssl, nachricht + ll, len);
  361. if (l <= 0)
  362. {
  363. #ifdef _DEBUG
  364. Framework::Logging::warning()
  365. << "SSL_read: " << l << " Error: " << SSL_get_error(ssl, l);
  366. #endif
  367. return 0; // Fehler
  368. }
  369. len -= l;
  370. ll += l;
  371. }
  372. downStreamBytes += ll;
  373. return 1;
  374. }
  375. int SSLKlient::getDownloadBytes(
  376. bool reset) // gibt die anzahl von empfangen bytes zurück
  377. {
  378. int ret = downStreamBytes;
  379. if (reset) downStreamBytes = 0;
  380. return ret;
  381. }
  382. int SSLKlient::getUploadBytes(
  383. bool reset) // gibt die anzahl von versendeter bytes zurück
  384. {
  385. int ret = upStreamBytes;
  386. if (reset) upStreamBytes = 0;
  387. return ret;
  388. }
  389. bool SSLKlient::trenne() // Trennt die Verbindung zum Server
  390. {
  391. BIO_ssl_shutdown(bio);
  392. connected = 0;
  393. return 1;
  394. }
  395. // constant
  396. bool SSLKlient::hatNachricht(
  397. int zeit) // Wartet eine Zeit Lang auf eine Nachricht
  398. {
  399. fd_set set;
  400. FD_ZERO(&set);
  401. FD_SET(SSL_get_rfd(ssl), &set);
  402. timeval time = {zeit / 1000, zeit};
  403. return SSL_pending(ssl) > 0 || select(0, &set, 0, 0, &time) == 1;
  404. }
  405. unsigned short
  406. SSLKlient::getServerPort() const // gibt den Port des Servers zurück
  407. {
  408. return port;
  409. }
  410. const char* SSLKlient::getServerIp() const // gibt die Ip des Servers zurück
  411. {
  412. return ip->getText();
  413. }