Coverage Report

Created: 2026-12-01 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/vquic/ngtcp2_shim.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#if !defined(CURL_DISABLE_HTTP) && defined(USE_NGTCP2) && defined(USE_NGHTTP3)
27
28
#include <ngtcp2/ngtcp2.h>
29
30
#include "vquic/ngtcp2_shim.h"
31
32
static void ntp2_clear_tuple(struct curl_ngtcp2 *ntp2)
33
157k
{
34
157k
  ntp2->path = NULL;
35
157k
  ntp2->pi = NULL;
36
157k
  ntp2->dest = NULL;
37
157k
  ntp2->destlen = 0;
38
157k
  ntp2->ts = 0;
39
157k
}
40
41
static void ntp2_set_normal(struct curl_ngtcp2 *ntp2)
42
155k
{
43
155k
  ntp2_clear_tuple(ntp2);
44
155k
  ntp2->error_rv = 0;
45
155k
  ntp2->state = CURL_NGTCP2_NORMAL;
46
155k
}
47
48
static void ntp2_set_close_only(struct curl_ngtcp2 *ntp2, ngtcp2_ssize rv)
49
2.14k
{
50
2.14k
  ntp2_clear_tuple(ntp2);
51
2.14k
  if((ntp2->state != CURL_NGTCP2_CLOSE_ONLY) ||
52
153
     (ntp2->error_rv >= 0))
53
1.99k
    ntp2->error_rv = (rv < 0) ? rv : NGTCP2_ERR_INVALID_STATE;
54
2.14k
  ntp2->state = CURL_NGTCP2_CLOSE_ONLY;
55
2.14k
}
56
57
static ngtcp2_ssize ntp2_error(const struct curl_ngtcp2 *ntp2)
58
1.55k
{
59
1.55k
  return (ntp2->error_rv < 0) ? ntp2->error_rv : NGTCP2_ERR_INVALID_STATE;
60
1.55k
}
61
62
static bool ntp2_allow_normal(struct curl_ngtcp2 *ntp2)
63
846k
{
64
846k
  if(ntp2->conn && (ntp2->state == CURL_NGTCP2_NORMAL))
65
843k
    return TRUE;
66
3.02k
  if(ntp2->conn && (ntp2->state == CURL_NGTCP2_COALESCING))
67
0
    ntp2_set_close_only(ntp2, NGTCP2_ERR_INVALID_STATE);
68
3.02k
  return FALSE;
69
846k
}
70
71
static bool ntp2_allow_terminal(struct curl_ngtcp2 *ntp2)
72
508
{
73
508
  if(!ntp2->conn)
74
0
    return FALSE;
75
508
  if(ntp2->state == CURL_NGTCP2_COALESCING) {
76
0
    ntp2_set_close_only(ntp2, NGTCP2_ERR_INVALID_STATE);
77
0
    return FALSE;
78
0
  }
79
508
  return TRUE;
80
508
}
81
82
static bool ntp2_allow_shutdown(struct curl_ngtcp2 *ntp2)
83
1.22k
{
84
1.22k
  return ntp2->conn && (ntp2->state != CURL_NGTCP2_CLOSE_ONLY);
85
1.22k
}
86
87
static int ntp2_check_int(struct curl_ngtcp2 *ntp2, int rv)
88
49.5k
{
89
49.5k
  if(rv < NGTCP2_ERR_FATAL)
90
65
    ntp2_set_close_only(ntp2, rv);
91
49.5k
  return (ntp2->state == CURL_NGTCP2_CLOSE_ONLY) ? (int)ntp2_error(ntp2) : rv;
92
49.5k
}
93
94
static int ntp2_check_terminal_int(struct curl_ngtcp2 *ntp2, int rv)
95
30.4k
{
96
30.4k
  if(rv < 0)
97
394
    ntp2_set_close_only(ntp2, rv);
98
30.4k
  return ntp2_check_int(ntp2, rv);
99
30.4k
}
100
101
static ngtcp2_ssize ntp2_check_ssize(struct curl_ngtcp2 *ntp2, ngtcp2_ssize rv)
102
1.90k
{
103
1.90k
  if(rv < NGTCP2_ERR_FATAL)
104
0
    ntp2_set_close_only(ntp2, rv);
105
1.90k
  return (ntp2->state == CURL_NGTCP2_CLOSE_ONLY) ? ntp2_error(ntp2) : rv;
106
1.90k
}
107
108
static bool ntp2_is_continuation(ngtcp2_ssize rv)
109
169k
{
110
169k
  return (rv == NGTCP2_ERR_WRITE_MORE) ||
111
151k
         (rv == NGTCP2_ERR_STREAM_DATA_BLOCKED) ||
112
151k
         (rv == NGTCP2_ERR_STREAM_NOT_FOUND) ||
113
151k
         (rv == NGTCP2_ERR_STREAM_SHUT_WR);
114
169k
}
115
116
bool Curl_ngtcp2_valid(const struct curl_ngtcp2 *ntp2)
117
1.94k
{
118
1.94k
  return ntp2->conn && (ntp2->state != CURL_NGTCP2_CLOSE_ONLY);
119
1.94k
}
120
121
bool Curl_ngtcp2_restricted(const struct curl_ngtcp2 *ntp2)
122
1.96k
{
123
1.96k
  return ntp2->conn && (ntp2->state != CURL_NGTCP2_NORMAL);
124
1.96k
}
125
126
ngtcp2_conn *Curl_ngtcp2_conn(const struct curl_ngtcp2 *ntp2)
127
588k
{
128
588k
  return ntp2->conn;
129
588k
}
130
131
int Curl_ngtcp2_conn_client_new(struct curl_ngtcp2 *ntp2,
132
                                const ngtcp2_cid *dcid, const ngtcp2_cid *scid,
133
                                const ngtcp2_path *path,
134
                                uint32_t client_chosen_version,
135
                                const ngtcp2_callbacks *callbacks,
136
                                const ngtcp2_settings *settings,
137
                                const ngtcp2_transport_params *params,
138
                                const ngtcp2_mem *mem, void *user_data)
139
3.34k
{
140
3.34k
  ngtcp2_conn *conn = NULL;
141
3.34k
  int rv;
142
143
3.34k
  if(ntp2->conn)
144
0
    return NGTCP2_ERR_INVALID_STATE;
145
3.34k
  ntp2_set_normal(ntp2);
146
3.34k
  rv = ngtcp2_conn_client_new(&conn, dcid, scid, path,
147
3.34k
                              client_chosen_version, callbacks, settings,
148
3.34k
                              params, mem, user_data);
149
3.34k
  if(!rv)
150
3.33k
    ntp2->conn = conn;
151
3.34k
  return rv;
152
3.34k
}
153
154
void Curl_ngtcp2_conn_del(struct curl_ngtcp2 *ntp2)
155
3.36k
{
156
3.36k
  if(ntp2->conn)
157
3.33k
    ngtcp2_conn_del(ntp2->conn);
158
3.36k
  memset(ntp2, 0, sizeof(*ntp2));
159
3.36k
}
160
161
int Curl_ngtcp2_conn_decode_and_set_0rtt_transport_params(
162
  struct curl_ngtcp2 *ntp2, const uint8_t *data, size_t datalen)
163
0
{
164
0
  if(!ntp2_allow_normal(ntp2))
165
0
    return (int)ntp2_error(ntp2);
166
0
  return ntp2_check_int(ntp2,
167
0
    ngtcp2_conn_decode_and_set_0rtt_transport_params(ntp2->conn,
168
0
                                                     data, datalen));
169
0
}
170
171
ngtcp2_ssize Curl_ngtcp2_conn_encode_0rtt_transport_params(
172
  struct curl_ngtcp2 *ntp2, uint8_t *dest, size_t destlen)
173
1.90k
{
174
1.90k
  if(!ntp2_allow_normal(ntp2))
175
0
    return ntp2_error(ntp2);
176
1.90k
  return ntp2_check_ssize(ntp2,
177
1.90k
    ngtcp2_conn_encode_0rtt_transport_params(ntp2->conn, dest, destlen));
178
1.90k
}
179
180
void Curl_ngtcp2_conn_extend_max_offset(struct curl_ngtcp2 *ntp2,
181
                                        uint64_t datalen)
182
7.66k
{
183
7.66k
  if(ntp2_allow_normal(ntp2))
184
7.66k
    ngtcp2_conn_extend_max_offset(ntp2->conn, datalen);
185
7.66k
}
186
187
int Curl_ngtcp2_conn_extend_max_stream_offset(struct curl_ngtcp2 *ntp2,
188
                                              int64_t stream_id,
189
                                              uint64_t datalen)
190
9.57k
{
191
9.57k
  if(!ntp2_allow_normal(ntp2))
192
0
    return (int)ntp2_error(ntp2);
193
9.57k
  return ntp2_check_int(ntp2,
194
9.57k
    ngtcp2_conn_extend_max_stream_offset(ntp2->conn, stream_id, datalen));
195
9.57k
}
196
197
const ngtcp2_ccerr *Curl_ngtcp2_conn_get_ccerr(struct curl_ngtcp2 *ntp2)
198
0
{
199
0
  if(!ntp2_allow_terminal(ntp2))
200
0
    return NULL;
201
0
  return ngtcp2_conn_get_ccerr(ntp2->conn);
202
0
}
203
204
uint64_t Curl_ngtcp2_conn_get_cwnd_left(struct curl_ngtcp2 *ntp2)
205
740
{
206
740
  if(!ntp2_allow_normal(ntp2))
207
0
    return 0;
208
740
  return ngtcp2_conn_get_cwnd_left(ntp2->conn);
209
740
}
210
211
ngtcp2_tstamp Curl_ngtcp2_conn_get_expiry(struct curl_ngtcp2 *ntp2)
212
127k
{
213
127k
  if(!ntp2_allow_normal(ntp2))
214
378
    return UINT64_MAX;
215
127k
  return ngtcp2_conn_get_expiry(ntp2->conn);
216
127k
}
217
218
int Curl_ngtcp2_conn_get_handshake_completed(struct curl_ngtcp2 *ntp2)
219
112k
{
220
112k
  if(!ntp2_allow_normal(ntp2))
221
0
    return 0;
222
112k
  return ngtcp2_conn_get_handshake_completed(ntp2->conn);
223
112k
}
224
225
uint64_t Curl_ngtcp2_conn_get_max_data_left(struct curl_ngtcp2 *ntp2)
226
30.6k
{
227
30.6k
  if(!ntp2_allow_normal(ntp2))
228
378
    return 0;
229
30.3k
  return ngtcp2_conn_get_max_data_left(ntp2->conn);
230
30.6k
}
231
232
size_t Curl_ngtcp2_conn_get_max_tx_udp_payload_size(struct curl_ngtcp2 *ntp2)
233
128k
{
234
128k
  if(!ntp2_allow_normal(ntp2))
235
378
    return 0;
236
128k
  return ngtcp2_conn_get_max_tx_udp_payload_size(ntp2->conn);
237
128k
}
238
239
size_t Curl_ngtcp2_conn_get_path_max_tx_udp_payload_size(
240
  struct curl_ngtcp2 *ntp2)
241
237k
{
242
237k
  if(!ntp2_allow_normal(ntp2))
243
378
    return 0;
244
236k
  return ngtcp2_conn_get_path_max_tx_udp_payload_size(ntp2->conn);
245
237k
}
246
247
const ngtcp2_transport_params *Curl_ngtcp2_conn_get_remote_transport_params(
248
  struct curl_ngtcp2 *ntp2)
249
4.03k
{
250
4.03k
  if(!ntp2_allow_normal(ntp2))
251
378
    return NULL;
252
3.66k
  return ngtcp2_conn_get_remote_transport_params(ntp2->conn);
253
4.03k
}
254
255
size_t Curl_ngtcp2_conn_get_send_quantum(struct curl_ngtcp2 *ntp2)
256
128k
{
257
128k
  if(!ntp2_allow_normal(ntp2))
258
378
    return 0;
259
128k
  return ngtcp2_conn_get_send_quantum(ntp2->conn);
260
128k
}
261
262
uint64_t Curl_ngtcp2_conn_get_streams_uni_left(struct curl_ngtcp2 *ntp2)
263
1.96k
{
264
1.96k
  if(!ntp2_allow_normal(ntp2))
265
0
    return 0;
266
1.96k
  return ngtcp2_conn_get_streams_uni_left(ntp2->conn);
267
1.96k
}
268
269
uint8_t Curl_ngtcp2_conn_get_tls_alert(struct curl_ngtcp2 *ntp2)
270
7
{
271
7
  if(!ntp2_allow_terminal(ntp2))
272
0
    return 0;
273
7
  return ngtcp2_conn_get_tls_alert(ntp2->conn);
274
7
}
275
276
#if NGTCP2_VERSION_NUM >= 0x011700
277
int Curl_ngtcp2_conn_get_tls_early_data_rejected2(struct curl_ngtcp2 *ntp2)
278
0
{
279
0
  if(!ntp2_allow_normal(ntp2))
280
0
    return 0;
281
0
  return ngtcp2_conn_get_tls_early_data_rejected2(ntp2->conn);
282
0
}
283
#endif
284
285
int Curl_ngtcp2_conn_handle_expiry(struct curl_ngtcp2 *ntp2,
286
                                   ngtcp2_tstamp ts)
287
294
{
288
294
  if(!ntp2_allow_normal(ntp2))
289
0
    return (int)ntp2_error(ntp2);
290
294
  return ntp2_check_terminal_int(ntp2,
291
294
                                 ngtcp2_conn_handle_expiry(ntp2->conn, ts));
292
294
}
293
294
int Curl_ngtcp2_conn_in_draining_period(struct curl_ngtcp2 *ntp2)
295
501
{
296
501
  if(!ntp2_allow_terminal(ntp2))
297
0
    return 0;
298
501
  return ngtcp2_conn_in_draining_period(ntp2->conn);
299
501
}
300
301
int Curl_ngtcp2_conn_open_bidi_stream(struct curl_ngtcp2 *ntp2,
302
                                      int64_t *pstream_id,
303
                                      void *stream_user_data)
304
1.92k
{
305
1.92k
  if(!ntp2_allow_normal(ntp2))
306
0
    return (int)ntp2_error(ntp2);
307
1.92k
  return ntp2_check_int(ntp2,
308
1.92k
    ngtcp2_conn_open_bidi_stream(ntp2->conn, pstream_id, stream_user_data));
309
1.92k
}
310
311
int Curl_ngtcp2_conn_open_uni_stream(struct curl_ngtcp2 *ntp2,
312
                                     int64_t *pstream_id,
313
                                     void *stream_user_data)
314
5.88k
{
315
5.88k
  if(!ntp2_allow_normal(ntp2))
316
0
    return (int)ntp2_error(ntp2);
317
5.88k
  return ntp2_check_int(ntp2,
318
5.88k
    ngtcp2_conn_open_uni_stream(ntp2->conn, pstream_id, stream_user_data));
319
5.88k
}
320
321
int Curl_ngtcp2_conn_read_pkt(struct curl_ngtcp2 *ntp2,
322
                              const ngtcp2_path *path,
323
                              const ngtcp2_pkt_info *pi, const uint8_t *pkt,
324
                              size_t pktlen, ngtcp2_tstamp ts)
325
30.1k
{
326
30.1k
  if(!ntp2_allow_normal(ntp2))
327
0
    return (int)ntp2_error(ntp2);
328
30.1k
  return ntp2_check_terminal_int(ntp2,
329
30.1k
    ngtcp2_conn_read_pkt(ntp2->conn, path, pi, pkt, pktlen, ts));
330
30.1k
}
331
332
void Curl_ngtcp2_conn_set_keep_alive_timeout(struct curl_ngtcp2 *ntp2,
333
                                             ngtcp2_duration timeout)
334
3.89k
{
335
3.89k
  if(ntp2_allow_normal(ntp2))
336
3.51k
    ngtcp2_conn_set_keep_alive_timeout(ntp2->conn, timeout);
337
3.89k
}
338
339
int Curl_ngtcp2_conn_set_stream_user_data(struct curl_ngtcp2 *ntp2,
340
                                          int64_t stream_id,
341
                                          void *stream_user_data)
342
1.22k
{
343
1.22k
  if(!ntp2_allow_normal(ntp2))
344
378
    return (int)ntp2_error(ntp2);
345
850
  return ntp2_check_int(ntp2,
346
850
    ngtcp2_conn_set_stream_user_data(ntp2->conn, stream_id,
347
850
                                     stream_user_data));
348
1.22k
}
349
350
void Curl_ngtcp2_conn_set_tls_native_handle(struct curl_ngtcp2 *ntp2,
351
                                            void *tls_native_handle)
352
3.33k
{
353
3.33k
  if(ntp2_allow_normal(ntp2))
354
3.33k
    ngtcp2_conn_set_tls_native_handle(ntp2->conn, tls_native_handle);
355
3.33k
}
356
357
int Curl_ngtcp2_conn_shutdown_stream(struct curl_ngtcp2 *ntp2,
358
                                     uint32_t flags, int64_t stream_id,
359
                                     uint64_t app_error_code)
360
1.22k
{
361
1.22k
  if(!ntp2_allow_shutdown(ntp2))
362
378
    return (int)ntp2_error(ntp2);
363
850
  return ntp2_check_int(ntp2,
364
850
    ngtcp2_conn_shutdown_stream(ntp2->conn, flags, stream_id,
365
850
                                app_error_code));
366
1.22k
}
367
368
int Curl_ngtcp2_conn_shutdown_stream_read(struct curl_ngtcp2 *ntp2,
369
                                          uint32_t flags, int64_t stream_id,
370
                                          uint64_t app_error_code)
371
0
{
372
0
  if(!ntp2_allow_shutdown(ntp2))
373
0
    return (int)ntp2_error(ntp2);
374
0
  return ntp2_check_int(ntp2,
375
0
    ngtcp2_conn_shutdown_stream_read(ntp2->conn, flags, stream_id,
376
0
                                     app_error_code));
377
0
}
378
379
int Curl_ngtcp2_conn_shutdown_stream_write(struct curl_ngtcp2 *ntp2,
380
                                           uint32_t flags, int64_t stream_id,
381
                                           uint64_t app_error_code)
382
0
{
383
0
  if(!ntp2_allow_shutdown(ntp2))
384
0
    return (int)ntp2_error(ntp2);
385
0
  return ntp2_check_int(ntp2,
386
0
    ngtcp2_conn_shutdown_stream_write(ntp2->conn, flags, stream_id,
387
0
                                      app_error_code));
388
0
}
389
390
void Curl_ngtcp2_conn_update_pkt_tx_time(struct curl_ngtcp2 *ntp2,
391
                                         ngtcp2_tstamp ts)
392
9.56k
{
393
9.56k
  if(ntp2_allow_normal(ntp2))
394
9.56k
    ngtcp2_conn_update_pkt_tx_time(ntp2->conn, ts);
395
9.56k
}
396
397
ngtcp2_ssize Curl_ngtcp2_conn_write_connection_close(
398
  struct curl_ngtcp2 *ntp2, ngtcp2_path *path, ngtcp2_pkt_info *pi,
399
  uint8_t *dest, size_t destlen, const ngtcp2_ccerr *ccerr,
400
  ngtcp2_tstamp ts)
401
1.66k
{
402
1.66k
  ngtcp2_ssize rv;
403
404
1.66k
  if(!ntp2->conn)
405
0
    return ntp2_error(ntp2);
406
1.66k
  rv = ngtcp2_conn_write_connection_close(ntp2->conn, path, pi, dest,
407
1.66k
                                           destlen, ccerr, ts);
408
1.66k
  if(rv < 0)
409
22
    ntp2_set_close_only(ntp2, rv);
410
1.64k
  else
411
1.64k
    ntp2_set_close_only(ntp2, ntp2->error_rv);
412
1.66k
  return rv;
413
1.66k
}
414
415
ngtcp2_ssize Curl_ngtcp2_conn_writev_stream(
416
  struct curl_ngtcp2 *ntp2, ngtcp2_path *path, ngtcp2_pkt_info *pi,
417
  uint8_t *dest, size_t destlen, ngtcp2_ssize *pdatalen, uint32_t flags,
418
  int64_t stream_id, const ngtcp2_vec *datav, size_t datavcnt,
419
  ngtcp2_tstamp ts)
420
161k
{
421
161k
  bool was_coalescing;
422
161k
  bool may_coalesce;
423
161k
  ngtcp2_ssize rv;
424
425
161k
  if(!ntp2->conn || (ntp2->state == CURL_NGTCP2_CLOSE_ONLY))
426
378
    return ntp2_error(ntp2);
427
428
160k
  was_coalescing = ntp2->state == CURL_NGTCP2_COALESCING;
429
160k
  if(was_coalescing && ((ntp2->path != path) || (ntp2->pi != pi) ||
430
8.96k
                        (ntp2->dest != dest) ||
431
8.96k
                        (ntp2->destlen != destlen) || (ntp2->ts != ts))) {
432
0
    ntp2_set_close_only(ntp2, NGTCP2_ERR_INVALID_ARGUMENT);
433
0
    return ntp2_error(ntp2);
434
0
  }
435
436
160k
  rv = ngtcp2_conn_writev_stream(ntp2->conn, path, pi, dest, destlen,
437
160k
                                  pdatalen, flags, stream_id, datav,
438
160k
                                  datavcnt, ts);
439
160k
  if(rv < NGTCP2_ERR_FATAL)
440
23
    ntp2_set_close_only(ntp2, rv);
441
160k
  if(ntp2->state == CURL_NGTCP2_CLOSE_ONLY)
442
23
    return ntp2_error(ntp2);
443
444
160k
  may_coalesce = was_coalescing || !!(flags & NGTCP2_WRITE_STREAM_FLAG_MORE);
445
160k
  if((rv < 0) && may_coalesce && !ntp2_is_continuation(rv)) {
446
0
    ntp2_set_close_only(ntp2, rv);
447
0
    return rv;
448
0
  }
449
450
160k
  if(may_coalesce && ntp2_is_continuation(rv)) {
451
8.96k
    ntp2->path = path;
452
8.96k
    ntp2->pi = pi;
453
8.96k
    ntp2->dest = dest;
454
8.96k
    ntp2->destlen = destlen;
455
8.96k
    ntp2->ts = ts;
456
8.96k
    ntp2->error_rv = 0;
457
8.96k
    ntp2->state = CURL_NGTCP2_COALESCING;
458
8.96k
  }
459
151k
  else
460
151k
    ntp2_set_normal(ntp2);
461
160k
  return rv;
462
160k
}
463
464
#endif /* !CURL_DISABLE_HTTP && USE_NGTCP2 && USE_NGHTTP3 */