-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathlsquic_conn.c
More file actions
358 lines (277 loc) · 7.5 KB
/
lsquic_conn.c
File metadata and controls
358 lines (277 loc) · 7.5 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc. See LICENSE. */
#include <assert.h>
#include <inttypes.h>
#include <string.h>
#include <sys/queue.h>
#include <openssl/rand.h>
#include "lsquic.h"
#include "lsquic_int_types.h"
#include "lsquic_hash.h"
#include "lsquic_conn.h"
#include "lsquic_packet_common.h"
#include "lsquic_packet_gquic.h"
#include "lsquic_packet_in.h"
#include "lsquic_str.h"
#include "lsquic_enc_sess.h"
#include "lsquic_mm.h"
#include "lsquic_engine_public.h"
#include "lsquic_ev_log.h"
#include "lsquic_logger.h"
const lsquic_cid_t *
lsquic_conn_id (const lsquic_conn_t *lconn)
{
/* TODO */
return lsquic_conn_log_cid(lconn);
}
void *
lsquic_conn_get_peer_ctx (struct lsquic_conn *lconn,
const struct sockaddr *local_sa)
{
const struct network_path *path;
path = lconn->cn_if->ci_get_path(lconn, local_sa);
return path->np_peer_ctx;
}
unsigned char
lsquic_conn_record_sockaddr (lsquic_conn_t *lconn, void *peer_ctx,
const struct sockaddr *local_sa, const struct sockaddr *peer_sa)
{
return lconn->cn_if->ci_record_addrs(lconn, peer_ctx, local_sa, peer_sa);
}
int
lsquic_conn_get_sockaddr (struct lsquic_conn *lconn,
const struct sockaddr **local, const struct sockaddr **peer)
{
const struct network_path *path;
path = lconn->cn_if->ci_get_path(lconn, NULL);
*local = NP_LOCAL_SA(path);
*peer = NP_PEER_SA(path);
return 0;
}
int
lsquic_conn_copy_and_release_pi_data (const lsquic_conn_t *conn,
struct lsquic_engine_public *enpub, lsquic_packet_in_t *packet_in)
{
unsigned char *copy;
assert(!(packet_in->pi_flags & PI_OWN_DATA));
copy = lsquic_mm_get_packet_in_buf(&enpub->enp_mm, packet_in->pi_data_sz);
if (!copy)
{
LSQ_WARN("cannot allocate memory to copy incoming packet data");
return -1;
}
memcpy(copy, packet_in->pi_data, packet_in->pi_data_sz);
packet_in->pi_data = copy;
packet_in->pi_flags |= PI_OWN_DATA;
return 0;
}
enum lsquic_version
lsquic_conn_quic_version (const lsquic_conn_t *lconn)
{
if (lconn->cn_flags & LSCONN_VER_SET)
return lconn->cn_version;
else
return -1;
}
enum lsquic_crypto_ver
lsquic_conn_crypto_ver (const lsquic_conn_t *lconn)
{
return LSQ_CRY_QUIC;
}
const char *
lsquic_conn_crypto_cipher (const lsquic_conn_t *lconn)
{
if (lconn->cn_enc_session)
return lconn->cn_esf_c->esf_cipher(lconn->cn_enc_session);
else
return NULL;
}
int
lsquic_conn_crypto_keysize (const lsquic_conn_t *lconn)
{
if (lconn->cn_enc_session)
return lconn->cn_esf_c->esf_keysize(lconn->cn_enc_session);
else
return -1;
}
int
lsquic_conn_crypto_alg_keysize (const lsquic_conn_t *lconn)
{
if (lconn->cn_enc_session)
return lconn->cn_esf_c->esf_alg_keysize(lconn->cn_enc_session);
else
return -1;
}
struct stack_st_X509 *
lsquic_conn_get_server_cert_chain (struct lsquic_conn *lconn)
{
if (lconn->cn_enc_session)
return lconn->cn_esf_c->esf_get_server_cert_chain(lconn->cn_enc_session);
else
return NULL;
}
struct stack_st_X509 *
lsquic_conn_get_full_cert_chain (struct lsquic_conn *lconn)
{
if (lconn->cn_enc_session)
return lconn->cn_esf_c->esf_get_full_cert_chain(lconn->cn_enc_session);
else
return NULL;
}
void
lsquic_conn_make_stream (struct lsquic_conn *lconn)
{
lconn->cn_if->ci_make_stream(lconn);
}
unsigned
lsquic_conn_n_pending_streams (const struct lsquic_conn *lconn)
{
return lconn->cn_if->ci_n_pending_streams(lconn);
}
unsigned
lsquic_conn_n_avail_streams (const struct lsquic_conn *lconn)
{
return lconn->cn_if->ci_n_avail_streams(lconn);
}
unsigned
lsquic_conn_cancel_pending_streams (struct lsquic_conn *lconn, unsigned count)
{
return lconn->cn_if->ci_cancel_pending_streams(lconn, count);
}
void
lsquic_conn_going_away (struct lsquic_conn *lconn)
{
lconn->cn_if->ci_going_away(lconn);
}
void
lsquic_conn_close (struct lsquic_conn *lconn)
{
lconn->cn_if->ci_close(lconn);
}
int
lsquic_conn_is_push_enabled (lsquic_conn_t *lconn)
{
return lconn->cn_if->ci_is_push_enabled(lconn);
}
struct lsquic_engine *
lsquic_conn_get_engine (struct lsquic_conn *lconn)
{
return lconn->cn_if->ci_get_engine(lconn);
}
int
lsquic_conn_push_stream (struct lsquic_conn *lconn, void *hset,
struct lsquic_stream *stream, const struct lsquic_http_headers *headers)
{
return lconn->cn_if->ci_push_stream(lconn, hset, stream, headers);
}
lsquic_conn_ctx_t *
lsquic_conn_get_ctx (const struct lsquic_conn *lconn)
{
return lconn->cn_conn_ctx;
}
void
lsquic_conn_set_ctx (struct lsquic_conn *lconn, lsquic_conn_ctx_t *ctx)
{
lconn->cn_conn_ctx = ctx;
}
void
lsquic_conn_abort (struct lsquic_conn *lconn)
{
lconn->cn_if->ci_abort(lconn);
}
void
lsquic_generate_cid (lsquic_cid_t *cid, size_t len)
{
if (!len)
{
/* If not set, generate ID between 8 and MAX_CID_LEN bytes in length */
RAND_bytes((uint8_t *) &len, sizeof(len));
len %= MAX_CID_LEN - 7;
len += 8;
}
RAND_bytes(cid->idbuf, len);
cid->len = len;
}
void
lsquic_generate_scid (void *ctx, struct lsquic_conn *lconn, uint8_t *cid,
unsigned len)
{
if (len)
RAND_bytes(cid, len);
}
void
lsquic_generate_cid_gquic (lsquic_cid_t *cid)
{
RAND_bytes(cid->idbuf, GQUIC_CID_LEN);
cid->len = GQUIC_CID_LEN;
}
void
lsquic_conn_retire_cid (struct lsquic_conn *lconn)
{
if (lconn->cn_if->ci_retire_cid)
lconn->cn_if->ci_retire_cid(lconn);
}
enum LSQUIC_CONN_STATUS
lsquic_conn_status (struct lsquic_conn *lconn, char *errbuf, size_t bufsz)
{
return lconn->cn_if->ci_status(lconn, errbuf, bufsz);
}
const lsquic_cid_t *
lsquic_conn_log_cid (const struct lsquic_conn *lconn)
{
return &lconn->cn_logid;
}
int
lsquic_conn_want_datagram_write (struct lsquic_conn *lconn, int is_want)
{
if (lconn->cn_if && lconn->cn_if->ci_want_datagram_write)
return lconn->cn_if->ci_want_datagram_write(lconn, is_want);
else
return -1;
}
int
lsquic_conn_set_min_datagram_size (struct lsquic_conn *lconn, size_t sz)
{
if (lconn->cn_if && lconn->cn_if->ci_set_min_datagram_size)
return lconn->cn_if->ci_set_min_datagram_size(lconn, sz);
else
return -1;
}
size_t
lsquic_conn_get_min_datagram_size (struct lsquic_conn *lconn)
{
if (lconn->cn_if && lconn->cn_if->ci_get_min_datagram_size)
return lconn->cn_if->ci_get_min_datagram_size(lconn);
else
return 0;
}
#if LSQUIC_CONN_STATS
void
lsquic_conn_stats_diff (const struct conn_stats *cumulative_stats,
const struct conn_stats *previous_stats,
struct conn_stats *new_stats)
{
const unsigned long *const cum = (void *) cumulative_stats,
*const prev = (void *) previous_stats;
unsigned long *const new = (void *) new_stats;
unsigned i;
for (i = 0; i < sizeof(*new_stats) / sizeof(new[0]); ++i)
new[i] = cum[i] - prev[i];
}
#endif
const char *
lsquic_conn_get_sni (struct lsquic_conn *lconn)
{
if (lconn->cn_esf_c && lconn->cn_esf_c->esf_get_sni)
return lconn->cn_esf_c->esf_get_sni(lconn->cn_enc_session);
else
return NULL;
}
int
lsquic_conn_get_info (lsquic_conn_t *lconn, struct lsquic_conn_info *info)
{
if (!lconn || !info)
return -1;
if (lconn->cn_if && lconn->cn_if->ci_get_info)
return lconn->cn_if->ci_get_info(lconn, info);
return -1;
}