PacketBase.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #ifndef PROTOCOLS__GNUTELLA__PACKETS__PACKET_BASE_H
00024 #define PROTOCOLS__GNUTELLA__PACKETS__PACKET_BASE_H
00025 
00026 #include "Imports.h"
00027 #include "BinaryReader.h"
00028 #include "BinaryWriter.h"
00029 
00030 namespace Protocols {
00031 namespace Gnutella {
00032 namespace Packets {
00033 
00039 enum PacketType
00040 {
00041     PingPacket              = 0x00, 
00042     PongPacket              = 0x01, 
00043     QueryPacket             = 0x80, 
00044     QueryHitsPacket         = 0x81, 
00045     PushPacket              = 0x40, 
00046     // Extension Decriptiors:
00047     ByePacket               = 0x02, 
00048     IbmcPacket              = 0x10, 
00049     QueryRoutingPacket      = 0x30, 
00050     OpenVendorPacket        = 0x31, 
00051     StandardVendorPacket    = 0x32  
00052 };
00053 
00055 
00071 enum PacketConstants
00072 {
00073     DefaultHops                 = 0,
00074     DefaultTtl                  = 5,
00075     MaximalTtl                  = 7,
00076     HeaderLength                = 23,
00077     MaximalPayloadLength        = 0x00010000 - HeaderLength,
00078     MaximalQueryPayloadLength   = 0x00001000 - HeaderLength,
00079     MaximalPacketLength         = HeaderLength + MaximalPayloadLength
00080 };
00081 
00083 
00103 class PacketBase : public GenericsPacketBase
00104 {
00105 public:
00106     virtual QString     protocol() const;
00107     virtual QString     name() const = 0;
00108     virtual auto_ptr <DataBase>
00109                         copy() const = 0;
00110 
00111     const QUuid &       descriptorId() const;
00112     void                setDescriptorId (const QUuid &descriptorId);
00113 
00114     PacketType          packetType() const;
00115 
00116     uchar               ttl() const;
00117     void                setTtl (uchar ttl);
00118 
00119     uchar               hops() const;
00120     void                setHops (uchar hops);
00121 
00122     void                doHop();
00123 
00124 protected:
00125     class Data;         // Needed for ctor declaration.
00126                         PacketBase (Data *d, PacketType packetType);
00127 
00128     virtual bool        readHeader (const QByteArray &rawHeader);
00129     virtual bool        readPayload (const QByteArray &rawPayload);
00130 
00131     virtual QByteArray  writeHeader () const;
00132     virtual QByteArray  writePayload () const;
00133 
00134     virtual void        readPayload (BinaryReader &) = 0;
00135     virtual void        writePayload (BinaryWriter &) const = 0;
00136 
00137 protected:
00139 
00148     class Data: public GenericsPacketBase::Data
00149     {
00150     public:
00151         QUuid               descriptorId;
00152         PacketType          packetType;
00153         uchar               ttl;
00154         uchar               hops;
00155     };
00156 
00157 private:
00158     Q_DECLARE_SHARED_DATA (Data);
00159 };
00160 
00162 inline const QUuid & PacketBase::descriptorId() const
00163 { Q_SD (const Data); return d->descriptorId; }
00164 
00166 inline void PacketBase::setDescriptorId (const QUuid &descriptorId)
00167 { Q_SD (Data); invalidateHeader(); d->descriptorId = descriptorId; }
00168 
00170 inline PacketType PacketBase::packetType() const
00171 { Q_SD (const Data); return d->packetType; }
00172 
00174 inline uchar PacketBase::ttl() const
00175 { Q_SD (const Data); return d->ttl; }
00176 
00178 inline void PacketBase::setTtl (uchar ttl)
00179 { Q_SD (Data); invalidateHeader(); d->ttl = ttl; }
00180 
00182 inline uchar PacketBase::hops() const
00183 { Q_SD (const Data); return d->hops; }
00184 
00186 inline void PacketBase::setHops (uchar hops)
00187 { Q_SD (Data); invalidateHeader(); d->hops = hops; }
00188 
00190 inline void PacketBase::doHop()
00191 { Q_SD (Data); invalidateHeader(); d->ttl--; d->hops++; }
00192 
00193 } // namespace Packets
00194 } // namespace Gnutella
00195 } // namespace Protocols
00196 
00197 #endif // PROTOCOLS__GNUTELLA__PACKETS__PACKET_BASE_H