chromium/net/third_party/quiche/src/quiche/common/capsule.h

// Copyright (c) 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef QUICHE_COMMON_CAPSULE_H_
#define QUICHE_COMMON_CAPSULE_H_

#include <cstdint>
#include <optional>
#include <string>
#include <vector>

#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/types/variant.h"
#include "quiche/common/platform/api/quiche_export.h"
#include "quiche/common/platform/api/quiche_logging.h"
#include "quiche/common/quiche_buffer_allocator.h"
#include "quiche/common/quiche_ip_address.h"
#include "quiche/web_transport/web_transport.h"

namespace quiche {

enum class CapsuleType : uint64_t {};

QUICHE_EXPORT std::string CapsuleTypeToString(CapsuleType capsule_type);
QUICHE_EXPORT std::ostream& operator<<(std::ostream& os,
                                       const CapsuleType& capsule_type);

// General.
struct QUICHE_EXPORT DatagramCapsule {};

struct QUICHE_EXPORT LegacyDatagramCapsule {};

struct QUICHE_EXPORT LegacyDatagramWithoutContextCapsule {};

// WebTransport over HTTP/3.
struct QUICHE_EXPORT CloseWebTransportSessionCapsule {};
struct QUICHE_EXPORT DrainWebTransportSessionCapsule {};

// MASQUE CONNECT-IP.
struct QUICHE_EXPORT PrefixWithId {};
struct QUICHE_EXPORT IpAddressRange {};

struct QUICHE_EXPORT AddressAssignCapsule {};
struct QUICHE_EXPORT AddressRequestCapsule {};
struct QUICHE_EXPORT RouteAdvertisementCapsule {};
struct QUICHE_EXPORT UnknownCapsule {};

// WebTransport over HTTP/2.
struct QUICHE_EXPORT WebTransportStreamDataCapsule {};
struct QUICHE_EXPORT WebTransportResetStreamCapsule {};
struct QUICHE_EXPORT WebTransportStopSendingCapsule {};
struct QUICHE_EXPORT WebTransportMaxStreamDataCapsule {};
struct QUICHE_EXPORT WebTransportMaxStreamsCapsule {};

// Capsule from RFC 9297.
// IMPORTANT NOTE: Capsule does not own any of the absl::string_view memory it
// points to. Strings saved into a capsule must outlive the capsule object. Any
// code that sees a capsule in a callback needs to either process it immediately
// or perform its own deep copy.
class QUICHE_EXPORT Capsule {};

namespace test {
class CapsuleParserPeer;
}  // namespace test

class QUICHE_EXPORT CapsuleParser {};

// Serializes |capsule| into a newly allocated buffer.
QUICHE_EXPORT quiche::QuicheBuffer SerializeCapsule(
    const Capsule& capsule, quiche::QuicheBufferAllocator* allocator);

// Serializes the header for a datagram of size |datagram_size|.
QUICHE_EXPORT QuicheBuffer SerializeDatagramCapsuleHeader(
    uint64_t datagram_size, QuicheBufferAllocator* allocator);

// Serializes the header for a WT_STREAM or a WT_STREAM_WITH_FIN capsule.
QUICHE_EXPORT QuicheBuffer SerializeWebTransportStreamCapsuleHeader(
    webtransport::StreamId stream_id, bool fin, uint64_t write_size,
    QuicheBufferAllocator* allocator);

}  // namespace quiche

#endif  // QUICHE_COMMON_CAPSULE_H_