PeerMessageUtil.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "Util.h"
  38. #include <netinet/in.h>
  39. int8_t PeerMessageUtil::getId(const unsigned char* msg) {
  40. return msg[0];
  41. }
  42. int32_t PeerMessageUtil::getIntParam(const unsigned char* msg, int32_t offset) {
  43. int32_t nParam;
  44. memcpy(&nParam, msg+offset, sizeof(int32_t));
  45. return ntohl(nParam);
  46. }
  47. int16_t PeerMessageUtil::getShortIntParam(const unsigned char* msg, int32_t offset) {
  48. int16_t nParam;
  49. memcpy(&nParam, msg+offset, sizeof(int16_t));
  50. return ntohs(nParam);
  51. }
  52. void PeerMessageUtil::checkIndex(int32_t index, int32_t pieces) {
  53. if(!(0 <= index && index < (int32_t)pieces)) {
  54. throw new DlAbortEx("Invalid index: %d", index);
  55. }
  56. }
  57. void PeerMessageUtil::checkBegin(int32_t begin, int32_t pieceLength) {
  58. if(!(0 <= begin && begin < (int32_t)pieceLength)) {
  59. throw new DlAbortEx("Invalid begin: %d", begin);
  60. }
  61. }
  62. void PeerMessageUtil::checkLength(int32_t length) {
  63. if(length > MAX_BLOCK_LENGTH) {
  64. throw new DlAbortEx("Length too long: %d > %dKB", length,
  65. MAX_BLOCK_LENGTH/1024);
  66. }
  67. if(length <= 0) {
  68. throw new DlAbortEx("Invalid length: %d", length);
  69. }
  70. if(!Util::isPowerOf(length, 2)) {
  71. throw new DlAbortEx("Invalid length: %d It is not power of 2",
  72. length);
  73. }
  74. }
  75. void PeerMessageUtil::checkRange(int32_t begin, int32_t length, int32_t pieceLength) {
  76. if(!(0 <= begin && 0 < length)) {
  77. throw new DlAbortEx("Invalid range: begin=%d, length=%d",
  78. begin, length);
  79. }
  80. int32_t end = begin+length;
  81. if(!(0 < end && end <= pieceLength)) {
  82. throw new DlAbortEx("Invalid range: begin=%d, length=%d",
  83. begin, length);
  84. }
  85. }
  86. void PeerMessageUtil::checkBitfield(const unsigned char* bitfield,
  87. int32_t bitfieldLength,
  88. int32_t pieces) {
  89. if(!(bitfieldLength == BITFIELD_LEN_FROM_PIECES(pieces))) {
  90. throw new DlAbortEx("Invalid bitfield length: %d",
  91. bitfieldLength);
  92. }
  93. char lastbyte = bitfield[bitfieldLength-1];
  94. for(int32_t i = 0; i < 8-pieces%8 && pieces%8 != 0; ++i) {
  95. if(!(((lastbyte >> i) & 1) == 0)) {
  96. throw new DlAbortEx("Invalid bitfield");
  97. }
  98. }
  99. }
  100. void PeerMessageUtil::setIntParam(unsigned char* dest, int32_t param) {
  101. int32_t nParam = htonl(param);
  102. memcpy(dest, &nParam, sizeof(int32_t));
  103. }
  104. void PeerMessageUtil::setShortIntParam(unsigned char* dest, int16_t param) {
  105. int16_t nParam = htons(param);
  106. memcpy(dest, &nParam, sizeof(int16_t));
  107. }
  108. void PeerMessageUtil::createPeerMessageString(unsigned char* msg,
  109. int32_t msgLength,
  110. int32_t payloadLength,
  111. int8_t messageId) {
  112. assert(msgLength >= 5);
  113. memset(msg, 0, msgLength);
  114. setIntParam(msg, payloadLength);
  115. msg[4] = messageId;
  116. }