ngtcp2_cid.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2018 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. #ifndef NGTCP2_CID_H
  26. #define NGTCP2_CID_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* defined(HAVE_CONFIG_H) */
  30. #include <ngtcp2/ngtcp2.h>
  31. #include "ngtcp2_pq.h"
  32. #include "ngtcp2_path.h"
  33. /* NGTCP2_SCID_FLAG_NONE indicates that no flag is set. */
  34. #define NGTCP2_SCID_FLAG_NONE 0x00u
  35. /* NGTCP2_SCID_FLAG_USED indicates that a local endpoint observed that
  36. a remote endpoint uses this particular Connection ID. */
  37. #define NGTCP2_SCID_FLAG_USED 0x01u
  38. /* NGTCP2_SCID_FLAG_RETIRED indicates that this particular Connection
  39. ID is retired. */
  40. #define NGTCP2_SCID_FLAG_RETIRED 0x02u
  41. typedef struct ngtcp2_scid {
  42. ngtcp2_pq_entry pe;
  43. /* seq is the sequence number associated to the Connection ID. */
  44. uint64_t seq;
  45. /* cid is a connection ID */
  46. ngtcp2_cid cid;
  47. /* retired_ts is the timestamp when a remote endpoint tells that
  48. this Connection ID is retired. */
  49. ngtcp2_tstamp retired_ts;
  50. /* flags is the bitwise OR of zero or more of NGTCP2_SCID_FLAG_*. */
  51. uint8_t flags;
  52. } ngtcp2_scid;
  53. /* NGTCP2_DCID_FLAG_NONE indicates that no flag is set. */
  54. #define NGTCP2_DCID_FLAG_NONE 0x00u
  55. /* NGTCP2_DCID_FLAG_PATH_VALIDATED indicates that an associated path
  56. has been validated. */
  57. #define NGTCP2_DCID_FLAG_PATH_VALIDATED 0x01u
  58. /* NGTCP2_DCID_FLAG_TOKEN_PRESENT indicates that a stateless reset
  59. token is set in token field. */
  60. #define NGTCP2_DCID_FLAG_TOKEN_PRESENT 0x02u
  61. typedef struct ngtcp2_dcid {
  62. /* seq is the sequence number associated to the Connection ID. */
  63. uint64_t seq;
  64. /* cid is a Connection ID */
  65. ngtcp2_cid cid;
  66. /* path is a path which cid is bound to. The addresses are zero
  67. length if cid has not been bound to a particular path yet. */
  68. ngtcp2_path_storage ps;
  69. /* retired_ts is the timestamp when this Connection ID is
  70. retired. */
  71. ngtcp2_tstamp retired_ts;
  72. /* bound_ts is the timestamp when this Connection ID is bound to a
  73. particular path. It is only assigned when a Connection ID is
  74. used just for sending PATH_RESPONSE, and is not zero-length. */
  75. ngtcp2_tstamp bound_ts;
  76. /* bytes_sent is the number of bytes sent to an associated path. */
  77. uint64_t bytes_sent;
  78. /* bytes_recv is the number of bytes received from an associated
  79. path. */
  80. uint64_t bytes_recv;
  81. /* max_udp_payload_size is the maximum size of UDP datagram payload
  82. that is allowed to be sent to this path. */
  83. size_t max_udp_payload_size;
  84. /* flags is bitwise OR of zero or more of NGTCP2_DCID_FLAG_*. */
  85. uint8_t flags;
  86. /* token is a stateless reset token received along with this
  87. Connection ID. The stateless reset token is tied to the
  88. connection, not to the particular Connection ID. */
  89. uint8_t token[NGTCP2_STATELESS_RESET_TOKENLEN];
  90. } ngtcp2_dcid;
  91. /* ngtcp2_cid_zero makes |cid| zero-length. */
  92. void ngtcp2_cid_zero(ngtcp2_cid *cid);
  93. /*
  94. * ngtcp2_cid_less returns nonzero if |lhs| is lexicographical smaller
  95. * than |rhs|.
  96. */
  97. int ngtcp2_cid_less(const ngtcp2_cid *lhs, const ngtcp2_cid *rhs);
  98. /*
  99. * ngtcp2_cid_empty returns nonzero if |cid| includes empty Connection
  100. * ID.
  101. */
  102. int ngtcp2_cid_empty(const ngtcp2_cid *cid);
  103. /*
  104. * ngtcp2_scid_init initializes |scid| with the given parameters.
  105. */
  106. void ngtcp2_scid_init(ngtcp2_scid *scid, uint64_t seq, const ngtcp2_cid *cid);
  107. /*
  108. * ngtcp2_scid_copy copies |src| into |dest|.
  109. */
  110. void ngtcp2_scid_copy(ngtcp2_scid *dest, const ngtcp2_scid *src);
  111. /*
  112. * ngtcp2_dcid_init initializes |dcid| with the given parameters. If
  113. * |token| is NULL, the function fills dcid->token with 0. |token|
  114. * must be NGTCP2_STATELESS_RESET_TOKENLEN bytes long.
  115. */
  116. void ngtcp2_dcid_init(ngtcp2_dcid *dcid, uint64_t seq, const ngtcp2_cid *cid,
  117. const uint8_t *token);
  118. /*
  119. * ngtcp2_dcid_set_token sets |token| to |dcid|. |token| must not be
  120. * NULL, and must be NGTCP2_STATELESS_RESET_TOKENLEN bytes long.
  121. */
  122. void ngtcp2_dcid_set_token(ngtcp2_dcid *dcid, const uint8_t *token);
  123. /*
  124. * ngtcp2_dcid_set_path sets |path| to |dcid|. It sets
  125. * max_udp_payload_size to the minimum UDP datagram payload size
  126. * supported by the IP protocol version.
  127. */
  128. void ngtcp2_dcid_set_path(ngtcp2_dcid *dcid, const ngtcp2_path *path);
  129. /*
  130. * ngtcp2_dcid_copy copies |src| into |dest|.
  131. */
  132. void ngtcp2_dcid_copy(ngtcp2_dcid *dest, const ngtcp2_dcid *src);
  133. /*
  134. * ngtcp2_dcid_copy_cid_token behaves like ngtcp2_dcid_copy, but it
  135. * only copies cid, seq, and token.
  136. */
  137. void ngtcp2_dcid_copy_cid_token(ngtcp2_dcid *dest, const ngtcp2_dcid *src);
  138. /*
  139. * ngtcp2_dcid_verify_uniqueness verifies uniqueness of (|seq|, |cid|,
  140. * |token|) tuple against |dcid|.
  141. */
  142. int ngtcp2_dcid_verify_uniqueness(const ngtcp2_dcid *dcid, uint64_t seq,
  143. const ngtcp2_cid *cid, const uint8_t *token);
  144. /*
  145. * ngtcp2_dcid_verify_stateless_reset_token verifies stateless reset
  146. * token |token| against the one included in |dcid|. Tokens are
  147. * compared in constant time. This function returns 0 if the
  148. * verification succeeds, or one of the following negative error
  149. * codes:
  150. *
  151. * NGTCP2_ERR_INVALID_ARGUMENT
  152. * Tokens do not match; or |dcid| does not contain a token.
  153. */
  154. int ngtcp2_dcid_verify_stateless_reset_token(const ngtcp2_dcid *dcid,
  155. const uint8_t *token);
  156. #endif /* !defined(NGTCP2_CID_H) */