PeerConnection.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <cstring>
  44. namespace aria2 {
  45. PeerConnection::PeerConnection(int32_t cuid,
  46. const SocketHandle& socket,
  47. const Option* op)
  48. :cuid(cuid),
  49. socket(socket),
  50. option(op),
  51. logger(LogFactory::getInstance()),
  52. resbufLength(0),
  53. currentPayloadLength(0),
  54. lenbufLength(0)
  55. {}
  56. PeerConnection::~PeerConnection() {}
  57. int32_t PeerConnection::sendMessage(const unsigned char* data, int32_t dataLength) {
  58. int32_t writtenLength = 0;
  59. if(socket->isWritable(0)) {
  60. // TODO fix this
  61. socket->writeData((const char*)data, dataLength);
  62. writtenLength += dataLength;
  63. }
  64. return writtenLength;
  65. }
  66. bool PeerConnection::receiveMessage(unsigned char* data, int32_t& dataLength) {
  67. if(resbufLength == 0 && 4 > lenbufLength) {
  68. if(!socket->isReadable(0)) {
  69. return false;
  70. }
  71. // read payload size, 32bit unsigned integer
  72. int32_t remaining = 4-lenbufLength;
  73. int32_t temp = remaining;
  74. socket->readData(lenbuf+lenbufLength, remaining);
  75. if(remaining == 0) {
  76. // we got EOF
  77. logger->debug("CUID#%d - In PeerConnection::receiveMessage(), remain=%d",
  78. cuid, temp);
  79. throw new DlAbortEx(EX_EOF_FROM_PEER);
  80. }
  81. lenbufLength += remaining;
  82. if(4 > lenbufLength) {
  83. // still 4-lenbufLength bytes to go
  84. return false;
  85. }
  86. uint32_t payloadLength = ntohl(*(reinterpret_cast<uint32_t*>(lenbuf)));
  87. if(payloadLength > MAX_PAYLOAD_LEN) {
  88. throw new DlAbortEx(EX_TOO_LONG_PAYLOAD, payloadLength);
  89. }
  90. currentPayloadLength = payloadLength;
  91. }
  92. if(!socket->isReadable(0)) {
  93. return false;
  94. }
  95. // we have currentPayloadLen-resbufLen bytes to read
  96. int32_t remaining = currentPayloadLength-resbufLength;
  97. int32_t temp = remaining;
  98. if(remaining > 0) {
  99. socket->readData(resbuf+resbufLength, remaining);
  100. if(remaining == 0) {
  101. // we got EOF
  102. logger->debug("CUID#%d - In PeerConnection::receiveMessage(), payloadlen=%d, remaining=%d",
  103. cuid, currentPayloadLength, temp);
  104. throw new DlAbortEx(EX_EOF_FROM_PEER);
  105. }
  106. resbufLength += remaining;
  107. if(currentPayloadLength > resbufLength) {
  108. return false;
  109. }
  110. }
  111. // we got whole payload.
  112. resbufLength = 0;
  113. lenbufLength = 0;
  114. memcpy(data, resbuf, currentPayloadLength);
  115. dataLength = currentPayloadLength;
  116. return true;
  117. }
  118. bool PeerConnection::receiveHandshake(unsigned char* data, int32_t& dataLength,
  119. bool peek) {
  120. int32_t remaining = BtHandshakeMessage::MESSAGE_LENGTH-resbufLength;
  121. if(remaining > 0 && !socket->isReadable(0)) {
  122. dataLength = 0;
  123. return false;
  124. }
  125. bool retval = true;
  126. if(remaining > 0) {
  127. int32_t temp = remaining;
  128. socket->readData(resbuf+resbufLength, remaining);
  129. if(remaining == 0) {
  130. // we got EOF
  131. logger->debug("CUID#%d - In PeerConnection::receiveHandshake(), remain=%d",
  132. cuid, temp);
  133. throw new DlAbortEx(EX_EOF_FROM_PEER);
  134. }
  135. resbufLength += remaining;
  136. if(BtHandshakeMessage::MESSAGE_LENGTH > resbufLength) {
  137. retval = false;
  138. }
  139. }
  140. int32_t writeLength = resbufLength > dataLength ? dataLength : resbufLength;
  141. memcpy(data, resbuf, writeLength);
  142. dataLength = writeLength;
  143. if(retval && !peek) {
  144. resbufLength = 0;
  145. }
  146. return retval;
  147. }
  148. } // namespace aria2