PeerMessageUtil.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <cassert>
  37. #include <cstring>
  38. #include "DlAbortEx.h"
  39. #include "a2netcompat.h"
  40. #include "StringFormat.h"
  41. #include "BtConstants.h"
  42. #include "message.h"
  43. #include "StringFormat.h"
  44. namespace aria2 {
  45. uint8_t PeerMessageUtil::getId(const unsigned char* msg) {
  46. return msg[0];
  47. }
  48. uint32_t PeerMessageUtil::getIntParam(const unsigned char* msg, size_t pos)
  49. {
  50. uint32_t nParam;
  51. memcpy(&nParam, msg+pos, sizeof(nParam));
  52. return ntohl(nParam);
  53. }
  54. uint16_t PeerMessageUtil::getShortIntParam(const unsigned char* msg, size_t pos) {
  55. uint16_t nParam;
  56. memcpy(&nParam, msg+pos, sizeof(nParam));
  57. return ntohs(nParam);
  58. }
  59. void PeerMessageUtil::checkIndex(size_t index, size_t pieces) {
  60. if(!(index < pieces)) {
  61. throw DlAbortEx(StringFormat("Invalid index: %lu",
  62. static_cast<unsigned long>(index)).str());
  63. }
  64. }
  65. void PeerMessageUtil::checkBegin(uint32_t begin, size_t pieceLength) {
  66. if(!(begin < pieceLength)) {
  67. throw DlAbortEx(StringFormat("Invalid begin: %u", begin).str());
  68. }
  69. }
  70. void PeerMessageUtil::checkLength(size_t length) {
  71. if(length > MAX_BLOCK_LENGTH) {
  72. throw DlAbortEx(StringFormat("Length too long: %lu > %uKB",
  73. static_cast<unsigned long>(length),
  74. MAX_BLOCK_LENGTH/1024).str());
  75. }
  76. if(length == 0) {
  77. throw DlAbortEx(StringFormat("Invalid length: %lu",
  78. static_cast<unsigned long>(length)).str());
  79. }
  80. }
  81. void PeerMessageUtil::checkRange(uint32_t begin, size_t length, size_t pieceLength) {
  82. if(!(0 < length)) {
  83. throw DlAbortEx(StringFormat("Invalid range: begin=%u, length=%lu",
  84. begin,
  85. static_cast<unsigned long>(length)).str());
  86. }
  87. uint32_t end = begin+length;
  88. if(!(end <= pieceLength)) {
  89. throw DlAbortEx(StringFormat("Invalid range: begin=%u, length=%lu",
  90. begin,
  91. static_cast<unsigned long>(length)).str());
  92. }
  93. }
  94. void PeerMessageUtil::checkBitfield(const unsigned char* bitfield,
  95. size_t bitfieldLength,
  96. size_t pieces) {
  97. if(!(bitfieldLength == (pieces+7)/8)) {
  98. throw DlAbortEx
  99. (StringFormat("Invalid bitfield length: %lu",
  100. static_cast<unsigned long>(bitfieldLength)).str());
  101. }
  102. char lastbyte = bitfield[bitfieldLength-1];
  103. for(size_t i = 0; i < 8-pieces%8 && pieces%8 != 0; ++i) {
  104. if(!(((lastbyte >> i) & 1) == 0)) {
  105. throw DlAbortEx("Invalid bitfield");
  106. }
  107. }
  108. }
  109. void PeerMessageUtil::setIntParam(unsigned char* dest, uint32_t param) {
  110. uint32_t nParam = htonl(param);
  111. memcpy(dest, &nParam, sizeof(nParam));
  112. }
  113. void PeerMessageUtil::setShortIntParam(unsigned char* dest, uint16_t param) {
  114. uint16_t nParam = htons(param);
  115. memcpy(dest, &nParam, sizeof(nParam));
  116. }
  117. void PeerMessageUtil::createPeerMessageString(unsigned char* msg,
  118. size_t msgLength,
  119. size_t payloadLength,
  120. uint8_t messageId) {
  121. assert(msgLength >= 5);
  122. memset(msg, 0, msgLength);
  123. setIntParam(msg, payloadLength);
  124. msg[4] = messageId;
  125. }
  126. bool
  127. PeerMessageUtil::createcompact(unsigned char* compact, const std::string& addr, uint16_t port)
  128. {
  129. struct addrinfo hints;
  130. struct addrinfo* res;
  131. memset(&hints, 0, sizeof(hints));
  132. hints.ai_family = AF_INET; // since compact peer format is ipv4 only.
  133. hints.ai_flags = AI_NUMERICHOST;
  134. if(getaddrinfo(addr.c_str(), 0, &hints, &res)) {
  135. return false;
  136. }
  137. struct sockaddr_in* in = reinterpret_cast<struct sockaddr_in*>(res->ai_addr);
  138. uint32_t* addrp = (uint32_t*)compact;
  139. *addrp = in->sin_addr.s_addr;
  140. uint16_t* portp = (uint16_t*)(compact+4);
  141. *portp = htons(port);
  142. freeaddrinfo(res);
  143. return true;
  144. }
  145. std::pair<std::string, uint16_t>
  146. PeerMessageUtil::unpackcompact(const unsigned char* compact)
  147. {
  148. struct sockaddr_in in;
  149. memset(&in, 0, sizeof(in));
  150. #ifdef HAVE_SOCKADDR_IN_SIN_LEN
  151. // For netbsd
  152. in.sin_len = sizeof(in);
  153. #endif // HAVE_SOCKADDR_IN_SIN_LEN
  154. in.sin_family = AF_INET;
  155. in.sin_addr.s_addr = *reinterpret_cast<const uint32_t*>(compact);
  156. in.sin_port = 0;
  157. char host[NI_MAXHOST];
  158. int s;
  159. s = getnameinfo(reinterpret_cast<const struct sockaddr*>(&in), sizeof(in),
  160. host, NI_MAXHOST, 0, NI_MAXSERV,
  161. NI_NUMERICHOST);
  162. if(s) {
  163. return std::pair<std::string, uint16_t>();
  164. }
  165. uint16_t port = ntohs(*(uint16_t*)(compact+sizeof(uint32_t)));
  166. return std::pair<std::string, uint16_t>(host, port);
  167. }
  168. void PeerMessageUtil::assertPayloadLengthGreater
  169. (size_t threshold, size_t actual, const std::string& msgName)
  170. {
  171. if(actual <= threshold) {
  172. throw DlAbortEx
  173. (StringFormat(MSG_TOO_SMALL_PAYLOAD_SIZE, msgName.c_str(), actual).str());
  174. }
  175. }
  176. void PeerMessageUtil::assertPayloadLengthEqual
  177. (size_t expected, size_t actual, const std::string& msgName)
  178. {
  179. if(expected != actual) {
  180. throw DlAbortEx
  181. (StringFormat(EX_INVALID_PAYLOAD_SIZE, msgName.c_str(),
  182. actual, expected).str());
  183. }
  184. }
  185. void PeerMessageUtil::assertID
  186. (uint8_t expected, const unsigned char* data, const std::string& msgName)
  187. {
  188. uint8_t id = getId(data);
  189. if(expected != id) {
  190. throw DlAbortEx
  191. (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, msgName.c_str(),
  192. expected).str());
  193. }
  194. }
  195. } // namespace aria2