Network.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "Network.h"
  2. #ifndef WIN32
  3. # include <iostream>
  4. # include <netdb.h>
  5. # include <signal.h>
  6. #endif
  7. #include <Logging.h>
  8. #include <openssl/bio.h>
  9. #include <openssl/err.h>
  10. #include <openssl/ssl.h>
  11. #include <Text.h>
  12. // Start network
  13. void Network::Start(int maxClients)
  14. {
  15. #ifdef WIN32
  16. WSADATA lpwd;
  17. lpwd.iMaxSockets = maxClients;
  18. int error = WSAStartup(MAKEWORD(2, 0), &lpwd);
  19. if (error != 0)
  20. Framework::Logging::error()
  21. << "Could not initialize Win Sock 2.0 WSAStartup: " << error;
  22. #else
  23. signal(SIGPIPE, SIG_IGN);
  24. #endif
  25. SSL_library_init();
  26. SSL_load_error_strings();
  27. OpenSSL_add_ssl_algorithms();
  28. }
  29. void Network::getHostName(char* name, int bufferLen)
  30. {
  31. gethostname(name, bufferLen);
  32. }
  33. char* Network::getHostAddress()
  34. {
  35. char* address;
  36. char name[255] = "";
  37. getHostName(name, 255);
  38. PHOSTENT hostinfo;
  39. hostinfo = gethostbyname(name);
  40. if (!hostinfo)
  41. {
  42. Framework::Logging::error()
  43. << "The ip address of host '" << name << "' could not be resolved";
  44. return 0;
  45. }
  46. address = inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list);
  47. return address;
  48. }
  49. // Stop network
  50. void Network::Exit()
  51. {
  52. #ifdef WIN32
  53. WSACleanup();
  54. #endif
  55. }
  56. Network::NetworkReader::NetworkReader(Connection* v)
  57. {
  58. connection = v;
  59. }
  60. Network::NetworkReader::~NetworkReader() {}
  61. //! Reads from the resource
  62. //! \param bytes An array to be filled with bytes from the resource
  63. //! \param len How many bytes to read from the resource
  64. void Network::NetworkReader::read(char* bytes, int len)
  65. {
  66. connection->getMessage(bytes, len);
  67. }
  68. //! Reads the next line from the resource
  69. //! \return The read line as text with line break
  70. Framework::Text* Network::NetworkReader::readLine()
  71. {
  72. int maxLength = 2048;
  73. char* buffer = new char[maxLength];
  74. char b;
  75. int index = 0;
  76. do
  77. {
  78. connection->getMessage(&b, 1);
  79. buffer[index++] = b;
  80. if (index == maxLength)
  81. {
  82. maxLength += 2048;
  83. char* tmp = new char[maxLength];
  84. memcpy(tmp, buffer, (__int64)maxLength - 2048);
  85. delete[] buffer;
  86. buffer = tmp;
  87. }
  88. } while (b != '\n');
  89. buffer[index] = 0;
  90. Framework::Text* result = new Framework::Text(buffer);
  91. delete[] buffer;
  92. return result;
  93. }
  94. //! Checks whether the resource has been fully read
  95. //! return 1 if the resource has been fully read. 0 otherwise
  96. bool Network::NetworkReader::isEnd() const
  97. {
  98. return 0;
  99. }
  100. Network::NetworkWriter::NetworkWriter(Connection* v)
  101. {
  102. connection = v;
  103. }
  104. Network::NetworkWriter::~NetworkWriter() {}
  105. //! Writes to the resource
  106. //! \param bytes An array containing the bytes to write to the resource
  107. //! \param len How many bytes to write to the resource
  108. void Network::NetworkWriter::write(const char* bytes, int len)
  109. {
  110. connection->send(bytes, len);
  111. }
  112. //! Checks whether the resource has been fully written
  113. //! return 1 if the resource has been fully written. 0 otherwise
  114. bool Network::NetworkWriter::isEnd() const
  115. {
  116. return 0;
  117. }
  118. Network::EncryptedNetworkReader::EncryptedNetworkReader(EncryptedConnection* v)
  119. {
  120. connection = v;
  121. }
  122. //! Reads from the resource
  123. //! \param bytes An array to be filled with bytes from the resource
  124. //! \param len How many bytes to read from the resource
  125. void Network::EncryptedNetworkReader::read(char* bytes, int len)
  126. {
  127. connection->getMessageEncrypted(bytes, len);
  128. }
  129. //! Reads the next line from the resource
  130. //! \return The read line as text with line break
  131. Framework::Text* Network::EncryptedNetworkReader::readLine()
  132. {
  133. int maxLength = 2048;
  134. char* buffer = new char[maxLength];
  135. char b;
  136. int index = 0;
  137. do
  138. {
  139. connection->getMessageEncrypted(&b, 1);
  140. buffer[index++] = b;
  141. if (index == maxLength)
  142. {
  143. maxLength += 2048;
  144. char* tmp = new char[maxLength];
  145. memcpy(tmp, buffer, (__int64)maxLength - 2048);
  146. delete[] buffer;
  147. buffer = tmp;
  148. }
  149. } while (b != '\n');
  150. buffer[index] = 0;
  151. Framework::Text* result = new Framework::Text(buffer);
  152. delete[] buffer;
  153. return result;
  154. }
  155. //! Checks whether the resource has been fully read
  156. //! return 1 if the resource has been fully read. 0 otherwise
  157. bool Network::EncryptedNetworkReader::isEnd() const
  158. {
  159. return 0;
  160. }
  161. Network::EncryptedNetworkWriter::EncryptedNetworkWriter(EncryptedConnection* v)
  162. {
  163. connection = v;
  164. }
  165. //! Writes to the resource
  166. //! \param bytes An array containing the bytes to write to the resource
  167. //! \param len How many bytes to write to the resource
  168. void Network::EncryptedNetworkWriter::write(const char* bytes, int len)
  169. {
  170. connection->sendEncrypted(bytes, len);
  171. }
  172. //! Checks whether the resource has been fully written
  173. //! return 1 if the resource has been fully written. 0 otherwise
  174. bool Network::EncryptedNetworkWriter::isEnd() const
  175. {
  176. return 0;
  177. }