IpPort.cpp

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 #include "Qt.h"
00024 #include "IpPort.h"
00025 #include "Gnutella/Packets/Extensions/GgepBlock.h"
00026 #include "Gnutella/Packets/Packet.h"
00027 
00028 using Gnutella::Packets::Extensions::Ggep;
00029 using Gnutella::Packets::Extensions::GgepBlock;
00030 using Gnutella::Packets::Extensions::Ggeps::IpPort;
00031 using Protocols::Transports::NodeAddress;
00032 
00033 const Ggep::GgepId IpPort::Id ("IPP", 3);
00034 
00035 IpPort::IpPort (const GgepId &id, int flags, int dataSize)
00036 : Ggep (id, flags, dataSize)
00037 {
00038 }
00039 
00040 IpPort::IpPort (const QList <NodeAddress> &addresses)
00041 : Ggep (Id)
00042 {
00043     p.addresses = addresses;
00044 }
00045 
00046 const IpPort * IpPort::findIn (const GgepBlock &ggepBlock)
00047 {
00048     const IpPort *res = 0;
00049     foreach (const Ggep *extension, ggepBlock.extensions()) {
00050         res = dynamic_cast <const IpPort *> (extension);
00051         if (res != 0)
00052             return res;
00053     }
00054     return 0;
00055 }
00056 
00057 IpPort * IpPort::copy() const
00058 {
00059     return new IpPort (*this);
00060 }
00061 
00062 bool IpPort::prepareReadData (const QByteArray &rawBytes)
00063 {
00064     return rawBytes.length() % 6 == 0; // Each IpPort should be 6 bytes!
00065 }
00066 
00067 void IpPort::readData (QDataStream &stream)
00068 {
00069     using namespace Gnutella::Packets;
00070 
00071     int count = dataLength() / 6;
00072     for (int i = 0; i < count; i++) {
00073         QHostAddress ip;
00074         quint16 port;
00075         Gnutella::Packets::operator>> (stream, ip);
00076         stream >> port;
00077         p.addresses.append (NodeAddress (ip, port));
00078     }
00079 }
00080 
00081 int IpPort::prepareWriteData() const
00082 {
00083     return p.addresses.size() * 6;
00084 }
00085 
00086 void IpPort::writeData (QDataStream &stream) const
00087 {
00088     using namespace Gnutella::Packets;
00089 
00090     foreach (NodeAddress address, p.addresses) {
00091         Gnutella::Packets::operator<< (stream, address.hostAddress());
00092         stream << address.hostPort();
00093     }
00094 }