Client.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #define INCLUDE_SSL
  2. #include "Client.h"
  3. #ifndef WIN32
  4. # include <netdb.h>
  5. # include <string.h>
  6. # include <sys/select.h>
  7. #endif
  8. #include <File.h>
  9. #include <Key.h>
  10. #include <Logging.h>
  11. using namespace Network;
  12. // Content of the Client class from Client.h
  13. // Constructor
  14. Client::Client()
  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. sendKey = 0;
  24. receiveKey = 0;
  25. }
  26. // Destructor
  27. Client::~Client()
  28. {
  29. if (sock) disconnect();
  30. if (sendKey) sendKey->release();
  31. if (receiveKey) receiveKey->release();
  32. }
  33. // non-constant
  34. void Client::setSendKeyZ(Encryption::Key* key) // Sets the key for sending
  35. {
  36. if (sendKey) sendKey->release();
  37. sendKey = key;
  38. }
  39. void Client::setReceiveKeyZ(Encryption::Key* key) // Sets the key for receiving
  40. {
  41. if (receiveKey) receiveKey->release();
  42. receiveKey = key;
  43. }
  44. void Client::setSendKey(const char* key, int len) // Sets the key for sending
  45. {
  46. if (!sendKey) sendKey = new Encryption::Key();
  47. sendKey->setKey(key, len);
  48. }
  49. void Client::setReceiveKey(
  50. const char* key, int len) // Sets the key for receiving
  51. {
  52. if (!receiveKey) receiveKey = new Encryption::Key();
  53. receiveKey->setKey(key, len);
  54. }
  55. bool Client::connect(unsigned short port, const char* ip) // connects to server
  56. {
  57. if (sendKey) sendKey->setPos(0);
  58. if (receiveKey) receiveKey->setPos(0);
  59. if (sock) closesocket(sock);
  60. sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  61. long sIp = inet_addr(ip); // ip address
  62. if (sIp == INADDR_NONE)
  63. {
  64. struct hostent* pHostInfo = gethostbyname(ip);
  65. if (pHostInfo == 0) return 0;
  66. sIp = *(long*)pHostInfo->h_addr_list[0];
  67. }
  68. memcpy((char*)&server.sin_addr, &sIp, sizeof(sIp));
  69. server.sin_port = htons(port); // port
  70. if (::connect(sock, (struct sockaddr*)&server, sizeof(server))
  71. < 0) // connect
  72. {
  73. errorOccured = 1;
  74. return 0; // Error
  75. }
  76. errorOccured = 0;
  77. return 1;
  78. }
  79. bool Client::send(const char* message, int len) // sends to server
  80. {
  81. int ll = 0;
  82. while (len > 0)
  83. {
  84. #ifdef WIN32
  85. int l = ::send(sock, message + ll, len, 0);
  86. #else
  87. int l = (int)::send(sock, message + ll, len, MSG_NOSIGNAL);
  88. #endif
  89. if (l <= 0)
  90. {
  91. #ifdef WIN32
  92. # ifdef _DEBUG
  93. Framework::Logging::warning()
  94. << "send: " << l << " Error: " << WSAGetLastError();
  95. # endif
  96. #endif
  97. errorOccured = 1;
  98. return 0; // Error
  99. }
  100. len -= l;
  101. ll += l;
  102. }
  103. upStreamBytes += ll;
  104. return 1;
  105. }
  106. bool Client::getMessage(char* message, int len) // receives message
  107. {
  108. int ll = 0;
  109. while (len > 0)
  110. {
  111. int l = (int)recv(sock, message + ll, len, MSG_WAITALL);
  112. if (l <= 0)
  113. {
  114. #ifdef WIN32
  115. # ifdef _DEBUG
  116. Framework::Logging::warning()
  117. << "recv: " << l << " Error: " << WSAGetLastError();
  118. # endif
  119. #endif
  120. errorOccured = 1;
  121. return 0; // Error
  122. }
  123. len -= l;
  124. ll += l;
  125. }
  126. downStreamBytes += ll;
  127. return 1;
  128. }
  129. bool Client::sendEncrypted(const char* message, int len) // sends to server
  130. {
  131. if (!sendKey) return send(message, len);
  132. Encryption::Bytes* n = new Encryption::Bytes(message, len);
  133. sendKey->encode(dynamic_cast<Framework::Encryption::Bytes*>(n->getThis()));
  134. int ll = 0;
  135. while (len > 0)
  136. {
  137. #ifdef WIN32
  138. int l = ::send(sock, n->getBytes() + ll, len, 0);
  139. #else
  140. int l = (int)::send(sock, n->getBytes() + ll, len, MSG_NOSIGNAL);
  141. #endif
  142. if (l <= 0)
  143. {
  144. #ifdef WIN32
  145. # ifdef _DEBUG
  146. Framework::Logging::warning()
  147. << "send: " << l << " Error: " << WSAGetLastError();
  148. # endif
  149. #endif
  150. n->release();
  151. errorOccured = 1;
  152. return 0; // Error
  153. }
  154. len -= l;
  155. ll += l;
  156. }
  157. upStreamBytes += ll;
  158. n->release();
  159. return 1;
  160. }
  161. bool Client::getMessageEncrypted(char* message, int len) // receives message
  162. {
  163. if (!receiveKey) return getMessage(message, len);
  164. int ll = 0;
  165. while (len > 0)
  166. {
  167. int l = (int)recv(sock, message + ll, len, MSG_WAITALL);
  168. if (l <= 0)
  169. {
  170. #ifdef WIN32
  171. # ifdef _DEBUG
  172. Framework::Logging::warning()
  173. << "recv: " << l << " Error: " << WSAGetLastError();
  174. # endif
  175. #endif
  176. errorOccured = 1;
  177. return 0; // Error
  178. }
  179. len -= l;
  180. ll += l;
  181. }
  182. Encryption::Bytes* n = new Encryption::Bytes();
  183. n->setBytesZ(message, ll);
  184. receiveKey->decode(n);
  185. downStreamBytes += ll;
  186. return 1;
  187. }
  188. int Client::getDownloadBytes(bool reset) // returns the number of received bytes
  189. {
  190. int ret = downStreamBytes;
  191. if (reset) downStreamBytes = 0;
  192. return ret;
  193. }
  194. int Client::getUploadBytes(bool reset) // returns the number of sent bytes
  195. {
  196. int ret = upStreamBytes;
  197. if (reset) upStreamBytes = 0;
  198. return ret;
  199. }
  200. bool Client::disconnect() // Disconnects from server
  201. {
  202. if (!sock) return 1;
  203. if (sendKey) sendKey->setPos(0);
  204. if (receiveKey) receiveKey->setPos(0);
  205. if (closesocket(sock) < 0) // disconnect
  206. {
  207. errorOccured = 1;
  208. return 0; // Error
  209. }
  210. sock = 0;
  211. return 1;
  212. }
  213. // constant
  214. bool Client::hasMessage(int waitTime) // Waits for a message for a given time
  215. {
  216. fd_set set;
  217. FD_ZERO(&set);
  218. FD_SET(sock, &set);
  219. timeval time = {waitTime / 1000, waitTime};
  220. int result = select(0, &set, 0, 0, &time);
  221. if (result < 0)
  222. {
  223. #ifdef WIN32
  224. # ifdef _DEBUG
  225. Framework::Logging::warning()
  226. << "select: " << result << " Error: " << WSAGetLastError();
  227. # endif
  228. #endif
  229. }
  230. return result > 0;
  231. }
  232. unsigned short Client::getServerPort() const // returns the port
  233. {
  234. return htons(server.sin_port);
  235. }
  236. const char* Client::getServerIp() const // returns the IP
  237. {
  238. return inet_ntoa(server.sin_addr);
  239. }
  240. bool Client::waitForNextMessage()
  241. const // waits until there is something to receive
  242. {
  243. fd_set set;
  244. int rv = 0;
  245. struct timeval timeout;
  246. while (rv == 0 && sock)
  247. {
  248. FD_ZERO(&set); /* clear the set */
  249. FD_SET(sock, &set); /* add our file descriptor to the set */
  250. timeout.tv_sec = 10;
  251. timeout.tv_usec = 0;
  252. rv = select((int)sock + 1, &set, NULL, NULL, &timeout);
  253. if (rv == -1)
  254. {
  255. #ifdef WIN32
  256. # ifdef _DEBUG
  257. Framework::Logging::warning()
  258. << "select: " << rv << " Error: " << WSAGetLastError();
  259. # endif
  260. #endif
  261. return 0;
  262. }
  263. }
  264. if (!sock) return 0;
  265. char c;
  266. #ifdef WIN32
  267. int l = (int)recv(sock, &c, 1, MSG_PEEK);
  268. #else
  269. int l = (int)recv(sock, &c, 1, MSG_WAITALL | MSG_PEEK);
  270. #endif
  271. if (l <= 0)
  272. {
  273. #ifdef WIN32
  274. # ifdef _DEBUG
  275. Framework::Logging::warning()
  276. << "recv: " << l << " Error: " << WSAGetLastError();
  277. # endif
  278. #endif
  279. return 0; // Error
  280. }
  281. return 1;
  282. }
  283. bool Client::isConnected() const // returns true if a connection exists
  284. {
  285. return sock != 0 && !errorOccured;
  286. }
  287. // Content of the SSLClient class from Client.h
  288. // Constructor
  289. SSLClient::SSLClient()
  290. : ReferenceCounter()
  291. {
  292. ctx = SSL_CTX_new(TLS_client_method());
  293. SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
  294. SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
  295. ip = 0;
  296. port = 0;
  297. bio = BIO_new_ssl_connect(ctx);
  298. BIO_get_ssl(bio, &ssl);
  299. downStreamBytes = 0;
  300. upStreamBytes = 0;
  301. connected = 0;
  302. }
  303. // Destructor
  304. SSLClient::~SSLClient()
  305. {
  306. if (this->ip) this->ip->release();
  307. if (connected) disconnect();
  308. BIO_free_all(bio);
  309. SSL_CTX_free(ctx);
  310. #ifdef WIN32
  311. OPENSSL_thread_stop();
  312. #endif
  313. }
  314. bool SSLClient::connect(
  315. unsigned short port, const char* ip) // connects to server
  316. {
  317. this->port = port;
  318. if (this->ip) this->ip->release();
  319. this->ip = new Text(ip);
  320. Text adr = ip;
  321. adr += ":";
  322. adr += port;
  323. BIO_set_conn_hostname(bio, (const char*)adr);
  324. connected = BIO_do_connect(bio) > 0;
  325. if (connected && BIO_do_handshake(bio) <= 0) disconnect();
  326. return connected;
  327. }
  328. bool SSLClient::send(const char* message, int len) // sends to server
  329. {
  330. int ll = 0;
  331. while (len > 0)
  332. {
  333. int l = SSL_write(ssl, message + ll, len);
  334. if (l <= 0)
  335. {
  336. #ifdef _DEBUG
  337. Framework::Logging::warning()
  338. << "SSL_write: " << l << " Error: " << SSL_get_error(ssl, l);
  339. #endif
  340. return 0; // Error
  341. }
  342. len -= l;
  343. ll += l;
  344. }
  345. upStreamBytes += ll;
  346. return 1;
  347. }
  348. bool SSLClient::getMessage(char* message, int len) // receives message
  349. {
  350. if (!connected) return 0;
  351. int ll = 0;
  352. while (len > 0)
  353. {
  354. int l = SSL_read(ssl, message + ll, len);
  355. if (l <= 0)
  356. {
  357. #ifdef _DEBUG
  358. Framework::Logging::warning()
  359. << "SSL_read: " << l << " Error: " << SSL_get_error(ssl, l);
  360. #endif
  361. return 0; // Error
  362. }
  363. len -= l;
  364. ll += l;
  365. }
  366. downStreamBytes += ll;
  367. return 1;
  368. }
  369. int SSLClient::getDownloadBytes(
  370. bool reset) // returns the number of received bytes
  371. {
  372. int ret = downStreamBytes;
  373. if (reset) downStreamBytes = 0;
  374. return ret;
  375. }
  376. int SSLClient::getUploadBytes(bool reset) // returns the number of sent bytes
  377. {
  378. int ret = upStreamBytes;
  379. if (reset) upStreamBytes = 0;
  380. return ret;
  381. }
  382. bool SSLClient::disconnect() // Disconnects from server
  383. {
  384. BIO_ssl_shutdown(bio);
  385. connected = 0;
  386. return 1;
  387. }
  388. // constant
  389. bool SSLClient::hasMessage(int waitTime) // Waits for a message for a given time
  390. {
  391. fd_set set;
  392. FD_ZERO(&set);
  393. FD_SET(SSL_get_rfd(ssl), &set);
  394. timeval time = {waitTime / 1000, waitTime};
  395. return SSL_pending(ssl) > 0 || select(0, &set, 0, 0, &time) == 1;
  396. }
  397. unsigned short SSLClient::getServerPort() const // returns the server port
  398. {
  399. return port;
  400. }
  401. const char* SSLClient::getServerIp() const // returns the server IP
  402. {
  403. return ip->getText();
  404. }