/
node_quic_socket-inl.h
208 lines (172 loc) Β· 5.34 KB
/
node_quic_socket-inl.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#ifndef SRC_QUIC_NODE_QUIC_SOCKET_INL_H_
#define SRC_QUIC_NODE_QUIC_SOCKET_INL_H_
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include "node_quic_socket.h"
#include "node_sockaddr.h"
#include "node_quic_session.h"
#include "node_crypto.h"
#include "debug_utils-inl.h"
namespace node {
using crypto::EntropySource;
namespace quic {
std::unique_ptr<QuicPacket> QuicPacket::Create(
const char* diagnostic_label,
size_t len) {
CHECK_LE(len, MAX_PKTLEN);
return std::make_unique<QuicPacket>(diagnostic_label, len);
}
std::unique_ptr<QuicPacket> QuicPacket::Copy(
const std::unique_ptr<QuicPacket>& other) {
return std::make_unique<QuicPacket>(*other.get());
}
void QuicPacket::set_length(size_t len) {
CHECK_LE(len, MAX_PKTLEN);
len_ = len;
}
int QuicEndpoint::Send(
uv_buf_t* buf,
size_t len,
const sockaddr* addr) {
int ret = static_cast<int>(udp_->Send(buf, len, addr));
if (ret == 0)
IncrementPendingCallbacks();
return ret;
}
int QuicEndpoint::ReceiveStart() {
return udp_->RecvStart();
}
int QuicEndpoint::ReceiveStop() {
return udp_->RecvStop();
}
void QuicEndpoint::WaitForPendingCallbacks() {
if (!has_pending_callbacks()) {
listener_->OnEndpointDone(this);
return;
}
waiting_for_callbacks_ = true;
}
void QuicSocket::AssociateCID(
const QuicCID& cid,
const QuicCID& scid) {
if (cid && scid)
dcid_to_scid_[cid] = scid;
}
void QuicSocket::DisassociateCID(const QuicCID& cid) {
if (cid) {
Debug(this, "Removing association for cid %s", cid);
dcid_to_scid_.erase(cid);
}
}
void QuicSocket::AssociateStatelessResetToken(
const StatelessResetToken& token,
BaseObjectPtr<QuicSession> session) {
Debug(this, "Associating stateless reset token %s", token);
token_map_[token] = session;
}
SocketAddress QuicSocket::local_address() const {
DCHECK(preferred_endpoint_);
return preferred_endpoint_->local_address();
}
void QuicSocket::DisassociateStatelessResetToken(
const StatelessResetToken& token) {
Debug(this, "Removing stateless reset token %s", token);
token_map_.erase(token);
}
void QuicSocket::ReceiveStart() {
for (const auto& endpoint : endpoints_)
CHECK_EQ(endpoint->ReceiveStart(), 0);
}
void QuicSocket::ReceiveStop() {
for (const auto& endpoint : endpoints_)
CHECK_EQ(endpoint->ReceiveStop(), 0);
}
void QuicSocket::RemoveSession(
const QuicCID& cid,
const SocketAddress& addr) {
DecrementSocketAddressCounter(addr);
sessions_.erase(cid);
}
void QuicSocket::ReportSendError(int error) {
listener_->OnError(error);
}
void QuicSocket::IncrementStatelessResetCounter(const SocketAddress& addr) {
reset_counts_[addr]++;
}
void QuicSocket::IncrementSocketAddressCounter(const SocketAddress& addr) {
addr_counts_[addr]++;
}
void QuicSocket::DecrementSocketAddressCounter(const SocketAddress& addr) {
auto it = addr_counts_.find(addr);
if (it == std::end(addr_counts_))
return;
it->second--;
// Remove the address if the counter reaches zero again.
if (it->second == 0) {
addr_counts_.erase(addr);
reset_counts_.erase(addr);
}
}
size_t QuicSocket::GetCurrentSocketAddressCounter(const SocketAddress& addr) {
auto it = addr_counts_.find(addr);
return it == std::end(addr_counts_) ? 0 : it->second;
}
size_t QuicSocket::GetCurrentStatelessResetCounter(const SocketAddress& addr) {
auto it = reset_counts_.find(addr);
return it == std::end(reset_counts_) ? 0 : it->second;
}
void QuicSocket::ServerBusy(bool on) {
Debug(this, "Turning Server Busy Response %s", on ? "on" : "off");
state_->server_busy = on ? 1 : 0;
listener_->OnServerBusy();
}
bool QuicSocket::is_diagnostic_packet_loss(double prob) const {
if (LIKELY(prob == 0.0)) return false;
unsigned char c = 255;
EntropySource(&c, 1);
return (static_cast<double>(c) / 255) < prob;
}
void QuicSocket::set_diagnostic_packet_loss(double rx, double tx) {
rx_loss_ = rx;
tx_loss_ = tx;
}
void QuicSocket::set_validated_address(const SocketAddress& addr) {
if (has_option_validate_address_lru()) {
// Remove the oldest item if we've hit the LRU limit
validated_addrs_.push_back(SocketAddress::Hash()(addr));
if (validated_addrs_.size() > kMaxValidateAddressLru)
validated_addrs_.pop_front();
}
}
bool QuicSocket::is_validated_address(const SocketAddress& addr) const {
if (has_option_validate_address_lru()) {
auto res = std::find(std::begin(validated_addrs_),
std::end(validated_addrs_),
SocketAddress::Hash()(addr));
return res != std::end(validated_addrs_);
}
return false;
}
void QuicSocket::AddSession(
const QuicCID& cid,
BaseObjectPtr<QuicSession> session) {
sessions_[cid] = session;
IncrementSocketAddressCounter(session->remote_address());
IncrementStat(
session->is_server() ?
&QuicSocketStats::server_sessions :
&QuicSocketStats::client_sessions);
}
void QuicSocket::AddEndpoint(
BaseObjectPtr<QuicEndpoint> endpoint_,
bool preferred) {
Debug(this, "Adding %sendpoint", preferred ? "preferred " : "");
if (preferred || endpoints_.empty())
preferred_endpoint_ = endpoint_;
endpoints_.emplace_back(endpoint_);
if (state_->server_listening)
endpoint_->ReceiveStart();
}
} // namespace quic
} // namespace node
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_QUIC_NODE_QUIC_SOCKET_INL_H_