PeerConnection.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <netinet/in.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(!socket->isReadable(0)) {
  71. return false;
  72. }
  73. if(resbufLength == 0 && lenbufLength != 4) {
  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, (int&)temp);
  79. if(temp == 0) {
  80. // we got EOF
  81. throw new DlAbortEx(EX_EOF_FROM_PEER);
  82. }
  83. if(remain != temp) {
  84. // still 4-temp bytes to go
  85. lenbufLength += temp;
  86. return false;
  87. }
  88. //payloadLen = ntohl(nPayloadLen);
  89. int32_t payloadLength = ntohl(*((int32_t*)lenbuf));
  90. if(payloadLength > MAX_PAYLOAD_LEN || payloadLength < 0) {
  91. throw new DlAbortEx("max payload length exceeded or invalid. length = %d",
  92. payloadLength);
  93. }
  94. currentPayloadLength = payloadLength;
  95. }
  96. if(!socket->isReadable(0)) {
  97. return false;
  98. }
  99. // we have currentPayloadLen-resbufLen bytes to read
  100. int32_t remaining = currentPayloadLength-resbufLength;
  101. if(remaining > 0) {
  102. socket->readData((char*)resbuf+resbufLength, (int&)remaining);
  103. if(remaining == 0) {
  104. // we got EOF
  105. throw new DlAbortEx(EX_EOF_FROM_PEER);
  106. }
  107. resbufLength += remaining;
  108. if(currentPayloadLength != resbufLength) {
  109. return false;
  110. }
  111. }
  112. // we got whole payload.
  113. resbufLength = 0;
  114. lenbufLength = 0;
  115. memcpy(data, resbuf, currentPayloadLength);
  116. dataLength = currentPayloadLength;
  117. return true;
  118. }
  119. bool PeerConnection::receiveHandshake(unsigned char* data, int32_t& dataLength) {
  120. if(!socket->isReadable(0)) {
  121. dataLength = 0;
  122. return false;
  123. }
  124. int32_t remain = BtHandshakeMessage::MESSAGE_LENGTH-resbufLength;
  125. int32_t temp = remain;
  126. socket->readData((char*)resbuf+resbufLength, (int&)temp);
  127. if(temp == 0) {
  128. // we got EOF
  129. throw new DlAbortEx(EX_EOF_FROM_PEER);
  130. }
  131. bool retval;
  132. if(remain != temp) {
  133. retval = false;
  134. } else {
  135. retval = true;
  136. }
  137. resbufLength += temp;
  138. // we got whole handshake payload
  139. int32_t writeLength = resbufLength > dataLength ? dataLength : resbufLength;
  140. memcpy(data, resbuf, writeLength);
  141. dataLength = writeLength;
  142. if(retval) {
  143. resbufLength = 0;
  144. }
  145. return retval;
  146. }