Klient.cpp 10 KB

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