11cb0ef41Sopenharmony_ci#pragma once
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
41cb0ef41Sopenharmony_ci#if HAVE_OPENSSL && NODE_OPENSSL_HAS_QUIC
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci#include <env.h>
71cb0ef41Sopenharmony_ci#include <ngtcp2/ngtcp2.h>
81cb0ef41Sopenharmony_ci#include <node_sockaddr.h>
91cb0ef41Sopenharmony_ci#include <optional>
101cb0ef41Sopenharmony_ci#include "bindingdata.h"
111cb0ef41Sopenharmony_ci#include "cid.h"
121cb0ef41Sopenharmony_ci#include "data.h"
131cb0ef41Sopenharmony_ci#include "tokens.h"
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cinamespace node {
161cb0ef41Sopenharmony_cinamespace quic {
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciclass Endpoint;
191cb0ef41Sopenharmony_ciclass Session;
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci// The Transport Params are the set of configuration options that are sent to
221cb0ef41Sopenharmony_ci// the remote peer. They communicate the protocol options the other peer
231cb0ef41Sopenharmony_ci// should use when communicating with this session.
241cb0ef41Sopenharmony_ciclass TransportParams final {
251cb0ef41Sopenharmony_ci public:
261cb0ef41Sopenharmony_ci  enum class Type {
271cb0ef41Sopenharmony_ci    CLIENT_HELLO = NGTCP2_TRANSPORT_PARAMS_TYPE_CLIENT_HELLO,
281cb0ef41Sopenharmony_ci    ENCRYPTED_EXTENSIONS = NGTCP2_TRANSPORT_PARAMS_TYPE_ENCRYPTED_EXTENSIONS,
291cb0ef41Sopenharmony_ci  };
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  static constexpr uint64_t DEFAULT_MAX_STREAM_DATA_BIDI_LOCAL = 256 * 1024;
321cb0ef41Sopenharmony_ci  static constexpr uint64_t DEFAULT_MAX_STREAM_DATA_BIDI_REMOTE = 256 * 1024;
331cb0ef41Sopenharmony_ci  static constexpr uint64_t DEFAULT_MAX_STREAM_DATA_UNI = 256 * 1024;
341cb0ef41Sopenharmony_ci  static constexpr uint64_t DEFAULT_MAX_DATA = 1 * 1024 * 1024;
351cb0ef41Sopenharmony_ci  static constexpr uint64_t DEFAULT_MAX_IDLE_TIMEOUT = 10;  // seconds
361cb0ef41Sopenharmony_ci  static constexpr uint64_t DEFAULT_MAX_STREAMS_BIDI = 100;
371cb0ef41Sopenharmony_ci  static constexpr uint64_t DEFAULT_MAX_STREAMS_UNI = 3;
381cb0ef41Sopenharmony_ci  static constexpr uint64_t DEFAULT_ACTIVE_CONNECTION_ID_LIMIT = 2;
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  struct Config {
411cb0ef41Sopenharmony_ci    Side side;
421cb0ef41Sopenharmony_ci    const CID& ocid;
431cb0ef41Sopenharmony_ci    const CID& retry_scid;
441cb0ef41Sopenharmony_ci    Config(Side side,
451cb0ef41Sopenharmony_ci           const CID& ocid = CID::kInvalid,
461cb0ef41Sopenharmony_ci           const CID& retry_scid = CID::kInvalid);
471cb0ef41Sopenharmony_ci  };
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  struct Options {
501cb0ef41Sopenharmony_ci    // Set only on server Sessions, the preferred address communicates the IP
511cb0ef41Sopenharmony_ci    // address and port that the server would prefer the client to use when
521cb0ef41Sopenharmony_ci    // communicating with it. See the QUIC specification for more detail on how
531cb0ef41Sopenharmony_ci    // the preferred address mechanism works.
541cb0ef41Sopenharmony_ci    std::optional<SocketAddress> preferred_address_ipv4{};
551cb0ef41Sopenharmony_ci    std::optional<SocketAddress> preferred_address_ipv6{};
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci    // The initial size of the flow control window of locally initiated streams.
581cb0ef41Sopenharmony_ci    // This is the maximum number of bytes that the *remote* endpoint can send
591cb0ef41Sopenharmony_ci    // when the connection is started.
601cb0ef41Sopenharmony_ci    uint64_t initial_max_stream_data_bidi_local =
611cb0ef41Sopenharmony_ci        DEFAULT_MAX_STREAM_DATA_BIDI_LOCAL;
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci    // The initial size of the flow control window of remotely initiated
641cb0ef41Sopenharmony_ci    // streams. This is the maximum number of bytes that the remote endpoint can
651cb0ef41Sopenharmony_ci    // send when the connection is started.
661cb0ef41Sopenharmony_ci    uint64_t initial_max_stream_data_bidi_remote =
671cb0ef41Sopenharmony_ci        DEFAULT_MAX_STREAM_DATA_BIDI_REMOTE;
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    // The initial size of the flow control window of remotely initiated
701cb0ef41Sopenharmony_ci    // unidirectional streams. This is the maximum number of bytes that the
711cb0ef41Sopenharmony_ci    // remote endpoint can send when the connection is started.
721cb0ef41Sopenharmony_ci    uint64_t initial_max_stream_data_uni = DEFAULT_MAX_STREAM_DATA_UNI;
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci    // The initial size of the session-level flow control window.
751cb0ef41Sopenharmony_ci    uint64_t initial_max_data = DEFAULT_MAX_DATA;
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci    // The initial maximum number of concurrent bidirectional streams the remote
781cb0ef41Sopenharmony_ci    // endpoint is permitted to open.
791cb0ef41Sopenharmony_ci    uint64_t initial_max_streams_bidi = DEFAULT_MAX_STREAMS_BIDI;
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci    // The initial maximum number of concurrent unidirectional streams the
821cb0ef41Sopenharmony_ci    // remote endpoint is permitted to open.
831cb0ef41Sopenharmony_ci    uint64_t initial_max_streams_uni = DEFAULT_MAX_STREAMS_UNI;
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci    // The maximum amount of time that a Session is permitted to remain idle
861cb0ef41Sopenharmony_ci    // before it is silently closed and state is discarded.
871cb0ef41Sopenharmony_ci    uint64_t max_idle_timeout = DEFAULT_MAX_IDLE_TIMEOUT;
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci    // The maximum number of Connection IDs that the peer can store. A single
901cb0ef41Sopenharmony_ci    // Session may have several connection IDs over it's lifetime.
911cb0ef41Sopenharmony_ci    uint64_t active_connection_id_limit = DEFAULT_ACTIVE_CONNECTION_ID_LIMIT;
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci    // Establishes the exponent used in ACK Delay field in the ACK frame. See
941cb0ef41Sopenharmony_ci    // the QUIC specification for details. This is an advanced option that
951cb0ef41Sopenharmony_ci    // should rarely be modified and only if there is really good reason.
961cb0ef41Sopenharmony_ci    uint64_t ack_delay_exponent = NGTCP2_DEFAULT_ACK_DELAY_EXPONENT;
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci    // The maximum amount of time by which the endpoint will delay sending
991cb0ef41Sopenharmony_ci    // acknowledgements. This is an advanced option that should rarely be
1001cb0ef41Sopenharmony_ci    // modified and only if there is a really good reason. It is used to
1011cb0ef41Sopenharmony_ci    // determine how long a Session will wait to determine that a packet has
1021cb0ef41Sopenharmony_ci    // been lost.
1031cb0ef41Sopenharmony_ci    uint64_t max_ack_delay = NGTCP2_DEFAULT_MAX_ACK_DELAY;
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci    // The maximum size of DATAGRAM frames that the endpoint will accept.
1061cb0ef41Sopenharmony_ci    // Setting the value to 0 will disable DATAGRAM support.
1071cb0ef41Sopenharmony_ci    uint64_t max_datagram_frame_size = kDefaultMaxPacketLength;
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci    // When true, communicates that the Session does not support active
1101cb0ef41Sopenharmony_ci    // connection migration. See the QUIC specification for more details on
1111cb0ef41Sopenharmony_ci    // connection migration.
1121cb0ef41Sopenharmony_ci    bool disable_active_migration = false;
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci    static v8::Maybe<const Options> From(Environment* env,
1151cb0ef41Sopenharmony_ci                                         v8::Local<v8::Value> value);
1161cb0ef41Sopenharmony_ci  };
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci  explicit TransportParams(Type type);
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci  // Creates an instance of TransportParams wrapping the existing const
1211cb0ef41Sopenharmony_ci  // ngtcp2_transport_params pointer.
1221cb0ef41Sopenharmony_ci  TransportParams(Type type, const ngtcp2_transport_params* ptr);
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci  TransportParams(const Config& config, const Options& options);
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci  // Creates an instance of TransportParams by decoding the given buffer.
1271cb0ef41Sopenharmony_ci  // If the parameters cannot be successfully decoded, the error()
1281cb0ef41Sopenharmony_ci  // property will be set with an appropriate QuicError and the bool()
1291cb0ef41Sopenharmony_ci  // operator will return false.
1301cb0ef41Sopenharmony_ci  TransportParams(Type type, const ngtcp2_vec& buf);
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  void GenerateStatelessResetToken(const TokenSecret& token_secret,
1331cb0ef41Sopenharmony_ci                                   const CID& cid);
1341cb0ef41Sopenharmony_ci  CID GeneratePreferredAddressToken(const Session& session);
1351cb0ef41Sopenharmony_ci  void SetPreferredAddress(const SocketAddress& address);
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci  Type type() const;
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci  operator const ngtcp2_transport_params&() const;
1401cb0ef41Sopenharmony_ci  operator const ngtcp2_transport_params*() const;
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci  operator bool() const;
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci  const QuicError& error() const;
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci  // Returns an ArrayBuffer containing the encoded transport parameters.
1471cb0ef41Sopenharmony_ci  // If an error occurs during encoding, an empty shared_ptr will be returned
1481cb0ef41Sopenharmony_ci  // and the error() property will be set to an appropriate QuicError.
1491cb0ef41Sopenharmony_ci  Store Encode(Environment* env);
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci private:
1521cb0ef41Sopenharmony_ci  Type type_;
1531cb0ef41Sopenharmony_ci  ngtcp2_transport_params params_{};
1541cb0ef41Sopenharmony_ci  const ngtcp2_transport_params* ptr_;
1551cb0ef41Sopenharmony_ci  QuicError error_ = QuicError::TRANSPORT_NO_ERROR;
1561cb0ef41Sopenharmony_ci};
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci}  // namespace quic
1591cb0ef41Sopenharmony_ci}  // namespace node
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci#endif  // HAVE_OPENSSL && NODE_OPENSSL_HAS_QUIC
1621cb0ef41Sopenharmony_ci#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
163