PeerMessageUtil.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "PeerMessageUtil.h"
  36. #include "DlAbortEx.h"
  37. #include "a2netcompat.h"
  38. #include <cassert>
  39. #include <cstring>
  40. namespace aria2 {
  41. int8_t PeerMessageUtil::getId(const unsigned char* msg) {
  42. return msg[0];
  43. }
  44. int32_t PeerMessageUtil::getIntParam(const unsigned char* msg, int32_t offset) {
  45. int32_t nParam;
  46. memcpy(&nParam, msg+offset, sizeof(int32_t));
  47. return ntohl(nParam);
  48. }
  49. int16_t PeerMessageUtil::getShortIntParam(const unsigned char* msg, int32_t offset) {
  50. int16_t nParam;
  51. memcpy(&nParam, msg+offset, sizeof(int16_t));
  52. return ntohs(nParam);
  53. }
  54. void PeerMessageUtil::checkIndex(int32_t index, int32_t pieces) {
  55. if(!(0 <= index && index < (int32_t)pieces)) {
  56. throw new DlAbortEx("Invalid index: %d", index);
  57. }
  58. }
  59. void PeerMessageUtil::checkBegin(int32_t begin, int32_t pieceLength) {
  60. if(!(0 <= begin && begin < (int32_t)pieceLength)) {
  61. throw new DlAbortEx("Invalid begin: %d", begin);
  62. }
  63. }
  64. void PeerMessageUtil::checkLength(int32_t length) {
  65. if(length > MAX_BLOCK_LENGTH) {
  66. throw new DlAbortEx("Length too long: %d > %dKB", length,
  67. MAX_BLOCK_LENGTH/1024);
  68. }
  69. if(length <= 0) {
  70. throw new DlAbortEx("Invalid length: %d", length);
  71. }
  72. }
  73. void PeerMessageUtil::checkRange(int32_t begin, int32_t length, int32_t pieceLength) {
  74. if(!(0 <= begin && 0 < length)) {
  75. throw new DlAbortEx("Invalid range: begin=%d, length=%d",
  76. begin, length);
  77. }
  78. int32_t end = begin+length;
  79. if(!(0 < end && end <= pieceLength)) {
  80. throw new DlAbortEx("Invalid range: begin=%d, length=%d",
  81. begin, length);
  82. }
  83. }
  84. void PeerMessageUtil::checkBitfield(const unsigned char* bitfield,
  85. int32_t bitfieldLength,
  86. int32_t pieces) {
  87. if(!(bitfieldLength == (pieces+7)/8)) {
  88. throw new DlAbortEx("Invalid bitfield length: %d",
  89. bitfieldLength);
  90. }
  91. char lastbyte = bitfield[bitfieldLength-1];
  92. for(int32_t i = 0; i < 8-pieces%8 && pieces%8 != 0; ++i) {
  93. if(!(((lastbyte >> i) & 1) == 0)) {
  94. throw new DlAbortEx("Invalid bitfield");
  95. }
  96. }
  97. }
  98. void PeerMessageUtil::setIntParam(unsigned char* dest, int32_t param) {
  99. int32_t nParam = htonl(param);
  100. memcpy(dest, &nParam, sizeof(int32_t));
  101. }
  102. void PeerMessageUtil::setShortIntParam(unsigned char* dest, int16_t param) {
  103. int16_t nParam = htons(param);
  104. memcpy(dest, &nParam, sizeof(int16_t));
  105. }
  106. void PeerMessageUtil::createPeerMessageString(unsigned char* msg,
  107. int32_t msgLength,
  108. int32_t payloadLength,
  109. int8_t messageId) {
  110. assert(msgLength >= 5);
  111. memset(msg, 0, msgLength);
  112. setIntParam(msg, payloadLength);
  113. msg[4] = messageId;
  114. }
  115. bool PeerMessageUtil::createcompact(char* compact, const std::string& addr, uint16_t port)
  116. {
  117. struct addrinfo hints;
  118. struct addrinfo* res;
  119. memset(&hints, 0, sizeof(hints));
  120. hints.ai_family = AF_INET; // since compact peer format is ipv4 only.
  121. hints.ai_flags = AI_NUMERICHOST;
  122. if(getaddrinfo(addr.c_str(), 0, &hints, &res)) {
  123. return false;
  124. }
  125. struct sockaddr_in* in = reinterpret_cast<struct sockaddr_in*>(res->ai_addr);
  126. uint32_t* addrp = (uint32_t*)compact;
  127. *addrp = in->sin_addr.s_addr;
  128. uint16_t* portp = (uint16_t*)(compact+4);
  129. *portp = htons(port);
  130. freeaddrinfo(res);
  131. return true;
  132. }
  133. std::pair<std::string, uint16_t> PeerMessageUtil::unpackcompact(const char* compact)
  134. {
  135. struct sockaddr_in in;
  136. memset(&in, 0, sizeof(in));
  137. in.sin_family = AF_INET;
  138. in.sin_addr.s_addr = *reinterpret_cast<const uint32_t*>(compact);
  139. in.sin_port = 0;
  140. char host[NI_MAXHOST];
  141. int s;
  142. s = getnameinfo(reinterpret_cast<const struct sockaddr*>(&in), sizeof(in),
  143. host, NI_MAXHOST, 0, NI_MAXSERV,
  144. NI_NUMERICHOST);
  145. if(s) {
  146. return std::pair<std::string, uint16_t>();
  147. }
  148. uint16_t port = ntohs(*(uint16_t*)(compact+sizeof(uint32_t)));
  149. return std::pair<std::string, uint16_t>(host, port);
  150. }
  151. } // namespace aria2