From b8da2077baccf5732a45f8bf9902bacfd8f65975 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 27 Mar 2020 19:13:37 +0100 Subject: [PATCH] Fix mask --- controller/audio/AudioPacketSender.cpp | 16 ++++ controller/controller/Init.cpp | 1 + controller/protocol/Protocol.cpp | 21 +++-- controller/protocol/packets/Legacy.cpp | 3 + controller/protocol/packets/PacketStructs.cpp | 11 ++- controller/protocol/packets/PacketStructs.h | 3 +- controller/protocol/protocol/Extra.cpp | 2 +- controller/protocol/protocol/Extra.h | 2 +- controller/protocol/protocol/Interface.h | 76 +++++++++++++++---- schemeNew.tlb | 3 +- 10 files changed, 112 insertions(+), 26 deletions(-) diff --git a/controller/audio/AudioPacketSender.cpp b/controller/audio/AudioPacketSender.cpp index 4d2e2cd..f7e3648 100644 --- a/controller/audio/AudioPacketSender.cpp +++ b/controller/audio/AudioPacketSender.cpp @@ -36,6 +36,22 @@ void AudioPacketSender::SendFrame(unsigned char *data, size_t len, unsigned char { ecAudioPackets.pop_front(); } + if (shittyInternetMode) + { + uint8_t maxEC = std::min( + std::min( + static_cast(ecAudioPackets.size()), + static_cast(pkt->seq - 1)), + extraEcLevel); + uint8_t offset = 8 - maxEC; + for (uint8_t i = 0; i < maxEC; i++) + { + if (!packetManager.wasLocalAcked(pkt->seq - (i + 1))) + { + pkt->extraEC.v[offset + i].d = std::make_shared(ecAudioPackets[i]); + } + } + } /* uint8_t fecCount = std::min(static_cast(ecAudioPackets.size()), extraEcLevel); pkt.WriteByte(fecCount); diff --git a/controller/controller/Init.cpp b/controller/controller/Init.cpp index de528b2..adf44c0 100644 --- a/controller/controller/Init.cpp +++ b/controller/controller/Init.cpp @@ -49,6 +49,7 @@ VoIPController::VoIPController() : rawSendQueue(64) void VoIPController::InitializeTimers() { + LOGE("Init timeout is %lf", config.initTimeout); initTimeoutID = messageThread.Post( [this] { LOGW("Init timeout, disconnecting"); diff --git a/controller/protocol/Protocol.cpp b/controller/protocol/Protocol.cpp index ac552fc..4c61de5 100644 --- a/controller/protocol/Protocol.cpp +++ b/controller/protocol/Protocol.cpp @@ -78,17 +78,24 @@ void VoIPController::ProcessIncomingPacket(NetworkPacket &npacket, Endpoint &src LOGW("Failure parsing incoming packet! %s", ver.isNew() ? "(new mode)" : ver.isLegacy() ? "(legacy mode)" : "(legacylegacy mode)"); packet.clear(); in.Seek(offset); - VersionInfo ver(9, 92); + VersionInfo ver(10, 110); if (!packet.parse(in, ver)) { LOGW("Failure parsing incoming packet! %s", ver.isNew() ? "(new mode)" : ver.isLegacy() ? "(legacy mode)" : "(legacylegacy mode)"); packet.clear(); in.Seek(offset); - VersionInfo ver(7, 91); + VersionInfo ver(9, 92); if (!packet.parse(in, ver)) { LOGW("Failure parsing incoming packet! %s", ver.isNew() ? "(new mode)" : ver.isLegacy() ? "(legacy mode)" : "(legacylegacy mode)"); - return; + packet.clear(); + in.Seek(offset); + VersionInfo ver(7, 91); + if (!packet.parse(in, ver)) + { + LOGW("Failure parsing incoming packet! %s", ver.isNew() ? "(new mode)" : ver.isLegacy() ? "(legacy mode)" : "(legacylegacy mode)"); + return; + } } } } @@ -201,7 +208,7 @@ void VoIPController::ProcessIncomingPacket(Packet &packet, Endpoint &srcEndpoint //LOGD("recv: %u -> %.2lf, %.2lf, %.2lf, %.2lf, %.2lf, %.2lf, %.2lf, %.2lf", getLastRemoteSeq(), recvPacketTimes[0], recvPacketTimes[1], recvPacketTimes[2], recvPacketTimes[3], recvPacketTimes[4], recvPacketTimes[5], recvPacketTimes[6], recvPacketTimes[7]); //LOGI("RTT = %.3lf", GetAverageRTT()); //LOGV("Packet %u type is %d", pseq, type); - if (packet.data && packet.data->Length()) + if (packet.data) { if (!receivedFirstStreamPacket) { @@ -249,7 +256,7 @@ void VoIPController::ProcessIncomingPacket(Packet &packet, Endpoint &srcEndpoint { if (packet.extraEC.v[i]) { - stm->jitterBuffer->HandleInput(std::move(packet.extraEC.v[i].get().data), pts - (8 - i) * stm->frameDuration, true); + stm->jitterBuffer->HandleInput(std::move(packet.extraEC.v[i].get().data), pts - (8 - i) * stm->frameDuration, true); } } } @@ -400,8 +407,10 @@ void VoIPController::ProcessExtraData(const Wrapped &_data, Endpoint &src { receivedInitAck = true; + LOGE("Canceling init timeout"); messageThread.Cancel(initTimeoutID); initTimeoutID = MessageThread::INVALID_ID; + LOGE("Canceled init timeout"); ver.peerVersion = data.peerVersion; if (data.minVersion > PROTOCOL_VERSION || data.peerVersion < MIN_PROTOCOL_VERSION) @@ -547,7 +556,7 @@ void VoIPController::ProcessExtraData(const Wrapped &_data, Endpoint &src stm->height = data.height; for (auto &v : data.data) { - stm->codecSpecificData.push_back(std::move(*v.data)); + stm->codecSpecificData.push_back(std::move(*v.get().data)); } } } diff --git a/controller/protocol/packets/Legacy.cpp b/controller/protocol/packets/Legacy.cpp index bfb6f31..8689827 100644 --- a/controller/protocol/packets/Legacy.cpp +++ b/controller/protocol/packets/Legacy.cpp @@ -142,6 +142,9 @@ bool Packet::parseLegacy(const BufferInputStream &in, const VersionInfo &ver) } } } + } else { + LOGW("Got unknown legacy packet type %hhu (probably new packet)", type); + return false; } return true; } diff --git a/controller/protocol/packets/PacketStructs.cpp b/controller/protocol/packets/PacketStructs.cpp index 7fb0a38..1cce4fa 100644 --- a/controller/protocol/packets/PacketStructs.cpp +++ b/controller/protocol/packets/PacketStructs.cpp @@ -11,7 +11,7 @@ bool Packet::parse(const BufferInputStream &in, const VersionInfo &ver) return parseLegacy(in, ver); } - uint16_t length; + uint16_t length = 0; uint8_t flags; bool res = in.TryRead(seq) && in.TryRead(ackSeq) && @@ -23,6 +23,11 @@ bool Packet::parse(const BufferInputStream &in, const VersionInfo &ver) streamId = flags & 3; flags >>= 2; + if (!(flags & Flags::Reserved)) + { + LOGW("No reserved flag in new flag, probably legacy packet"); + return false; + } if (streamId == StreamId::Extended && !in.TryRead(streamId)) return false; if (!(flags & Flags::Len16 ? in.TryRead(length) : in.TryReadCompat(length))) @@ -45,7 +50,7 @@ bool Packet::parse(const BufferInputStream &in, const VersionInfo &ver) if (!in.TryRead(*data)) return false; } - + if ((flags & Flags::RecvTS) && !in.TryRead(recvTS)) return false; if ((flags & Flags::ExtraEC) && !in.TryRead(extraEC, ver)) @@ -59,7 +64,7 @@ bool Packet::parse(const BufferInputStream &in, const VersionInfo &ver) void Packet::serialize(BufferOutputStream &out, const VersionInfo &ver) const { uint8_t shortStreamId = streamId > StreamId::Extended ? StreamId::Extended : streamId; - uint8_t flags = 0; + uint8_t flags = Flags::Reserved; // Always true, allows us to distinguish from legacy packets if the maxLayer is wrong. size_t length = data ? data->Length() : 0; if (length > 0xFF || eFlags) diff --git a/controller/protocol/packets/PacketStructs.h b/controller/protocol/packets/PacketStructs.h index 9cccabc..969022a 100644 --- a/controller/protocol/packets/PacketStructs.h +++ b/controller/protocol/packets/PacketStructs.h @@ -92,7 +92,8 @@ public: Len16 = 1, RecvTS = 2, ExtraEC = 4, - ExtraSignaling = 8 + ExtraSignaling = 8, + Reserved = 16 }; enum EFlags : uint8_t { diff --git a/controller/protocol/protocol/Extra.cpp b/controller/protocol/protocol/Extra.cpp index eaadb6f..c9446bb 100644 --- a/controller/protocol/protocol/Extra.cpp +++ b/controller/protocol/protocol/Extra.cpp @@ -33,7 +33,7 @@ std::shared_ptr Extra::choose(const BufferInputStream &in, const VersionI return nullptr; } unsigned char fullHash[SHA1_LENGTH]; - VoIPController::crypto.sha1(const_cast(in.GetRawBuffer()), in.Remaining(), fullHash); + VoIPController::crypto.sha1(const_cast(in.GetRawBuffer() - in.GetOffset()), in.GetLength(), fullHash); #ifdef LOG_PACKETS LOGE("Got extra ID %hhu", id); diff --git a/controller/protocol/protocol/Extra.h b/controller/protocol/protocol/Extra.h index 4c32d54..f3cebef 100644 --- a/controller/protocol/protocol/Extra.h +++ b/controller/protocol/protocol/Extra.h @@ -146,7 +146,7 @@ public: uint16_t width = 0; uint16_t height = 0; - Array data; + Array> data; uint8_t getID() const { diff --git a/controller/protocol/protocol/Interface.h b/controller/protocol/protocol/Interface.h index d92da67..6b37e6d 100644 --- a/controller/protocol/protocol/Interface.h +++ b/controller/protocol/protocol/Interface.h @@ -96,7 +96,7 @@ struct Mask : public Serializable, SingleChoice> } for (uint8_t i = 0; i < 8; i++) { - if (!(i & (1 << i))) + if (!(mask & (1 << i))) continue; if (!in.TryRead(v.at(i), ver)) @@ -186,9 +186,7 @@ struct Array : public Serializable, SingleChoice> { auto data = T::choose(in, ver); if (!data || !data->parse(in, ver)) - { return false; - } v.push_back(std::move(*data)); } return true; @@ -258,13 +256,17 @@ struct Wrapped : public Serializable, SingleChoice> uint8_t len; if (!in.TryRead(len)) return false; + if (in.Remaining() < len) + return false; #ifdef LOG_PACKETS LOGW("Got buffer of length %hhu", len); #endif auto buf = in.GetPartBuffer(len); d = T::choose(buf, ver); if (!d) + { return false; + } return d->parse(buf, ver); } void serialize(BufferOutputStream &out, const VersionInfo &ver) const override @@ -326,25 +328,25 @@ struct Wrapped : public Serializable, SingleChoice> std::shared_ptr d; }; +class OutputBytes; struct Bytes : public Serializable, - SingleChoice + SingleChoice { virtual ~Bytes() = default; Bytes() = default; - Bytes(Buffer &&_data) : data(std::make_unique(std::move(_data))){}; - Bytes(Bytes &&_data) : data(std::move(_data.data)){}; + bool parse(const BufferInputStream &in, const VersionInfo &ver) { - data = std::make_unique(in.GetLength()); - return in.TryRead(*data); + setData(std::make_unique(in.GetLength())); + return in.TryRead(*getData()); } void serialize(BufferOutputStream &out, const VersionInfo &ver) const override { - out.WriteBytes(*data); + out.WriteBytes(*getData()); } operator bool() { - return data && !data->IsEmpty(); + return getData() && !getData()->IsEmpty(); } std::string print() const override { @@ -352,12 +354,60 @@ struct Bytes : public Serializable, } size_t getSize(const VersionInfo &ver) const override { - return data->Length(); + return getData()->Length(); } - std::unique_ptr data; + + virtual Buffer *getData() = 0; + virtual const Buffer *getData() const = 0; + virtual void setData(std::unique_ptr &&) = 0; }; -struct UInt32 : public Serializable, SingleChoice +struct OutputBytes : public Bytes +{ + virtual ~OutputBytes() = default; + OutputBytes() = default; + OutputBytes(Buffer &&_data) : data(std::make_unique(std::move(_data))){}; + + virtual Buffer *getData() + { + return data.get(); + } + virtual const Buffer *getData() const + { + return data.get(); + } + + virtual void setData(std::unique_ptr &&buf) + { + data = std::move(buf); + } + + std::unique_ptr data; +}; +struct InputBytes : public Bytes +{ + virtual ~InputBytes() = default; + InputBytes() = default; + InputBytes(std::shared_ptr _data) : data(_data){}; + + virtual Buffer *getData() + { + return data.get(); + } + virtual const Buffer *getData() const + { + return data.get(); + } + virtual void setData(std::unique_ptr &&buf) + { + data = std::move(buf); + } + + std::shared_ptr data; +}; + +struct UInt32 : public Serializable, + SingleChoice { virtual ~UInt32() = default; UInt32() = default; diff --git a/schemeNew.tlb b/schemeNew.tlb index 442de61..89ae0db 100644 --- a/schemeNew.tlb +++ b/schemeNew.tlb @@ -25,7 +25,8 @@ packet$_ recvTS:flags.1?int extraFEC:flags.2?ExtraFEC extraSignaling:flags.3?Extras - + reserved:flags.4?true:true // Always true, allows us to distinguish from legacy packets if the maxLayer is wrong. + = Packet; // ExtraFEC