PeerMessageUtil.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "PeerMessageUtil.h"
  23. #include "DlAbortEx.h"
  24. #include "Util.h"
  25. #include <netinet/in.h>
  26. int PeerMessageUtil::getId(const char* msg) {
  27. return (int)msg[0];
  28. }
  29. int PeerMessageUtil::getIntParam(const char* msg, int offset) {
  30. int nParam;
  31. memcpy(&nParam, msg+offset, 4);
  32. return ntohl(nParam);
  33. }
  34. int PeerMessageUtil::getShortIntParam(const char* msg, int offset) {
  35. short int nParam;
  36. memcpy(&nParam, msg+offset, 2);
  37. return ntohs(nParam);
  38. }
  39. void PeerMessageUtil::checkIndex(int index, int pieces) {
  40. if(!(0 <= index && index < pieces)) {
  41. throw new DlAbortEx("invalid index = %d", index);
  42. }
  43. }
  44. void PeerMessageUtil::checkBegin(int begin, int pieceLength) {
  45. if(!(0 <= begin && begin < pieceLength)) {
  46. throw new DlAbortEx("invalid begin = %d", begin);
  47. }
  48. }
  49. void PeerMessageUtil::checkLength(int length) {
  50. if(length > MAX_BLOCK_LENGTH) {
  51. throw new DlAbortEx("too large length %d > %dKB", length,
  52. MAX_BLOCK_LENGTH/1024);
  53. }
  54. if(length <= 0) {
  55. throw new DlAbortEx("invalid length %d", length);
  56. }
  57. if(!Util::isPowerOf(length, 2)) {
  58. throw new DlAbortEx("invalid length %d, which is not power of 2",
  59. length);
  60. }
  61. }
  62. void PeerMessageUtil::checkRange(int begin, int length, int pieceLength) {
  63. if(!(0 <= begin && 0 < length)) {
  64. throw new DlAbortEx("invalid range, begin = %d, length = %d",
  65. begin, length);
  66. }
  67. int end = begin+length;
  68. if(!(0 < end && end <= pieceLength)) {
  69. throw new DlAbortEx("invalid range, begin = %d, length = %d",
  70. begin, length);
  71. }
  72. }
  73. void PeerMessageUtil::checkBitfield(const unsigned char* bitfield, int bitfieldLength, int pieces) {
  74. if(!(bitfieldLength == BITFIELD_LEN_FROM_PIECES(pieces))) {
  75. throw new DlAbortEx("invalid bitfield length = %d",
  76. bitfieldLength);
  77. }
  78. char lastbyte = bitfield[bitfieldLength-1];
  79. for(int i = 0; i < 8-pieces%8 && pieces%8 != 0; i++) {
  80. if(!(((lastbyte >> i) & 1) == 0)) {
  81. throw new DlAbortEx("invalid bitfield");
  82. }
  83. }
  84. }
  85. void PeerMessageUtil::checkHandshake(const HandshakeMessage* message, const unsigned char* infoHash) {
  86. if(message->pstrlen != 19) {
  87. throw new DlAbortEx("invalid handshake pstrlen = %d", (int)message->pstrlen);
  88. }
  89. if(message->pstr != PSTR) {
  90. throw new DlAbortEx("invalid handshake pstr");
  91. }
  92. string myInfoHash = Util::toHex(infoHash, 20);
  93. string peerInfoHash = Util::toHex(message->infoHash, 20);
  94. if(myInfoHash != peerInfoHash) {
  95. throw new DlAbortEx("invalid handshake info hash: expected:%s, actual:%s",
  96. myInfoHash.c_str(), peerInfoHash.c_str());
  97. }
  98. }
  99. void PeerMessageUtil::setIntParam(char* dest, int param) {
  100. int nParam = htonl(param);
  101. memcpy(dest, &nParam, 4);
  102. }
  103. void PeerMessageUtil::createPeerMessageString(char* msg, int msgLength,
  104. int payloadLength,
  105. int messageId) {
  106. assert(msgLength >= 5);
  107. memset(msg, 0, msgLength);
  108. setIntParam(msg, payloadLength);
  109. msg[4] = (char)messageId;
  110. }