| 476 | } |
| 477 | |
| 478 | auto EncryptedConnection::processPacket( |
| 479 | const rtc::Buffer &fullBuffer, |
| 480 | uint32_t packetSeq) |
| 481 | -> absl::optional<DecryptedPacket> { |
| 482 | assert(fullBuffer.size() >= 5); |
| 483 | |
| 484 | auto additionalMessage = false; |
| 485 | auto firstMessageRequiringAck = true; |
| 486 | auto newRequiringAckReceived = false; |
| 487 | |
| 488 | auto currentSeq = packetSeq; |
| 489 | auto currentCounter = CounterFromSeq(currentSeq); |
| 490 | rtc::ByteBufferReader reader(rtc::ArrayView<const uint8_t>( |
| 491 | reinterpret_cast<const uint8_t *>(fullBuffer.data() + 4), // Skip seq. |
| 492 | fullBuffer.size() - 4)); |
| 493 | |
| 494 | auto result = absl::optional<DecryptedPacket>(); |
| 495 | while (true) { |
| 496 | const auto type = uint8_t(*reader.Data()); |
| 497 | const auto singleMessagePacket = ((currentSeq & kSingleMessagePacketSeqBit) != 0); |
| 498 | if (singleMessagePacket && additionalMessage) { |
| 499 | return LogError("Single message packet bit in not first message."); |
| 500 | } |
| 501 | |
| 502 | if (type == kEmptyId) { |
| 503 | if (additionalMessage) { |
| 504 | return LogError("Empty message should be only the first one in the packet."); |
| 505 | } |
| 506 | RTC_LOG(LS_INFO) << logHeader() |
| 507 | << "Got RECV:empty" << "#" << currentCounter; |
| 508 | reader.Consume(1); |
| 509 | } else if (type == kAckId) { |
| 510 | if (!additionalMessage) { |
| 511 | return LogError("Ack message must not be the first one in the packet."); |
| 512 | } |
| 513 | ackMyMessage(currentSeq); |
| 514 | reader.Consume(1); |
| 515 | } else if (auto message = DeserializeMessage(reader, singleMessagePacket)) { |
| 516 | const auto messageRequiresAck = ((currentSeq & kMessageRequiresAckSeqBit) != 0); |
| 517 | const auto skipMessage = messageRequiresAck |
| 518 | ? !registerSentAck(currentCounter, firstMessageRequiringAck) |
| 519 | : (additionalMessage && !registerIncomingCounter(currentCounter)); |
| 520 | if (messageRequiresAck) { |
| 521 | firstMessageRequiringAck = false; |
| 522 | if (!skipMessage) { |
| 523 | newRequiringAckReceived = true; |
| 524 | } |
| 525 | sendAckPostponed(currentSeq); |
| 526 | RTC_LOG(LS_INFO) << logHeader() |
| 527 | << (skipMessage ? "Repeated RECV:type" : "Got RECV:type") << type << "#" << currentCounter; |
| 528 | } |
| 529 | if (!skipMessage) { |
| 530 | appendReceivedMessage(result, std::move(*message), currentSeq); |
| 531 | } |
| 532 | } else { |
| 533 | return LogError("Could not parse message from packet, type: ", std::to_string(type)); |
| 534 | } |
| 535 | if (!reader.Length()) { |
nothing calls this directly
no test coverage detected