File: | out/../deps/ngtcp2/ngtcp2/lib/ngtcp2_pkt.c |
Warning: | line 1738, column 3 Value stored to 'p' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* |
2 | * ngtcp2 |
3 | * |
4 | * Copyright (c) 2017 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_pkt.h" |
26 | |
27 | #include <assert.h> |
28 | #include <string.h> |
29 | #include <stdio.h> |
30 | |
31 | #include "ngtcp2_conv.h" |
32 | #include "ngtcp2_str.h" |
33 | #include "ngtcp2_macro.h" |
34 | #include "ngtcp2_cid.h" |
35 | #include "ngtcp2_mem.h" |
36 | #include "ngtcp2_vec.h" |
37 | |
38 | int ngtcp2_pkt_chain_new(ngtcp2_pkt_chain **ppc, const ngtcp2_path *path, |
39 | const ngtcp2_pkt_info *pi, const uint8_t *pkt, |
40 | size_t pktlen, size_t dgramlen, ngtcp2_tstamp ts, |
41 | const ngtcp2_mem *mem) { |
42 | *ppc = ngtcp2_mem_malloc(mem, sizeof(ngtcp2_pkt_chain) + pktlen); |
43 | if (*ppc == NULL((void*)0)) { |
44 | return NGTCP2_ERR_NOMEM-501; |
45 | } |
46 | |
47 | ngtcp2_path_storage_init2(&(*ppc)->path, path); |
48 | (*ppc)->pi = *pi; |
49 | (*ppc)->next = NULL((void*)0); |
50 | (*ppc)->pkt = (uint8_t *)(*ppc) + sizeof(ngtcp2_pkt_chain); |
51 | (*ppc)->pktlen = pktlen; |
52 | (*ppc)->dgramlen = dgramlen; |
53 | (*ppc)->ts = ts; |
54 | |
55 | memcpy((*ppc)->pkt, pkt, pktlen); |
56 | |
57 | return 0; |
58 | } |
59 | |
60 | void ngtcp2_pkt_chain_del(ngtcp2_pkt_chain *pc, const ngtcp2_mem *mem) { |
61 | ngtcp2_mem_free(mem, pc); |
62 | } |
63 | |
64 | int ngtcp2_pkt_decode_version_cid(uint32_t *pversion, const uint8_t **pdcid, |
65 | size_t *pdcidlen, const uint8_t **pscid, |
66 | size_t *pscidlen, const uint8_t *data, |
67 | size_t datalen, size_t short_dcidlen) { |
68 | size_t len; |
69 | uint32_t version; |
70 | size_t dcidlen, scidlen; |
71 | |
72 | assert(datalen)((void) (0)); |
73 | |
74 | if (data[0] & NGTCP2_HEADER_FORM_BIT0x80) { |
75 | /* 1 byte (Header Form, Fixed Bit, Long Packet Type, Type-Specific bits) |
76 | * 4 bytes Version |
77 | * 1 byte DCID Length |
78 | * 1 byte SCID Length |
79 | */ |
80 | len = 1 + 4 + 1 + 1; |
81 | if (datalen < len) { |
82 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
83 | } |
84 | |
85 | dcidlen = data[5]; |
86 | len += dcidlen; |
87 | if (datalen < len) { |
88 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
89 | } |
90 | scidlen = data[5 + 1 + dcidlen]; |
91 | len += scidlen; |
92 | if (datalen < len) { |
93 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
94 | } |
95 | |
96 | version = ngtcp2_get_uint32(&data[1]); |
97 | |
98 | if ((version == 0 || version == NGTCP2_PROTO_VER_V10x00000001u || |
99 | (NGTCP2_PROTO_VER_DRAFT_MIN0xff00001du <= version && |
100 | version <= NGTCP2_PROTO_VER_DRAFT_MAX0xff000020u)) && |
101 | (dcidlen > NGTCP2_MAX_CIDLEN20 || scidlen > NGTCP2_MAX_CIDLEN20)) { |
102 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
103 | } |
104 | |
105 | *pversion = version; |
106 | *pdcid = &data[6]; |
107 | *pdcidlen = dcidlen; |
108 | *pscid = &data[6 + dcidlen + 1]; |
109 | *pscidlen = scidlen; |
110 | |
111 | if (version && version != NGTCP2_PROTO_VER_V10x00000001u && |
112 | (version < NGTCP2_PROTO_VER_DRAFT_MIN0xff00001du || |
113 | NGTCP2_PROTO_VER_DRAFT_MAX0xff000020u < version)) { |
114 | return 1; |
115 | } |
116 | return 0; |
117 | } |
118 | |
119 | assert(short_dcidlen <= NGTCP2_MAX_CIDLEN)((void) (0)); |
120 | |
121 | len = 1 + short_dcidlen; |
122 | if (datalen < len) { |
123 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
124 | } |
125 | |
126 | *pversion = 0; |
127 | *pdcid = &data[1]; |
128 | *pdcidlen = short_dcidlen; |
129 | *pscid = NULL((void*)0); |
130 | *pscidlen = 0; |
131 | |
132 | return 0; |
133 | } |
134 | |
135 | void ngtcp2_pkt_hd_init(ngtcp2_pkt_hd *hd, uint8_t flags, uint8_t type, |
136 | const ngtcp2_cid *dcid, const ngtcp2_cid *scid, |
137 | int64_t pkt_num, size_t pkt_numlen, uint32_t version, |
138 | size_t len) { |
139 | hd->flags = flags; |
140 | hd->type = type; |
141 | if (dcid) { |
142 | hd->dcid = *dcid; |
143 | } else { |
144 | ngtcp2_cid_zero(&hd->dcid); |
145 | } |
146 | if (scid) { |
147 | hd->scid = *scid; |
148 | } else { |
149 | ngtcp2_cid_zero(&hd->scid); |
150 | } |
151 | hd->pkt_num = pkt_num; |
152 | hd->token.base = NULL((void*)0); |
153 | hd->token.len = 0; |
154 | hd->pkt_numlen = pkt_numlen; |
155 | hd->version = version; |
156 | hd->len = len; |
157 | } |
158 | |
159 | static int has_mask(uint8_t b, uint8_t mask) { return (b & mask) == mask; } |
160 | |
161 | ngtcp2_ssize ngtcp2_pkt_decode_hd_long(ngtcp2_pkt_hd *dest, const uint8_t *pkt, |
162 | size_t pktlen) { |
163 | uint8_t type; |
164 | uint32_t version; |
165 | size_t dcil, scil; |
166 | const uint8_t *p; |
167 | size_t len = 0; |
168 | size_t n; |
169 | size_t ntokenlen = 0; |
170 | const uint8_t *token = NULL((void*)0); |
171 | size_t tokenlen = 0; |
172 | uint64_t vi; |
173 | |
174 | if (pktlen < 5) { |
175 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
176 | } |
177 | |
178 | if (!(pkt[0] & NGTCP2_HEADER_FORM_BIT0x80)) { |
179 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
180 | } |
181 | |
182 | version = ngtcp2_get_uint32(&pkt[1]); |
183 | |
184 | if (version == 0) { |
185 | type = NGTCP2_PKT_VERSION_NEGOTIATION; |
186 | /* This must be Version Negotiation packet which lacks packet |
187 | number and payload length fields. */ |
188 | len = 5 + 2; |
189 | } else { |
190 | if (!(pkt[0] & NGTCP2_FIXED_BIT_MASK0x40)) { |
191 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
192 | } |
193 | |
194 | type = ngtcp2_pkt_get_type_long(pkt[0]); |
195 | switch (type) { |
196 | case NGTCP2_PKT_INITIAL: |
197 | len = 1 /* Token Length */ + NGTCP2_MIN_LONG_HEADERLEN(1 + 4 + 1 + 1 + 1 + 1) - |
198 | 1; /* Cut packet number field */ |
199 | break; |
200 | case NGTCP2_PKT_RETRY: |
201 | /* Retry packet does not have packet number and length fields */ |
202 | len = 5 + 2; |
203 | break; |
204 | case NGTCP2_PKT_HANDSHAKE: |
205 | case NGTCP2_PKT_0RTT: |
206 | len = NGTCP2_MIN_LONG_HEADERLEN(1 + 4 + 1 + 1 + 1 + 1) - 1; /* Cut packet number field */ |
207 | break; |
208 | default: |
209 | /* Unreachable */ |
210 | assert(0)((void) (0)); |
211 | } |
212 | } |
213 | |
214 | if (pktlen < len) { |
215 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
216 | } |
217 | |
218 | p = &pkt[5]; |
219 | dcil = *p; |
220 | if (dcil > NGTCP2_MAX_CIDLEN20) { |
221 | /* QUIC v1 implementation never expect to receive CID length more |
222 | than NGTCP2_MAX_CIDLEN. */ |
223 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
224 | } |
225 | len += dcil; |
226 | |
227 | if (pktlen < len) { |
228 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
229 | } |
230 | |
231 | p += 1 + dcil; |
232 | scil = *p; |
233 | if (scil > NGTCP2_MAX_CIDLEN20) { |
234 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
235 | } |
236 | len += scil; |
237 | |
238 | if (pktlen < len) { |
239 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
240 | } |
241 | |
242 | p += 1 + scil; |
243 | |
244 | if (type == NGTCP2_PKT_INITIAL) { |
245 | /* Token Length */ |
246 | ntokenlen = ngtcp2_get_varint_len(p); |
247 | len += ntokenlen - 1; |
248 | |
249 | if (pktlen < len) { |
250 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
251 | } |
252 | |
253 | vi = ngtcp2_get_varint(&ntokenlen, p); |
254 | if (pktlen - len < vi) { |
255 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
256 | } |
257 | tokenlen = (size_t)vi; |
258 | len += tokenlen; |
259 | |
260 | p += ntokenlen; |
261 | |
262 | if (tokenlen) { |
263 | token = p; |
264 | } |
265 | |
266 | p += tokenlen; |
267 | } |
268 | |
269 | switch (type) { |
270 | case NGTCP2_PKT_VERSION_NEGOTIATION: |
271 | case NGTCP2_PKT_RETRY: |
272 | break; |
273 | default: |
274 | /* Length */ |
275 | n = ngtcp2_get_varint_len(p); |
276 | len += n - 1; |
277 | |
278 | if (pktlen < len) { |
279 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
280 | } |
281 | } |
282 | |
283 | dest->flags = NGTCP2_PKT_FLAG_LONG_FORM0x01; |
284 | dest->type = type; |
285 | dest->version = version; |
286 | dest->pkt_num = 0; |
287 | dest->pkt_numlen = 0; |
288 | |
289 | p = &pkt[6]; |
290 | ngtcp2_cid_init(&dest->dcid, p, dcil); |
291 | p += dcil + 1; |
292 | ngtcp2_cid_init(&dest->scid, p, scil); |
293 | p += scil; |
294 | |
295 | dest->token.base = (uint8_t *)token; |
296 | dest->token.len = tokenlen; |
297 | p += ntokenlen + tokenlen; |
298 | |
299 | switch (type) { |
300 | case NGTCP2_PKT_VERSION_NEGOTIATION: |
301 | case NGTCP2_PKT_RETRY: |
302 | dest->len = 0; |
303 | break; |
304 | default: |
305 | vi = ngtcp2_get_varint(&n, p); |
306 | if (vi > SIZE_MAX(18446744073709551615UL)) { |
307 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
308 | } |
309 | dest->len = (size_t)vi; |
310 | p += n; |
311 | } |
312 | |
313 | assert((size_t)(p - pkt) == len)((void) (0)); |
314 | |
315 | return (ngtcp2_ssize)len; |
316 | } |
317 | |
318 | ngtcp2_ssize ngtcp2_pkt_decode_hd_short(ngtcp2_pkt_hd *dest, const uint8_t *pkt, |
319 | size_t pktlen, size_t dcidlen) { |
320 | size_t len = 1 + dcidlen; |
321 | const uint8_t *p = pkt; |
322 | |
323 | assert(dcidlen <= NGTCP2_MAX_CIDLEN)((void) (0)); |
324 | |
325 | if (pktlen < len) { |
326 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
327 | } |
328 | |
329 | if ((pkt[0] & NGTCP2_HEADER_FORM_BIT0x80) || |
330 | (pkt[0] & NGTCP2_FIXED_BIT_MASK0x40) == 0) { |
331 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
332 | } |
333 | |
334 | p = &pkt[1]; |
335 | |
336 | dest->type = NGTCP2_PKT_SHORT; |
337 | |
338 | ngtcp2_cid_init(&dest->dcid, p, dcidlen); |
339 | p += dcidlen; |
340 | |
341 | /* Set 0 to SCID so that we don't accidentally reference it and gets |
342 | garbage. */ |
343 | ngtcp2_cid_zero(&dest->scid); |
344 | |
345 | dest->flags = NGTCP2_PKT_FLAG_NONE0; |
346 | dest->version = 0; |
347 | dest->len = 0; |
348 | dest->pkt_num = 0; |
349 | dest->pkt_numlen = 0; |
350 | |
351 | assert((size_t)(p - pkt) == len)((void) (0)); |
352 | |
353 | return (ngtcp2_ssize)len; |
354 | } |
355 | |
356 | ngtcp2_ssize ngtcp2_pkt_encode_hd_long(uint8_t *out, size_t outlen, |
357 | const ngtcp2_pkt_hd *hd) { |
358 | uint8_t *p; |
359 | size_t len = NGTCP2_MIN_LONG_HEADERLEN(1 + 4 + 1 + 1 + 1 + 1) + hd->dcid.datalen + hd->scid.datalen - |
360 | 2; /* NGTCP2_MIN_LONG_HEADERLEN includes 1 byte for |
361 | len and 1 byte for packet number. */ |
362 | |
363 | if (hd->type != NGTCP2_PKT_RETRY) { |
364 | len += 2 /* Length */ + hd->pkt_numlen; |
365 | } |
366 | |
367 | if (hd->type == NGTCP2_PKT_INITIAL) { |
368 | len += ngtcp2_put_varint_len(hd->token.len) + hd->token.len; |
369 | } |
370 | |
371 | if (outlen < len) { |
372 | return NGTCP2_ERR_NOBUF-203; |
373 | } |
374 | |
375 | p = out; |
376 | |
377 | *p++ = (uint8_t)(NGTCP2_HEADER_FORM_BIT0x80 | NGTCP2_FIXED_BIT_MASK0x40 | |
378 | (hd->type << 4) | (uint8_t)(hd->pkt_numlen - 1)); |
379 | p = ngtcp2_put_uint32be(p, hd->version); |
380 | *p++ = (uint8_t)hd->dcid.datalen; |
381 | if (hd->dcid.datalen) { |
382 | p = ngtcp2_cpymem(p, hd->dcid.data, hd->dcid.datalen); |
383 | } |
384 | *p++ = (uint8_t)hd->scid.datalen; |
385 | if (hd->scid.datalen) { |
386 | p = ngtcp2_cpymem(p, hd->scid.data, hd->scid.datalen); |
387 | } |
388 | |
389 | if (hd->type == NGTCP2_PKT_INITIAL) { |
390 | p = ngtcp2_put_varint(p, hd->token.len); |
391 | if (hd->token.len) { |
392 | p = ngtcp2_cpymem(p, hd->token.base, hd->token.len); |
393 | } |
394 | } |
395 | |
396 | if (hd->type != NGTCP2_PKT_RETRY) { |
397 | p = ngtcp2_put_varint14(p, (uint16_t)hd->len); |
398 | p = ngtcp2_put_pkt_num(p, hd->pkt_num, hd->pkt_numlen); |
399 | } |
400 | |
401 | assert((size_t)(p - out) == len)((void) (0)); |
402 | |
403 | return (ngtcp2_ssize)len; |
404 | } |
405 | |
406 | ngtcp2_ssize ngtcp2_pkt_encode_hd_short(uint8_t *out, size_t outlen, |
407 | const ngtcp2_pkt_hd *hd) { |
408 | uint8_t *p; |
409 | size_t len = 1 + hd->dcid.datalen + hd->pkt_numlen; |
410 | |
411 | if (outlen < len) { |
412 | return NGTCP2_ERR_NOBUF-203; |
413 | } |
414 | |
415 | p = out; |
416 | |
417 | *p = NGTCP2_FIXED_BIT_MASK0x40 | (uint8_t)(hd->pkt_numlen - 1); |
418 | if (hd->flags & NGTCP2_PKT_FLAG_KEY_PHASE0x04) { |
419 | *p |= NGTCP2_SHORT_KEY_PHASE_BIT0x04; |
420 | } |
421 | |
422 | ++p; |
423 | |
424 | if (hd->dcid.datalen) { |
425 | p = ngtcp2_cpymem(p, hd->dcid.data, hd->dcid.datalen); |
426 | } |
427 | |
428 | p = ngtcp2_put_pkt_num(p, hd->pkt_num, hd->pkt_numlen); |
429 | |
430 | assert((size_t)(p - out) == len)((void) (0)); |
431 | |
432 | return (ngtcp2_ssize)len; |
433 | } |
434 | |
435 | ngtcp2_ssize ngtcp2_pkt_decode_frame(ngtcp2_frame *dest, const uint8_t *payload, |
436 | size_t payloadlen) { |
437 | uint8_t type; |
438 | |
439 | if (payloadlen == 0) { |
440 | return 0; |
441 | } |
442 | |
443 | type = payload[0]; |
444 | |
445 | switch (type) { |
446 | case NGTCP2_FRAME_PADDING: |
447 | return (ngtcp2_ssize)ngtcp2_pkt_decode_padding_frame(&dest->padding, |
448 | payload, payloadlen); |
449 | case NGTCP2_FRAME_RESET_STREAM: |
450 | return ngtcp2_pkt_decode_reset_stream_frame(&dest->reset_stream, payload, |
451 | payloadlen); |
452 | case NGTCP2_FRAME_CONNECTION_CLOSE: |
453 | case NGTCP2_FRAME_CONNECTION_CLOSE_APP: |
454 | return ngtcp2_pkt_decode_connection_close_frame(&dest->connection_close, |
455 | payload, payloadlen); |
456 | case NGTCP2_FRAME_MAX_DATA: |
457 | return ngtcp2_pkt_decode_max_data_frame(&dest->max_data, payload, |
458 | payloadlen); |
459 | case NGTCP2_FRAME_MAX_STREAM_DATA: |
460 | return ngtcp2_pkt_decode_max_stream_data_frame(&dest->max_stream_data, |
461 | payload, payloadlen); |
462 | case NGTCP2_FRAME_MAX_STREAMS_BIDI: |
463 | case NGTCP2_FRAME_MAX_STREAMS_UNI: |
464 | return ngtcp2_pkt_decode_max_streams_frame(&dest->max_streams, payload, |
465 | payloadlen); |
466 | case NGTCP2_FRAME_PING: |
467 | return ngtcp2_pkt_decode_ping_frame(&dest->ping, payload, payloadlen); |
468 | case NGTCP2_FRAME_DATA_BLOCKED: |
469 | return ngtcp2_pkt_decode_data_blocked_frame(&dest->data_blocked, payload, |
470 | payloadlen); |
471 | case NGTCP2_FRAME_STREAM_DATA_BLOCKED: |
472 | return ngtcp2_pkt_decode_stream_data_blocked_frame( |
473 | &dest->stream_data_blocked, payload, payloadlen); |
474 | case NGTCP2_FRAME_STREAMS_BLOCKED_BIDI: |
475 | case NGTCP2_FRAME_STREAMS_BLOCKED_UNI: |
476 | return ngtcp2_pkt_decode_streams_blocked_frame(&dest->streams_blocked, |
477 | payload, payloadlen); |
478 | case NGTCP2_FRAME_NEW_CONNECTION_ID: |
479 | return ngtcp2_pkt_decode_new_connection_id_frame(&dest->new_connection_id, |
480 | payload, payloadlen); |
481 | case NGTCP2_FRAME_STOP_SENDING: |
482 | return ngtcp2_pkt_decode_stop_sending_frame(&dest->stop_sending, payload, |
483 | payloadlen); |
484 | case NGTCP2_FRAME_ACK: |
485 | case NGTCP2_FRAME_ACK_ECN: |
486 | return ngtcp2_pkt_decode_ack_frame(&dest->ack, payload, payloadlen); |
487 | case NGTCP2_FRAME_PATH_CHALLENGE: |
488 | return ngtcp2_pkt_decode_path_challenge_frame(&dest->path_challenge, |
489 | payload, payloadlen); |
490 | case NGTCP2_FRAME_PATH_RESPONSE: |
491 | return ngtcp2_pkt_decode_path_response_frame(&dest->path_response, payload, |
492 | payloadlen); |
493 | case NGTCP2_FRAME_CRYPTO: |
494 | return ngtcp2_pkt_decode_crypto_frame(&dest->crypto, payload, payloadlen); |
495 | case NGTCP2_FRAME_NEW_TOKEN: |
496 | return ngtcp2_pkt_decode_new_token_frame(&dest->new_token, payload, |
497 | payloadlen); |
498 | case NGTCP2_FRAME_RETIRE_CONNECTION_ID: |
499 | return ngtcp2_pkt_decode_retire_connection_id_frame( |
500 | &dest->retire_connection_id, payload, payloadlen); |
501 | case NGTCP2_FRAME_HANDSHAKE_DONE: |
502 | return ngtcp2_pkt_decode_handshake_done_frame(&dest->handshake_done, |
503 | payload, payloadlen); |
504 | case NGTCP2_FRAME_DATAGRAM: |
505 | case NGTCP2_FRAME_DATAGRAM_LEN: |
506 | return ngtcp2_pkt_decode_datagram_frame(&dest->datagram, payload, |
507 | payloadlen); |
508 | default: |
509 | if (has_mask(type, NGTCP2_FRAME_STREAM)) { |
510 | return ngtcp2_pkt_decode_stream_frame(&dest->stream, payload, payloadlen); |
511 | } |
512 | return NGTCP2_ERR_FRAME_ENCODING-219; |
513 | } |
514 | } |
515 | |
516 | ngtcp2_ssize ngtcp2_pkt_decode_stream_frame(ngtcp2_stream *dest, |
517 | const uint8_t *payload, |
518 | size_t payloadlen) { |
519 | uint8_t type; |
520 | size_t len = 1 + 1; |
521 | const uint8_t *p; |
522 | size_t datalen; |
523 | size_t ndatalen = 0; |
524 | size_t n; |
525 | uint64_t vi; |
526 | |
527 | if (payloadlen < len) { |
528 | return NGTCP2_ERR_FRAME_ENCODING-219; |
529 | } |
530 | |
531 | type = payload[0]; |
532 | |
533 | p = payload + 1; |
534 | |
535 | n = ngtcp2_get_varint_len(p); |
536 | len += n - 1; |
537 | |
538 | if (payloadlen < len) { |
539 | return NGTCP2_ERR_FRAME_ENCODING-219; |
540 | } |
541 | |
542 | p += n; |
543 | |
544 | if (type & NGTCP2_STREAM_OFF_BIT0x04) { |
545 | ++len; |
546 | if (payloadlen < len) { |
547 | return NGTCP2_ERR_FRAME_ENCODING-219; |
548 | } |
549 | |
550 | n = ngtcp2_get_varint_len(p); |
551 | len += n - 1; |
552 | |
553 | if (payloadlen < len) { |
554 | return NGTCP2_ERR_FRAME_ENCODING-219; |
555 | } |
556 | |
557 | p += n; |
558 | } |
559 | |
560 | if (type & NGTCP2_STREAM_LEN_BIT0x02) { |
561 | ++len; |
562 | if (payloadlen < len) { |
563 | return NGTCP2_ERR_FRAME_ENCODING-219; |
564 | } |
565 | |
566 | ndatalen = ngtcp2_get_varint_len(p); |
567 | len += ndatalen - 1; |
568 | |
569 | if (payloadlen < len) { |
570 | return NGTCP2_ERR_FRAME_ENCODING-219; |
571 | } |
572 | |
573 | vi = ngtcp2_get_varint(&ndatalen, p); |
574 | if (payloadlen - len < vi) { |
575 | return NGTCP2_ERR_FRAME_ENCODING-219; |
576 | } |
577 | datalen = (size_t)vi; |
578 | len += datalen; |
579 | } else { |
580 | len = payloadlen; |
581 | } |
582 | |
583 | p = payload + 1; |
584 | |
585 | dest->type = NGTCP2_FRAME_STREAM; |
586 | dest->flags = (uint8_t)(type & ~NGTCP2_FRAME_STREAM); |
587 | dest->fin = (type & NGTCP2_STREAM_FIN_BIT0x01) != 0; |
588 | dest->stream_id = (int64_t)ngtcp2_get_varint(&n, p); |
589 | p += n; |
590 | |
591 | if (type & NGTCP2_STREAM_OFF_BIT0x04) { |
592 | dest->offset = ngtcp2_get_varint(&n, p); |
593 | p += n; |
594 | } else { |
595 | dest->offset = 0; |
596 | } |
597 | |
598 | if (type & NGTCP2_STREAM_LEN_BIT0x02) { |
599 | p += ndatalen; |
600 | } else { |
601 | datalen = payloadlen - (size_t)(p - payload); |
602 | } |
603 | |
604 | if (datalen) { |
605 | dest->data[0].len = datalen; |
606 | dest->data[0].base = (uint8_t *)p; |
607 | dest->datacnt = 1; |
608 | p += datalen; |
609 | } else { |
610 | dest->datacnt = 0; |
611 | } |
612 | |
613 | assert((size_t)(p - payload) == len)((void) (0)); |
614 | |
615 | return (ngtcp2_ssize)len; |
616 | } |
617 | |
618 | ngtcp2_ssize ngtcp2_pkt_decode_ack_frame(ngtcp2_ack *dest, |
619 | const uint8_t *payload, |
620 | size_t payloadlen) { |
621 | size_t num_blks, max_num_blks; |
622 | size_t nnum_blks; |
623 | size_t len = 1 + 1 + 1 + 1 + 1; |
624 | const uint8_t *p; |
625 | size_t i, j; |
626 | ngtcp2_ack_blk *blk; |
627 | size_t n; |
628 | uint8_t type; |
629 | uint64_t vi; |
630 | |
631 | if (payloadlen < len) { |
632 | return NGTCP2_ERR_FRAME_ENCODING-219; |
633 | } |
634 | |
635 | type = payload[0]; |
636 | |
637 | p = payload + 1; |
638 | |
639 | /* Largest Acknowledged */ |
640 | n = ngtcp2_get_varint_len(p); |
641 | len += n - 1; |
642 | |
643 | if (payloadlen < len) { |
644 | return NGTCP2_ERR_FRAME_ENCODING-219; |
645 | } |
646 | |
647 | p += n; |
648 | |
649 | /* ACK Delay */ |
650 | n = ngtcp2_get_varint_len(p); |
651 | len += n - 1; |
652 | |
653 | if (payloadlen < len) { |
654 | return NGTCP2_ERR_FRAME_ENCODING-219; |
655 | } |
656 | |
657 | p += n; |
658 | |
659 | /* ACK Block Count */ |
660 | nnum_blks = ngtcp2_get_varint_len(p); |
661 | len += nnum_blks - 1; |
662 | |
663 | if (payloadlen < len) { |
664 | return NGTCP2_ERR_FRAME_ENCODING-219; |
665 | } |
666 | |
667 | vi = ngtcp2_get_varint(&nnum_blks, p); |
668 | if (vi > SIZE_MAX(18446744073709551615UL) / (1 + 1) || payloadlen - len < vi * (1 + 1)) { |
669 | return NGTCP2_ERR_FRAME_ENCODING-219; |
670 | } |
671 | |
672 | num_blks = (size_t)vi; |
673 | len += num_blks * (1 + 1); |
674 | |
675 | p += nnum_blks; |
676 | |
677 | /* First ACK Block */ |
678 | n = ngtcp2_get_varint_len(p); |
679 | len += n - 1; |
680 | |
681 | if (payloadlen < len) { |
682 | return NGTCP2_ERR_FRAME_ENCODING-219; |
683 | } |
684 | |
685 | p += n; |
686 | |
687 | for (i = 0; i < num_blks; ++i) { |
688 | /* Gap, and Additional ACK Block */ |
689 | for (j = 0; j < 2; ++j) { |
690 | n = ngtcp2_get_varint_len(p); |
691 | len += n - 1; |
692 | |
693 | if (payloadlen < len) { |
694 | return NGTCP2_ERR_FRAME_ENCODING-219; |
695 | } |
696 | |
697 | p += n; |
698 | } |
699 | } |
700 | |
701 | if (type == NGTCP2_FRAME_ACK_ECN) { |
702 | len += 3; |
703 | if (payloadlen < len) { |
704 | return NGTCP2_ERR_FRAME_ENCODING-219; |
705 | } |
706 | |
707 | for (i = 0; i < 3; ++i) { |
708 | n = ngtcp2_get_varint_len(p); |
709 | len += n - 1; |
710 | |
711 | if (payloadlen < len) { |
712 | return NGTCP2_ERR_FRAME_ENCODING-219; |
713 | } |
714 | |
715 | p += n; |
716 | } |
717 | } |
718 | |
719 | /* TODO We might not decode all blocks. It could be very large. */ |
720 | max_num_blks = ngtcp2_min(NGTCP2_MAX_ACK_BLKS, num_blks)((32) < (num_blks) ? (32) : (num_blks)); |
721 | |
722 | p = payload + 1; |
723 | |
724 | dest->type = type; |
725 | dest->largest_ack = (int64_t)ngtcp2_get_varint(&n, p); |
726 | p += n; |
727 | dest->ack_delay = ngtcp2_get_varint(&n, p); |
728 | /* This value will be assigned in the upper layer. */ |
729 | dest->ack_delay_unscaled = 0; |
730 | p += n; |
731 | dest->num_blks = max_num_blks; |
732 | p += nnum_blks; |
733 | dest->first_ack_blklen = ngtcp2_get_varint(&n, p); |
734 | p += n; |
735 | |
736 | for (i = 0; i < max_num_blks; ++i) { |
737 | blk = &dest->blks[i]; |
738 | blk->gap = ngtcp2_get_varint(&n, p); |
739 | p += n; |
740 | blk->blklen = ngtcp2_get_varint(&n, p); |
741 | p += n; |
742 | } |
743 | for (i = max_num_blks; i < num_blks; ++i) { |
744 | p += ngtcp2_get_varint_len(p); |
745 | p += ngtcp2_get_varint_len(p); |
746 | } |
747 | |
748 | if (type == NGTCP2_FRAME_ACK_ECN) { |
749 | dest->ecn.ect0 = ngtcp2_get_varint(&n, p); |
750 | p += n; |
751 | |
752 | dest->ecn.ect1 = ngtcp2_get_varint(&n, p); |
753 | p += n; |
754 | |
755 | dest->ecn.ce = ngtcp2_get_varint(&n, p); |
756 | p += n; |
757 | } |
758 | |
759 | assert((size_t)(p - payload) == len)((void) (0)); |
760 | |
761 | return (ngtcp2_ssize)len; |
762 | } |
763 | |
764 | size_t ngtcp2_pkt_decode_padding_frame(ngtcp2_padding *dest, |
765 | const uint8_t *payload, |
766 | size_t payloadlen) { |
767 | const uint8_t *p, *ep; |
768 | |
769 | assert(payloadlen > 0)((void) (0)); |
770 | |
771 | p = payload + 1; |
772 | ep = payload + payloadlen; |
773 | |
774 | for (; p != ep && *p == NGTCP2_FRAME_PADDING; ++p) |
775 | ; |
776 | |
777 | dest->type = NGTCP2_FRAME_PADDING; |
778 | dest->len = (size_t)(p - payload); |
779 | |
780 | return dest->len; |
781 | } |
782 | |
783 | ngtcp2_ssize ngtcp2_pkt_decode_reset_stream_frame(ngtcp2_reset_stream *dest, |
784 | const uint8_t *payload, |
785 | size_t payloadlen) { |
786 | size_t len = 1 + 1 + 1 + 1; |
787 | const uint8_t *p; |
788 | size_t n; |
789 | |
790 | if (payloadlen < len) { |
791 | return NGTCP2_ERR_FRAME_ENCODING-219; |
792 | } |
793 | |
794 | p = payload + 1; |
795 | |
796 | n = ngtcp2_get_varint_len(p); |
797 | len += n - 1; |
798 | if (payloadlen < len) { |
799 | return NGTCP2_ERR_FRAME_ENCODING-219; |
800 | } |
801 | p += n; |
802 | n = ngtcp2_get_varint_len(p); |
803 | len += n - 1; |
804 | if (payloadlen < len) { |
805 | return NGTCP2_ERR_FRAME_ENCODING-219; |
806 | } |
807 | p += n; |
808 | n = ngtcp2_get_varint_len(p); |
809 | len += n - 1; |
810 | if (payloadlen < len) { |
811 | return NGTCP2_ERR_FRAME_ENCODING-219; |
812 | } |
813 | |
814 | p = payload + 1; |
815 | |
816 | dest->type = NGTCP2_FRAME_RESET_STREAM; |
817 | dest->stream_id = (int64_t)ngtcp2_get_varint(&n, p); |
818 | p += n; |
819 | dest->app_error_code = ngtcp2_get_varint(&n, p); |
820 | p += n; |
821 | dest->final_size = ngtcp2_get_varint(&n, p); |
822 | p += n; |
823 | |
824 | assert((size_t)(p - payload) == len)((void) (0)); |
825 | |
826 | return (ngtcp2_ssize)len; |
827 | } |
828 | |
829 | ngtcp2_ssize ngtcp2_pkt_decode_connection_close_frame( |
830 | ngtcp2_connection_close *dest, const uint8_t *payload, size_t payloadlen) { |
831 | size_t len = 1 + 1 + 1; |
832 | const uint8_t *p; |
833 | size_t reasonlen; |
834 | size_t nreasonlen; |
835 | size_t n; |
836 | uint8_t type; |
837 | uint64_t vi; |
838 | |
839 | if (payloadlen < len) { |
840 | return NGTCP2_ERR_FRAME_ENCODING-219; |
841 | } |
842 | |
843 | type = payload[0]; |
844 | |
845 | p = payload + 1; |
846 | |
847 | n = ngtcp2_get_varint_len(p); |
848 | len += n - 1; |
849 | if (payloadlen < len) { |
850 | return NGTCP2_ERR_FRAME_ENCODING-219; |
851 | } |
852 | |
853 | p += n; |
854 | |
855 | if (type == NGTCP2_FRAME_CONNECTION_CLOSE) { |
856 | ++len; |
857 | |
858 | n = ngtcp2_get_varint_len(p); |
859 | len += n - 1; |
860 | if (payloadlen < len) { |
861 | return NGTCP2_ERR_FRAME_ENCODING-219; |
862 | } |
863 | |
864 | p += n; |
865 | } |
866 | |
867 | nreasonlen = ngtcp2_get_varint_len(p); |
868 | len += nreasonlen - 1; |
869 | if (payloadlen < len) { |
870 | return NGTCP2_ERR_FRAME_ENCODING-219; |
871 | } |
872 | |
873 | vi = ngtcp2_get_varint(&nreasonlen, p); |
874 | if (payloadlen - len < vi) { |
875 | return NGTCP2_ERR_FRAME_ENCODING-219; |
876 | } |
877 | reasonlen = (size_t)vi; |
878 | len += reasonlen; |
879 | |
880 | p = payload + 1; |
881 | |
882 | dest->type = type; |
883 | dest->error_code = ngtcp2_get_varint(&n, p); |
884 | p += n; |
885 | if (type == NGTCP2_FRAME_CONNECTION_CLOSE) { |
886 | dest->frame_type = ngtcp2_get_varint(&n, p); |
887 | p += n; |
888 | } else { |
889 | dest->frame_type = 0; |
890 | } |
891 | dest->reasonlen = reasonlen; |
892 | p += nreasonlen; |
893 | if (reasonlen == 0) { |
894 | dest->reason = NULL((void*)0); |
895 | } else { |
896 | dest->reason = (uint8_t *)p; |
897 | p += reasonlen; |
898 | } |
899 | |
900 | assert((size_t)(p - payload) == len)((void) (0)); |
901 | |
902 | return (ngtcp2_ssize)len; |
903 | } |
904 | |
905 | ngtcp2_ssize ngtcp2_pkt_decode_max_data_frame(ngtcp2_max_data *dest, |
906 | const uint8_t *payload, |
907 | size_t payloadlen) { |
908 | size_t len = 1 + 1; |
909 | const uint8_t *p; |
910 | size_t n; |
911 | |
912 | if (payloadlen < len) { |
913 | return NGTCP2_ERR_FRAME_ENCODING-219; |
914 | } |
915 | |
916 | p = payload + 1; |
917 | |
918 | n = ngtcp2_get_varint_len(p); |
919 | len += n - 1; |
920 | |
921 | if (payloadlen < len) { |
922 | return NGTCP2_ERR_FRAME_ENCODING-219; |
923 | } |
924 | |
925 | dest->type = NGTCP2_FRAME_MAX_DATA; |
926 | dest->max_data = ngtcp2_get_varint(&n, p); |
927 | p += n; |
928 | |
929 | assert((size_t)(p - payload) == len)((void) (0)); |
930 | |
931 | return (ngtcp2_ssize)len; |
932 | } |
933 | |
934 | ngtcp2_ssize ngtcp2_pkt_decode_max_stream_data_frame( |
935 | ngtcp2_max_stream_data *dest, const uint8_t *payload, size_t payloadlen) { |
936 | size_t len = 1 + 1 + 1; |
937 | const uint8_t *p; |
938 | size_t n; |
939 | |
940 | if (payloadlen < len) { |
941 | return NGTCP2_ERR_FRAME_ENCODING-219; |
942 | } |
943 | |
944 | p = payload + 1; |
945 | |
946 | n = ngtcp2_get_varint_len(p); |
947 | len += n - 1; |
948 | |
949 | if (payloadlen < len) { |
950 | return NGTCP2_ERR_FRAME_ENCODING-219; |
951 | } |
952 | |
953 | p += n; |
954 | |
955 | n = ngtcp2_get_varint_len(p); |
956 | len += n - 1; |
957 | |
958 | if (payloadlen < len) { |
959 | return NGTCP2_ERR_FRAME_ENCODING-219; |
960 | } |
961 | |
962 | p = payload + 1; |
963 | |
964 | dest->type = NGTCP2_FRAME_MAX_STREAM_DATA; |
965 | dest->stream_id = (int64_t)ngtcp2_get_varint(&n, p); |
966 | p += n; |
967 | dest->max_stream_data = ngtcp2_get_varint(&n, p); |
968 | p += n; |
969 | |
970 | assert((size_t)(p - payload) == len)((void) (0)); |
971 | |
972 | return (ngtcp2_ssize)len; |
973 | } |
974 | |
975 | ngtcp2_ssize ngtcp2_pkt_decode_max_streams_frame(ngtcp2_max_streams *dest, |
976 | const uint8_t *payload, |
977 | size_t payloadlen) { |
978 | size_t len = 1 + 1; |
979 | const uint8_t *p; |
980 | size_t n; |
981 | |
982 | if (payloadlen < len) { |
983 | return NGTCP2_ERR_FRAME_ENCODING-219; |
984 | } |
985 | |
986 | p = payload + 1; |
987 | |
988 | n = ngtcp2_get_varint_len(p); |
989 | len += n - 1; |
990 | |
991 | if (payloadlen < len) { |
992 | return NGTCP2_ERR_FRAME_ENCODING-219; |
993 | } |
994 | |
995 | dest->type = payload[0]; |
996 | dest->max_streams = ngtcp2_get_varint(&n, p); |
997 | p += n; |
998 | |
999 | assert((size_t)(p - payload) == len)((void) (0)); |
1000 | |
1001 | return (ngtcp2_ssize)len; |
1002 | } |
1003 | |
1004 | ngtcp2_ssize ngtcp2_pkt_decode_ping_frame(ngtcp2_ping *dest, |
1005 | const uint8_t *payload, |
1006 | size_t payloadlen) { |
1007 | (void)payload; |
1008 | (void)payloadlen; |
1009 | |
1010 | dest->type = NGTCP2_FRAME_PING; |
1011 | return 1; |
1012 | } |
1013 | |
1014 | ngtcp2_ssize ngtcp2_pkt_decode_data_blocked_frame(ngtcp2_data_blocked *dest, |
1015 | const uint8_t *payload, |
1016 | size_t payloadlen) { |
1017 | size_t len = 1 + 1; |
1018 | const uint8_t *p; |
1019 | size_t n; |
1020 | |
1021 | if (payloadlen < len) { |
1022 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1023 | } |
1024 | |
1025 | p = payload + 1; |
1026 | |
1027 | n = ngtcp2_get_varint_len(p); |
1028 | len += n - 1; |
1029 | |
1030 | if (payloadlen < len) { |
1031 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1032 | } |
1033 | |
1034 | dest->type = NGTCP2_FRAME_DATA_BLOCKED; |
1035 | dest->offset = ngtcp2_get_varint(&n, p); |
1036 | p += n; |
1037 | |
1038 | assert((size_t)(p - payload) == len)((void) (0)); |
1039 | |
1040 | return (ngtcp2_ssize)len; |
1041 | } |
1042 | |
1043 | ngtcp2_ssize |
1044 | ngtcp2_pkt_decode_stream_data_blocked_frame(ngtcp2_stream_data_blocked *dest, |
1045 | const uint8_t *payload, |
1046 | size_t payloadlen) { |
1047 | size_t len = 1 + 1 + 1; |
1048 | const uint8_t *p; |
1049 | size_t n; |
1050 | |
1051 | if (payloadlen < len) { |
1052 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1053 | } |
1054 | |
1055 | p = payload + 1; |
1056 | |
1057 | n = ngtcp2_get_varint_len(p); |
1058 | len += n - 1; |
1059 | |
1060 | if (payloadlen < len) { |
1061 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1062 | } |
1063 | |
1064 | p += n; |
1065 | |
1066 | n = ngtcp2_get_varint_len(p); |
1067 | len += n - 1; |
1068 | |
1069 | if (payloadlen < len) { |
1070 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1071 | } |
1072 | |
1073 | p = payload + 1; |
1074 | |
1075 | dest->type = NGTCP2_FRAME_STREAM_DATA_BLOCKED; |
1076 | dest->stream_id = (int64_t)ngtcp2_get_varint(&n, p); |
1077 | p += n; |
1078 | dest->offset = ngtcp2_get_varint(&n, p); |
1079 | p += n; |
1080 | |
1081 | assert((size_t)(p - payload) == len)((void) (0)); |
1082 | |
1083 | return (ngtcp2_ssize)len; |
1084 | } |
1085 | |
1086 | ngtcp2_ssize ngtcp2_pkt_decode_streams_blocked_frame( |
1087 | ngtcp2_streams_blocked *dest, const uint8_t *payload, size_t payloadlen) { |
1088 | size_t len = 1 + 1; |
1089 | const uint8_t *p; |
1090 | size_t n; |
1091 | |
1092 | if (payloadlen < len) { |
1093 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1094 | } |
1095 | |
1096 | p = payload + 1; |
1097 | |
1098 | n = ngtcp2_get_varint_len(p); |
1099 | len += n - 1; |
1100 | |
1101 | if (payloadlen < len) { |
1102 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1103 | } |
1104 | |
1105 | dest->type = payload[0]; |
1106 | dest->max_streams = ngtcp2_get_varint(&n, p); |
1107 | p += n; |
1108 | |
1109 | assert((size_t)(p - payload) == len)((void) (0)); |
1110 | |
1111 | return (ngtcp2_ssize)len; |
1112 | } |
1113 | |
1114 | ngtcp2_ssize ngtcp2_pkt_decode_new_connection_id_frame( |
1115 | ngtcp2_new_connection_id *dest, const uint8_t *payload, size_t payloadlen) { |
1116 | size_t len = 1 + 1 + 1 + 1 + 16; |
1117 | const uint8_t *p; |
1118 | size_t n; |
1119 | size_t cil; |
1120 | |
1121 | if (payloadlen < len) { |
1122 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1123 | } |
1124 | |
1125 | p = payload + 1; |
1126 | |
1127 | n = ngtcp2_get_varint_len(p); |
1128 | len += n - 1; |
1129 | if (payloadlen < len) { |
1130 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1131 | } |
1132 | |
1133 | p += n; |
1134 | |
1135 | n = ngtcp2_get_varint_len(p); |
1136 | len += n - 1; |
1137 | if (payloadlen < len) { |
1138 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1139 | } |
1140 | |
1141 | p += n; |
1142 | |
1143 | cil = *p; |
1144 | if (cil < NGTCP2_MIN_CIDLEN1 || cil > NGTCP2_MAX_CIDLEN20) { |
1145 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1146 | } |
1147 | |
1148 | len += cil; |
1149 | if (payloadlen < len) { |
1150 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1151 | } |
1152 | |
1153 | p = payload + 1; |
1154 | |
1155 | dest->type = NGTCP2_FRAME_NEW_CONNECTION_ID; |
1156 | dest->seq = ngtcp2_get_varint(&n, p); |
1157 | p += n; |
1158 | dest->retire_prior_to = ngtcp2_get_varint(&n, p); |
1159 | p += n + 1; |
1160 | ngtcp2_cid_init(&dest->cid, p, cil); |
1161 | p += cil; |
1162 | memcpy(dest->stateless_reset_token, p, NGTCP2_STATELESS_RESET_TOKENLEN16); |
1163 | p += NGTCP2_STATELESS_RESET_TOKENLEN16; |
1164 | |
1165 | assert((size_t)(p - payload) == len)((void) (0)); |
1166 | |
1167 | return (ngtcp2_ssize)len; |
1168 | } |
1169 | |
1170 | ngtcp2_ssize ngtcp2_pkt_decode_stop_sending_frame(ngtcp2_stop_sending *dest, |
1171 | const uint8_t *payload, |
1172 | size_t payloadlen) { |
1173 | size_t len = 1 + 1 + 1; |
1174 | const uint8_t *p; |
1175 | size_t n; |
1176 | |
1177 | if (payloadlen < len) { |
1178 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1179 | } |
1180 | |
1181 | p = payload + 1; |
1182 | |
1183 | n = ngtcp2_get_varint_len(p); |
1184 | len += n - 1; |
1185 | |
1186 | if (payloadlen < len) { |
1187 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1188 | } |
1189 | p += n; |
1190 | n = ngtcp2_get_varint_len(p); |
1191 | len += n - 1; |
1192 | |
1193 | if (payloadlen < len) { |
1194 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1195 | } |
1196 | |
1197 | p = payload + 1; |
1198 | |
1199 | dest->type = NGTCP2_FRAME_STOP_SENDING; |
1200 | dest->stream_id = (int64_t)ngtcp2_get_varint(&n, p); |
1201 | p += n; |
1202 | dest->app_error_code = ngtcp2_get_varint(&n, p); |
1203 | p += n; |
1204 | |
1205 | assert((size_t)(p - payload) == len)((void) (0)); |
1206 | |
1207 | return (ngtcp2_ssize)len; |
1208 | } |
1209 | |
1210 | ngtcp2_ssize ngtcp2_pkt_decode_path_challenge_frame(ngtcp2_path_challenge *dest, |
1211 | const uint8_t *payload, |
1212 | size_t payloadlen) { |
1213 | size_t len = 1 + 8; |
1214 | const uint8_t *p; |
1215 | |
1216 | if (payloadlen < len) { |
1217 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1218 | } |
1219 | |
1220 | p = payload + 1; |
1221 | |
1222 | dest->type = NGTCP2_FRAME_PATH_CHALLENGE; |
1223 | ngtcp2_cpymem(dest->data, p, sizeof(dest->data)); |
1224 | p += sizeof(dest->data); |
1225 | |
1226 | assert((size_t)(p - payload) == len)((void) (0)); |
1227 | |
1228 | return (ngtcp2_ssize)len; |
1229 | } |
1230 | |
1231 | ngtcp2_ssize ngtcp2_pkt_decode_path_response_frame(ngtcp2_path_response *dest, |
1232 | const uint8_t *payload, |
1233 | size_t payloadlen) { |
1234 | size_t len = 1 + 8; |
1235 | const uint8_t *p; |
1236 | |
1237 | if (payloadlen < len) { |
1238 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1239 | } |
1240 | |
1241 | p = payload + 1; |
1242 | |
1243 | dest->type = NGTCP2_FRAME_PATH_RESPONSE; |
1244 | ngtcp2_cpymem(dest->data, p, sizeof(dest->data)); |
1245 | p += sizeof(dest->data); |
1246 | |
1247 | assert((size_t)(p - payload) == len)((void) (0)); |
1248 | |
1249 | return (ngtcp2_ssize)len; |
1250 | } |
1251 | |
1252 | ngtcp2_ssize ngtcp2_pkt_decode_crypto_frame(ngtcp2_crypto *dest, |
1253 | const uint8_t *payload, |
1254 | size_t payloadlen) { |
1255 | size_t len = 1 + 1 + 1; |
1256 | const uint8_t *p; |
1257 | size_t datalen; |
1258 | size_t ndatalen; |
1259 | size_t n; |
1260 | uint64_t vi; |
1261 | |
1262 | if (payloadlen < len) { |
1263 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1264 | } |
1265 | |
1266 | p = payload + 1; |
1267 | |
1268 | n = ngtcp2_get_varint_len(p); |
1269 | len += n - 1; |
1270 | |
1271 | if (payloadlen < len) { |
1272 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1273 | } |
1274 | |
1275 | p += n; |
1276 | |
1277 | ndatalen = ngtcp2_get_varint_len(p); |
1278 | len += ndatalen - 1; |
1279 | |
1280 | if (payloadlen < len) { |
1281 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1282 | } |
1283 | |
1284 | vi = ngtcp2_get_varint(&ndatalen, p); |
1285 | if (payloadlen - len < vi) { |
1286 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1287 | } |
1288 | |
1289 | datalen = (size_t)vi; |
1290 | len += datalen; |
1291 | |
1292 | p = payload + 1; |
1293 | |
1294 | dest->type = NGTCP2_FRAME_CRYPTO; |
1295 | dest->offset = ngtcp2_get_varint(&n, p); |
1296 | p += n; |
1297 | dest->data[0].len = datalen; |
1298 | p += ndatalen; |
1299 | if (dest->data[0].len) { |
1300 | dest->data[0].base = (uint8_t *)p; |
1301 | p += dest->data[0].len; |
1302 | dest->datacnt = 1; |
1303 | } else { |
1304 | dest->data[0].base = NULL((void*)0); |
1305 | dest->datacnt = 0; |
1306 | } |
1307 | |
1308 | assert((size_t)(p - payload) == len)((void) (0)); |
1309 | |
1310 | return (ngtcp2_ssize)len; |
1311 | } |
1312 | |
1313 | ngtcp2_ssize ngtcp2_pkt_decode_new_token_frame(ngtcp2_new_token *dest, |
1314 | const uint8_t *payload, |
1315 | size_t payloadlen) { |
1316 | size_t len = 1 + 1; |
1317 | const uint8_t *p; |
1318 | size_t n; |
1319 | size_t datalen; |
1320 | uint64_t vi; |
1321 | |
1322 | if (payloadlen < len) { |
1323 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1324 | } |
1325 | |
1326 | p = payload + 1; |
1327 | |
1328 | n = ngtcp2_get_varint_len(p); |
1329 | len += n - 1; |
1330 | |
1331 | if (payloadlen < len) { |
1332 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1333 | } |
1334 | |
1335 | vi = ngtcp2_get_varint(&n, p); |
1336 | if (payloadlen - len < vi) { |
1337 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1338 | } |
1339 | datalen = (size_t)vi; |
1340 | len += datalen; |
1341 | |
1342 | dest->type = NGTCP2_FRAME_NEW_TOKEN; |
1343 | dest->token.len = datalen; |
1344 | p += n; |
1345 | dest->token.base = (uint8_t *)p; |
1346 | p += dest->token.len; |
1347 | |
1348 | assert((size_t)(p - payload) == len)((void) (0)); |
1349 | |
1350 | return (ngtcp2_ssize)len; |
1351 | } |
1352 | |
1353 | ngtcp2_ssize |
1354 | ngtcp2_pkt_decode_retire_connection_id_frame(ngtcp2_retire_connection_id *dest, |
1355 | const uint8_t *payload, |
1356 | size_t payloadlen) { |
1357 | size_t len = 1 + 1; |
1358 | const uint8_t *p; |
1359 | size_t n; |
1360 | |
1361 | if (payloadlen < len) { |
1362 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1363 | } |
1364 | |
1365 | p = payload + 1; |
1366 | |
1367 | n = ngtcp2_get_varint_len(p); |
1368 | len += n - 1; |
1369 | |
1370 | if (payloadlen < len) { |
1371 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1372 | } |
1373 | |
1374 | dest->type = NGTCP2_FRAME_RETIRE_CONNECTION_ID; |
1375 | dest->seq = ngtcp2_get_varint(&n, p); |
1376 | p += n; |
1377 | |
1378 | assert((size_t)(p - payload) == len)((void) (0)); |
1379 | |
1380 | return (ngtcp2_ssize)len; |
1381 | } |
1382 | |
1383 | ngtcp2_ssize ngtcp2_pkt_decode_handshake_done_frame(ngtcp2_handshake_done *dest, |
1384 | const uint8_t *payload, |
1385 | size_t payloadlen) { |
1386 | (void)payload; |
1387 | (void)payloadlen; |
1388 | |
1389 | dest->type = NGTCP2_FRAME_HANDSHAKE_DONE; |
1390 | return 1; |
1391 | } |
1392 | |
1393 | ngtcp2_ssize ngtcp2_pkt_decode_datagram_frame(ngtcp2_datagram *dest, |
1394 | const uint8_t *payload, |
1395 | size_t payloadlen) { |
1396 | size_t len = 1; |
1397 | const uint8_t *p; |
1398 | uint8_t type; |
1399 | size_t datalen; |
1400 | size_t n; |
1401 | uint64_t vi; |
1402 | |
1403 | if (payloadlen < len) { |
1404 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1405 | } |
1406 | |
1407 | type = payload[0]; |
1408 | |
1409 | p = payload + 1; |
1410 | |
1411 | switch (type) { |
1412 | case NGTCP2_FRAME_DATAGRAM: |
1413 | datalen = payloadlen - 1; |
1414 | len = payloadlen; |
1415 | break; |
1416 | case NGTCP2_FRAME_DATAGRAM_LEN: |
1417 | ++len; |
1418 | if (payloadlen < len) { |
1419 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1420 | } |
1421 | |
1422 | n = ngtcp2_get_varint_len(p); |
1423 | len += n - 1; |
1424 | |
1425 | if (payloadlen < len) { |
1426 | return NGTCP2_ERR_FRAME_ENCODING-219; |
1427 | } |
1428 | |
1429 | vi = ngtcp2_get_varint(&n, p); |
1430 | if (payloadlen - len < vi) { |
1431 | return NGTCP2_FRAME_ENCODING_ERROR0x7u; |
1432 | } |
1433 | |
1434 | datalen = (size_t)vi; |
1435 | len += datalen; |
1436 | break; |
1437 | default: |
1438 | assert(0)((void) (0)); |
1439 | } |
1440 | |
1441 | dest->type = type; |
1442 | |
1443 | if (datalen == 0) { |
1444 | dest->datacnt = 0; |
1445 | dest->data = NULL((void*)0); |
1446 | |
1447 | if (type == NGTCP2_FRAME_DATAGRAM_LEN) { |
1448 | p += n; |
1449 | } |
1450 | } else { |
1451 | dest->datacnt = 1; |
1452 | dest->data = dest->rdata; |
1453 | dest->rdata[0].len = datalen; |
1454 | |
1455 | if (type == NGTCP2_FRAME_DATAGRAM_LEN) { |
1456 | p += n; |
1457 | } |
1458 | |
1459 | dest->rdata[0].base = (uint8_t *)p; |
1460 | p += datalen; |
1461 | } |
1462 | |
1463 | assert((size_t)(p - payload) == len)((void) (0)); |
1464 | |
1465 | return (ngtcp2_ssize)len; |
1466 | } |
1467 | |
1468 | ngtcp2_ssize ngtcp2_pkt_encode_frame(uint8_t *out, size_t outlen, |
1469 | ngtcp2_frame *fr) { |
1470 | switch (fr->type) { |
1471 | case NGTCP2_FRAME_STREAM: |
1472 | return ngtcp2_pkt_encode_stream_frame(out, outlen, &fr->stream); |
1473 | case NGTCP2_FRAME_ACK: |
1474 | case NGTCP2_FRAME_ACK_ECN: |
1475 | return ngtcp2_pkt_encode_ack_frame(out, outlen, &fr->ack); |
1476 | case NGTCP2_FRAME_PADDING: |
1477 | return ngtcp2_pkt_encode_padding_frame(out, outlen, &fr->padding); |
1478 | case NGTCP2_FRAME_RESET_STREAM: |
1479 | return ngtcp2_pkt_encode_reset_stream_frame(out, outlen, &fr->reset_stream); |
1480 | case NGTCP2_FRAME_CONNECTION_CLOSE: |
1481 | case NGTCP2_FRAME_CONNECTION_CLOSE_APP: |
1482 | return ngtcp2_pkt_encode_connection_close_frame(out, outlen, |
1483 | &fr->connection_close); |
1484 | case NGTCP2_FRAME_MAX_DATA: |
1485 | return ngtcp2_pkt_encode_max_data_frame(out, outlen, &fr->max_data); |
1486 | case NGTCP2_FRAME_MAX_STREAM_DATA: |
1487 | return ngtcp2_pkt_encode_max_stream_data_frame(out, outlen, |
1488 | &fr->max_stream_data); |
1489 | case NGTCP2_FRAME_MAX_STREAMS_BIDI: |
1490 | case NGTCP2_FRAME_MAX_STREAMS_UNI: |
1491 | return ngtcp2_pkt_encode_max_streams_frame(out, outlen, &fr->max_streams); |
1492 | case NGTCP2_FRAME_PING: |
1493 | return ngtcp2_pkt_encode_ping_frame(out, outlen, &fr->ping); |
1494 | case NGTCP2_FRAME_DATA_BLOCKED: |
1495 | return ngtcp2_pkt_encode_data_blocked_frame(out, outlen, &fr->data_blocked); |
1496 | case NGTCP2_FRAME_STREAM_DATA_BLOCKED: |
1497 | return ngtcp2_pkt_encode_stream_data_blocked_frame( |
1498 | out, outlen, &fr->stream_data_blocked); |
1499 | case NGTCP2_FRAME_STREAMS_BLOCKED_BIDI: |
1500 | case NGTCP2_FRAME_STREAMS_BLOCKED_UNI: |
1501 | return ngtcp2_pkt_encode_streams_blocked_frame(out, outlen, |
1502 | &fr->streams_blocked); |
1503 | case NGTCP2_FRAME_NEW_CONNECTION_ID: |
1504 | return ngtcp2_pkt_encode_new_connection_id_frame(out, outlen, |
1505 | &fr->new_connection_id); |
1506 | case NGTCP2_FRAME_STOP_SENDING: |
1507 | return ngtcp2_pkt_encode_stop_sending_frame(out, outlen, &fr->stop_sending); |
1508 | case NGTCP2_FRAME_PATH_CHALLENGE: |
1509 | return ngtcp2_pkt_encode_path_challenge_frame(out, outlen, |
1510 | &fr->path_challenge); |
1511 | case NGTCP2_FRAME_PATH_RESPONSE: |
1512 | return ngtcp2_pkt_encode_path_response_frame(out, outlen, |
1513 | &fr->path_response); |
1514 | case NGTCP2_FRAME_CRYPTO: |
1515 | return ngtcp2_pkt_encode_crypto_frame(out, outlen, &fr->crypto); |
1516 | case NGTCP2_FRAME_NEW_TOKEN: |
1517 | return ngtcp2_pkt_encode_new_token_frame(out, outlen, &fr->new_token); |
1518 | case NGTCP2_FRAME_RETIRE_CONNECTION_ID: |
1519 | return ngtcp2_pkt_encode_retire_connection_id_frame( |
1520 | out, outlen, &fr->retire_connection_id); |
1521 | case NGTCP2_FRAME_HANDSHAKE_DONE: |
1522 | return ngtcp2_pkt_encode_handshake_done_frame(out, outlen, |
1523 | &fr->handshake_done); |
1524 | case NGTCP2_FRAME_DATAGRAM: |
1525 | case NGTCP2_FRAME_DATAGRAM_LEN: |
1526 | return ngtcp2_pkt_encode_datagram_frame(out, outlen, &fr->datagram); |
1527 | default: |
1528 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
1529 | } |
1530 | } |
1531 | |
1532 | ngtcp2_ssize ngtcp2_pkt_encode_stream_frame(uint8_t *out, size_t outlen, |
1533 | ngtcp2_stream *fr) { |
1534 | size_t len = 1; |
1535 | uint8_t flags = NGTCP2_STREAM_LEN_BIT0x02; |
1536 | uint8_t *p; |
1537 | size_t i; |
1538 | size_t datalen = 0; |
1539 | |
1540 | if (fr->fin) { |
1541 | flags |= NGTCP2_STREAM_FIN_BIT0x01; |
1542 | } |
1543 | |
1544 | if (fr->offset) { |
1545 | flags |= NGTCP2_STREAM_OFF_BIT0x04; |
1546 | len += ngtcp2_put_varint_len(fr->offset); |
1547 | } |
1548 | |
1549 | len += ngtcp2_put_varint_len((uint64_t)fr->stream_id); |
1550 | |
1551 | for (i = 0; i < fr->datacnt; ++i) { |
1552 | datalen += fr->data[i].len; |
1553 | } |
1554 | |
1555 | len += ngtcp2_put_varint_len(datalen); |
1556 | len += datalen; |
1557 | |
1558 | if (outlen < len) { |
1559 | return NGTCP2_ERR_NOBUF-203; |
1560 | } |
1561 | |
1562 | p = out; |
1563 | |
1564 | *p++ = flags | NGTCP2_FRAME_STREAM; |
1565 | |
1566 | fr->flags = flags; |
1567 | |
1568 | p = ngtcp2_put_varint(p, (uint64_t)fr->stream_id); |
1569 | |
1570 | if (fr->offset) { |
1571 | p = ngtcp2_put_varint(p, fr->offset); |
1572 | } |
1573 | |
1574 | p = ngtcp2_put_varint(p, datalen); |
1575 | |
1576 | for (i = 0; i < fr->datacnt; ++i) { |
1577 | assert(fr->data[i].len)((void) (0)); |
1578 | assert(fr->data[i].base)((void) (0)); |
1579 | p = ngtcp2_cpymem(p, fr->data[i].base, fr->data[i].len); |
1580 | } |
1581 | |
1582 | assert((size_t)(p - out) == len)((void) (0)); |
1583 | |
1584 | return (ngtcp2_ssize)len; |
1585 | } |
1586 | |
1587 | ngtcp2_ssize ngtcp2_pkt_encode_ack_frame(uint8_t *out, size_t outlen, |
1588 | ngtcp2_ack *fr) { |
1589 | size_t len = 1 + ngtcp2_put_varint_len((uint64_t)fr->largest_ack) + |
1590 | ngtcp2_put_varint_len(fr->ack_delay) + |
1591 | ngtcp2_put_varint_len(fr->num_blks) + |
1592 | ngtcp2_put_varint_len(fr->first_ack_blklen); |
1593 | uint8_t *p; |
1594 | size_t i; |
1595 | const ngtcp2_ack_blk *blk; |
1596 | |
1597 | for (i = 0; i < fr->num_blks; ++i) { |
1598 | blk = &fr->blks[i]; |
1599 | len += ngtcp2_put_varint_len(blk->gap); |
1600 | len += ngtcp2_put_varint_len(blk->blklen); |
1601 | } |
1602 | |
1603 | if (fr->type == NGTCP2_FRAME_ACK_ECN) { |
1604 | len += ngtcp2_put_varint_len(fr->ecn.ect0) + |
1605 | ngtcp2_put_varint_len(fr->ecn.ect1) + |
1606 | ngtcp2_put_varint_len(fr->ecn.ce); |
1607 | } |
1608 | |
1609 | if (outlen < len) { |
1610 | return NGTCP2_ERR_NOBUF-203; |
1611 | } |
1612 | |
1613 | p = out; |
1614 | |
1615 | *p++ = fr->type; |
1616 | p = ngtcp2_put_varint(p, (uint64_t)fr->largest_ack); |
1617 | p = ngtcp2_put_varint(p, fr->ack_delay); |
1618 | p = ngtcp2_put_varint(p, fr->num_blks); |
1619 | p = ngtcp2_put_varint(p, fr->first_ack_blklen); |
1620 | |
1621 | for (i = 0; i < fr->num_blks; ++i) { |
1622 | blk = &fr->blks[i]; |
1623 | p = ngtcp2_put_varint(p, blk->gap); |
1624 | p = ngtcp2_put_varint(p, blk->blklen); |
1625 | } |
1626 | |
1627 | if (fr->type == NGTCP2_FRAME_ACK_ECN) { |
1628 | p = ngtcp2_put_varint(p, fr->ecn.ect0); |
1629 | p = ngtcp2_put_varint(p, fr->ecn.ect1); |
1630 | p = ngtcp2_put_varint(p, fr->ecn.ce); |
1631 | } |
1632 | |
1633 | assert((size_t)(p - out) == len)((void) (0)); |
1634 | |
1635 | return (ngtcp2_ssize)len; |
1636 | } |
1637 | |
1638 | ngtcp2_ssize ngtcp2_pkt_encode_padding_frame(uint8_t *out, size_t outlen, |
1639 | const ngtcp2_padding *fr) { |
1640 | if (outlen < fr->len) { |
1641 | return NGTCP2_ERR_NOBUF-203; |
1642 | } |
1643 | |
1644 | memset(out, 0, fr->len); |
1645 | |
1646 | return (ngtcp2_ssize)fr->len; |
1647 | } |
1648 | |
1649 | ngtcp2_ssize |
1650 | ngtcp2_pkt_encode_reset_stream_frame(uint8_t *out, size_t outlen, |
1651 | const ngtcp2_reset_stream *fr) { |
1652 | size_t len = 1 + ngtcp2_put_varint_len((uint64_t)fr->stream_id) + |
1653 | ngtcp2_put_varint_len(fr->app_error_code) + |
1654 | ngtcp2_put_varint_len(fr->final_size); |
1655 | uint8_t *p; |
1656 | |
1657 | if (outlen < len) { |
1658 | return NGTCP2_ERR_NOBUF-203; |
1659 | } |
1660 | |
1661 | p = out; |
1662 | |
1663 | *p++ = NGTCP2_FRAME_RESET_STREAM; |
1664 | p = ngtcp2_put_varint(p, (uint64_t)fr->stream_id); |
1665 | p = ngtcp2_put_varint(p, fr->app_error_code); |
1666 | p = ngtcp2_put_varint(p, fr->final_size); |
1667 | |
1668 | assert((size_t)(p - out) == len)((void) (0)); |
1669 | |
1670 | return (ngtcp2_ssize)len; |
1671 | } |
1672 | |
1673 | ngtcp2_ssize |
1674 | ngtcp2_pkt_encode_connection_close_frame(uint8_t *out, size_t outlen, |
1675 | const ngtcp2_connection_close *fr) { |
1676 | size_t len = 1 + ngtcp2_put_varint_len(fr->error_code) + |
1677 | (fr->type == NGTCP2_FRAME_CONNECTION_CLOSE |
1678 | ? ngtcp2_put_varint_len(fr->frame_type) |
1679 | : 0) + |
1680 | ngtcp2_put_varint_len(fr->reasonlen) + fr->reasonlen; |
1681 | uint8_t *p; |
1682 | |
1683 | if (outlen < len) { |
1684 | return NGTCP2_ERR_NOBUF-203; |
1685 | } |
1686 | |
1687 | p = out; |
1688 | |
1689 | *p++ = fr->type; |
1690 | p = ngtcp2_put_varint(p, fr->error_code); |
1691 | if (fr->type == NGTCP2_FRAME_CONNECTION_CLOSE) { |
1692 | p = ngtcp2_put_varint(p, fr->frame_type); |
1693 | } |
1694 | p = ngtcp2_put_varint(p, fr->reasonlen); |
1695 | if (fr->reasonlen) { |
1696 | p = ngtcp2_cpymem(p, fr->reason, fr->reasonlen); |
1697 | } |
1698 | |
1699 | assert((size_t)(p - out) == len)((void) (0)); |
1700 | |
1701 | return (ngtcp2_ssize)len; |
1702 | } |
1703 | |
1704 | ngtcp2_ssize ngtcp2_pkt_encode_max_data_frame(uint8_t *out, size_t outlen, |
1705 | const ngtcp2_max_data *fr) { |
1706 | size_t len = 1 + ngtcp2_put_varint_len(fr->max_data); |
1707 | uint8_t *p; |
1708 | |
1709 | if (outlen < len) { |
1710 | return NGTCP2_ERR_NOBUF-203; |
1711 | } |
1712 | |
1713 | p = out; |
1714 | |
1715 | *p++ = NGTCP2_FRAME_MAX_DATA; |
1716 | p = ngtcp2_put_varint(p, fr->max_data); |
1717 | |
1718 | assert((size_t)(p - out) == len)((void) (0)); |
1719 | |
1720 | return (ngtcp2_ssize)len; |
1721 | } |
1722 | |
1723 | ngtcp2_ssize |
1724 | ngtcp2_pkt_encode_max_stream_data_frame(uint8_t *out, size_t outlen, |
1725 | const ngtcp2_max_stream_data *fr) { |
1726 | size_t len = 1 + ngtcp2_put_varint_len((uint64_t)fr->stream_id) + |
1727 | ngtcp2_put_varint_len(fr->max_stream_data); |
1728 | uint8_t *p; |
1729 | |
1730 | if (outlen < len) { |
1731 | return NGTCP2_ERR_NOBUF-203; |
1732 | } |
1733 | |
1734 | p = out; |
1735 | |
1736 | *p++ = NGTCP2_FRAME_MAX_STREAM_DATA; |
1737 | p = ngtcp2_put_varint(p, (uint64_t)fr->stream_id); |
1738 | p = ngtcp2_put_varint(p, fr->max_stream_data); |
Value stored to 'p' is never read | |
1739 | |
1740 | assert((size_t)(p - out) == len)((void) (0)); |
1741 | |
1742 | return (ngtcp2_ssize)len; |
1743 | } |
1744 | |
1745 | ngtcp2_ssize ngtcp2_pkt_encode_max_streams_frame(uint8_t *out, size_t outlen, |
1746 | const ngtcp2_max_streams *fr) { |
1747 | size_t len = 1 + ngtcp2_put_varint_len(fr->max_streams); |
1748 | uint8_t *p; |
1749 | |
1750 | if (outlen < len) { |
1751 | return NGTCP2_ERR_NOBUF-203; |
1752 | } |
1753 | |
1754 | p = out; |
1755 | |
1756 | *p++ = fr->type; |
1757 | p = ngtcp2_put_varint(p, fr->max_streams); |
1758 | |
1759 | assert((size_t)(p - out) == len)((void) (0)); |
1760 | |
1761 | return (ngtcp2_ssize)len; |
1762 | } |
1763 | |
1764 | ngtcp2_ssize ngtcp2_pkt_encode_ping_frame(uint8_t *out, size_t outlen, |
1765 | const ngtcp2_ping *fr) { |
1766 | (void)fr; |
1767 | |
1768 | if (outlen < 1) { |
1769 | return NGTCP2_ERR_NOBUF-203; |
1770 | } |
1771 | |
1772 | *out++ = NGTCP2_FRAME_PING; |
1773 | |
1774 | return 1; |
1775 | } |
1776 | |
1777 | ngtcp2_ssize |
1778 | ngtcp2_pkt_encode_data_blocked_frame(uint8_t *out, size_t outlen, |
1779 | const ngtcp2_data_blocked *fr) { |
1780 | size_t len = 1 + ngtcp2_put_varint_len(fr->offset); |
1781 | uint8_t *p; |
1782 | |
1783 | if (outlen < len) { |
1784 | return NGTCP2_ERR_NOBUF-203; |
1785 | } |
1786 | |
1787 | p = out; |
1788 | |
1789 | *p++ = NGTCP2_FRAME_DATA_BLOCKED; |
1790 | p = ngtcp2_put_varint(p, fr->offset); |
1791 | |
1792 | assert((size_t)(p - out) == len)((void) (0)); |
1793 | |
1794 | return (ngtcp2_ssize)len; |
1795 | } |
1796 | |
1797 | ngtcp2_ssize ngtcp2_pkt_encode_stream_data_blocked_frame( |
1798 | uint8_t *out, size_t outlen, const ngtcp2_stream_data_blocked *fr) { |
1799 | size_t len = 1 + ngtcp2_put_varint_len((uint64_t)fr->stream_id) + |
1800 | ngtcp2_put_varint_len(fr->offset); |
1801 | uint8_t *p; |
1802 | |
1803 | if (outlen < len) { |
1804 | return NGTCP2_ERR_NOBUF-203; |
1805 | } |
1806 | |
1807 | p = out; |
1808 | |
1809 | *p++ = NGTCP2_FRAME_STREAM_DATA_BLOCKED; |
1810 | p = ngtcp2_put_varint(p, (uint64_t)fr->stream_id); |
1811 | p = ngtcp2_put_varint(p, fr->offset); |
1812 | |
1813 | assert((size_t)(p - out) == len)((void) (0)); |
1814 | |
1815 | return (ngtcp2_ssize)len; |
1816 | } |
1817 | |
1818 | ngtcp2_ssize |
1819 | ngtcp2_pkt_encode_streams_blocked_frame(uint8_t *out, size_t outlen, |
1820 | const ngtcp2_streams_blocked *fr) { |
1821 | size_t len = 1 + ngtcp2_put_varint_len(fr->max_streams); |
1822 | uint8_t *p; |
1823 | |
1824 | if (outlen < len) { |
1825 | return NGTCP2_ERR_NOBUF-203; |
1826 | } |
1827 | |
1828 | p = out; |
1829 | |
1830 | *p++ = fr->type; |
1831 | p = ngtcp2_put_varint(p, fr->max_streams); |
1832 | |
1833 | assert((size_t)(p - out) == len)((void) (0)); |
1834 | |
1835 | return (ngtcp2_ssize)len; |
1836 | } |
1837 | |
1838 | ngtcp2_ssize |
1839 | ngtcp2_pkt_encode_new_connection_id_frame(uint8_t *out, size_t outlen, |
1840 | const ngtcp2_new_connection_id *fr) { |
1841 | size_t len = 1 + ngtcp2_put_varint_len(fr->seq) + |
1842 | ngtcp2_put_varint_len(fr->retire_prior_to) + 1 + |
1843 | fr->cid.datalen + NGTCP2_STATELESS_RESET_TOKENLEN16; |
1844 | uint8_t *p; |
1845 | |
1846 | if (outlen < len) { |
1847 | return NGTCP2_ERR_NOBUF-203; |
1848 | } |
1849 | |
1850 | p = out; |
1851 | |
1852 | *p++ = NGTCP2_FRAME_NEW_CONNECTION_ID; |
1853 | p = ngtcp2_put_varint(p, fr->seq); |
1854 | p = ngtcp2_put_varint(p, fr->retire_prior_to); |
1855 | *p++ = (uint8_t)fr->cid.datalen; |
1856 | p = ngtcp2_cpymem(p, fr->cid.data, fr->cid.datalen); |
1857 | p = ngtcp2_cpymem(p, fr->stateless_reset_token, |
1858 | NGTCP2_STATELESS_RESET_TOKENLEN16); |
1859 | |
1860 | assert((size_t)(p - out) == len)((void) (0)); |
1861 | |
1862 | return (ngtcp2_ssize)len; |
1863 | } |
1864 | |
1865 | ngtcp2_ssize |
1866 | ngtcp2_pkt_encode_stop_sending_frame(uint8_t *out, size_t outlen, |
1867 | const ngtcp2_stop_sending *fr) { |
1868 | size_t len = 1 + ngtcp2_put_varint_len((uint64_t)fr->stream_id) + |
1869 | ngtcp2_put_varint_len(fr->app_error_code); |
1870 | uint8_t *p; |
1871 | |
1872 | if (outlen < len) { |
1873 | return NGTCP2_ERR_NOBUF-203; |
1874 | } |
1875 | |
1876 | p = out; |
1877 | |
1878 | *p++ = NGTCP2_FRAME_STOP_SENDING; |
1879 | p = ngtcp2_put_varint(p, (uint64_t)fr->stream_id); |
1880 | p = ngtcp2_put_varint(p, fr->app_error_code); |
1881 | |
1882 | assert((size_t)(p - out) == len)((void) (0)); |
1883 | |
1884 | return (ngtcp2_ssize)len; |
1885 | } |
1886 | |
1887 | ngtcp2_ssize |
1888 | ngtcp2_pkt_encode_path_challenge_frame(uint8_t *out, size_t outlen, |
1889 | const ngtcp2_path_challenge *fr) { |
1890 | size_t len = 1 + 8; |
1891 | uint8_t *p; |
1892 | |
1893 | if (outlen < len) { |
1894 | return NGTCP2_ERR_NOBUF-203; |
1895 | } |
1896 | |
1897 | p = out; |
1898 | |
1899 | *p++ = NGTCP2_FRAME_PATH_CHALLENGE; |
1900 | p = ngtcp2_cpymem(p, fr->data, sizeof(fr->data)); |
1901 | |
1902 | assert((size_t)(p - out) == len)((void) (0)); |
1903 | |
1904 | return (ngtcp2_ssize)len; |
1905 | } |
1906 | |
1907 | ngtcp2_ssize |
1908 | ngtcp2_pkt_encode_path_response_frame(uint8_t *out, size_t outlen, |
1909 | const ngtcp2_path_response *fr) { |
1910 | size_t len = 1 + 8; |
1911 | uint8_t *p; |
1912 | |
1913 | if (outlen < len) { |
1914 | return NGTCP2_ERR_NOBUF-203; |
1915 | } |
1916 | |
1917 | p = out; |
1918 | |
1919 | *p++ = NGTCP2_FRAME_PATH_RESPONSE; |
1920 | p = ngtcp2_cpymem(p, fr->data, sizeof(fr->data)); |
1921 | |
1922 | assert((size_t)(p - out) == len)((void) (0)); |
1923 | |
1924 | return (ngtcp2_ssize)len; |
1925 | } |
1926 | |
1927 | ngtcp2_ssize ngtcp2_pkt_encode_crypto_frame(uint8_t *out, size_t outlen, |
1928 | const ngtcp2_crypto *fr) { |
1929 | size_t len = 1; |
1930 | uint8_t *p; |
1931 | size_t i; |
1932 | size_t datalen = 0; |
1933 | |
1934 | len += ngtcp2_put_varint_len(fr->offset); |
1935 | |
1936 | for (i = 0; i < fr->datacnt; ++i) { |
1937 | datalen += fr->data[i].len; |
1938 | } |
1939 | |
1940 | len += ngtcp2_put_varint_len(datalen); |
1941 | len += datalen; |
1942 | |
1943 | if (outlen < len) { |
1944 | return NGTCP2_ERR_NOBUF-203; |
1945 | } |
1946 | |
1947 | p = out; |
1948 | |
1949 | *p++ = NGTCP2_FRAME_CRYPTO; |
1950 | |
1951 | p = ngtcp2_put_varint(p, fr->offset); |
1952 | p = ngtcp2_put_varint(p, datalen); |
1953 | |
1954 | for (i = 0; i < fr->datacnt; ++i) { |
1955 | assert(fr->data[i].base)((void) (0)); |
1956 | p = ngtcp2_cpymem(p, fr->data[i].base, fr->data[i].len); |
1957 | } |
1958 | |
1959 | assert((size_t)(p - out) == len)((void) (0)); |
1960 | |
1961 | return (ngtcp2_ssize)len; |
1962 | } |
1963 | |
1964 | ngtcp2_ssize ngtcp2_pkt_encode_new_token_frame(uint8_t *out, size_t outlen, |
1965 | const ngtcp2_new_token *fr) { |
1966 | size_t len = 1 + ngtcp2_put_varint_len(fr->token.len) + fr->token.len; |
1967 | uint8_t *p; |
1968 | |
1969 | assert(fr->token.len)((void) (0)); |
1970 | |
1971 | if (outlen < len) { |
1972 | return NGTCP2_ERR_NOBUF-203; |
1973 | } |
1974 | |
1975 | p = out; |
1976 | |
1977 | *p++ = NGTCP2_FRAME_NEW_TOKEN; |
1978 | |
1979 | p = ngtcp2_put_varint(p, fr->token.len); |
1980 | p = ngtcp2_cpymem(p, fr->token.base, fr->token.len); |
1981 | |
1982 | assert((size_t)(p - out) == len)((void) (0)); |
1983 | |
1984 | return (ngtcp2_ssize)len; |
1985 | } |
1986 | |
1987 | ngtcp2_ssize ngtcp2_pkt_encode_retire_connection_id_frame( |
1988 | uint8_t *out, size_t outlen, const ngtcp2_retire_connection_id *fr) { |
1989 | size_t len = 1 + ngtcp2_put_varint_len(fr->seq); |
1990 | uint8_t *p; |
1991 | |
1992 | if (outlen < len) { |
1993 | return NGTCP2_ERR_NOBUF-203; |
1994 | } |
1995 | |
1996 | p = out; |
1997 | |
1998 | *p++ = NGTCP2_FRAME_RETIRE_CONNECTION_ID; |
1999 | |
2000 | p = ngtcp2_put_varint(p, fr->seq); |
2001 | |
2002 | assert((size_t)(p - out) == len)((void) (0)); |
2003 | |
2004 | return (ngtcp2_ssize)len; |
2005 | } |
2006 | |
2007 | ngtcp2_ssize |
2008 | ngtcp2_pkt_encode_handshake_done_frame(uint8_t *out, size_t outlen, |
2009 | const ngtcp2_handshake_done *fr) { |
2010 | (void)fr; |
2011 | |
2012 | if (outlen < 1) { |
2013 | return NGTCP2_ERR_NOBUF-203; |
2014 | } |
2015 | |
2016 | *out++ = NGTCP2_FRAME_HANDSHAKE_DONE; |
2017 | |
2018 | return 1; |
2019 | } |
2020 | |
2021 | ngtcp2_ssize ngtcp2_pkt_encode_datagram_frame(uint8_t *out, size_t outlen, |
2022 | const ngtcp2_datagram *fr) { |
2023 | size_t datalen = ngtcp2_vec_len(fr->data, fr->datacnt); |
2024 | size_t len = |
2025 | 1 + |
2026 | (fr->type == NGTCP2_FRAME_DATAGRAM ? 0 : ngtcp2_put_varint_len(datalen)) + |
2027 | datalen; |
2028 | uint8_t *p; |
2029 | size_t i; |
2030 | |
2031 | assert(fr->type == NGTCP2_FRAME_DATAGRAM ||((void) (0)) |
2032 | fr->type == NGTCP2_FRAME_DATAGRAM_LEN)((void) (0)); |
2033 | |
2034 | if (outlen < len) { |
2035 | return NGTCP2_ERR_NOBUF-203; |
2036 | } |
2037 | |
2038 | p = out; |
2039 | |
2040 | *p++ = fr->type; |
2041 | if (fr->type == NGTCP2_FRAME_DATAGRAM_LEN) { |
2042 | p = ngtcp2_put_varint(p, (uint64_t)datalen); |
2043 | } |
2044 | |
2045 | for (i = 0; i < fr->datacnt; ++i) { |
2046 | assert(fr->data[i].len)((void) (0)); |
2047 | assert(fr->data[i].base)((void) (0)); |
2048 | p = ngtcp2_cpymem(p, fr->data[i].base, fr->data[i].len); |
2049 | } |
2050 | |
2051 | assert((size_t)(p - out) == len)((void) (0)); |
2052 | |
2053 | return (ngtcp2_ssize)len; |
2054 | } |
2055 | |
2056 | ngtcp2_ssize ngtcp2_pkt_write_version_negotiation( |
2057 | uint8_t *dest, size_t destlen, uint8_t unused_random, const uint8_t *dcid, |
2058 | size_t dcidlen, const uint8_t *scid, size_t scidlen, const uint32_t *sv, |
2059 | size_t nsv) { |
2060 | size_t len = 1 + 4 + 1 + dcidlen + 1 + scidlen + nsv * 4; |
2061 | uint8_t *p; |
2062 | size_t i; |
2063 | |
2064 | assert(dcidlen < 256)((void) (0)); |
2065 | assert(scidlen < 256)((void) (0)); |
2066 | |
2067 | if (destlen < len) { |
2068 | return NGTCP2_ERR_NOBUF-203; |
2069 | } |
2070 | |
2071 | p = dest; |
2072 | |
2073 | *p++ = 0x80 | unused_random; |
2074 | p = ngtcp2_put_uint32be(p, 0); |
2075 | *p++ = (uint8_t)dcidlen; |
2076 | if (dcidlen) { |
2077 | p = ngtcp2_cpymem(p, dcid, dcidlen); |
2078 | } |
2079 | *p++ = (uint8_t)scidlen; |
2080 | if (scidlen) { |
2081 | p = ngtcp2_cpymem(p, scid, scidlen); |
2082 | } |
2083 | |
2084 | for (i = 0; i < nsv; ++i) { |
2085 | p = ngtcp2_put_uint32be(p, sv[i]); |
2086 | } |
2087 | |
2088 | assert((size_t)(p - dest) == len)((void) (0)); |
2089 | |
2090 | return (ngtcp2_ssize)len; |
2091 | } |
2092 | |
2093 | size_t ngtcp2_pkt_decode_version_negotiation(uint32_t *dest, |
2094 | const uint8_t *payload, |
2095 | size_t payloadlen) { |
2096 | const uint8_t *end = payload + payloadlen; |
2097 | |
2098 | assert((payloadlen % sizeof(uint32_t)) == 0)((void) (0)); |
2099 | |
2100 | for (; payload != end; payload += sizeof(uint32_t)) { |
2101 | *dest++ = ngtcp2_get_uint32(payload); |
2102 | } |
2103 | |
2104 | return payloadlen / sizeof(uint32_t); |
2105 | } |
2106 | |
2107 | int ngtcp2_pkt_decode_stateless_reset(ngtcp2_pkt_stateless_reset *sr, |
2108 | const uint8_t *payload, |
2109 | size_t payloadlen) { |
2110 | const uint8_t *p = payload; |
2111 | |
2112 | if (payloadlen < |
2113 | NGTCP2_MIN_STATELESS_RESET_RANDLEN5 + NGTCP2_STATELESS_RESET_TOKENLEN16) { |
2114 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
2115 | } |
2116 | |
2117 | sr->rand = p; |
2118 | sr->randlen = payloadlen - NGTCP2_STATELESS_RESET_TOKENLEN16; |
2119 | p += sr->randlen; |
2120 | memcpy(sr->stateless_reset_token, p, NGTCP2_STATELESS_RESET_TOKENLEN16); |
2121 | |
2122 | return 0; |
2123 | } |
2124 | |
2125 | int ngtcp2_pkt_decode_retry(ngtcp2_pkt_retry *dest, const uint8_t *payload, |
2126 | size_t payloadlen) { |
2127 | size_t len = /* token */ 1 + NGTCP2_RETRY_TAGLEN16; |
2128 | |
2129 | if (payloadlen < len) { |
2130 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
2131 | } |
2132 | |
2133 | dest->token.base = (uint8_t *)payload; |
2134 | dest->token.len = (size_t)(payloadlen - NGTCP2_RETRY_TAGLEN16); |
2135 | ngtcp2_cpymem(dest->tag, payload + dest->token.len, NGTCP2_RETRY_TAGLEN16); |
2136 | |
2137 | return 0; |
2138 | } |
2139 | |
2140 | int64_t ngtcp2_pkt_adjust_pkt_num(int64_t max_pkt_num, int64_t pkt_num, |
2141 | size_t n) { |
2142 | int64_t expected = max_pkt_num + 1; |
2143 | int64_t win = (int64_t)1 << n; |
2144 | int64_t hwin = win / 2; |
2145 | int64_t mask = win - 1; |
2146 | int64_t cand = (expected & ~mask) | pkt_num; |
2147 | |
2148 | if (cand <= expected - hwin) { |
2149 | assert(cand <= (int64_t)NGTCP2_MAX_VARINT - win)((void) (0)); |
2150 | return cand + win; |
2151 | } |
2152 | if (cand > expected + hwin && cand >= win) { |
2153 | return cand - win; |
2154 | } |
2155 | return cand; |
2156 | } |
2157 | |
2158 | int ngtcp2_pkt_validate_ack(ngtcp2_ack *fr) { |
2159 | int64_t largest_ack = fr->largest_ack; |
2160 | size_t i; |
2161 | |
2162 | if (largest_ack < (int64_t)fr->first_ack_blklen) { |
2163 | return NGTCP2_ERR_ACK_FRAME-207; |
2164 | } |
2165 | |
2166 | largest_ack -= (int64_t)fr->first_ack_blklen; |
2167 | |
2168 | for (i = 0; i < fr->num_blks; ++i) { |
2169 | if (largest_ack < (int64_t)fr->blks[i].gap + 2) { |
2170 | return NGTCP2_ERR_ACK_FRAME-207; |
2171 | } |
2172 | |
2173 | largest_ack -= (int64_t)fr->blks[i].gap + 2; |
2174 | |
2175 | if (largest_ack < (int64_t)fr->blks[i].blklen) { |
2176 | return NGTCP2_ERR_ACK_FRAME-207; |
2177 | } |
2178 | |
2179 | largest_ack -= (int64_t)fr->blks[i].blklen; |
2180 | } |
2181 | |
2182 | return 0; |
2183 | } |
2184 | |
2185 | ngtcp2_ssize |
2186 | ngtcp2_pkt_write_stateless_reset(uint8_t *dest, size_t destlen, |
2187 | const uint8_t *stateless_reset_token, |
2188 | const uint8_t *rand, size_t randlen) { |
2189 | uint8_t *p; |
2190 | |
2191 | if (destlen < |
2192 | NGTCP2_MIN_STATELESS_RESET_RANDLEN5 + NGTCP2_STATELESS_RESET_TOKENLEN16) { |
2193 | return NGTCP2_ERR_NOBUF-203; |
2194 | } |
2195 | |
2196 | if (randlen < NGTCP2_MIN_STATELESS_RESET_RANDLEN5) { |
2197 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
2198 | } |
2199 | |
2200 | p = dest; |
2201 | |
2202 | randlen = ngtcp2_min(destlen - NGTCP2_STATELESS_RESET_TOKENLEN, randlen)((destlen - 16) < (randlen) ? (destlen - 16) : (randlen)); |
2203 | |
2204 | p = ngtcp2_cpymem(p, rand, randlen); |
2205 | p = ngtcp2_cpymem(p, stateless_reset_token, NGTCP2_STATELESS_RESET_TOKENLEN16); |
2206 | *dest = (uint8_t)((*dest & 0x7fu) | 0x40u); |
2207 | |
2208 | return p - dest; |
2209 | } |
2210 | |
2211 | ngtcp2_ssize ngtcp2_pkt_write_retry( |
2212 | uint8_t *dest, size_t destlen, uint32_t version, const ngtcp2_cid *dcid, |
2213 | const ngtcp2_cid *scid, const ngtcp2_cid *odcid, const uint8_t *token, |
2214 | size_t tokenlen, ngtcp2_encrypt encrypt, const ngtcp2_crypto_aead *aead, |
2215 | const ngtcp2_crypto_aead_ctx *aead_ctx) { |
2216 | ngtcp2_pkt_hd hd; |
2217 | uint8_t pseudo_retry[1500]; |
2218 | ngtcp2_ssize pseudo_retrylen; |
2219 | uint8_t tag[NGTCP2_RETRY_TAGLEN16]; |
2220 | int rv; |
2221 | uint8_t *p; |
2222 | size_t offset; |
2223 | const uint8_t *nonce; |
2224 | size_t noncelen; |
2225 | |
2226 | assert(tokenlen > 0)((void) (0)); |
2227 | assert(!ngtcp2_cid_eq(scid, odcid))((void) (0)); |
2228 | |
2229 | /* Retry packet is sent at most once per one connection attempt. In |
2230 | the first connection attempt, client has to send random DCID |
2231 | which is at least 8 bytes long. */ |
2232 | if (odcid->datalen < 8) { |
2233 | return NGTCP2_ERR_INVALID_ARGUMENT-201; |
2234 | } |
2235 | |
2236 | ngtcp2_pkt_hd_init(&hd, NGTCP2_PKT_FLAG_LONG_FORM0x01, NGTCP2_PKT_RETRY, dcid, |
2237 | scid, /* pkt_num = */ 0, /* pkt_numlen = */ 1, version, |
2238 | /* len = */ 0); |
2239 | |
2240 | pseudo_retrylen = |
2241 | ngtcp2_pkt_encode_pseudo_retry(pseudo_retry, sizeof(pseudo_retry), &hd, |
2242 | /* unused = */ 0, odcid, token, tokenlen); |
2243 | if (pseudo_retrylen < 0) { |
2244 | return pseudo_retrylen; |
2245 | } |
2246 | |
2247 | if (version == NGTCP2_PROTO_VER_V10x00000001u) { |
2248 | nonce = (const uint8_t *)NGTCP2_RETRY_NONCE_V1"\x46\x15\x99\xd3\x5d\x63\x2b\xf2\x23\x98\x25\xbb"; |
2249 | noncelen = sizeof(NGTCP2_RETRY_NONCE_V1"\x46\x15\x99\xd3\x5d\x63\x2b\xf2\x23\x98\x25\xbb") - 1; |
2250 | } else { |
2251 | nonce = (const uint8_t *)NGTCP2_RETRY_NONCE_DRAFT"\xe5\x49\x30\xf9\x7f\x21\x36\xf0\x53\x0a\x8c\x1c"; |
2252 | noncelen = sizeof(NGTCP2_RETRY_NONCE_DRAFT"\xe5\x49\x30\xf9\x7f\x21\x36\xf0\x53\x0a\x8c\x1c") - 1; |
2253 | } |
2254 | |
2255 | /* OpenSSL does not like NULL plaintext. */ |
2256 | rv = encrypt(tag, aead, aead_ctx, (const uint8_t *)"", 0, nonce, noncelen, |
2257 | pseudo_retry, (size_t)pseudo_retrylen); |
2258 | if (rv != 0) { |
2259 | return rv; |
2260 | } |
2261 | |
2262 | offset = 1 + odcid->datalen; |
2263 | if (destlen < (size_t)pseudo_retrylen + sizeof(tag) - offset) { |
2264 | return NGTCP2_ERR_NOBUF-203; |
2265 | } |
2266 | |
2267 | p = ngtcp2_cpymem(dest, pseudo_retry + offset, |
2268 | (size_t)pseudo_retrylen - offset); |
2269 | p = ngtcp2_cpymem(p, tag, sizeof(tag)); |
2270 | |
2271 | return p - dest; |
2272 | } |
2273 | |
2274 | ngtcp2_ssize ngtcp2_pkt_encode_pseudo_retry( |
2275 | uint8_t *dest, size_t destlen, const ngtcp2_pkt_hd *hd, uint8_t unused, |
2276 | const ngtcp2_cid *odcid, const uint8_t *token, size_t tokenlen) { |
2277 | uint8_t *p = dest; |
2278 | ngtcp2_ssize nwrite; |
2279 | |
2280 | if (destlen < 1 + odcid->datalen) { |
2281 | return NGTCP2_ERR_NOBUF-203; |
2282 | } |
2283 | |
2284 | *p++ = (uint8_t)odcid->datalen; |
2285 | p = ngtcp2_cpymem(p, odcid->data, odcid->datalen); |
2286 | destlen -= (size_t)(p - dest); |
2287 | |
2288 | nwrite = ngtcp2_pkt_encode_hd_long(p, destlen, hd); |
2289 | if (nwrite < 0) { |
2290 | return nwrite; |
2291 | } |
2292 | |
2293 | if (destlen < (size_t)nwrite + tokenlen) { |
2294 | return NGTCP2_ERR_NOBUF-203; |
2295 | } |
2296 | |
2297 | *p &= 0xf0; |
2298 | *p |= unused; |
2299 | |
2300 | p += nwrite; |
2301 | |
2302 | p = ngtcp2_cpymem(p, token, tokenlen); |
2303 | |
2304 | return p - dest; |
2305 | } |
2306 | |
2307 | int ngtcp2_pkt_verify_retry_tag(uint32_t version, const ngtcp2_pkt_retry *retry, |
2308 | const uint8_t *pkt, size_t pktlen, |
2309 | ngtcp2_encrypt encrypt, |
2310 | const ngtcp2_crypto_aead *aead, |
2311 | const ngtcp2_crypto_aead_ctx *aead_ctx) { |
2312 | uint8_t pseudo_retry[1500]; |
2313 | size_t pseudo_retrylen; |
2314 | uint8_t *p = pseudo_retry; |
2315 | int rv; |
2316 | uint8_t tag[NGTCP2_RETRY_TAGLEN16]; |
2317 | const uint8_t *nonce; |
2318 | size_t noncelen; |
2319 | |
2320 | assert(pktlen >= sizeof(retry->tag))((void) (0)); |
2321 | |
2322 | if (sizeof(pseudo_retry) < |
2323 | 1 + retry->odcid.datalen + pktlen - sizeof(retry->tag)) { |
2324 | return NGTCP2_ERR_PROTO-205; |
2325 | } |
2326 | |
2327 | *p++ = (uint8_t)retry->odcid.datalen; |
2328 | p = ngtcp2_cpymem(p, retry->odcid.data, retry->odcid.datalen); |
2329 | p = ngtcp2_cpymem(p, pkt, pktlen - sizeof(retry->tag)); |
2330 | |
2331 | pseudo_retrylen = (size_t)(p - pseudo_retry); |
2332 | |
2333 | if (version == NGTCP2_PROTO_VER_V10x00000001u) { |
2334 | nonce = (const uint8_t *)NGTCP2_RETRY_NONCE_V1"\x46\x15\x99\xd3\x5d\x63\x2b\xf2\x23\x98\x25\xbb"; |
2335 | noncelen = sizeof(NGTCP2_RETRY_NONCE_V1"\x46\x15\x99\xd3\x5d\x63\x2b\xf2\x23\x98\x25\xbb") - 1; |
2336 | } else { |
2337 | nonce = (const uint8_t *)NGTCP2_RETRY_NONCE_DRAFT"\xe5\x49\x30\xf9\x7f\x21\x36\xf0\x53\x0a\x8c\x1c"; |
2338 | noncelen = sizeof(NGTCP2_RETRY_NONCE_DRAFT"\xe5\x49\x30\xf9\x7f\x21\x36\xf0\x53\x0a\x8c\x1c") - 1; |
2339 | } |
2340 | |
2341 | /* OpenSSL does not like NULL plaintext. */ |
2342 | rv = encrypt(tag, aead, aead_ctx, (const uint8_t *)"", 0, nonce, noncelen, |
2343 | pseudo_retry, pseudo_retrylen); |
2344 | if (rv != 0) { |
2345 | return rv; |
2346 | } |
2347 | |
2348 | if (0 != memcmp(retry->tag, tag, sizeof(retry->tag))) { |
2349 | return NGTCP2_ERR_PROTO-205; |
2350 | } |
2351 | |
2352 | return 0; |
2353 | } |
2354 | |
2355 | size_t ngtcp2_pkt_stream_max_datalen(int64_t stream_id, uint64_t offset, |
2356 | size_t len, size_t left) { |
2357 | size_t n = 1 /* type */ + ngtcp2_put_varint_len((uint64_t)stream_id) + |
2358 | (offset ? ngtcp2_put_varint_len(offset) : 0); |
2359 | |
2360 | if (left <= n) { |
2361 | return (size_t)-1; |
2362 | } |
2363 | |
2364 | left -= n; |
2365 | |
2366 | if (left > 8 + 1073741823 && len > 1073741823) { |
2367 | #if SIZE_MAX(18446744073709551615UL) > UINT32_MAX(4294967295U) |
2368 | len = ngtcp2_min(len, 4611686018427387903lu)((len) < (4611686018427387903lu) ? (len) : (4611686018427387903lu )); |
2369 | #endif /* SIZE_MAX > UINT32_MAX */ |
2370 | return ngtcp2_min(len, left - 8)((len) < (left - 8) ? (len) : (left - 8)); |
2371 | } |
2372 | |
2373 | if (left > 4 + 16383 && len > 16383) { |
2374 | len = ngtcp2_min(len, 1073741823)((len) < (1073741823) ? (len) : (1073741823)); |
2375 | return ngtcp2_min(len, left - 4)((len) < (left - 4) ? (len) : (left - 4)); |
2376 | } |
2377 | |
2378 | if (left > 2 + 63 && len > 63) { |
2379 | len = ngtcp2_min(len, 16383)((len) < (16383) ? (len) : (16383)); |
2380 | return ngtcp2_min(len, left - 2)((len) < (left - 2) ? (len) : (left - 2)); |
2381 | } |
2382 | |
2383 | len = ngtcp2_min(len, 63)((len) < (63) ? (len) : (63)); |
2384 | return ngtcp2_min(len, left - 1)((len) < (left - 1) ? (len) : (left - 1)); |
2385 | } |
2386 | |
2387 | size_t ngtcp2_pkt_crypto_max_datalen(uint64_t offset, size_t len, size_t left) { |
2388 | size_t n = 1 /* type */ + ngtcp2_put_varint_len(offset); |
2389 | |
2390 | /* CRYPTO frame must contain nonzero length data. Return -1 if |
2391 | there is no space to write crypto data. */ |
2392 | if (left <= n + 1) { |
2393 | return (size_t)-1; |
2394 | } |
2395 | |
2396 | left -= n; |
2397 | |
2398 | if (left > 8 + 1073741823 && len > 1073741823) { |
2399 | #if SIZE_MAX(18446744073709551615UL) > UINT32_MAX(4294967295U) |
2400 | len = ngtcp2_min(len, 4611686018427387903lu)((len) < (4611686018427387903lu) ? (len) : (4611686018427387903lu )); |
2401 | #endif /* SIZE_MAX > UINT32_MAX */ |
2402 | return ngtcp2_min(len, left - 8)((len) < (left - 8) ? (len) : (left - 8)); |
2403 | } |
2404 | |
2405 | if (left > 4 + 16383 && len > 16383) { |
2406 | len = ngtcp2_min(len, 1073741823)((len) < (1073741823) ? (len) : (1073741823)); |
2407 | return ngtcp2_min(len, left - 4)((len) < (left - 4) ? (len) : (left - 4)); |
2408 | } |
2409 | |
2410 | if (left > 2 + 63 && len > 63) { |
2411 | len = ngtcp2_min(len, 16383)((len) < (16383) ? (len) : (16383)); |
2412 | return ngtcp2_min(len, left - 2)((len) < (left - 2) ? (len) : (left - 2)); |
2413 | } |
2414 | |
2415 | len = ngtcp2_min(len, 63)((len) < (63) ? (len) : (63)); |
2416 | return ngtcp2_min(len, left - 1)((len) < (left - 1) ? (len) : (left - 1)); |
2417 | } |
2418 | |
2419 | size_t ngtcp2_pkt_datagram_framelen(size_t len) { |
2420 | return 1 /* type */ + ngtcp2_put_varint_len((uint64_t)len) + len; |
2421 | } |
2422 | |
2423 | uint8_t ngtcp2_pkt_get_type_long(uint8_t c) { |
2424 | return (uint8_t)((c & NGTCP2_LONG_TYPE_MASK0x30) >> 4); |
2425 | } |
2426 | |
2427 | int ngtcp2_pkt_verify_reserved_bits(uint8_t c) { |
2428 | if (c & NGTCP2_HEADER_FORM_BIT0x80) { |
2429 | return (c & NGTCP2_LONG_RESERVED_BIT_MASK0x0c) == 0 ? 0 : NGTCP2_ERR_PROTO-205; |
2430 | } |
2431 | return (c & NGTCP2_SHORT_RESERVED_BIT_MASK0x18) == 0 ? 0 : NGTCP2_ERR_PROTO-205; |
2432 | } |