/* * Copyright 2024 The WebRTC Project Authors. All rights reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ #ifndef P2P_DTLS_DTLS_UTILS_H_ #define P2P_DTLS_DTLS_UTILS_H_ #include #include #include #include #include "absl/container/flat_hash_set.h" #include "api/array_view.h" #include "rtc_base/buffer.h" namespace webrtc { const size_t kDtlsRecordHeaderLen = 13; const size_t kMaxDtlsPacketLen = 2048; bool IsDtlsPacket(ArrayView payload); bool IsDtlsClientHelloPacket(ArrayView payload); bool IsDtlsHandshakePacket(ArrayView payload); uint32_t ComputeDtlsPacketHash(ArrayView dtls_packet); class PacketStash { public: PacketStash() {} void Add(ArrayView packet); bool AddIfUnique(ArrayView packet); void Prune(const absl::flat_hash_set& packet_hashes); void Prune(uint32_t max_size); ArrayView GetNext(); std::vector> GetAll() const; void clear() { packets_.clear(); pos_ = 0; } bool empty() const { return packets_.empty(); } int size() const { return packets_.size(); } static uint32_t Hash(ArrayView packet) { return ComputeDtlsPacketHash(packet); } private: struct StashedPacket { uint32_t hash; std::unique_ptr buffer; }; // This vector will only contain very few items, // so it is appropriate to use a vector rather than // e.g. a hash map. uint32_t pos_ = 0; std::vector packets_; }; } // namespace webrtc #endif // P2P_DTLS_DTLS_UTILS_H_