PeerConnection.cc 5.0 KB

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