From 87e17ca5f212d0942d0517c6a0d4dbeabe68456d Mon Sep 17 00:00:00 2001
From: Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>
Date: Tue, 26 May 2020 18:06:50 +0900
Subject: [PATCH] Use ngtcp2_vec in ngtcp2_pkt_retry

---
 lib/includes/ngtcp2/ngtcp2.h | 3 +--
 lib/ngtcp2_conn.c            | 8 ++++----
 lib/ngtcp2_pkt.c             | 6 +++---
 tests/ngtcp2_pkt_test.c      | 3 ++-
 4 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/lib/includes/ngtcp2/ngtcp2.h b/lib/includes/ngtcp2/ngtcp2.h
index 9567dde9..04b568c1 100644
--- a/lib/includes/ngtcp2/ngtcp2.h
+++ b/lib/includes/ngtcp2/ngtcp2.h
@@ -385,8 +385,7 @@ typedef struct ngtcp2_pkt_stateless_reset {
 
 typedef struct ngtcp2_pkt_retry {
   ngtcp2_cid odcid;
-  const uint8_t *token;
-  size_t tokenlen;
+  ngtcp2_vec token;
   uint8_t tag[NGTCP2_RETRY_TAGLEN];
 } ngtcp2_pkt_retry;
 
diff --git a/lib/ngtcp2_conn.c b/lib/ngtcp2_conn.c
index b70d030e..c0831da3 100644
--- a/lib/ngtcp2_conn.c
+++ b/lib/ngtcp2_conn.c
@@ -3642,7 +3642,7 @@ static int conn_on_retry(ngtcp2_conn *conn, const ngtcp2_pkt_hd *hd,
                   (const char *)ngtcp2_encode_hex(cidbuf, retry.odcid.data,
                                                   retry.odcid.datalen));
 
-  if (retry.tokenlen == 0) {
+  if (retry.token.len == 0) {
     return NGTCP2_ERR_PROTO;
   }
 
@@ -3693,13 +3693,13 @@ static int conn_on_retry(ngtcp2_conn *conn, const ngtcp2_pkt_hd *hd,
   token->base = NULL;
   token->len = 0;
 
-  token->base = ngtcp2_mem_malloc(conn->mem, retry.tokenlen);
+  token->base = ngtcp2_mem_malloc(conn->mem, retry.token.len);
   if (token->base == NULL) {
     return NGTCP2_ERR_NOMEM;
   }
-  token->len = retry.tokenlen;
+  token->len = retry.token.len;
 
-  ngtcp2_cpymem(token->base, retry.token, retry.tokenlen);
+  ngtcp2_cpymem(token->base, retry.token.base, retry.token.len);
 
   conn_reset_congestion_state(conn);
 
diff --git a/lib/ngtcp2_pkt.c b/lib/ngtcp2_pkt.c
index 07c9e146..0e20aa45 100644
--- a/lib/ngtcp2_pkt.c
+++ b/lib/ngtcp2_pkt.c
@@ -1988,9 +1988,9 @@ int ngtcp2_pkt_decode_retry(ngtcp2_pkt_retry *dest, const uint8_t *payload,
     return NGTCP2_ERR_INVALID_ARGUMENT;
   }
 
-  dest->token = payload;
-  dest->tokenlen = (size_t)(payloadlen - NGTCP2_RETRY_TAGLEN);
-  ngtcp2_cpymem(dest->tag, payload + dest->tokenlen, NGTCP2_RETRY_TAGLEN);
+  dest->token.base = (uint8_t *)payload;
+  dest->token.len = (size_t)(payloadlen - NGTCP2_RETRY_TAGLEN);
+  ngtcp2_cpymem(dest->tag, payload + dest->token.len, NGTCP2_RETRY_TAGLEN);
 
   return 0;
 }
diff --git a/tests/ngtcp2_pkt_test.c b/tests/ngtcp2_pkt_test.c
index 8946d09c..670fb187 100644
--- a/tests/ngtcp2_pkt_test.c
+++ b/tests/ngtcp2_pkt_test.c
@@ -1288,7 +1288,8 @@ void test_ngtcp2_pkt_write_retry(void) {
   rv = ngtcp2_pkt_decode_retry(&retry, buf + nread, (size_t)(spktlen - nread));
 
   CU_ASSERT(0 == rv);
-  CU_ASSERT(0 == memcmp(token, retry.token, sizeof(token)));
+  CU_ASSERT(sizeof(token) == retry.token.len);
+  CU_ASSERT(0 == memcmp(token, retry.token.base, sizeof(token)));
   CU_ASSERT(0 == memcmp(tag, retry.tag, sizeof(tag)));
 }
 
-- 
GitLab