/* */ #include "PeerMessageUtil.h" #include "DlAbortEx.h" #include "Util.h" #include int PeerMessageUtil::getId(const char* msg) { return (int)msg[0]; } int PeerMessageUtil::getIntParam(const char* msg, int offset) { int nParam; memcpy(&nParam, msg+offset, 4); return ntohl(nParam); } int PeerMessageUtil::getShortIntParam(const char* msg, int offset) { short int nParam; memcpy(&nParam, msg+offset, 2); return ntohs(nParam); } void PeerMessageUtil::checkIndex(int index, int pieces) { if(!(0 <= index && index < pieces)) { throw new DlAbortEx("invalid index = %d", index); } } void PeerMessageUtil::checkBegin(int begin, int pieceLength) { if(!(0 <= begin && begin < pieceLength)) { throw new DlAbortEx("invalid begin = %d", begin); } } void PeerMessageUtil::checkLength(int length) { if(length > MAX_BLOCK_LENGTH) { throw new DlAbortEx("too large length %d > %dKB", length, MAX_BLOCK_LENGTH/1024); } if(length <= 0) { throw new DlAbortEx("invalid length %d", length); } if(!Util::isPowerOf(length, 2)) { throw new DlAbortEx("invalid length %d, which is not power of 2", length); } } void PeerMessageUtil::checkRange(int begin, int length, int pieceLength) { if(!(0 <= begin && 0 < length)) { throw new DlAbortEx("invalid range, begin = %d, length = %d", begin, length); } int end = begin+length; if(!(0 < end && end <= pieceLength)) { throw new DlAbortEx("invalid range, begin = %d, length = %d", begin, length); } } void PeerMessageUtil::checkBitfield(const unsigned char* bitfield, int bitfieldLength, int pieces) { if(!(bitfieldLength == BITFIELD_LEN_FROM_PIECES(pieces))) { throw new DlAbortEx("invalid bitfield length = %d", bitfieldLength); } char lastbyte = bitfield[bitfieldLength-1]; for(int i = 0; i < 8-pieces%8 && pieces%8 != 0; i++) { if(!(((lastbyte >> i) & 1) == 0)) { throw new DlAbortEx("invalid bitfield"); } } } void PeerMessageUtil::checkHandshake(const HandshakeMessage* message, const unsigned char* infoHash) { if(message->pstrlen != 19) { throw new DlAbortEx("invalid handshake pstrlen = %d", (int)message->pstrlen); } if(message->pstr != PSTR) { throw new DlAbortEx("invalid handshake pstr"); } string myInfoHash = Util::toHex(infoHash, 20); string peerInfoHash = Util::toHex(message->infoHash, 20); if(myInfoHash != peerInfoHash) { throw new DlAbortEx("invalid handshake info hash: expected:%s, actual:%s", myInfoHash.c_str(), peerInfoHash.c_str()); } } void PeerMessageUtil::setIntParam(char* dest, int param) { int nParam = htonl(param); memcpy(dest, &nParam, 4); } void PeerMessageUtil::createPeerMessageString(char* msg, int msgLength, int payloadLength, int messageId) { assert(msgLength >= 5); memset(msg, 0, msgLength); setIntParam(msg, payloadLength); msg[4] = (char)messageId; }