/* */ #ifndef _D_PEER_MESSAGE_UTIL_H_ #define _D_PEER_MESSAGE_UTIL_H_ #include "common.h" #include #include namespace aria2 { class PeerMessageUtil { private: PeerMessageUtil() {} public: static uint32_t getIntParam(const unsigned char* msg, size_t pos); static uint16_t getShortIntParam(const unsigned char* msg, size_t pos); static void setIntParam(unsigned char* dest, uint32_t param); static void setShortIntParam(unsigned char* dest, uint16_t param); static uint8_t getId(const unsigned char* msg); static void checkIndex(size_t index, size_t pieces); static void checkBegin(uint32_t begin, size_t pieceLength); static void checkLength(size_t length); static void checkRange(uint32_t begin, size_t length, size_t pieceLength); static void checkBitfield(const unsigned char* bitfield, size_t bitfieldLength, size_t pieces); static void createPeerMessageString(unsigned char* msg, size_t msgLength, size_t payloadLength, uint8_t messageId); /** * Creates compact tracker format(6bytes for ipv4 address and port) * and stores the results in compact. * compact must be at least 6 bytes and pre-allocated. * Returns true if creation is successful, otherwise returns false. * The example of failure reason is that addr is not numbers-and-dots * notation. */ static bool createcompact(unsigned char* compact, const std::string& addr, uint16_t port); static std::pair unpackcompact(const unsigned char* compact); }; } // namespace aria2 #endif // _D_PEER_MESSAGE_UTIL_H_