PeerMessageUtil.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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(!Util::isPowerOf(length, 2)) {
  55. throw new DlAbortEx("invalid length %d, which is not power of 2",
  56. length);
  57. }
  58. }
  59. void PeerMessageUtil::checkRange(int begin, int length, int pieceLength) {
  60. if(!(0 <= begin && 0 < length)) {
  61. throw new DlAbortEx("invalid range, begin = %d, length = %d",
  62. begin, length);
  63. }
  64. int end = begin+length;
  65. if(!(0 < end && end <= pieceLength)) {
  66. throw new DlAbortEx("invalid range, begin = %d, length = %d",
  67. begin, length);
  68. }
  69. }
  70. void PeerMessageUtil::checkBitfield(const unsigned char* bitfield, int bitfieldLength, int pieces) {
  71. if(!(bitfieldLength == BITFIELD_LEN_FROM_PIECES(pieces))) {
  72. throw new DlAbortEx("invalid bitfield length = %d",
  73. bitfieldLength);
  74. }
  75. char lastbyte = bitfield[bitfieldLength-1];
  76. for(int i = 0; i < 8-pieces%8 && pieces%8 != 0; i++) {
  77. if(!(((lastbyte >> i) & 1) == 0)) {
  78. throw new DlAbortEx("invalid bitfield");
  79. }
  80. }
  81. }
  82. void PeerMessageUtil::checkHandshake(const HandshakeMessage* message, const unsigned char* infoHash) {
  83. if(message->pstrlen != 19) {
  84. throw new DlAbortEx("invalid handshake pstrlen = %d", (int)message->pstrlen);
  85. }
  86. if(message->pstr != PSTR) {
  87. throw new DlAbortEx("invalid handshake pstr");
  88. }
  89. string myInfoHash = Util::toHex(infoHash, 20);
  90. string peerInfoHash = Util::toHex(message->infoHash, 20);
  91. if(myInfoHash != peerInfoHash) {
  92. throw new DlAbortEx("invalid handshake info hash: expected:%s, actual:%s",
  93. myInfoHash.c_str(), peerInfoHash.c_str());
  94. }
  95. }
  96. void PeerMessageUtil::setIntParam(char* dest, int param) {
  97. int nParam = htonl(param);
  98. memcpy(dest, &nParam, 4);
  99. }
  100. void PeerMessageUtil::createPeerMessageString(char* msg, int msgLength,
  101. int payloadLength,
  102. int messageId) {
  103. assert(msgLength >= 5);
  104. memset(msg, 0, msgLength);
  105. setIntParam(msg, payloadLength);
  106. msg[4] = (char)messageId;
  107. }