From 46449a02a3bd44dc2b26fe29d25cfef1b70b76d6 Mon Sep 17 00:00:00 2001 From: Srivats P Date: Tue, 1 Nov 2016 16:30:13 +0530 Subject: [PATCH] sign: fix PayloadCksum for odd boundary between protocols --- common/abstractprotocol.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/common/abstractprotocol.cpp b/common/abstractprotocol.cpp index 15f7d15..78e959b 100644 --- a/common/abstractprotocol.cpp +++ b/common/abstractprotocol.cpp @@ -22,6 +22,8 @@ along with this program. If not, see #include "protocollistiterator.h" #include "streambase.h" +#include "byteswap.h" + #include /*! @@ -972,18 +974,29 @@ out: quint32 AbstractProtocol::protocolFramePayloadCksum(int streamIndex, CksumType cksumType, CksumScope cksumScope) const { - quint32 sum = 0; + quint32 sum; quint16 cksum; AbstractProtocol *p = next; Q_ASSERT(cksumType == CksumIp); + if (!p) + return 0xFFFF; + + cksum = p->protocolFrameCksum(streamIndex, cksumType); + sum = (quint16) ~cksum; + if (cksumScope == CksumScopeAdjacentProtocol) + goto out; + + p = p->next; while (p) { + // when combining cksums, a non-first protocol starting at odd offset + // needs a byte swap (see RFC 1071 section(s) 2A, 2B) cksum = p->protocolFrameCksum(streamIndex, cksumType); + if (p->protocolFrameOffset(streamIndex) & 0x1) + cksum = swap16(cksum); sum += (quint16) ~cksum; - if (cksumScope == CksumScopeAdjacentProtocol) - goto out; p = p->next; } @@ -998,7 +1011,9 @@ out: while(sum>>16) sum = (sum & 0xFFFF) + (sum >> 16); - return (quint16) ~sum; + cksum = (quint16) ~sum; + qDebug("%s: cksum = %u", __FUNCTION__, cksum); + return cksum; } // Stein's binary GCD algo - from wikipedia