Skip to content
Snippets Groups Projects
Commit b3333426 authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa
Browse files

Add Version Negotiation encoder

parent 635b5f58
No related merge requests found
......@@ -309,9 +309,9 @@ NGTCP2_EXTERN void ngtcp2_upe_padding(ngtcp2_upe *upe);
* :enum:`NGTCP2_ERR_NOBUF`
* Buffer does not have enough capacity to write a payload.
*/
NGTCP2_EXTERN int
ngtcp2_upe_encode_version_negotiation(ngtcp2_upe *upe, uint8_t *out,
size_t outlen, uint32_t *sv, size_t nsv);
NGTCP2_EXTERN int ngtcp2_upe_encode_version_negotiation(ngtcp2_upe *upe,
const uint32_t *sv,
size_t nsv);
/**
* @function
......
......@@ -80,6 +80,29 @@ void ngtcp2_upe_padding(ngtcp2_upe *upe) {
buf->last += len;
}
int ngtcp2_upe_encode_version_negotiation(ngtcp2_upe *upe, const uint32_t *sv,
size_t nsv) {
ngtcp2_buf *buf = &upe->buf;
uint8_t *p;
size_t i;
if (ngtcp2_buf_left(buf) < sizeof(uint32_t) * nsv + NGTCP2_PKT_MDLEN) {
return NGTCP2_ERR_NOBUF;
}
p = buf->last;
for (i = 0; i < nsv; ++i) {
p = ngtcp2_put_uint32be(p, sv[i]);
}
assert((size_t)(p - buf->last) == sizeof(uint32_t) * nsv);
buf->last = p;
return 0;
}
size_t ngtcp2_upe_final(ngtcp2_upe *upe, const uint8_t **ppkt) {
ngtcp2_buf *buf = &upe->buf;
uint64_t h;
......
......@@ -68,7 +68,9 @@ int main() {
test_ngtcp2_pkt_encode_stream_frame) ||
!CU_add_test(pSuite, "pkt_encode_ack_frame",
test_ngtcp2_pkt_encode_ack_frame) ||
!CU_add_test(pSuite, "upe_encode", test_ngtcp2_upe_encode)) {
!CU_add_test(pSuite, "upe_encode", test_ngtcp2_upe_encode) ||
!CU_add_test(pSuite, "upe_encode_version_negotiation",
test_ngtcp2_upe_encode_version_negotiation)) {
CU_cleanup_registry();
return CU_get_error();
}
......
......@@ -36,6 +36,11 @@
*/
#define strsize(S) (sizeof(S) - 1)
/*
* arraylen macro returns the number of elements in array |A|.
*/
#define arraylen(A) (sizeof(A) / sizeof(A[0]))
/*
* ngtcp2_t_encode_stream_frame encodes STREAM frame into |out| with
* the given parameters. If NGTCP2_STREAM_D_BIT is set in |flags|,
......
......@@ -30,6 +30,7 @@
#include "ngtcp2_upe.h"
#include "ngtcp2_pkt.h"
#include "ngtcp2_conv.h"
#include "ngtcp2_test_helper.h"
void test_ngtcp2_upe_encode(void) {
......@@ -136,3 +137,48 @@ void test_ngtcp2_upe_encode(void) {
CU_ASSERT(NGTCP2_FRAME_PADDING == ns.type);
CU_ASSERT(pktlen == ns.padding.len);
}
void test_ngtcp2_upe_encode_version_negotiation(void) {
ngtcp2_upe upe;
const uint32_t sv[] = {0x01, 0x02, 0x03};
uint8_t buf[256];
ngtcp2_pkt_hd hd, nhd;
int rv;
const uint8_t *out;
size_t pktlen;
ssize_t nread;
size_t i;
ngtcp2_upe_init(&upe, buf, sizeof(buf));
hd.flags = NGTCP2_PKT_FLAG_LONG_FORM;
hd.type = NGTCP2_PKT_VERSION_NEGOTIATION;
hd.conn_id = 1000000009;
hd.pkt_num = 1000000007;
hd.version = 0xff;
rv = ngtcp2_upe_encode_hd(&upe, &hd);
CU_ASSERT(0 == rv);
rv = ngtcp2_upe_encode_version_negotiation(&upe, sv, arraylen(sv));
CU_ASSERT(0 == rv);
pktlen = ngtcp2_upe_final(&upe, &out);
pktlen -= NGTCP2_PKT_MDLEN;
nread = ngtcp2_pkt_decode_hd_long(&nhd, out, pktlen);
CU_ASSERT(NGTCP2_LONG_HEADERLEN == nread);
out += nread;
pktlen -= (size_t)nread;
CU_ASSERT(sizeof(sv) == pktlen);
for (i = 0; i < arraylen(sv); ++i) {
uint32_t v = ngtcp2_get_uint32(out + sizeof(uint32_t) * i);
CU_ASSERT(sv[i] == v);
}
}
......@@ -30,5 +30,6 @@
#endif /* HAVE_CONFIG_H */
void test_ngtcp2_upe_encode(void);
void test_ngtcp2_upe_encode_version_negotiation(void);
#endif /* NGTCP2_UPE_TEST_H */
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment