2020-03-17 20:11:27 +01:00
|
|
|
#include "PacketStructs.h"
|
2020-03-21 21:33:51 +01:00
|
|
|
#include "../../PrivateDefines.cpp"
|
2020-03-22 21:25:02 +01:00
|
|
|
#include "PacketManager.h"
|
2020-03-17 20:11:27 +01:00
|
|
|
|
|
|
|
using namespace tgvoip;
|
|
|
|
|
2020-03-18 19:54:56 +01:00
|
|
|
bool Packet::parse(const BufferInputStream &in, const VersionInfo &ver)
|
2020-03-17 20:11:27 +01:00
|
|
|
{
|
2020-03-18 19:54:56 +01:00
|
|
|
if (!ver.isNew())
|
2020-03-17 20:11:27 +01:00
|
|
|
{
|
2020-03-19 14:20:01 +01:00
|
|
|
legacy = true;
|
2020-03-18 21:43:44 +01:00
|
|
|
return parseLegacy(in, ver);
|
2020-03-17 20:11:27 +01:00
|
|
|
}
|
2020-03-19 14:20:01 +01:00
|
|
|
|
2020-03-19 17:43:50 +01:00
|
|
|
uint16_t length;
|
|
|
|
uint8_t flags;
|
2020-03-19 14:20:01 +01:00
|
|
|
bool res = in.TryRead(seq) &&
|
|
|
|
in.TryRead(ackSeq) &&
|
|
|
|
in.TryRead(ackMask) &&
|
|
|
|
in.TryRead(flags);
|
|
|
|
if (!res)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
streamId = flags & 3;
|
|
|
|
flags >>= 2;
|
|
|
|
|
2020-03-19 17:43:50 +01:00
|
|
|
if (streamId == StreamId::Extended && !in.TryRead(streamId))
|
|
|
|
return false;
|
2020-03-19 14:20:01 +01:00
|
|
|
if (!(flags & Flags::Len16 ? in.TryRead(length) : in.TryReadCompat<uint8_t>(length)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
eFlags = length >> 11;
|
|
|
|
length &= 0x7FF;
|
|
|
|
|
|
|
|
if (eFlags & EFlags::Fragmented)
|
|
|
|
{
|
|
|
|
if (!in.TryRead(fragmentCount))
|
|
|
|
return false;
|
|
|
|
if (!in.TryRead(fragmentIndex))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = Buffer(length);
|
|
|
|
if (in.TryRead(data))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ((flags & Flags::RecvTS) && !in.TryRead(recvTS))
|
|
|
|
return false;
|
2020-03-19 17:43:50 +01:00
|
|
|
if ((flags & Flags::ExtraEC) && !in.TryRead(extraEC, ver))
|
2020-03-19 14:20:01 +01:00
|
|
|
return false;
|
|
|
|
if ((flags & Flags::ExtraSignaling) && !in.TryRead(extraSignaling, ver))
|
|
|
|
return false;
|
2020-03-19 17:43:50 +01:00
|
|
|
|
2020-03-19 14:20:01 +01:00
|
|
|
return true;
|
2020-03-17 20:11:27 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 17:43:50 +01:00
|
|
|
void Packet::serialize(BufferOutputStream &out, const VersionInfo &ver) const
|
2020-03-17 20:11:27 +01:00
|
|
|
{
|
2020-03-19 17:43:50 +01:00
|
|
|
uint8_t shortStreamId = streamId > StreamId::Extended ? StreamId::Extended : streamId;
|
|
|
|
uint8_t flags = 0;
|
|
|
|
if (data.Length() > 0xFF || eFlags)
|
|
|
|
flags |= Flags::Len16;
|
|
|
|
if (recvTS)
|
|
|
|
flags |= Flags::RecvTS;
|
|
|
|
if (extraEC)
|
|
|
|
flags |= Flags::ExtraEC;
|
|
|
|
if (extraSignaling)
|
|
|
|
flags |= Flags::ExtraSignaling;
|
|
|
|
|
|
|
|
out.WriteUInt32(seq);
|
|
|
|
out.WriteUInt32(ackSeq);
|
|
|
|
out.WriteUInt32(ackMask);
|
|
|
|
out.WriteByte(shortStreamId | (flags << 2));
|
|
|
|
|
|
|
|
if (shortStreamId == StreamId::Extended)
|
|
|
|
out.WriteByte(streamId);
|
|
|
|
|
|
|
|
if (flags & Flags::Len16)
|
|
|
|
out.WriteUInt16(data.Length() | (eFlags << 11));
|
2020-03-17 20:11:27 +01:00
|
|
|
else
|
2020-03-19 17:43:50 +01:00
|
|
|
out.WriteByte(data.Length());
|
2020-03-22 21:25:02 +01:00
|
|
|
|
|
|
|
out.WriteBytes(data);
|
2020-03-19 17:43:50 +01:00
|
|
|
|
|
|
|
if (flags & Flags::RecvTS)
|
|
|
|
out.WriteUInt32(recvTS);
|
|
|
|
if (flags & Flags::ExtraEC)
|
|
|
|
out.Write(extraEC, ver);
|
|
|
|
if (flags & Flags::ExtraSignaling)
|
|
|
|
out.Write(extraSignaling, ver);
|
|
|
|
}
|
2020-03-17 20:11:27 +01:00
|
|
|
|
2020-03-22 21:25:02 +01:00
|
|
|
void Packet::prepare(PacketManager &pm, std::vector<UnacknowledgedExtraData> ¤tExtras)
|
|
|
|
{
|
|
|
|
if (!seq)
|
|
|
|
{
|
|
|
|
seq = pm.nextLocalSeq();
|
|
|
|
ackSeq = pm.getLastRemoteSeq();
|
|
|
|
ackMask = pm.getRemoteAckMask();
|
|
|
|
}
|
|
|
|
extraSignaling.v.clear();
|
|
|
|
for (auto &extra : currentExtras)
|
|
|
|
{
|
|
|
|
extraSignaling.v.push_back(extra.data);
|
|
|
|
extra.firstContainingSeq = seq;
|
|
|
|
}
|
|
|
|
}
|
2020-03-21 21:33:51 +01:00
|
|
|
#include "Legacy.cpp"
|