From 210bdf11a99246b0fa803c19e8f23823e76d6c54 Mon Sep 17 00:00:00 2001 From: "Srivats P." Date: Sun, 9 Oct 2011 17:57:02 +0530 Subject: [PATCH] Implemented new method protocolFrameVariableCount() and associated code for all the protocols that require it. With this commit, the set of changes required to reduce the time taken to prebuild the packets for transmit is done. Fixes issue 35 Fixes issue 49 --- common/abstractprotocol.cpp | 29 +++++++++++++++++++- common/abstractprotocol.h | 1 + common/arp.cpp | 53 ++++++++++++++++++++++++++++++++----- common/arp.h | 1 + common/comboprotocol.h | 6 +++++ common/dot3.cpp | 5 ++++ common/dot3.h | 1 + common/gmp.cpp | 19 +++++++++++++ common/gmp.h | 1 + common/ip6.cpp | 13 +++++++++ common/ip6.h | 1 + common/mac.cpp | 12 +++++++++ common/mac.h | 1 + common/payload.cpp | 26 ++++++++++++++++++ common/payload.h | 1 + common/sample.cpp | 12 +++++++++ common/sample.h | 1 + common/tcp.cpp | 8 ++++++ common/tcp.h | 1 + common/udp.cpp | 8 ++++++ common/udp.h | 1 + common/userscript.cpp | 21 +++++++++++++++ common/userscript.h | 15 ++++++++--- server/pcapport.cpp | 2 ++ 24 files changed, 228 insertions(+), 11 deletions(-) diff --git a/common/abstractprotocol.cpp b/common/abstractprotocol.cpp index 333bd51..77def8f 100644 --- a/common/abstractprotocol.cpp +++ b/common/abstractprotocol.cpp @@ -57,6 +57,7 @@ along with this program. If not, see - protocolFrameSize() - isProtocolFrameValueVariable() - isProtocolFrameSizeVariable() + - protocolFrameVariableCount() See the description of the methods for more information. @@ -554,7 +555,7 @@ QByteArray AbstractProtocol::protocolFrameValue(int streamIndex, bool forCksum) */ bool AbstractProtocol::isProtocolFrameValueVariable() const { - return false; + return (protocolFrameVariableCount() > 1); } /*! @@ -633,6 +634,32 @@ bool AbstractProtocol::isProtocolFramePayloadSizeVariable() const return false; } +/*! + Returns true if the payload size for a protocol varies at run-time, + false otherwise + + This is useful for subclasses which have fields dependent on payload size + (e.g. UDP has a checksum field that varies if the payload varies) +*/ +int AbstractProtocol::protocolFramePayloadVariableCount() const +{ + int count = 1; + AbstractProtocol *p = next; + + while (p) + { + if (p->isProtocolFrameValueVariable() + || p->isProtocolFrameSizeVariable()) + count = lcm(count, p->protocolFrameVariableCount()); + p = p->next; + } + if (parent && (parent->isProtocolFramePayloadValueVariable() + || parent->isProtocolFramePayloadSizeVariable())) + count = lcm(count, parent->protocolFramePayloadVariableCount()); + + return false; +} + /*! Returns true if the protocol typically contains a payload or other protocols following it e.g. TCP, UDP have payloads, while ARP, IGMP do not diff --git a/common/abstractprotocol.h b/common/abstractprotocol.h index eb6dd81..ab3b7e9 100644 --- a/common/abstractprotocol.h +++ b/common/abstractprotocol.h @@ -142,6 +142,7 @@ public: virtual int protocolFrameVariableCount() const; bool isProtocolFramePayloadValueVariable() const; bool isProtocolFramePayloadSizeVariable() const; + int protocolFramePayloadVariableCount() const; bool protocolHasPayload() const; diff --git a/common/arp.cpp b/common/arp.cpp index f23b6b6..84b10a8 100644 --- a/common/arp.cpp +++ b/common/arp.cpp @@ -835,15 +835,54 @@ _exit: return isOk; } -/*! - If your protocol has any variable fields, return true \n - - Otherwise you don't need to reimplement this method - the base class always - returns false -*/ bool ArpProtocol::isProtocolFrameValueVariable() const { - return true; + if (fieldData(arp_senderHwAddrMode, FieldValue).toUInt() + != uint(OstProto::Arp::kFixed) + || fieldData(arp_senderProtoAddrMode, FieldValue).toUInt() + != uint(OstProto::Arp::kFixed) + || fieldData(arp_targetHwAddrMode, FieldValue).toUInt() + != uint(OstProto::Arp::kFixed) + || fieldData(arp_targetProtoAddrMode, FieldValue).toUInt() + != uint(OstProto::Arp::kFixed)) + return true; + + return false; +} + +int ArpProtocol::protocolFrameVariableCount() const +{ + int count = 1; + + if (fieldData(arp_senderHwAddrMode, FieldValue).toUInt() + != uint(OstProto::Arp::kFixed)) + { + count = AbstractProtocol::lcm(count, + fieldData(arp_senderHwAddrCount, FieldValue).toUInt()); + } + + if (fieldData(arp_senderProtoAddrMode, FieldValue).toUInt() + != uint(OstProto::Arp::kFixed)) + { + count = AbstractProtocol::lcm(count, + fieldData(arp_senderProtoAddrCount, FieldValue).toUInt()); + } + + if (fieldData(arp_targetHwAddrMode, FieldValue).toUInt() + != uint(OstProto::Arp::kFixed)) + { + count = AbstractProtocol::lcm(count, + fieldData(arp_targetHwAddrCount, FieldValue).toUInt()); + } + + if (fieldData(arp_targetProtoAddrMode, FieldValue).toUInt() + != uint(OstProto::Arp::kFixed)) + { + count = AbstractProtocol::lcm(count, + fieldData(arp_targetProtoAddrCount, FieldValue).toUInt()); + } + + return count; } QWidget* ArpProtocol::configWidget() diff --git a/common/arp.h b/common/arp.h index 677b73a..d5582d2 100644 --- a/common/arp.h +++ b/common/arp.h @@ -111,6 +111,7 @@ public: FieldAttrib attrib = FieldValue); virtual bool isProtocolFrameValueVariable() const; + virtual int protocolFrameVariableCount() const; virtual QWidget* configWidget(); virtual void loadConfigWidget(); diff --git a/common/comboprotocol.h b/common/comboprotocol.h index 29cf71d..bc148d3 100644 --- a/common/comboprotocol.h +++ b/common/comboprotocol.h @@ -169,6 +169,12 @@ public: return (protoA->isProtocolFrameSizeVariable() || protoB->isProtocolFrameSizeVariable()); } + virtual int protocolFrameVariableCount() const + { + return AbstractProtocol::lcm( + protoA->protocolFrameVariableCount(), + protoB->protocolFrameVariableCount()); + } virtual quint32 protocolFrameCksum(int streamIndex = 0, CksumType cksumType = CksumIp) const diff --git a/common/dot3.cpp b/common/dot3.cpp index ea5f6a5..6b7afb0 100644 --- a/common/dot3.cpp +++ b/common/dot3.cpp @@ -203,6 +203,11 @@ bool Dot3Protocol::isProtocolFrameValueVariable() const return isProtocolFramePayloadSizeVariable(); } +int Dot3Protocol::protocolFrameVariableCount() const +{ + return protocolFramePayloadVariableCount(); +} + QWidget* Dot3Protocol::configWidget() { if (configForm == NULL) diff --git a/common/dot3.h b/common/dot3.h index 403a2ce..22c3f5b 100644 --- a/common/dot3.h +++ b/common/dot3.h @@ -70,6 +70,7 @@ public: FieldAttrib attrib = FieldValue); virtual bool isProtocolFrameValueVariable() const; + virtual int protocolFrameVariableCount() const; virtual QWidget* configWidget(); virtual void loadConfigWidget(); diff --git a/common/gmp.cpp b/common/gmp.cpp index 7bc7ced..a7b4a3d 100755 --- a/common/gmp.cpp +++ b/common/gmp.cpp @@ -925,6 +925,25 @@ bool GmpProtocol::isProtocolFrameValueVariable() const return false; } +int GmpProtocol::protocolFrameVariableCount() const +{ + int count = 1; + + // No fields vary for Ssm Query and Report + if (isSsmReport() || isSsmQuery()) + return count; + + // For all other msg types, check the group mode + if (fieldData(kGroupMode, FieldValue).toUInt() + != uint(OstProto::Gmp::kFixed)) + { + count = AbstractProtocol::lcm(count, + fieldData(kGroupCount, FieldValue).toUInt()); + } + + return count; +} + void GmpProtocol::loadConfigWidget() { configWidget(); diff --git a/common/gmp.h b/common/gmp.h index 97dc5fd..7f3fd29 100755 --- a/common/gmp.h +++ b/common/gmp.h @@ -80,6 +80,7 @@ public: virtual int protocolFrameSize(int streamIndex = 0) const; virtual bool isProtocolFrameValueVariable() const; + virtual int protocolFrameVariableCount() const; virtual void loadConfigWidget(); virtual void storeConfigWidget(); diff --git a/common/ip6.cpp b/common/ip6.cpp index fe5d29b..266c8c2 100644 --- a/common/ip6.cpp +++ b/common/ip6.cpp @@ -808,6 +808,19 @@ bool Ip6Protocol::isProtocolFrameValueVariable() const return false; } +int Ip6Protocol::protocolFrameVariableCount() const +{ + int count = 1; + + if (data.src_addr_mode() != OstProto::Ip6::kFixed) + count = AbstractProtocol::lcm(count, data.src_addr_count()); + + if (data.dst_addr_mode() != OstProto::Ip6::kFixed) + count = AbstractProtocol::lcm(count, data.dst_addr_count()); + + return count; +} + quint32 Ip6Protocol::protocolFrameCksum(int streamIndex, CksumType cksumType) const { diff --git a/common/ip6.h b/common/ip6.h index 1856bc8..dfaf02d 100644 --- a/common/ip6.h +++ b/common/ip6.h @@ -118,6 +118,7 @@ public: FieldAttrib attrib = FieldValue); virtual bool isProtocolFrameValueVariable() const; + virtual int protocolFrameVariableCount() const; virtual quint32 protocolFrameCksum(int streamIndex = 0, CksumType cksumType = CksumIp) const; diff --git a/common/mac.cpp b/common/mac.cpp index 040f870..788a10d 100644 --- a/common/mac.cpp +++ b/common/mac.cpp @@ -268,6 +268,18 @@ bool MacProtocol::isProtocolFrameValueVariable() const return false; } +int MacProtocol::protocolFrameVariableCount() const +{ + int count = 1; + + if (data.dst_mac_mode() != OstProto::Mac::e_mm_fixed) + count = AbstractProtocol::lcm(count, data.dst_mac_count()); + + if (data.src_mac_mode() != OstProto::Mac::e_mm_fixed) + count = AbstractProtocol::lcm(count, data.src_mac_count()); + + return count; +} QWidget* MacProtocol::configWidget() { diff --git a/common/mac.h b/common/mac.h index 48d0e35..32cd7e2 100644 --- a/common/mac.h +++ b/common/mac.h @@ -81,6 +81,7 @@ public: FieldAttrib attrib = FieldValue); virtual bool isProtocolFrameValueVariable() const; + virtual int protocolFrameVariableCount() const; virtual QWidget* configWidget(); virtual void loadConfigWidget(); diff --git a/common/payload.cpp b/common/payload.cpp index d5fb2e5..7fd0dd5 100644 --- a/common/payload.cpp +++ b/common/payload.cpp @@ -226,6 +226,32 @@ bool PayloadProtocol::isProtocolFrameSizeVariable() const return true; } +int PayloadProtocol::protocolFrameVariableCount() const +{ + int count = 1; + + if (data.pattern_mode() == OstProto::Payload::e_dp_random) + { + switch(mpStream->sendUnit()) + { + case OstProto::StreamControl::e_su_packets: + return mpStream->numPackets(); + + case OstProto::StreamControl::e_su_bursts: + return int(mpStream->numBursts() + * mpStream->burstSize() + * mpStream->burstRate()); + } + } + + if (mpStream->lenMode() != StreamBase::e_fl_fixed) + { + count = AbstractProtocol::lcm(count, + mpStream->frameLenMax() - mpStream->frameLenMin() + 1); + } + + return count; +} QWidget* PayloadProtocol::configWidget() { diff --git a/common/payload.h b/common/payload.h index 2fd32d2..822ee37 100644 --- a/common/payload.h +++ b/common/payload.h @@ -75,6 +75,7 @@ public: virtual bool isProtocolFrameValueVariable() const; virtual bool isProtocolFrameSizeVariable() const; + virtual int protocolFrameVariableCount() const; virtual QWidget* configWidget(); virtual void loadConfigWidget(); diff --git a/common/sample.cpp b/common/sample.cpp index 2a79660..42f9f32 100644 --- a/common/sample.cpp +++ b/common/sample.cpp @@ -473,6 +473,18 @@ bool SampleProtocol::isProtocolFrameSizeVariable() const return false; } +/*! + TODO: If your protocol frame has any variable fields or has a variable + size, return the minimum number of frames required to vary the fields \n + + Otherwise you don't need to reimplement this method - the base class always + returns 1 +*/ +int SampleProtocol::protocolFrameVariableCount() const +{ + return 1; +} + QWidget* SampleProtocol::configWidget() { /* Lazy creation of the configWidget */ diff --git a/common/sample.h b/common/sample.h index 91e6573..90b9be8 100644 --- a/common/sample.h +++ b/common/sample.h @@ -93,6 +93,7 @@ public: virtual bool isProtocolFrameValueVariable() const; virtual bool isProtocolFrameSizeVariable() const; + virtual int protocolFrameVariableCount() const; virtual QWidget* configWidget(); virtual void loadConfigWidget(); diff --git a/common/tcp.cpp b/common/tcp.cpp index 02faa6a..c08ca74 100644 --- a/common/tcp.cpp +++ b/common/tcp.cpp @@ -606,6 +606,14 @@ bool TcpProtocol::isProtocolFrameValueVariable() const return isProtocolFramePayloadValueVariable(); } +int TcpProtocol::protocolFrameVariableCount() const +{ + if (data.is_override_cksum()) + return 1; + + return protocolFramePayloadVariableCount(); +} + QWidget* TcpProtocol::configWidget() { if (configForm == NULL) diff --git a/common/tcp.h b/common/tcp.h index 265937a..9addef0 100644 --- a/common/tcp.h +++ b/common/tcp.h @@ -91,6 +91,7 @@ public: FieldAttrib attrib = FieldValue); virtual bool isProtocolFrameValueVariable() const; + virtual int protocolFrameVariableCount() const; virtual QWidget* configWidget(); virtual void loadConfigWidget(); diff --git a/common/udp.cpp b/common/udp.cpp index 8856751..5a4e14b 100644 --- a/common/udp.cpp +++ b/common/udp.cpp @@ -433,6 +433,14 @@ bool UdpProtocol::isProtocolFrameValueVariable() const return isProtocolFramePayloadValueVariable(); } +int UdpProtocol::protocolFrameVariableCount() const +{ + if (data.is_override_totlen() && data.is_override_cksum()) + return 1; + + return protocolFramePayloadVariableCount(); +} + QWidget* UdpProtocol::configWidget() { if (configForm == NULL) diff --git a/common/udp.h b/common/udp.h index 623e999..7bdf200 100644 --- a/common/udp.h +++ b/common/udp.h @@ -78,6 +78,7 @@ public: FieldAttrib attrib = FieldValue); virtual bool isProtocolFrameValueVariable() const; + virtual int protocolFrameVariableCount() const; virtual QWidget* configWidget(); virtual void loadConfigWidget(); diff --git a/common/userscript.cpp b/common/userscript.cpp index fa084f1..fece963 100644 --- a/common/userscript.cpp +++ b/common/userscript.cpp @@ -271,6 +271,11 @@ bool UserScriptProtocol::isProtocolFrameSizeVariable() const return userProtocol_.isProtocolFrameSizeVariable(); } +int UserScriptProtocol::protocolFrameVariableCount() const +{ + return userProtocol_.protocolFrameVariableCount(); +} + quint32 UserScriptProtocol::protocolFrameCksum(int streamIndex, CksumType cksumType) const { @@ -532,6 +537,7 @@ void UserProtocol::reset() name_ = QString(); protocolFrameValueVariable_ = false; protocolFrameSizeVariable_ = false; + protocolFrameVariableCount_ = 1; } QString UserProtocol::name() const @@ -564,6 +570,16 @@ void UserProtocol::setProtocolFrameSizeVariable(bool variable) protocolFrameSizeVariable_ = variable; } +int UserProtocol::protocolFrameVariableCount() const +{ + return protocolFrameVariableCount_; +} + +void UserProtocol::setProtocolFrameVariableCount(int count) +{ + protocolFrameVariableCount_ = count; +} + quint32 UserProtocol::payloadProtocolId(UserProtocol::ProtocolIdType type) const { return parent_->payloadProtocolId( @@ -590,6 +606,11 @@ bool UserProtocol::isProtocolFramePayloadSizeVariable() const return parent_->isProtocolFramePayloadSizeVariable(); } +int UserProtocol::protocolFramePayloadVariableCount() const +{ + return parent_->protocolFramePayloadVariableCount(); +} + quint32 UserProtocol::protocolFrameHeaderCksum(int streamIndex, AbstractProtocol::CksumType cksumType) const { diff --git a/common/userscript.h b/common/userscript.h index 4ef0d91..99ac226 100644 --- a/common/userscript.h +++ b/common/userscript.h @@ -42,13 +42,17 @@ class UserProtocol : public QObject Q_PROPERTY(bool protocolFrameSizeVariable READ isProtocolFrameSizeVariable WRITE setProtocolFrameSizeVariable); + Q_PROPERTY(int protocolFrameVariableCount + READ protocolFrameVariableCount + WRITE setProtocolFrameVariableCount); public: enum ProtocolIdType { ProtocolIdLlc = AbstractProtocol::ProtocolIdLlc, ProtocolIdEth = AbstractProtocol::ProtocolIdEth, - ProtocolIdIp = AbstractProtocol::ProtocolIdIp + ProtocolIdIp = AbstractProtocol::ProtocolIdIp, + ProtocolIdTcpUdp = AbstractProtocol::ProtocolIdTcpUdp }; enum CksumType @@ -70,6 +74,8 @@ public slots: void setProtocolFrameValueVariable(bool variable); bool isProtocolFrameSizeVariable() const; void setProtocolFrameSizeVariable(bool variable); + int protocolFrameVariableCount() const; + void setProtocolFrameVariableCount(int count); quint32 payloadProtocolId(UserProtocol::ProtocolIdType type) const; int protocolFrameOffset(int streamIndex = 0) const; @@ -77,6 +83,7 @@ public slots: bool isProtocolFramePayloadValueVariable() const; bool isProtocolFramePayloadSizeVariable() const; + int protocolFramePayloadVariableCount() const; quint32 protocolFrameHeaderCksum(int streamIndex = 0, AbstractProtocol::CksumType cksumType = AbstractProtocol::CksumIp) const; @@ -87,8 +94,9 @@ private: AbstractProtocol *parent_; QString name_; - bool protocolFrameValueVariable_; - bool protocolFrameSizeVariable_; + bool protocolFrameValueVariable_; + bool protocolFrameSizeVariable_; + int protocolFrameVariableCount_; }; @@ -141,6 +149,7 @@ public: virtual bool isProtocolFrameValueVariable() const; virtual bool isProtocolFrameSizeVariable() const; + virtual int protocolFrameVariableCount() const; virtual quint32 protocolFrameCksum(int streamIndex = 0, CksumType cksumType = CksumIp) const; diff --git a/server/pcapport.cpp b/server/pcapport.cpp index 3f38ed2..d31291b 100644 --- a/server/pcapport.cpp +++ b/server/pcapport.cpp @@ -298,6 +298,8 @@ void PcapPort::PortTransmitter::clearPacketList() repeatSize_ = 0; packetCount_ = 0; + returnToQIdx_ = -1; + setPacketListLoopMode(false, 0, 0); }