PeerConnection.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "PeerConnection.h"
  36. #include "message.h"
  37. #include "DlAbortEx.h"
  38. #include "LogFactory.h"
  39. #include "Logger.h"
  40. #include "BtHandshakeMessage.h"
  41. #include "Socket.h"
  42. #include "a2netcompat.h"
  43. #include "ARC4Encryptor.h"
  44. #include "ARC4Decryptor.h"
  45. #include <cstring>
  46. #include <cassert>
  47. #include <algorithm>
  48. namespace aria2 {
  49. PeerConnection::PeerConnection(int32_t cuid,
  50. const SocketHandle& socket,
  51. const Option* op)
  52. :cuid(cuid),
  53. socket(socket),
  54. option(op),
  55. logger(LogFactory::getInstance()),
  56. resbufLength(0),
  57. currentPayloadLength(0),
  58. lenbufLength(0),
  59. _encryptionEnabled(false),
  60. _encryptor(0),
  61. _decryptor(0)
  62. {}
  63. PeerConnection::~PeerConnection() {}
  64. int32_t PeerConnection::sendMessage(const unsigned char* data, int32_t dataLength) {
  65. int32_t writtenLength = 0;
  66. if(socket->isWritable(0)) {
  67. // TODO fix this
  68. sendData(data, dataLength, _encryptionEnabled);
  69. writtenLength += dataLength;
  70. }
  71. return writtenLength;
  72. }
  73. bool PeerConnection::receiveMessage(unsigned char* data, int32_t& dataLength) {
  74. if(resbufLength == 0 && 4 > lenbufLength) {
  75. if(!socket->isReadable(0)) {
  76. return false;
  77. }
  78. // read payload size, 32bit unsigned integer
  79. int32_t remaining = 4-lenbufLength;
  80. int32_t temp = remaining;
  81. readData(lenbuf+lenbufLength, remaining, _encryptionEnabled);
  82. if(remaining == 0) {
  83. // we got EOF
  84. logger->debug("CUID#%d - In PeerConnection::receiveMessage(), remain=%d",
  85. cuid, temp);
  86. throw new DlAbortEx(EX_EOF_FROM_PEER);
  87. }
  88. lenbufLength += remaining;
  89. if(4 > lenbufLength) {
  90. // still 4-lenbufLength bytes to go
  91. return false;
  92. }
  93. uint32_t payloadLength = ntohl(*(reinterpret_cast<uint32_t*>(lenbuf)));
  94. if(payloadLength > MAX_PAYLOAD_LEN) {
  95. throw new DlAbortEx(EX_TOO_LONG_PAYLOAD, payloadLength);
  96. }
  97. currentPayloadLength = payloadLength;
  98. }
  99. if(!socket->isReadable(0)) {
  100. return false;
  101. }
  102. // we have currentPayloadLen-resbufLen bytes to read
  103. int32_t remaining = currentPayloadLength-resbufLength;
  104. int32_t temp = remaining;
  105. if(remaining > 0) {
  106. readData(resbuf+resbufLength, remaining, _encryptionEnabled);
  107. if(remaining == 0) {
  108. // we got EOF
  109. logger->debug("CUID#%d - In PeerConnection::receiveMessage(), payloadlen=%d, remaining=%d",
  110. cuid, currentPayloadLength, temp);
  111. throw new DlAbortEx(EX_EOF_FROM_PEER);
  112. }
  113. resbufLength += remaining;
  114. if(currentPayloadLength > resbufLength) {
  115. return false;
  116. }
  117. }
  118. // we got whole payload.
  119. resbufLength = 0;
  120. lenbufLength = 0;
  121. memcpy(data, resbuf, currentPayloadLength);
  122. dataLength = currentPayloadLength;
  123. return true;
  124. }
  125. bool PeerConnection::receiveHandshake(unsigned char* data, int32_t& dataLength,
  126. bool peek) {
  127. int32_t remaining = BtHandshakeMessage::MESSAGE_LENGTH-resbufLength;
  128. if(remaining > 0 && !socket->isReadable(0)) {
  129. dataLength = 0;
  130. return false;
  131. }
  132. bool retval = true;
  133. if(remaining > 0) {
  134. int32_t temp = remaining;
  135. readData(resbuf+resbufLength, remaining, _encryptionEnabled);
  136. if(remaining == 0) {
  137. // we got EOF
  138. logger->debug("CUID#%d - In PeerConnection::receiveHandshake(), remain=%d",
  139. cuid, temp);
  140. throw new DlAbortEx(EX_EOF_FROM_PEER);
  141. }
  142. resbufLength += remaining;
  143. if(BtHandshakeMessage::MESSAGE_LENGTH > resbufLength) {
  144. retval = false;
  145. }
  146. }
  147. int32_t writeLength = resbufLength > dataLength ? dataLength : resbufLength;
  148. memcpy(data, resbuf, writeLength);
  149. dataLength = writeLength;
  150. if(retval && !peek) {
  151. resbufLength = 0;
  152. }
  153. return retval;
  154. }
  155. void PeerConnection::readData(unsigned char* data, int32_t& length, bool encryption)
  156. {
  157. if(encryption) {
  158. unsigned char temp[MAX_PAYLOAD_LEN];
  159. assert(MAX_PAYLOAD_LEN >= length);
  160. socket->readData(temp, length);
  161. _decryptor->decrypt(data, length, temp, length);
  162. } else {
  163. socket->readData(data, length);
  164. }
  165. }
  166. void PeerConnection::sendData(const unsigned char* data, size_t length, bool encryption)
  167. {
  168. if(encryption) {
  169. unsigned char temp[4096];
  170. const unsigned char* dptr = data;
  171. size_t r = length;
  172. while(r > 0) {
  173. size_t s = std::min(r, sizeof(temp));
  174. _encryptor->encrypt(temp, s, dptr, s);
  175. socket->writeData(temp, s);
  176. dptr += s;
  177. r -= s;
  178. }
  179. } else {
  180. socket->writeData(data, length);
  181. }
  182. }
  183. void PeerConnection::enableEncryption(const SharedHandle<ARC4Encryptor>& encryptor,
  184. const SharedHandle<ARC4Decryptor>& decryptor)
  185. {
  186. _encryptor = encryptor;
  187. _decryptor = decryptor;
  188. _encryptionEnabled = true;
  189. }
  190. void PeerConnection::presetBuffer(const unsigned char* data, size_t length)
  191. {
  192. size_t nwrite = std::min((size_t)MAX_PAYLOAD_LEN, length);
  193. memcpy(resbuf, data, nwrite);
  194. resbufLength = length;
  195. }
  196. } // namespace aria2