forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpacket.h
155 lines (128 loc) · 4.52 KB
/
packet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#pragma once
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#if HAVE_OPENSSL && NODE_OPENSSL_HAS_QUIC
#include <base_object.h>
#include <env.h>
#include <ngtcp2/ngtcp2.h>
#include <node_external_reference.h>
#include <node_sockaddr.h>
#include <req_wrap.h>
#include <uv.h>
#include <v8.h>
#include <string>
#include "bindingdata.h"
#include "cid.h"
#include "data.h"
#include "defs.h"
#include "tokens.h"
namespace node::quic {
struct PathDescriptor {
uint32_t version;
const CID& dcid;
const CID& scid;
const SocketAddress& local_address;
const SocketAddress& remote_address;
std::string ToString() const;
};
// A Packet encapsulates serialized outbound QUIC data.
// Packets must never be larger than the path MTU. The
// default QUIC packet maximum length is 1200 bytes,
// which we assume by default. The packet storage will
// be stack allocated up to this size.
//
// Packets are maintained in a freelist held by the
// BindingData instance. When using Create() to create
// a Packet, we'll check to see if there is a free
// packet in the freelist and use it instead of starting
// fresh with a new packet. The freelist can store at
// most kMaxFreeList packets
//
// Packets are always encrypted so their content should
// be considered opaque to us. We leave it entirely up
// to ngtcp2 how to encode QUIC frames into the packet.
class Packet final : public ReqWrap<uv_udp_send_t> {
private:
struct Data;
public:
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
Environment* env);
class Listener {
public:
virtual void PacketDone(int status) = 0;
};
// Do not use the Packet constructors directly to create
// them. These are public only to support MakeBaseObject.
// Use the Create, or Create variants to create or
// acquire packet instances.
Packet(Environment* env,
Listener* listener,
v8::Local<v8::Object> object,
const SocketAddress& destination,
size_t length,
const char* diagnostic_label = "<unknown>");
Packet(Environment* env,
Listener* listener,
v8::Local<v8::Object> object,
const SocketAddress& destination,
std::shared_ptr<Data> data);
DISALLOW_COPY_AND_MOVE(Packet)
const SocketAddress& destination() const;
size_t length() const;
operator uv_buf_t() const;
operator ngtcp2_vec() const;
// Modify the size of the packet after ngtcp2 has written
// to it. len must be <= length(). We call this after we've
// asked ngtcp2 to encode frames into the packet and ngtcp2
// tells us how many of the packets bytes were used.
void Truncate(size_t len);
static BaseObjectPtr<Packet> Create(
Environment* env,
Listener* listener,
const SocketAddress& destination,
size_t length = kDefaultMaxPacketLength,
const char* diagnostic_label = "<unknown>");
BaseObjectPtr<Packet> Clone() const;
void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(Packet)
SET_SELF_SIZE(Packet)
std::string ToString() const;
static BaseObjectPtr<Packet> CreateRetryPacket(
Environment* env,
Listener* listener,
const PathDescriptor& path_descriptor,
const TokenSecret& token_secret);
static BaseObjectPtr<Packet> CreateConnectionClosePacket(
Environment* env,
Listener* listener,
const SocketAddress& destination,
ngtcp2_conn* conn,
const QuicError& error);
static BaseObjectPtr<Packet> CreateImmediateConnectionClosePacket(
Environment* env,
Listener* listener,
const PathDescriptor& path_descriptor,
const QuicError& reason);
static BaseObjectPtr<Packet> CreateStatelessResetPacket(
Environment* env,
Listener* listener,
const PathDescriptor& path_descriptor,
const TokenSecret& token_secret,
size_t source_len);
static BaseObjectPtr<Packet> CreateVersionNegotiationPacket(
Environment* env,
Listener* listener,
const PathDescriptor& path_descriptor);
// Called when the packet is done being sent.
void Done(int status);
private:
static BaseObjectPtr<Packet> FromFreeList(Environment* env,
std::shared_ptr<Data> data,
Listener* listener,
const SocketAddress& destination);
Listener* listener_;
SocketAddress destination_;
std::shared_ptr<Data> data_;
};
} // namespace node::quic
#endif // HAVE_OPENSSL && NODE_OPENSSL_HAS_QUIC
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS