PeerMessageUtil.cc 5.4 KB

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