/* * Packet.h * * Copyright (C) 2008 Matthias Schiffer * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #ifndef MAD_NET_PACKET_H_ #define MAD_NET_PACKET_H_ #include #include #include #include namespace Mad { namespace Net { class Packet { public: enum Type { OK = 0x0000, ERROR = 0x0001, DISCONNECT = 0x0002, LOG = 0x0003, GSSAPI_AUTH = 0x0010, IDENTIFY = 0x0011, LIST_DAEMONS = 0x0020, STATUS = 0x0030, DAEMON_STATUS = 0x0031, FS_INFO = 0x0032, DAEMON_FS_INFO = 0x0033, COMMAND_SHUTDOWN = 0x0040, COMMAND_REBOOT = 0x0041, DAEMON_COMMAND_SHUTDOWN = 0x0050, DAEMON_COMMAND_REBOOT = 0x0051, DAEMON_STATE_UPDATE = 0x0060 }; struct Data { uint16_t type; uint16_t requestId; uint16_t reserved; uint16_t length; uint8_t data[0]; }; protected: Data *rawData; void setLength(uint16_t length) { rawData->length = htons(length); rawData = (Data*)std::realloc(rawData, getRawDataLength()); } public: Packet(Type type, uint16_t requestId, const void *data = 0, uint16_t length = 0); Packet(const Packet &p) { rawData = (Data*)std::malloc(p.getRawDataLength()); std::memcpy(rawData, p.rawData, p.getRawDataLength()); } virtual ~Packet() { std::free(rawData); } Packet& operator=(const Packet &p); Type getType() const { return (Type)ntohs(rawData->type); } uint16_t getRequestId() const { return ntohs(rawData->requestId); } uint16_t getLength() const { return ntohs(rawData->length); } const uint8_t* getData() const { return rawData->data; } const Data* getRawData() const { return rawData; } unsigned long getRawDataLength() const { return sizeof(Data) + ntohs(rawData->length); } static uint64_t htonll(uint64_t val); static uint64_t ntohll(uint64_t val); }; } } #endif /*MAD_NET_PACKET_H_*/