Debian Codegraph @

contrib/libngtcp2/ngtcp2/lib/ngtcp2_qlog.c

raw symbols SCIP
1 /*
2 * ngtcp2
3 *
4 * Copyright (c) 2019 ngtcp2 contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #include "ngtcp2_qlog.h"
26
27 #include <assert.h>
28
29 #include "ngtcp2_str.h"
30 #include "ngtcp2_vec.h"
31 #include "ngtcp2_conv.h"
32 #include "ngtcp2_net.h"
33 #include "ngtcp2_unreachable.h"
34 #include "ngtcp2_conn_stat.h"
35
36 void ngtcp2_qlog_init(ngtcp2_qlog *qlog, ngtcp2_qlog_write write,
37 ngtcp2_tstamp ts, void *user_data) {
38 qlog->write = write;
39 qlog->ts = qlog->last_ts = ts;
40 qlog->user_data = user_data;
41 }
42
43 #define write_verbatim(DEST, S) ngtcp2_cpymem((DEST), (S), ngtcp2_strlen_lit(S))
44
45 static uint8_t *write_string_impl(uint8_t *p, const uint8_t *data,
46 size_t datalen) {
47 *p++ = '"';
48 if (datalen) {
49 p = ngtcp2_cpymem(p, data, datalen);
50 }
51 *p++ = '"';
52 return p;
53 }
54
55 #define write_string(DEST, S) \
56 write_string_impl((DEST), (const uint8_t *)(S), ngtcp2_strlen_lit(S))
57
58 static uint8_t *write_hex(uint8_t *p, const uint8_t *data, size_t datalen) {
59 *p++ = '"';
60 p = ngtcp2_encode_hex(p, data, datalen);
61 *p++ = '"';
62 return p;
63 }
64
65 static uint8_t *write_cid(uint8_t *p, const ngtcp2_cid *cid) {
66 return write_hex(p, cid->data, cid->datalen);
67 }
68
69 static uint8_t *write_tstamp(uint8_t *p, ngtcp2_tstamp ts) {
70 return ngtcp2_encode_uint(p, ts / NGTCP2_MILLISECONDS);
71 }
72
73 static uint8_t *write_duration(uint8_t *p, ngtcp2_duration duration) {
74 return ngtcp2_encode_uint(p, duration / NGTCP2_MILLISECONDS);
75 }
76
77 static uint8_t *write_bool(uint8_t *p, int b) {
78 if (b) {
79 return ngtcp2_cpymem(p, "true", ngtcp2_strlen_lit("true"));
80 }
81 return ngtcp2_cpymem(p, "false", ngtcp2_strlen_lit("false"));
82 }
83
84 static uint8_t *write_pair_impl(uint8_t *p, const uint8_t *name, size_t namelen,
85 const ngtcp2_vec *value) {
86 p = write_string_impl(p, name, namelen);
87 *p++ = ':';
88 return write_string_impl(p, value->base, value->len);
89 }
90
91 #define write_pair(DEST, NAME, VALUE) \
92 write_pair_impl((DEST), (const uint8_t *)(NAME), ngtcp2_strlen_lit(NAME), \
93 (VALUE))
94
95 static uint8_t *write_pair_hex_impl(uint8_t *p, const uint8_t *name,
96 size_t namelen, const uint8_t *value,
97 size_t valuelen) {
98 p = write_string_impl(p, name, namelen);
99 *p++ = ':';
100 return write_hex(p, value, valuelen);
101 }
102
103 #define write_pair_hex(DEST, NAME, VALUE, VALUELEN) \
104 write_pair_hex_impl((DEST), (const uint8_t *)(NAME), \
105 ngtcp2_strlen_lit(NAME), (VALUE), (VALUELEN))
106
107 static uint8_t *write_pair_number_impl(uint8_t *p, const uint8_t *name,
108 size_t namelen, uint64_t value) {
109 p = write_string_impl(p, name, namelen);
110 *p++ = ':';
111 return ngtcp2_encode_uint(p, value);
112 }
113
114 #define write_pair_number(DEST, NAME, VALUE) \
115 write_pair_number_impl((DEST), (const uint8_t *)(NAME), \
116 ngtcp2_strlen_lit(NAME), (VALUE))
117
118 static uint8_t *write_pair_duration_impl(uint8_t *p, const uint8_t *name,
119 size_t namelen,
120 ngtcp2_duration duration) {
121 p = write_string_impl(p, name, namelen);
122 *p++ = ':';
123 return write_duration(p, duration);
124 }
125
126 #define write_pair_duration(DEST, NAME, VALUE) \
127 write_pair_duration_impl((DEST), (const uint8_t *)(NAME), \
128 ngtcp2_strlen_lit(NAME), (VALUE))
129
130 static uint8_t *write_pair_tstamp_impl(uint8_t *p, const uint8_t *name,
131 size_t namelen, ngtcp2_tstamp ts) {
132 p = write_string_impl(p, name, namelen);
133 *p++ = ':';
134 return write_tstamp(p, ts);
135 }
136
137 #define write_pair_tstamp(DEST, NAME, VALUE) \
138 write_pair_tstamp_impl((DEST), (const uint8_t *)(NAME), \
139 ngtcp2_strlen_lit(NAME), (VALUE))
140
141 static uint8_t *write_pair_bool_impl(uint8_t *p, const uint8_t *name,
142 size_t namelen, int b) {
143 p = write_string_impl(p, name, namelen);
144 *p++ = ':';
145 return write_bool(p, b);
146 }
147
148 #define write_pair_bool(DEST, NAME, VALUE) \
149 write_pair_bool_impl((DEST), (const uint8_t *)(NAME), \
150 ngtcp2_strlen_lit(NAME), (VALUE))
151
152 static uint8_t *write_pair_cid_impl(uint8_t *p, const uint8_t *name,
153 size_t namelen, const ngtcp2_cid *cid) {
154 p = write_string_impl(p, name, namelen);
155 *p++ = ':';
156 return write_cid(p, cid);
157 }
158
159 #define write_pair_cid(DEST, NAME, VALUE) \
160 write_pair_cid_impl((DEST), (const uint8_t *)(NAME), \
161 ngtcp2_strlen_lit(NAME), (VALUE))
162
163 #define ngtcp2_make_vec_lit(S) {(uint8_t *)(S), ngtcp2_strlen_lit((S))}
164
165 static uint8_t *write_common_fields(uint8_t *p, const ngtcp2_cid *odcid) {
166 p = write_verbatim(
167 p, "\"common_fields\":{\"protocol_type\":[\"QUIC\"],\"time_format\":"
168 "\"relative\",\"reference_time\":0,\"group_id\":");
169 p = write_cid(p, odcid);
170 *p++ = '}';
171 return p;
172 }
173
174 static uint8_t *write_trace(uint8_t *p, int server, const ngtcp2_cid *odcid) {
175 p = write_verbatim(
176 p, "\"trace\":{\"vantage_point\":{\"name\":\"ngtcp2\",\"type\":");
177 if (server) {
178 p = write_string(p, "server");
179 } else {
180 p = write_string(p, "client");
181 }
182 p = write_verbatim(p, "},");
183 p = write_common_fields(p, odcid);
184 *p++ = '}';
185 return p;
186 }
187
188 void ngtcp2_qlog_start(ngtcp2_qlog *qlog, const ngtcp2_cid *odcid, int server) {
189 uint8_t buf[1024];
190 uint8_t *p = buf;
191
192 if (!qlog->write) {
193 return;
194 }
195
196 p = write_verbatim(
197 p, "\x1e{\"qlog_format\":\"JSON-SEQ\",\"qlog_version\":\"0.3\",");
198 p = write_trace(p, server, odcid);
199 p = write_verbatim(p, "}\n");
200
201 qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_NONE, buf,
202 (size_t)(p - buf));
203 }
204
205 void ngtcp2_qlog_end(ngtcp2_qlog *qlog) {
206 if (!qlog->write) {
207 return;
208 }
209
210 qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_FIN, "", 0);
211 }
212
213 static const ngtcp2_vec vec_pkt_type_initial = ngtcp2_make_vec_lit("initial");
214 static const ngtcp2_vec vec_pkt_type_handshake =
215 ngtcp2_make_vec_lit("handshake");
216 static const ngtcp2_vec vec_pkt_type_0rtt = ngtcp2_make_vec_lit("0RTT");
217 static const ngtcp2_vec vec_pkt_type_1rtt = ngtcp2_make_vec_lit("1RTT");
218 static const ngtcp2_vec vec_pkt_type_retry = ngtcp2_make_vec_lit("retry");
219 static const ngtcp2_vec vec_pkt_type_version_negotiation =
220 ngtcp2_make_vec_lit("version_negotiation");
221 static const ngtcp2_vec vec_pkt_type_stateless_reset =
222 ngtcp2_make_vec_lit("stateless_reset");
223 static const ngtcp2_vec vec_pkt_type_unknown = ngtcp2_make_vec_lit("unknown");
224
225 static const ngtcp2_vec *qlog_pkt_type(const ngtcp2_pkt_hd *hd) {
226 if (hd->flags & NGTCP2_PKT_FLAG_LONG_FORM) {
227 switch (hd->type) {
228 case NGTCP2_PKT_INITIAL:
229 return &vec_pkt_type_initial;
230 case NGTCP2_PKT_HANDSHAKE:
231 return &vec_pkt_type_handshake;
232 case NGTCP2_PKT_0RTT:
233 return &vec_pkt_type_0rtt;
234 case NGTCP2_PKT_RETRY:
235 return &vec_pkt_type_retry;
236 default:
237 return &vec_pkt_type_unknown;
238 }
239 }
240
241 switch (hd->type) {
242 case NGTCP2_PKT_VERSION_NEGOTIATION:
243 return &vec_pkt_type_version_negotiation;
244 case NGTCP2_PKT_STATELESS_RESET:
245 return &vec_pkt_type_stateless_reset;
246 case NGTCP2_PKT_1RTT:
247 return &vec_pkt_type_1rtt;
248 default:
249 return &vec_pkt_type_unknown;
250 }
251 }
252
253 static uint8_t *write_pkt_hd(uint8_t *p, const ngtcp2_pkt_hd *hd) {
254 /*
255 * {"packet_type":"version_negotiation","packet_number":"0000000000000000000","token":{"data":""}}
256 */
257 #define NGTCP2_QLOG_PKT_HD_OVERHEAD 95
258
259 *p++ = '{';
260 p = write_pair(p, "packet_type", qlog_pkt_type(hd));
261 *p++ = ',';
262 p = write_pair_number(p, "packet_number", (uint64_t)hd->pkt_num);
263 if (hd->type == NGTCP2_PKT_INITIAL && hd->tokenlen) {
264 p = write_verbatim(p, ",\"token\":{");
265 p = write_pair_hex(p, "data", hd->token, hd->tokenlen);
266 *p++ = '}';
267 }
268 /* TODO Write DCIL and DCID */
269 /* TODO Write SCIL and SCID */
270 *p++ = '}';
271 return p;
272 }
273
274 static uint8_t *write_padding_frame(uint8_t *p, const ngtcp2_padding *fr) {
275 (void)fr;
276
277 /* {"frame_type":"padding"} */
278 #define NGTCP2_QLOG_PADDING_FRAME_OVERHEAD 24
279
280 return write_verbatim(p, "{\"frame_type\":\"padding\"}");
281 }
282
283 static uint8_t *write_ping_frame(uint8_t *p, const ngtcp2_ping *fr) {
284 (void)fr;
285
286 /* {"frame_type":"ping"} */
287 #define NGTCP2_QLOG_PING_FRAME_OVERHEAD 21
288
289 return write_verbatim(p, "{\"frame_type\":\"ping\"}");
290 }
291
292 static uint8_t *write_ack_frame(uint8_t *p, const ngtcp2_ack *fr) {
293 int64_t largest_ack, min_ack;
294 size_t i;
295 const ngtcp2_ack_range *range;
296
297 /*
298 * {"frame_type":"ack","ack_delay":0000000000000000000,"acked_ranges":[]}
299 *
300 * each range:
301 * [0000000000000000000,0000000000000000000],
302 *
303 * ecn:
304 * ,"ect1":0000000000000000000,"ect0":0000000000000000000,"ce":0000000000000000000
305 */
306 #define NGTCP2_QLOG_ACK_FRAME_BASE_OVERHEAD 70
307 #define NGTCP2_QLOG_ACK_FRAME_RANGE_OVERHEAD 42
308 #define NGTCP2_QLOG_ACK_FRAME_ECN_OVERHEAD 79
309
310 p = write_verbatim(p, "{\"frame_type\":\"ack\",");
311 p = write_pair_duration(p, "ack_delay", fr->ack_delay_unscaled);
312 p = write_verbatim(p, ",\"acked_ranges\":[");
313
314 largest_ack = fr->largest_ack;
315 min_ack = fr->largest_ack - (int64_t)fr->first_ack_range;
316
317 *p++ = '[';
318 p = ngtcp2_encode_uint(p, (uint64_t)min_ack);
319 if (largest_ack != min_ack) {
320 *p++ = ',';
321 p = ngtcp2_encode_uint(p, (uint64_t)largest_ack);
322 }
323 *p++ = ']';
324
325 for (i = 0; i < fr->rangecnt; ++i) {
326 range = &fr->ranges[i];
327 largest_ack = min_ack - (int64_t)range->gap - 2;
328 min_ack = largest_ack - (int64_t)range->len;
329 *p++ = ',';
330 *p++ = '[';
331 p = ngtcp2_encode_uint(p, (uint64_t)min_ack);
332 if (largest_ack != min_ack) {
333 *p++ = ',';
334 p = ngtcp2_encode_uint(p, (uint64_t)largest_ack);
335 }
336 *p++ = ']';
337 }
338
339 *p++ = ']';
340
341 if (fr->type == NGTCP2_FRAME_ACK_ECN) {
342 *p++ = ',';
343 p = write_pair_number(p, "ect1", fr->ecn.ect1);
344 *p++ = ',';
345 p = write_pair_number(p, "ect0", fr->ecn.ect0);
346 *p++ = ',';
347 p = write_pair_number(p, "ce", fr->ecn.ce);
348 }
349
350 *p++ = '}';
351
352 return p;
353 }
354
355 static uint8_t *write_reset_stream_frame(uint8_t *p,
356 const ngtcp2_reset_stream *fr) {
357 /*
358 * {"frame_type":"reset_stream","stream_id":0000000000000000000,"error_code":0000000000000000000,"final_size":0000000000000000000}
359 */
360 #define NGTCP2_QLOG_RESET_STREAM_FRAME_OVERHEAD 127
361
362 p = write_verbatim(p, "{\"frame_type\":\"reset_stream\",");
363 p = write_pair_number(p, "stream_id", (uint64_t)fr->stream_id);
364 *p++ = ',';
365 p = write_pair_number(p, "error_code", fr->app_error_code);
366 *p++ = ',';
367 p = write_pair_number(p, "final_size", fr->final_size);
368 *p++ = '}';
369
370 return p;
371 }
372
373 static uint8_t *write_stop_sending_frame(uint8_t *p,
374 const ngtcp2_stop_sending *fr) {
375 /*
376 * {"frame_type":"stop_sending","stream_id":0000000000000000000,"error_code":0000000000000000000}
377 */
378 #define NGTCP2_QLOG_STOP_SENDING_FRAME_OVERHEAD 94
379
380 p = write_verbatim(p, "{\"frame_type\":\"stop_sending\",");
381 p = write_pair_number(p, "stream_id", (uint64_t)fr->stream_id);
382 *p++ = ',';
383 p = write_pair_number(p, "error_code", fr->app_error_code);
384 *p++ = '}';
385
386 return p;
387 }
388
389 static uint8_t *write_crypto_frame(uint8_t *p, const ngtcp2_stream *fr) {
390 /*
391 * {"frame_type":"crypto","offset":0000000000000000000,"length":0000000000000000000}
392 */
393 #define NGTCP2_QLOG_CRYPTO_FRAME_OVERHEAD 81
394
395 p = write_verbatim(p, "{\"frame_type\":\"crypto\",");
396 p = write_pair_number(p, "offset", fr->offset);
397 *p++ = ',';
398 p = write_pair_number(p, "length", ngtcp2_vec_len(fr->data, fr->datacnt));
399 *p++ = '}';
400
401 return p;
402 }
403
404 static uint8_t *write_new_token_frame(uint8_t *p, const ngtcp2_new_token *fr) {
405 /*
406 * {"frame_type":"new_token","length":0000000000000000000,"token":{"data":""}}
407 */
408 #define NGTCP2_QLOG_NEW_TOKEN_FRAME_OVERHEAD 75
409
410 p = write_verbatim(p, "{\"frame_type\":\"new_token\",");
411 p = write_pair_number(p, "length", fr->tokenlen);
412 p = write_verbatim(p, ",\"token\":{");
413 p = write_pair_hex(p, "data", fr->token, fr->tokenlen);
414 *p++ = '}';
415 *p++ = '}';
416
417 return p;
418 }
419
420 static uint8_t *write_stream_frame(uint8_t *p, const ngtcp2_stream *fr) {
421 /*
422 * {"frame_type":"stream","stream_id":0000000000000000000,"offset":0000000000000000000,"length":0000000000000000000,"fin":true}
423 */
424 #define NGTCP2_QLOG_STREAM_FRAME_OVERHEAD 124
425
426 p = write_verbatim(p, "{\"frame_type\":\"stream\",");
427 p = write_pair_number(p, "stream_id", (uint64_t)fr->stream_id);
428 *p++ = ',';
429 p = write_pair_number(p, "offset", fr->offset);
430 *p++ = ',';
431 p = write_pair_number(p, "length", ngtcp2_vec_len(fr->data, fr->datacnt));
432 if (fr->fin) {
433 *p++ = ',';
434 p = write_pair_bool(p, "fin", 1);
435 }
436 *p++ = '}';
437
438 return p;
439 }
440
441 static uint8_t *write_max_data_frame(uint8_t *p, const ngtcp2_max_data *fr) {
442 /*
443 * {"frame_type":"max_data","maximum":0000000000000000000}
444 */
445 #define NGTCP2_QLOG_MAX_DATA_FRAME_OVERHEAD 55
446
447 p = write_verbatim(p, "{\"frame_type\":\"max_data\",");
448 p = write_pair_number(p, "maximum", fr->max_data);
449 *p++ = '}';
450
451 return p;
452 }
453
454 static uint8_t *write_max_stream_data_frame(uint8_t *p,
455 const ngtcp2_max_stream_data *fr) {
456 /*
457 * {"frame_type":"max_stream_data","stream_id":0000000000000000000,"maximum":0000000000000000000}
458 */
459 #define NGTCP2_QLOG_MAX_STREAM_DATA_FRAME_OVERHEAD 94
460
461 p = write_verbatim(p, "{\"frame_type\":\"max_stream_data\",");
462 p = write_pair_number(p, "stream_id", (uint64_t)fr->stream_id);
463 *p++ = ',';
464 p = write_pair_number(p, "maximum", fr->max_stream_data);
465 *p++ = '}';
466
467 return p;
468 }
469
470 static uint8_t *write_max_streams_frame(uint8_t *p,
471 const ngtcp2_max_streams *fr) {
472 /*
473 * {"frame_type":"max_streams","stream_type":"unidirectional","maximum":0000000000000000000}
474 */
475 #define NGTCP2_QLOG_MAX_STREAMS_FRAME_OVERHEAD 89
476
477 p = write_verbatim(p, "{\"frame_type\":\"max_streams\",\"stream_type\":");
478 if (fr->type == NGTCP2_FRAME_MAX_STREAMS_BIDI) {
479 p = write_string(p, "bidirectional");
480 } else {
481 p = write_string(p, "unidirectional");
482 }
483 *p++ = ',';
484 p = write_pair_number(p, "maximum", fr->max_streams);
485 *p++ = '}';
486
487 return p;
488 }
489
490 static uint8_t *write_data_blocked_frame(uint8_t *p,
491 const ngtcp2_data_blocked *fr) {
492 /*
493 * {"frame_type":"data_blocked","limit":0000000000000000000}
494 */
495 #define NGTCP2_QLOG_DATA_BLOCKED_FRAME_OVERHEAD 57
496
497 p = write_verbatim(p, "{\"frame_type\":\"data_blocked\",");
498 p = write_pair_number(p, "limit", fr->offset);
499 *p++ = '}';
500
501 return p;
502 }
503
504 static uint8_t *
505 write_stream_data_blocked_frame(uint8_t *p,
506 const ngtcp2_stream_data_blocked *fr) {
507 /*
508 * {"frame_type":"stream_data_blocked","stream_id":0000000000000000000,"limit":0000000000000000000}
509 */
510 #define NGTCP2_QLOG_STREAM_DATA_BLOCKED_FRAME_OVERHEAD 96
511
512 p = write_verbatim(p, "{\"frame_type\":\"stream_data_blocked\",");
513 p = write_pair_number(p, "stream_id", (uint64_t)fr->stream_id);
514 *p++ = ',';
515 p = write_pair_number(p, "limit", fr->offset);
516 *p++ = '}';
517
518 return p;
519 }
520
521 static uint8_t *write_streams_blocked_frame(uint8_t *p,
522 const ngtcp2_streams_blocked *fr) {
523 /*
524 * {"frame_type":"streams_blocked","stream_type":"unidirectional","limit":0000000000000000000}
525 */
526 #define NGTCP2_QLOG_STREAMS_BLOCKED_FRAME_OVERHEAD 91
527
528 p = write_verbatim(p, "{\"frame_type\":\"streams_blocked\",\"stream_type\":");
529 if (fr->type == NGTCP2_FRAME_STREAMS_BLOCKED_BIDI) {
530 p = write_string(p, "bidirectional");
531 } else {
532 p = write_string(p, "unidirectional");
533 }
534 *p++ = ',';
535 p = write_pair_number(p, "limit", fr->max_streams);
536 *p++ = '}';
537
538 return p;
539 }
540
541 static uint8_t *
542 write_new_connection_id_frame(uint8_t *p, const ngtcp2_new_connection_id *fr) {
543 /*
544 * {"frame_type":"new_connection_id","sequence_number":0000000000000000000,"retire_prior_to":0000000000000000000,"connection_id_length":0000000000000000000,"connection_id":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","stateless_reset_token":{"data":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}}
545 */
546 #define NGTCP2_QLOG_NEW_CONNECTION_ID_FRAME_OVERHEAD 280
547
548 p = write_verbatim(p, "{\"frame_type\":\"new_connection_id\",");
549 p = write_pair_number(p, "sequence_number", fr->seq);
550 *p++ = ',';
551 p = write_pair_number(p, "retire_prior_to", fr->retire_prior_to);
552 *p++ = ',';
553 p = write_pair_number(p, "connection_id_length", fr->cid.datalen);
554 *p++ = ',';
555 p = write_pair_cid(p, "connection_id", &fr->cid);
556 p = write_verbatim(p, ",\"stateless_reset_token\":{");
557 p = write_pair_hex(p, "data", fr->stateless_reset_token,
558 sizeof(fr->stateless_reset_token));
559 *p++ = '}';
560 *p++ = '}';
561
562 return p;
563 }
564
565 static uint8_t *
566 write_retire_connection_id_frame(uint8_t *p,
567 const ngtcp2_retire_connection_id *fr) {
568 /*
569 * {"frame_type":"retire_connection_id","sequence_number":0000000000000000000}
570 */
571 #define NGTCP2_QLOG_RETIRE_CONNECTION_ID_FRAME_OVERHEAD 75
572
573 p = write_verbatim(p, "{\"frame_type\":\"retire_connection_id\",");
574 p = write_pair_number(p, "sequence_number", fr->seq);
575 *p++ = '}';
576
577 return p;
578 }
579
580 static uint8_t *write_path_challenge_frame(uint8_t *p,
581 const ngtcp2_path_challenge *fr) {
582 /*
583 * {"frame_type":"path_challenge","data":"xxxxxxxxxxxxxxxx"}
584 */
585 #define NGTCP2_QLOG_PATH_CHALLENGE_FRAME_OVERHEAD 57
586
587 p = write_verbatim(p, "{\"frame_type\":\"path_challenge\",");
588 p = write_pair_hex(p, "data", fr->data, sizeof(fr->data));
589 *p++ = '}';
590
591 return p;
592 }
593
594 static uint8_t *write_path_response_frame(uint8_t *p,
595 const ngtcp2_path_response *fr) {
596 /*
597 * {"frame_type":"path_response","data":"xxxxxxxxxxxxxxxx"}
598 */
599 #define NGTCP2_QLOG_PATH_RESPONSE_FRAME_OVERHEAD 56
600
601 p = write_verbatim(p, "{\"frame_type\":\"path_response\",");
602 p = write_pair_hex(p, "data", fr->data, sizeof(fr->data));
603 *p++ = '}';
604
605 return p;
606 }
607
608 static uint8_t *
609 write_connection_close_frame(uint8_t *p, const ngtcp2_connection_close *fr) {
610 /*
611 * {"frame_type":"connection_close","error_space":"application","error_code":0000000000000000000,"raw_error_code":0000000000000000000}
612 */
613 #define NGTCP2_QLOG_CONNECTION_CLOSE_FRAME_OVERHEAD 131
614
615 p =
616 write_verbatim(p, "{\"frame_type\":\"connection_close\",\"error_space\":");
617 if (fr->type == NGTCP2_FRAME_CONNECTION_CLOSE) {
618 p = write_string(p, "transport");
619 } else {
620 p = write_string(p, "application");
621 }
622 *p++ = ',';
623 p = write_pair_number(p, "error_code", fr->error_code);
624 *p++ = ',';
625 p = write_pair_number(p, "raw_error_code", fr->error_code);
626 /* TODO Write reason by escaping non-printables */
627 /* TODO Write trigger_frame_type */
628 *p++ = '}';
629
630 return p;
631 }
632
633 static uint8_t *write_handshake_done_frame(uint8_t *p,
634 const ngtcp2_handshake_done *fr) {
635 (void)fr;
636
637 /*
638 * {"frame_type":"handshake_done"}
639 */
640 #define NGTCP2_QLOG_HANDSHAKE_DONE_FRAME_OVERHEAD 31
641
642 return write_verbatim(p, "{\"frame_type\":\"handshake_done\"}");
643 }
644
645 static uint8_t *write_datagram_frame(uint8_t *p, const ngtcp2_datagram *fr) {
646 /*
647 * {"frame_type":"datagram","length":0000000000000000000}
648 */
649 #define NGTCP2_QLOG_DATAGRAM_FRAME_OVERHEAD 54
650
651 p = write_verbatim(p, "{\"frame_type\":\"datagram\",");
652 p = write_pair_number(p, "length", ngtcp2_vec_len(fr->data, fr->datacnt));
653 *p++ = '}';
654
655 return p;
656 }
657
658 static uint8_t *qlog_write_time(ngtcp2_qlog *qlog, uint8_t *p) {
659 return write_pair_tstamp(p, "time", qlog->last_ts - qlog->ts);
660 }
661
662 static void qlog_pkt_write_start(ngtcp2_qlog *qlog, int sent) {
663 uint8_t *p;
664
665 if (!qlog->write) {
666 return;
667 }
668
669 ngtcp2_buf_reset(&qlog->buf);
670 p = qlog->buf.last;
671
672 *p++ = '\x1e';
673 *p++ = '{';
674 p = qlog_write_time(qlog, p);
675 p = write_verbatim(p, ",\"name\":");
676 if (sent) {
677 p = write_string(p, "transport:packet_sent");
678 } else {
679 p = write_string(p, "transport:packet_received");
680 }
681 p = write_verbatim(p, ",\"data\":{\"frames\":[");
682 qlog->buf.last = p;
683 }
684
685 static void qlog_pkt_write_end(ngtcp2_qlog *qlog, const ngtcp2_pkt_hd *hd,
686 size_t pktlen) {
687 uint8_t *p = qlog->buf.last;
688
689 if (!qlog->write) {
690 return;
691 }
692
693 /*
694 * ],"header":,"raw":{"length":0000000000000000000}}}
695 *
696 * plus, terminating LF
697 */
698 #define NGTCP2_QLOG_PKT_WRITE_END_OVERHEAD \
699 (1 + 50 + NGTCP2_QLOG_PKT_HD_OVERHEAD)
700
701 if (ngtcp2_buf_left(&qlog->buf) <
702 NGTCP2_QLOG_PKT_WRITE_END_OVERHEAD + hd->tokenlen * 2) {
703 return;
704 }
705
706 assert(ngtcp2_buf_len(&qlog->buf));
707
708 /* Eat last ',' */
709 if (*(p - 1) == ',') {
710 --p;
711 }
712
713 p = write_verbatim(p, "],\"header\":");
714 p = write_pkt_hd(p, hd);
715 p = write_verbatim(p, ",\"raw\":{\"length\":");
716 p = ngtcp2_encode_uint(p, pktlen);
717 p = write_verbatim(p, "}}}\n");
718
719 qlog->buf.last = p;
720
721 qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_NONE, qlog->buf.pos,
722 ngtcp2_buf_len(&qlog->buf));
723 }
724
725 void ngtcp2_qlog_write_frame(ngtcp2_qlog *qlog, const ngtcp2_frame *fr) {
726 uint8_t *p = qlog->buf.last;
727
728 if (!qlog->write) {
729 return;
730 }
731
732 switch (fr->hd.type) {
733 case NGTCP2_FRAME_PADDING:
734 if (ngtcp2_buf_left(&qlog->buf) < NGTCP2_QLOG_PADDING_FRAME_OVERHEAD + 1) {
735 return;
736 }
737 p = write_padding_frame(p, &fr->padding);
738 break;
739 case NGTCP2_FRAME_PING:
740 if (ngtcp2_buf_left(&qlog->buf) < NGTCP2_QLOG_PING_FRAME_OVERHEAD + 1) {
741 return;
742 }
743 p = write_ping_frame(p, &fr->ping);
744 break;
745 case NGTCP2_FRAME_ACK:
746 case NGTCP2_FRAME_ACK_ECN:
747 if (ngtcp2_buf_left(&qlog->buf) <
748 NGTCP2_QLOG_ACK_FRAME_BASE_OVERHEAD +
749 (size_t)(fr->ack.type == NGTCP2_FRAME_ACK_ECN
750 ? NGTCP2_QLOG_ACK_FRAME_ECN_OVERHEAD
751 : 0) +
752 NGTCP2_QLOG_ACK_FRAME_RANGE_OVERHEAD * (1 + fr->ack.rangecnt) + 1) {
753 return;
754 }
755 p = write_ack_frame(p, &fr->ack);
756 break;
757 case NGTCP2_FRAME_RESET_STREAM:
758 if (ngtcp2_buf_left(&qlog->buf) <
759 NGTCP2_QLOG_RESET_STREAM_FRAME_OVERHEAD + 1) {
760 return;
761 }
762 p = write_reset_stream_frame(p, &fr->reset_stream);
763 break;
764 case NGTCP2_FRAME_STOP_SENDING:
765 if (ngtcp2_buf_left(&qlog->buf) <
766 NGTCP2_QLOG_STOP_SENDING_FRAME_OVERHEAD + 1) {
767 return;
768 }
769 p = write_stop_sending_frame(p, &fr->stop_sending);
770 break;
771 case NGTCP2_FRAME_CRYPTO:
772 if (ngtcp2_buf_left(&qlog->buf) < NGTCP2_QLOG_CRYPTO_FRAME_OVERHEAD + 1) {
773 return;
774 }
775 p = write_crypto_frame(p, &fr->stream);
776 break;
777 case NGTCP2_FRAME_NEW_TOKEN:
778 if (ngtcp2_buf_left(&qlog->buf) <
779 NGTCP2_QLOG_NEW_TOKEN_FRAME_OVERHEAD + fr->new_token.tokenlen * 2 + 1) {
780 return;
781 }
782 p = write_new_token_frame(p, &fr->new_token);
783 break;
784 case NGTCP2_FRAME_STREAM:
785 if (ngtcp2_buf_left(&qlog->buf) < NGTCP2_QLOG_STREAM_FRAME_OVERHEAD + 1) {
786 return;
787 }
788 p = write_stream_frame(p, &fr->stream);
789 break;
790 case NGTCP2_FRAME_MAX_DATA:
791 if (ngtcp2_buf_left(&qlog->buf) < NGTCP2_QLOG_MAX_DATA_FRAME_OVERHEAD + 1) {
792 return;
793 }
794 p = write_max_data_frame(p, &fr->max_data);
795 break;
796 case NGTCP2_FRAME_MAX_STREAM_DATA:
797 if (ngtcp2_buf_left(&qlog->buf) <
798 NGTCP2_QLOG_MAX_STREAM_DATA_FRAME_OVERHEAD + 1) {
799 return;
800 }
801 p = write_max_stream_data_frame(p, &fr->max_stream_data);
802 break;
803 case NGTCP2_FRAME_MAX_STREAMS_BIDI:
804 case NGTCP2_FRAME_MAX_STREAMS_UNI:
805 if (ngtcp2_buf_left(&qlog->buf) <
806 NGTCP2_QLOG_MAX_STREAMS_FRAME_OVERHEAD + 1) {
807 return;
808 }
809 p = write_max_streams_frame(p, &fr->max_streams);
810 break;
811 case NGTCP2_FRAME_DATA_BLOCKED:
812 if (ngtcp2_buf_left(&qlog->buf) <
813 NGTCP2_QLOG_DATA_BLOCKED_FRAME_OVERHEAD + 1) {
814 return;
815 }
816 p = write_data_blocked_frame(p, &fr->data_blocked);
817 break;
818 case NGTCP2_FRAME_STREAM_DATA_BLOCKED:
819 if (ngtcp2_buf_left(&qlog->buf) <
820 NGTCP2_QLOG_STREAM_DATA_BLOCKED_FRAME_OVERHEAD + 1) {
821 return;
822 }
823 p = write_stream_data_blocked_frame(p, &fr->stream_data_blocked);
824 break;
825 case NGTCP2_FRAME_STREAMS_BLOCKED_BIDI:
826 case NGTCP2_FRAME_STREAMS_BLOCKED_UNI:
827 if (ngtcp2_buf_left(&qlog->buf) <
828 NGTCP2_QLOG_STREAMS_BLOCKED_FRAME_OVERHEAD + 1) {
829 return;
830 }
831 p = write_streams_blocked_frame(p, &fr->streams_blocked);
832 break;
833 case NGTCP2_FRAME_NEW_CONNECTION_ID:
834 if (ngtcp2_buf_left(&qlog->buf) <
835 NGTCP2_QLOG_NEW_CONNECTION_ID_FRAME_OVERHEAD + 1) {
836 return;
837 }
838 p = write_new_connection_id_frame(p, &fr->new_connection_id);
839 break;
840 case NGTCP2_FRAME_RETIRE_CONNECTION_ID:
841 if (ngtcp2_buf_left(&qlog->buf) <
842 NGTCP2_QLOG_RETIRE_CONNECTION_ID_FRAME_OVERHEAD + 1) {
843 return;
844 }
845 p = write_retire_connection_id_frame(p, &fr->retire_connection_id);
846 break;
847 case NGTCP2_FRAME_PATH_CHALLENGE:
848 if (ngtcp2_buf_left(&qlog->buf) <
849 NGTCP2_QLOG_PATH_CHALLENGE_FRAME_OVERHEAD + 1) {
850 return;
851 }
852 p = write_path_challenge_frame(p, &fr->path_challenge);
853 break;
854 case NGTCP2_FRAME_PATH_RESPONSE:
855 if (ngtcp2_buf_left(&qlog->buf) <
856 NGTCP2_QLOG_PATH_RESPONSE_FRAME_OVERHEAD + 1) {
857 return;
858 }
859 p = write_path_response_frame(p, &fr->path_response);
860 break;
861 case NGTCP2_FRAME_CONNECTION_CLOSE:
862 case NGTCP2_FRAME_CONNECTION_CLOSE_APP:
863 if (ngtcp2_buf_left(&qlog->buf) <
864 NGTCP2_QLOG_CONNECTION_CLOSE_FRAME_OVERHEAD + 1) {
865 return;
866 }
867 p = write_connection_close_frame(p, &fr->connection_close);
868 break;
869 case NGTCP2_FRAME_HANDSHAKE_DONE:
870 if (ngtcp2_buf_left(&qlog->buf) <
871 NGTCP2_QLOG_HANDSHAKE_DONE_FRAME_OVERHEAD + 1) {
872 return;
873 }
874 p = write_handshake_done_frame(p, &fr->handshake_done);
875 break;
876 case NGTCP2_FRAME_DATAGRAM:
877 case NGTCP2_FRAME_DATAGRAM_LEN:
878 if (ngtcp2_buf_left(&qlog->buf) < NGTCP2_QLOG_DATAGRAM_FRAME_OVERHEAD + 1) {
879 return;
880 }
881 p = write_datagram_frame(p, &fr->datagram);
882 break;
883 default:
884 ngtcp2_unreachable();
885 }
886
887 *p++ = ',';
888
889 qlog->buf.last = p;
890 }
891
892 void ngtcp2_qlog_pkt_received_start(ngtcp2_qlog *qlog) {
893 qlog_pkt_write_start(qlog, /* sent = */ 0);
894 }
895
896 void ngtcp2_qlog_pkt_received_end(ngtcp2_qlog *qlog, const ngtcp2_pkt_hd *hd,
897 size_t pktlen) {
898 qlog_pkt_write_end(qlog, hd, pktlen);
899 }
900
901 void ngtcp2_qlog_pkt_sent_start(ngtcp2_qlog *qlog) {
902 qlog_pkt_write_start(qlog, /* sent = */ 1);
903 }
904
905 void ngtcp2_qlog_pkt_sent_end(ngtcp2_qlog *qlog, const ngtcp2_pkt_hd *hd,
906 size_t pktlen) {
907 qlog_pkt_write_end(qlog, hd, pktlen);
908 }
909
910 void ngtcp2_qlog_parameters_set_transport_params(
911 ngtcp2_qlog *qlog, const ngtcp2_transport_params *params, int server,
912 ngtcp2_qlog_side side) {
913 uint8_t buf[1024];
914 uint8_t *p = buf;
915 const ngtcp2_preferred_addr *paddr;
916 const ngtcp2_sockaddr_in *sa_in;
917 const ngtcp2_sockaddr_in6 *sa_in6;
918
919 if (!qlog->write) {
920 return;
921 }
922
923 *p++ = '\x1e';
924 *p++ = '{';
925 p = qlog_write_time(qlog, p);
926 p = write_verbatim(
927 p, ",\"name\":\"transport:parameters_set\",\"data\":{\"owner\":");
928
929 if (side == NGTCP2_QLOG_SIDE_LOCAL) {
930 p = write_string(p, "local");
931 } else {
932 p = write_string(p, "remote");
933 }
934
935 *p++ = ',';
936 p = write_pair_cid(p, "initial_source_connection_id", &params->initial_scid);
937 *p++ = ',';
938 if (side == (server ? NGTCP2_QLOG_SIDE_LOCAL : NGTCP2_QLOG_SIDE_REMOTE)) {
939 p = write_pair_cid(p, "original_destination_connection_id",
940 &params->original_dcid);
941 *p++ = ',';
942 }
943 if (params->retry_scid_present) {
944 p = write_pair_cid(p, "retry_source_connection_id", &params->retry_scid);
945 *p++ = ',';
946 }
947 if (params->stateless_reset_token_present) {
948 p = write_verbatim(p, "\"stateless_reset_token\":{");
949 p = write_pair_hex(p, "data", params->stateless_reset_token,
950 sizeof(params->stateless_reset_token));
951 *p++ = '}';
952 *p++ = ',';
953 }
954 p = write_pair_bool(p, "disable_active_migration",
955 params->disable_active_migration);
956 *p++ = ',';
957 p = write_pair_duration(p, "max_idle_timeout", params->max_idle_timeout);
958 *p++ = ',';
959 p =
960 write_pair_number(p, "max_udp_payload_size", params->max_udp_payload_size);
961 *p++ = ',';
962 p = write_pair_number(p, "ack_delay_exponent", params->ack_delay_exponent);
963 *p++ = ',';
964 p = write_pair_duration(p, "max_ack_delay", params->max_ack_delay);
965 *p++ = ',';
966 p = write_pair_number(p, "active_connection_id_limit",
967 params->active_connection_id_limit);
968 *p++ = ',';
969 p = write_pair_number(p, "initial_max_data", params->initial_max_data);
970 *p++ = ',';
971 p = write_pair_number(p, "initial_max_stream_data_bidi_local",
972 params->initial_max_stream_data_bidi_local);
973 *p++ = ',';
974 p = write_pair_number(p, "initial_max_stream_data_bidi_remote",
975 params->initial_max_stream_data_bidi_remote);
976 *p++ = ',';
977 p = write_pair_number(p, "initial_max_stream_data_uni",
978 params->initial_max_stream_data_uni);
979 *p++ = ',';
980 p = write_pair_number(p, "initial_max_streams_bidi",
981 params->initial_max_streams_bidi);
982 *p++ = ',';
983 p = write_pair_number(p, "initial_max_streams_uni",
984 params->initial_max_streams_uni);
985 if (params->preferred_addr_present) {
986 *p++ = ',';
987 paddr = &params->preferred_addr;
988 p = write_string(p, "preferred_address");
989 *p++ = ':';
990 *p++ = '{';
991
992 if (paddr->ipv4_present) {
993 sa_in = &paddr->ipv4;
994
995 p = write_pair_hex(p, "ip_v4", (const uint8_t *)&sa_in->sin_addr,
996 sizeof(sa_in->sin_addr));
997 *p++ = ',';
998 p = write_pair_number(p, "port_v4", ngtcp2_ntohs(sa_in->sin_port));
999 *p++ = ',';
1000 }
1001
1002 if (paddr->ipv6_present) {
1003 sa_in6 = &paddr->ipv6;
1004
1005 p = write_pair_hex(p, "ip_v6", (const uint8_t *)&sa_in6->sin6_addr,
1006 sizeof(sa_in6->sin6_addr));
1007 *p++ = ',';
1008 p = write_pair_number(p, "port_v6", ngtcp2_ntohs(sa_in6->sin6_port));
1009 *p++ = ',';
1010 }
1011
1012 p = write_pair_cid(p, "connection_id", &paddr->cid);
1013 p = write_verbatim(p, ",\"stateless_reset_token\":{");
1014 p = write_pair_hex(p, "data", paddr->stateless_reset_token,
1015 sizeof(paddr->stateless_reset_token));
1016 *p++ = '}';
1017 *p++ = '}';
1018 }
1019 *p++ = ',';
1020 p = write_pair_number(p, "max_datagram_frame_size",
1021 params->max_datagram_frame_size);
1022 *p++ = ',';
1023 p = write_pair_bool(p, "grease_quic_bit", params->grease_quic_bit);
1024 p = write_verbatim(p, "}}\n");
1025
1026 qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_NONE, buf,
1027 (size_t)(p - buf));
1028 }
1029
1030 void ngtcp2_qlog_metrics_updated(ngtcp2_qlog *qlog,
1031 const ngtcp2_conn_stat *cstat) {
1032 uint8_t buf[1024];
1033 uint8_t *p = buf;
1034
1035 if (!qlog->write) {
1036 return;
1037 }
1038
1039 *p++ = '\x1e';
1040 *p++ = '{';
1041 p = qlog_write_time(qlog, p);
1042 p = write_verbatim(p, ",\"name\":\"recovery:metrics_updated\",\"data\":{");
1043
1044 if (cstat->min_rtt != UINT64_MAX) {
1045 p = write_pair_duration(p, "min_rtt", cstat->min_rtt);
1046 *p++ = ',';
1047 }
1048 p = write_pair_duration(p, "smoothed_rtt", cstat->smoothed_rtt);
1049 *p++ = ',';
1050 p = write_pair_duration(p, "latest_rtt", cstat->latest_rtt);
1051 *p++ = ',';
1052 p = write_pair_duration(p, "rtt_variance", cstat->rttvar);
1053 *p++ = ',';
1054 p = write_pair_number(p, "pto_count", cstat->pto_count);
1055 *p++ = ',';
1056 p = write_pair_number(p, "congestion_window", cstat->cwnd);
1057 *p++ = ',';
1058 p = write_pair_number(p, "bytes_in_flight", cstat->bytes_in_flight);
1059 if (cstat->ssthresh != UINT64_MAX) {
1060 *p++ = ',';
1061 p = write_pair_number(p, "ssthresh", cstat->ssthresh);
1062 }
1063
1064 p = write_verbatim(p, "}}\n");
1065
1066 qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_NONE, buf,
1067 (size_t)(p - buf));
1068 }
1069
1070 void ngtcp2_qlog_pkt_lost(ngtcp2_qlog *qlog, ngtcp2_rtb_entry *ent) {
1071 uint8_t buf[256];
1072 uint8_t *p = buf;
1073
1074 if (!qlog->write) {
1075 return;
1076 }
1077
1078 *p++ = '\x1e';
1079 *p++ = '{';
1080 p = qlog_write_time(qlog, p);
1081 p = write_verbatim(
1082 p, ",\"name\":\"recovery:packet_lost\",\"data\":{\"header\":");
1083
1084 p = write_pkt_hd(p, &(ngtcp2_pkt_hd){
1085 .pkt_num = ent->hd.pkt_num,
1086 .type = ent->hd.type,
1087 .flags = ent->hd.flags,
1088 });
1089 p = write_verbatim(p, "}}\n");
1090
1091 qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_NONE, buf,
1092 (size_t)(p - buf));
1093 }
1094
1095 void ngtcp2_qlog_retry_pkt_received(ngtcp2_qlog *qlog, const ngtcp2_pkt_hd *hd,
1096 const ngtcp2_pkt_retry *retry) {
1097 uint8_t rawbuf[1024];
1098 ngtcp2_buf buf;
1099
1100 if (!qlog->write) {
1101 return;
1102 }
1103
1104 ngtcp2_buf_init(&buf, rawbuf, sizeof(rawbuf));
1105
1106 *buf.last++ = '\x1e';
1107 *buf.last++ = '{';
1108 buf.last = qlog_write_time(qlog, buf.last);
1109 buf.last = write_verbatim(
1110 buf.last, ",\"name\":\"transport:packet_received\",\"data\":{\"header\":");
1111
1112 if (ngtcp2_buf_left(&buf) < NGTCP2_QLOG_PKT_HD_OVERHEAD + hd->tokenlen * 2 +
1113 sizeof(",\"retry_token\":{\"data\":\"\"}}}\n") -
1114 1 + retry->tokenlen * 2) {
1115 return;
1116 }
1117
1118 buf.last = write_pkt_hd(buf.last, hd);
1119 buf.last = write_verbatim(buf.last, ",\"retry_token\":{");
1120 buf.last = write_pair_hex(buf.last, "data", retry->token, retry->tokenlen);
1121 buf.last = write_verbatim(buf.last, "}}}\n");
1122
1123 qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_NONE, buf.pos,
1124 ngtcp2_buf_len(&buf));
1125 }
1126
1127 void ngtcp2_qlog_stateless_reset_pkt_received(
1128 ngtcp2_qlog *qlog, const ngtcp2_pkt_stateless_reset *sr) {
1129 uint8_t buf[256];
1130 uint8_t *p = buf;
1131
1132 if (!qlog->write) {
1133 return;
1134 }
1135
1136 *p++ = '\x1e';
1137 *p++ = '{';
1138 p = qlog_write_time(qlog, p);
1139 p = write_verbatim(
1140 p, ",\"name\":\"transport:packet_received\",\"data\":{\"header\":");
1141 p = write_pkt_hd(p, &(ngtcp2_pkt_hd){
1142 .type = NGTCP2_PKT_STATELESS_RESET,
1143 });
1144 *p++ = ',';
1145 p = write_pair_hex(p, "stateless_reset_token", sr->stateless_reset_token,
1146 NGTCP2_STATELESS_RESET_TOKENLEN);
1147 p = write_verbatim(p, "}}\n");
1148
1149 qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_NONE, buf,
1150 (size_t)(p - buf));
1151 }
1152
1153 void ngtcp2_qlog_version_negotiation_pkt_received(ngtcp2_qlog *qlog,
1154 const ngtcp2_pkt_hd *hd,
1155 const uint32_t *sv,
1156 size_t nsv) {
1157 uint8_t rawbuf[512];
1158 ngtcp2_buf buf;
1159 size_t i;
1160 uint32_t v;
1161
1162 if (!qlog->write) {
1163 return;
1164 }
1165
1166 ngtcp2_buf_init(&buf, rawbuf, sizeof(rawbuf));
1167
1168 *buf.last++ = '\x1e';
1169 *buf.last++ = '{';
1170 buf.last = qlog_write_time(qlog, buf.last);
1171 buf.last = write_verbatim(
1172 buf.last, ",\"name\":\"transport:packet_received\",\"data\":{\"header\":");
1173 buf.last = write_pkt_hd(buf.last, hd);
1174 buf.last = write_verbatim(buf.last, ",\"supported_versions\":[");
1175
1176 if (nsv) {
1177 if (ngtcp2_buf_left(&buf) < ngtcp2_strlen_lit("\"xxxxxxxx\",") * nsv - 1 +
1178 ngtcp2_strlen_lit("]}}\n")) {
1179 return;
1180 }
1181
1182 v = ngtcp2_htonl(sv[0]);
1183 buf.last = write_hex(buf.last, (const uint8_t *)&v, sizeof(v));
1184
1185 for (i = 1; i < nsv; ++i) {
1186 *buf.last++ = ',';
1187 v = ngtcp2_htonl(sv[i]);
1188 buf.last = write_hex(buf.last, (const uint8_t *)&v, sizeof(v));
1189 }
1190 }
1191
1192 buf.last = write_verbatim(buf.last, "]}}\n");
1193
1194 qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_NONE, buf.pos,
1195 ngtcp2_buf_len(&buf));
1196 }