• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 
3 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
4 #if HAVE_OPENSSL && NODE_OPENSSL_HAS_QUIC
5 
6 #include <env.h>
7 #include <memory_tracker.h>
8 #include <nghttp3/nghttp3.h>
9 #include <ngtcp2/ngtcp2.h>
10 #include <node_internals.h>
11 #include <node_sockaddr.h>
12 #include <v8.h>
13 
14 namespace node {
15 namespace quic {
16 
17 struct Path final : public ngtcp2_path {
18   Path(const SocketAddress& local, const SocketAddress& remote);
19 };
20 
21 struct PathStorage final : public ngtcp2_path_storage {
22   PathStorage();
23   operator ngtcp2_path();
24 };
25 
26 class Store final : public MemoryRetainer {
27  public:
28   Store() = default;
29 
30   Store(std::shared_ptr<v8::BackingStore> store,
31         size_t length,
32         size_t offset = 0);
33   Store(std::unique_ptr<v8::BackingStore> store,
34         size_t length,
35         size_t offset = 0);
36 
37   enum class Option {
38     NONE,
39     DETACH,
40   };
41 
42   Store(v8::Local<v8::ArrayBuffer> buffer, Option option = Option::NONE);
43   Store(v8::Local<v8::ArrayBufferView> view, Option option = Option::NONE);
44 
45   v8::Local<v8::Uint8Array> ToUint8Array(Environment* env) const;
46 
47   operator uv_buf_t() const;
48   operator ngtcp2_vec() const;
49   operator nghttp3_vec() const;
50   operator bool() const;
51   size_t length() const;
52 
53   void MemoryInfo(MemoryTracker* tracker) const override;
54   SET_MEMORY_INFO_NAME(Store)
55   SET_SELF_SIZE(Store)
56 
57  private:
58   template <typename T, typename t>
59   T convert() const;
60   std::shared_ptr<v8::BackingStore> store_;
61   size_t length_ = 0;
62   size_t offset_ = 0;
63 };
64 
65 class QuicError final : public MemoryRetainer {
66  public:
67   using error_code = uint64_t;
68 
69   static constexpr error_code QUIC_NO_ERROR = NGTCP2_NO_ERROR;
70   static constexpr error_code QUIC_APP_NO_ERROR = 65280;
71 
72   enum class Type {
73     TRANSPORT = NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT,
74     APPLICATION = NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_APPLICATION,
75     VERSION_NEGOTIATION =
76         NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT_VERSION_NEGOTIATION,
77     IDLE_CLOSE = NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT_IDLE_CLOSE,
78   };
79 
80   static constexpr error_code QUIC_ERROR_TYPE_TRANSPORT =
81       NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT;
82   static constexpr error_code QUIC_ERROR_TYPE_APPLICATION =
83       NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_APPLICATION;
84 
85   explicit QuicError(const std::string_view reason = "");
86   explicit QuicError(const ngtcp2_connection_close_error* ptr);
87   explicit QuicError(const ngtcp2_connection_close_error& error);
88 
89   Type type() const;
90   error_code code() const;
91   const std::string_view reason() const;
92   uint64_t frame_type() const;
93 
94   operator const ngtcp2_connection_close_error&() const;
95   operator const ngtcp2_connection_close_error*() const;
96 
97   // Returns false if the QuicError uses a no_error code with type
98   // transport or application.
99   operator bool() const;
100 
101   bool operator==(const QuicError& other) const;
102   bool operator!=(const QuicError& other) const;
103 
104   void MemoryInfo(MemoryTracker* tracker) const override;
105   SET_MEMORY_INFO_NAME(QuicError)
106   SET_SELF_SIZE(QuicError)
107 
108   std::string ToString() const;
109   v8::MaybeLocal<v8::Value> ToV8Value(Environment* env) const;
110 
111   static QuicError ForTransport(error_code code,
112                                 const std::string_view reason = "");
113   static QuicError ForApplication(error_code code,
114                                   const std::string_view reason = "");
115   static QuicError ForVersionNegotiation(const std::string_view reason = "");
116   static QuicError ForIdleClose(const std::string_view reason = "");
117   static QuicError ForNgtcp2Error(int code, const std::string_view reason = "");
118   static QuicError ForTlsAlert(int code, const std::string_view reason = "");
119 
120   static QuicError FromConnectionClose(ngtcp2_conn* session);
121 
122   static QuicError TRANSPORT_NO_ERROR;
123   static QuicError APPLICATION_NO_ERROR;
124   static QuicError VERSION_NEGOTIATION;
125   static QuicError IDLE_CLOSE;
126   static QuicError INTERNAL_ERROR;
127 
128  private:
129   const uint8_t* reason_c_str() const;
130 
131   std::string reason_;
132   ngtcp2_connection_close_error error_ = ngtcp2_connection_close_error();
133   const ngtcp2_connection_close_error* ptr_ = nullptr;
134 };
135 
136 }  // namespace quic
137 }  // namespace node
138 
139 #endif  // HAVE_OPENSSL && NODE_OPENSSL_HAS_QUIC
140 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
141