#include <Packet.h> [code]
Inherited by Gnutella::Packets::Bye, Gnutella::Packets::Ping, Gnutella::Packets::Pong, Gnutella::Packets::Push, Gnutella::Packets::Query, Gnutella::Packets::QueryHits, Gnutella::Packets::QueryRouting, Gnutella::Packets::UnknownPacket, and Gnutella::Packets::VendorMessages::VendorMessage.
Inheritance diagram for Gnutella::Packets::Packet:


A typical Gnutella packet consists of two parts: header and payload. All packet types share the same header structure but have different payload structure. The table below presents the data fields stored in a header and how they can be accessed using the Packet interface. For a detailed documentation of all basic Gnutella packets and the usage of all their fileds please refer to The Annotated Gnutella Protocol Specification v0.4.
| Byte Offset | Field Name | getFunction | setFunction |
| 0..15 | Descriptor ID | descriptorId() | N/A |
| 16 | Payload Descriptor | payloadDescriptor() | N/A |
| 17 | Time to Live | ttl() | setTtl() |
| 18 | Hops | hops() | setHops() |
| 19..23 | Payload Length | payloadLength() | setPayloadLength() |
The Packet class is an abstract class that defines a common interface for all packets. You can create packets of type PingPacket, PongPacket, QueryPacket, QueryHitsPacket and PushPacket by using two types of constructors:
If you derive from Packet, make sure you provide implementation for isPayloadValid(), readPayload() and writePayload(). To support reading a Packet of the new class, you will also need to extend PacketReader::getPacket().
Definition at line 137 of file Packet.h.
Public Member Functions | |
| virtual | ~Packet () |
| Destructor. | |
| virtual QString | name () const =0 |
| const QUuid & | descriptorId () const |
| Gets the descriptor ID header field. | |
| PayloadDescriptor | payloadDescriptor () const |
| Gets the payload desctiptor header field. | |
| uchar | ttl () const |
| Gets the ttl header field. | |
| uchar | hops () const |
| Gets the hops header filed. | |
| quint32 | payloadLength () const |
| Gets the payload length header field. | |
| quint32 | packetLength () const |
| Gets the total packet length in bytes (header length + payload length). | |
| bool | isValid () const |
| Checks if the packet is valid (has correct structure and valid data). | |
| void | doHop () |
| Increments hops and decrements ttl by one. | |
| void | setDescriptorId (const QUuid &descriptorId) |
| Sets the descriptror ID header field. | |
| void | setTtl (uchar ttl) |
| Sets the ttl header filed. | |
| void | setHops (uchar hops) |
| Sets the hops header field. | |
| QByteArray | rawHeader () const |
| Returns a QByteArray containing the raw header ready for transmission. | |
| QByteArray | rawPayload () const |
| Returns a QByteArray containing the raw payload ready for transmission. | |
| virtual Packet * | copy () const =0 |
| A virtual copy contructor. | |
Static Public Member Functions | |
| static Packet * | fromRawData (const QByteArray &rawHeader, const QByteArray &rawPayload) |
Protected Member Functions | |
| Packet (PayloadDescriptor payloadDescriptor) | |
| Packet (const QByteArray &rawHeader, const QByteArray &rawPayload) | |
| Packet (const Packet &other) | |
| Packet & | operator= (const Packet &other) |
| Assignment operator. | |
| void | setIsValid (bool isValid) |
| Marks the packet as either valid or invalid. | |
| void | invalidateHeader () |
| Marks the header as modified. The header should be rebuilt next time rawHeader() is called. | |
| virtual void | invalidatePayload () |
| Marks the payload as modified. The payload should be rebuilt next time rawPayload() is called. | |
| void | parse () |
| Parses the raw header and payload and fills the packet fields. | |
| virtual bool | prepareReadPayload (const QByteArray &rawPayload)=0 |
| Verifies the structure of the rawPayload. | |
| virtual void | readPayload (QDataStream &stream)=0 |
| Reads the payload fields from the stream. The structure of the data in the stream is already verified by verifyPayload(). | |
| virtual int | prepareWritePayload () const =0 |
| Gets ready to write the payload and returns the payload length. | |
| virtual void | writePayload (QDataStream &stream) const =0 |
| Writes the payload fields into the stream. | |
Private Member Functions | |
| bool | prepareReadHeader (const QByteArray &rawHeader) const |
| Verifies the structure of the rawHeader. | |
| void | readHeader (QDataStream &stream) |
| Reads the header fields from the stream. The structure of the data in the stream is verified by verifyHeader(). | |
| void | writeHeader (QDataStream &stream) const |
| Writes the header into the stream. | |
Private Attributes | |
| Data * | d |
| Private data with reference counting. | |
Friends | |
| class | Testing::ByeConcretePacket |
| class | Testing::TestPacket |
Classes | |
| struct | Data |
| Stores the private members of Packet. More... | |
|
|
Destructor. Destroys a Packet object. Definition at line 243 of file Packet.cpp. |
|
|
Constructs a Packet object. payloadDescriptor is used to initialize the corresponding header field. The other fields are initialized with default values, which can later be changed using the setter functions.
Definition at line 197 of file Packet.cpp. |
|
||||||||||||
|
Constructs a Packet object without setting any of the header fields. It just stores the passed rawHeader and rawPayload. This data is cached and returned when calling rawHeader() and rawPayload(). Refer to their documentation for more info on caching the raw data.
Definition at line 226 of file Packet.cpp. |
|
|
|
|
|
|
Gets the descriptor ID header field.
|
|
|
Increments hops and decrements ttl by one.
|
|
||||||||||||
|
This function plays the role of a virtual constructor. Once you have read the complete raw data for a packet (yes, you should be aware of the format in order to do this, see PacketReader), call this function, which will create an object calling new and return it. Make sure you delete the object when done with it.
Reimplemented in Gnutella::Packets::VendorMessages::VendorMessage. Definition at line 141 of file Packet.cpp. |
|
|
Gets the hops header filed.
|
|
|
Marks the header as modified. The header should be rebuilt next time rawHeader() is called. Invalidates the header data, so that the rawHeader will be rebuilt next time it is needed. Definition at line 262 of file Packet.cpp. |
|
|
Marks the payload as modified. The payload should be rebuilt next time rawPayload() is called. The header is invalidated, due to the fact that the payloadLength field may have changed, and the payload is marked as invalid too.
Reimplemented in Gnutella::Packets::Bye, Gnutella::Packets::Pong, and Gnutella::Packets::Testing::ByeConcretePacket. Definition at line 279 of file Packet.cpp. |
|
|
Checks if the packet is valid (has correct structure and valid data).
|
|
|
|
Assignment operator. Copies other into this object. Copying is fast due to reference counting. Definition at line 253 of file Packet.cpp. |
|
|
Gets the total packet length in bytes (header length + payload length).
|
|
|
Parses the raw header and payload and fills the packet fields. The function checks the structure of the bytes in rawHeader and rawPayload by calling prepareReadHeader() and prepareReadPayload() respectively. If any of the functions returns false, then the packet is marked as invalid. If the functions return true, then a QStreamObject is created around the raw bytes and the functions readHeader() and readPayload() are called to read the packet fields out of the stream. If any of the function returns false, meaning that some field had an invalid value, then the packet is marked as invalid. You can check if a packet is OK by calling isValid(). You should discard the packet if the function returns false, as most probably some of the packet fields are not set! Definition at line 297 of file Packet.cpp. |
|
|
Gets the payload desctiptor header field.
|
|
|
Gets the payload length header field.
|
|
|
Verifies the structure of the rawHeader. Simply checks if the header is of correct size. Definition at line 322 of file Packet.cpp. |
|
|
Verifies the structure of the rawPayload. Verifies if the rawPayload can be correctly parsed by readPayload(). The function just goes over the whole rawPayload and checks if the structure of the payload is correct. Some data may be gathered and stored to ease the work of readPayload(). For example, the size of the query hit data field can only be calculated with knowledge of the payloadLength, which is not available in the QDataStream object passed to readPayload(). The size of this field is read here and stored for use later in readPayload(). Implemented in Gnutella::Packets::Bye, Gnutella::Packets::Ping, Gnutella::Packets::Pong, Gnutella::Packets::Push, Gnutella::Packets::Query, Gnutella::Packets::QueryHits, Gnutella::Packets::QueryRouting, Gnutella::Packets::QueryRoutingPatch, Gnutella::Packets::QueryRoutingReset, Gnutella::Packets::UnknownPacket, Gnutella::Packets::VendorMessages::QueryStatusRequest, Gnutella::Packets::VendorMessages::QueryStatusResponse, Gnutella::Packets::VendorMessages::SupportedMessages, and Gnutella::Packets::VendorMessages::VendorMessage. |
|
|
|
Returns a QByteArray containing the raw header ready for transmission. The function returns a cached QByteArray containing the raw header bytes, unless the header was marked as modified by calling invalidateHeader(). In this case, the header is rebuilt by calling writeHeader() and cached for future use. Caching of the packet's raw data improves performance especially when the same packet is sent to more than one peer. In this case, due to reference counting, the raw data is generated just once and is also stored just once, although the packets are copied in multiple connections.
Definition at line 388 of file Packet.cpp. |
|
|
Returns a QByteArray containing the raw payload ready for transmission. The function returns a cached QByteArray containing the raw payload bytes, unless the payload was marked as modified by calling invalidatePayload(). In this case, the payload is rebuilt by calling writePayload() and cached for future use. Read the docs for rawHeader() for more info on raw data caching. This function is called by rawHeader() and payloadLength() to ensure that the header field payloadLength has the correct value.
Definition at line 441 of file Packet.cpp. |
|
|
Reads the header fields from the stream. The structure of the data in the stream is verified by verifyHeader(). Reads the header fields from the stream, which uses little endian byte order - Gnutella's default byte order. Some fields may be in big endian if explicitly stated in the specifications.
Definition at line 336 of file Packet.cpp. |
|
|
Reads the payload fields from the stream. The structure of the data in the stream is already verified by verifyPayload().
Implemented in Gnutella::Packets::Bye, Gnutella::Packets::Ping, Gnutella::Packets::Pong, Gnutella::Packets::Push, Gnutella::Packets::Query, Gnutella::Packets::QueryHits, Gnutella::Packets::QueryRouting, Gnutella::Packets::QueryRoutingPatch, Gnutella::Packets::QueryRoutingReset, Gnutella::Packets::UnknownPacket, Gnutella::Packets::VendorMessages::QueryStatusRequest, Gnutella::Packets::VendorMessages::QueryStatusResponse, Gnutella::Packets::VendorMessages::SupportedMessages, and Gnutella::Packets::VendorMessages::VendorMessage. |
|
|
Sets the descriptror ID header field.
|
|
|
Sets the hops header field.
|
|
|
Marks the packet as either valid or invalid.
|
|
|
Sets the ttl header filed.
|
|
|
Gets the ttl header field.
|
|
|
Writes the header into the stream. Writes the header fields into the stream, which uses little endian byte order - Gnutella's default byte order. Some fields may be in big endian if explicitly stated in the specifications.
Definition at line 354 of file Packet.cpp. |
|
|
|
|
|
|
|
|
|
Private data with reference counting.
Reimplemented in Gnutella::Packets::Bye, and Gnutella::Packets::Pong. |