11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * ngtcp2
31cb0ef41Sopenharmony_ci *
41cb0ef41Sopenharmony_ci * Copyright (c) 2018 ngtcp2 contributors
51cb0ef41Sopenharmony_ci *
61cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
71cb0ef41Sopenharmony_ci * a copy of this software and associated documentation files (the
81cb0ef41Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
91cb0ef41Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
101cb0ef41Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
111cb0ef41Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
121cb0ef41Sopenharmony_ci * the following conditions:
131cb0ef41Sopenharmony_ci *
141cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be
151cb0ef41Sopenharmony_ci * included in all copies or substantial portions of the Software.
161cb0ef41Sopenharmony_ci *
171cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
181cb0ef41Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
191cb0ef41Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
201cb0ef41Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
211cb0ef41Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
221cb0ef41Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
231cb0ef41Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
241cb0ef41Sopenharmony_ci */
251cb0ef41Sopenharmony_ci#include "ngtcp2_cid.h"
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci#include <assert.h>
281cb0ef41Sopenharmony_ci#include <string.h>
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci#include "ngtcp2_path.h"
311cb0ef41Sopenharmony_ci#include "ngtcp2_str.h"
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_civoid ngtcp2_cid_zero(ngtcp2_cid *cid) { memset(cid, 0, sizeof(*cid)); }
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_civoid ngtcp2_cid_init(ngtcp2_cid *cid, const uint8_t *data, size_t datalen) {
361cb0ef41Sopenharmony_ci  assert(datalen <= NGTCP2_MAX_CIDLEN);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  cid->datalen = datalen;
391cb0ef41Sopenharmony_ci  if (datalen) {
401cb0ef41Sopenharmony_ci    ngtcp2_cpymem(cid->data, data, datalen);
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciint ngtcp2_cid_eq(const ngtcp2_cid *cid, const ngtcp2_cid *other) {
451cb0ef41Sopenharmony_ci  return cid->datalen == other->datalen &&
461cb0ef41Sopenharmony_ci         0 == memcmp(cid->data, other->data, cid->datalen);
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciint ngtcp2_cid_less(const ngtcp2_cid *lhs, const ngtcp2_cid *rhs) {
501cb0ef41Sopenharmony_ci  int s = lhs->datalen < rhs->datalen;
511cb0ef41Sopenharmony_ci  size_t n = s ? lhs->datalen : rhs->datalen;
521cb0ef41Sopenharmony_ci  int c = memcmp(lhs->data, rhs->data, n);
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  return c < 0 || (c == 0 && s);
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ciint ngtcp2_cid_empty(const ngtcp2_cid *cid) { return cid->datalen == 0; }
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_civoid ngtcp2_scid_init(ngtcp2_scid *scid, uint64_t seq, const ngtcp2_cid *cid) {
601cb0ef41Sopenharmony_ci  scid->pe.index = NGTCP2_PQ_BAD_INDEX;
611cb0ef41Sopenharmony_ci  scid->seq = seq;
621cb0ef41Sopenharmony_ci  scid->cid = *cid;
631cb0ef41Sopenharmony_ci  scid->retired_ts = UINT64_MAX;
641cb0ef41Sopenharmony_ci  scid->flags = NGTCP2_SCID_FLAG_NONE;
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_civoid ngtcp2_scid_copy(ngtcp2_scid *dest, const ngtcp2_scid *src) {
681cb0ef41Sopenharmony_ci  ngtcp2_scid_init(dest, src->seq, &src->cid);
691cb0ef41Sopenharmony_ci  dest->retired_ts = src->retired_ts;
701cb0ef41Sopenharmony_ci  dest->flags = src->flags;
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_civoid ngtcp2_dcid_init(ngtcp2_dcid *dcid, uint64_t seq, const ngtcp2_cid *cid,
741cb0ef41Sopenharmony_ci                      const uint8_t *token) {
751cb0ef41Sopenharmony_ci  dcid->seq = seq;
761cb0ef41Sopenharmony_ci  dcid->cid = *cid;
771cb0ef41Sopenharmony_ci  if (token) {
781cb0ef41Sopenharmony_ci    memcpy(dcid->token, token, NGTCP2_STATELESS_RESET_TOKENLEN);
791cb0ef41Sopenharmony_ci    dcid->flags = NGTCP2_DCID_FLAG_TOKEN_PRESENT;
801cb0ef41Sopenharmony_ci  } else {
811cb0ef41Sopenharmony_ci    dcid->flags = NGTCP2_DCID_FLAG_NONE;
821cb0ef41Sopenharmony_ci  }
831cb0ef41Sopenharmony_ci  ngtcp2_path_storage_zero(&dcid->ps);
841cb0ef41Sopenharmony_ci  dcid->retired_ts = UINT64_MAX;
851cb0ef41Sopenharmony_ci  dcid->bound_ts = UINT64_MAX;
861cb0ef41Sopenharmony_ci  dcid->bytes_sent = 0;
871cb0ef41Sopenharmony_ci  dcid->bytes_recv = 0;
881cb0ef41Sopenharmony_ci  dcid->max_udp_payload_size = NGTCP2_MAX_UDP_PAYLOAD_SIZE;
891cb0ef41Sopenharmony_ci}
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_civoid ngtcp2_dcid_set_token(ngtcp2_dcid *dcid, const uint8_t *token) {
921cb0ef41Sopenharmony_ci  assert(token);
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  dcid->flags |= NGTCP2_DCID_FLAG_TOKEN_PRESENT;
951cb0ef41Sopenharmony_ci  memcpy(dcid->token, token, NGTCP2_STATELESS_RESET_TOKENLEN);
961cb0ef41Sopenharmony_ci}
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_civoid ngtcp2_dcid_set_path(ngtcp2_dcid *dcid, const ngtcp2_path *path) {
991cb0ef41Sopenharmony_ci  ngtcp2_path_copy(&dcid->ps.path, path);
1001cb0ef41Sopenharmony_ci}
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_civoid ngtcp2_dcid_copy(ngtcp2_dcid *dest, const ngtcp2_dcid *src) {
1031cb0ef41Sopenharmony_ci  ngtcp2_dcid_init(dest, src->seq, &src->cid,
1041cb0ef41Sopenharmony_ci                   (src->flags & NGTCP2_DCID_FLAG_TOKEN_PRESENT) ? src->token
1051cb0ef41Sopenharmony_ci                                                                 : NULL);
1061cb0ef41Sopenharmony_ci  ngtcp2_path_copy(&dest->ps.path, &src->ps.path);
1071cb0ef41Sopenharmony_ci  dest->retired_ts = src->retired_ts;
1081cb0ef41Sopenharmony_ci  dest->bound_ts = src->bound_ts;
1091cb0ef41Sopenharmony_ci  dest->flags = src->flags;
1101cb0ef41Sopenharmony_ci  dest->bytes_sent = src->bytes_sent;
1111cb0ef41Sopenharmony_ci  dest->bytes_recv = src->bytes_recv;
1121cb0ef41Sopenharmony_ci  dest->max_udp_payload_size = src->max_udp_payload_size;
1131cb0ef41Sopenharmony_ci}
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_civoid ngtcp2_dcid_copy_cid_token(ngtcp2_dcid *dest, const ngtcp2_dcid *src) {
1161cb0ef41Sopenharmony_ci  dest->seq = src->seq;
1171cb0ef41Sopenharmony_ci  dest->cid = src->cid;
1181cb0ef41Sopenharmony_ci  if (src->flags & NGTCP2_DCID_FLAG_TOKEN_PRESENT) {
1191cb0ef41Sopenharmony_ci    dest->flags |= NGTCP2_DCID_FLAG_TOKEN_PRESENT;
1201cb0ef41Sopenharmony_ci    memcpy(dest->token, src->token, NGTCP2_STATELESS_RESET_TOKENLEN);
1211cb0ef41Sopenharmony_ci  } else if (dest->flags & NGTCP2_DCID_FLAG_TOKEN_PRESENT) {
1221cb0ef41Sopenharmony_ci    dest->flags &= (uint8_t)~NGTCP2_DCID_FLAG_TOKEN_PRESENT;
1231cb0ef41Sopenharmony_ci  }
1241cb0ef41Sopenharmony_ci}
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ciint ngtcp2_dcid_verify_uniqueness(ngtcp2_dcid *dcid, uint64_t seq,
1271cb0ef41Sopenharmony_ci                                  const ngtcp2_cid *cid, const uint8_t *token) {
1281cb0ef41Sopenharmony_ci  if (dcid->seq == seq) {
1291cb0ef41Sopenharmony_ci    return ngtcp2_cid_eq(&dcid->cid, cid) &&
1301cb0ef41Sopenharmony_ci                   (dcid->flags & NGTCP2_DCID_FLAG_TOKEN_PRESENT) &&
1311cb0ef41Sopenharmony_ci                   memcmp(dcid->token, token,
1321cb0ef41Sopenharmony_ci                          NGTCP2_STATELESS_RESET_TOKENLEN) == 0
1331cb0ef41Sopenharmony_ci               ? 0
1341cb0ef41Sopenharmony_ci               : NGTCP2_ERR_PROTO;
1351cb0ef41Sopenharmony_ci  }
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci  return !ngtcp2_cid_eq(&dcid->cid, cid) ? 0 : NGTCP2_ERR_PROTO;
1381cb0ef41Sopenharmony_ci}
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ciint ngtcp2_dcid_verify_stateless_reset_token(const ngtcp2_dcid *dcid,
1411cb0ef41Sopenharmony_ci                                             const uint8_t *token) {
1421cb0ef41Sopenharmony_ci  return (dcid->flags & NGTCP2_DCID_FLAG_TOKEN_PRESENT) &&
1431cb0ef41Sopenharmony_ci                 ngtcp2_cmemeq(dcid->token, token,
1441cb0ef41Sopenharmony_ci                               NGTCP2_STATELESS_RESET_TOKENLEN)
1451cb0ef41Sopenharmony_ci             ? 0
1461cb0ef41Sopenharmony_ci             : NGTCP2_ERR_INVALID_ARGUMENT;
1471cb0ef41Sopenharmony_ci}
148