| 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 | |
| 28 | #ifdef HAVE_CONFIG_H |
| 29 | # include <config.h> |
| 30 | #endif /* defined(HAVE_CONFIG_H) */ |
| 31 | |
| 32 | #include <ngtcp2/ngtcp2.h> |
| 33 | |
| 34 | #include "ngtcp2_pq.h" |
| 35 | #include "ngtcp2_path.h" |
| 36 | |
| 37 | /* NGTCP2_SCID_FLAG_NONE indicates that no flag is set. */ |
| 38 | #define NGTCP2_SCID_FLAG_NONE 0x00u |
| 39 | /* NGTCP2_SCID_FLAG_USED indicates that a local endpoint observed that |
| 40 | a remote endpoint uses this particular Connection ID. */ |
| 41 | #define NGTCP2_SCID_FLAG_USED 0x01u |
| 42 | /* NGTCP2_SCID_FLAG_RETIRED indicates that this particular Connection |
| 43 | ID is retired. */ |
| 44 | #define NGTCP2_SCID_FLAG_RETIRED 0x02u |
| 45 | |
| 46 | typedef struct ngtcp2_scid { |
| 47 | ngtcp2_pq_entry pe; |
| 48 | /* seq is the sequence number associated to the Connection ID. */ |
| 49 | uint64_t seq; |
| 50 | /* cid is a connection ID */ |
| 51 | ngtcp2_cid cid; |
| 52 | /* retired_ts is the timestamp when a remote endpoint tells that |
| 53 | this Connection ID is retired. */ |
| 54 | ngtcp2_tstamp retired_ts; |
| 55 | /* flags is the bitwise OR of zero or more of NGTCP2_SCID_FLAG_*. */ |
| 56 | uint8_t flags; |
| 57 | } ngtcp2_scid; |
| 58 | |
| 59 | /* NGTCP2_DCID_FLAG_NONE indicates that no flag is set. */ |
| 60 | #define NGTCP2_DCID_FLAG_NONE 0x00u |
| 61 | /* NGTCP2_DCID_FLAG_PATH_VALIDATED indicates that an associated path |
| 62 | has been validated. */ |
| 63 | #define NGTCP2_DCID_FLAG_PATH_VALIDATED 0x01u |
| 64 | /* NGTCP2_DCID_FLAG_TOKEN_PRESENT indicates that a stateless reset |
| 65 | token is set in token field. */ |
| 66 | #define NGTCP2_DCID_FLAG_TOKEN_PRESENT 0x02u |
| 67 | |
| 68 | typedef struct ngtcp2_dcid { |
| 69 | /* seq is the sequence number associated to the Connection ID. */ |
| 70 | uint64_t seq; |
| 71 | /* cid is a Connection ID */ |
| 72 | ngtcp2_cid cid; |
| 73 | /* path is a path which cid is bound to. The addresses are zero |
| 74 | length if cid has not been bound to a particular path yet. */ |
| 75 | ngtcp2_path_storage ps; |
| 76 | /* retired_ts is the timestamp when this Connection ID is |
| 77 | retired. */ |
| 78 | ngtcp2_tstamp retired_ts; |
| 79 | /* bound_ts is the timestamp when this Connection ID is bound to a |
| 80 | particular path. It is only assigned when a Connection ID is |
| 81 | used just for sending PATH_RESPONSE, and is not zero-length. */ |
| 82 | ngtcp2_tstamp bound_ts; |
| 83 | /* bytes_sent is the number of bytes sent to an associated path. */ |
| 84 | uint64_t bytes_sent; |
| 85 | /* bytes_recv is the number of bytes received from an associated |
| 86 | path. */ |
| 87 | uint64_t bytes_recv; |
| 88 | /* max_udp_payload_size is the maximum size of UDP datagram payload |
| 89 | that is allowed to be sent to this path. */ |
| 90 | size_t max_udp_payload_size; |
| 91 | /* flags is bitwise OR of zero or more of NGTCP2_DCID_FLAG_*. */ |
| 92 | uint8_t flags; |
| 93 | /* token is a stateless reset token received along with this |
| 94 | Connection ID. The stateless reset token is tied to the |
| 95 | connection, not to the particular Connection ID. */ |
| 96 | uint8_t token[NGTCP2_STATELESS_RESET_TOKENLEN]; |
| 97 | } ngtcp2_dcid; |
| 98 | |
| 99 | /* ngtcp2_cid_zero makes |cid| zero-length. */ |
| 100 | void ngtcp2_cid_zero(ngtcp2_cid *cid); |
| 101 | |
| 102 | /* |
| 103 | * ngtcp2_cid_less returns nonzero if |lhs| is lexicographical smaller |
| 104 | * than |rhs|. |
| 105 | */ |
| 106 | int ngtcp2_cid_less(const ngtcp2_cid *lhs, const ngtcp2_cid *rhs); |
| 107 | |
| 108 | /* |
| 109 | * ngtcp2_cid_empty returns nonzero if |cid| includes empty Connection |
| 110 | * ID. |
| 111 | */ |
| 112 | int ngtcp2_cid_empty(const ngtcp2_cid *cid); |
| 113 | |
| 114 | /* |
| 115 | * ngtcp2_scid_init initializes |scid| with the given parameters. |
| 116 | */ |
| 117 | void ngtcp2_scid_init(ngtcp2_scid *scid, uint64_t seq, const ngtcp2_cid *cid); |
| 118 | |
| 119 | /* |
| 120 | * ngtcp2_scid_copy copies |src| into |dest|. |
| 121 | */ |
| 122 | void ngtcp2_scid_copy(ngtcp2_scid *dest, const ngtcp2_scid *src); |
| 123 | |
| 124 | /* |
| 125 | * ngtcp2_dcid_init initializes |dcid| with the given parameters. If |
| 126 | * |token| is NULL, the function fills dcid->token with 0. |token| |
| 127 | * must be NGTCP2_STATELESS_RESET_TOKENLEN bytes long. |
| 128 | */ |
| 129 | void ngtcp2_dcid_init(ngtcp2_dcid *dcid, uint64_t seq, const ngtcp2_cid *cid, |
| 130 | const uint8_t *token); |
| 131 | |
| 132 | /* |
| 133 | * ngtcp2_dcid_set_token sets |token| to |dcid|. |token| must not be |
| 134 | * NULL, and must be NGTCP2_STATELESS_RESET_TOKENLEN bytes long. |
| 135 | */ |
| 136 | void ngtcp2_dcid_set_token(ngtcp2_dcid *dcid, const uint8_t *token); |
| 137 | |
| 138 | /* |
| 139 | * ngtcp2_dcid_set_path sets |path| to |dcid|. It sets |
| 140 | * max_udp_payload_size to the minimum UDP datagram payload size |
| 141 | * supported by the IP protocol version. |
| 142 | */ |
| 143 | void ngtcp2_dcid_set_path(ngtcp2_dcid *dcid, const ngtcp2_path *path); |
| 144 | |
| 145 | /* |
| 146 | * ngtcp2_dcid_copy copies |src| into |dest|. |
| 147 | */ |
| 148 | void ngtcp2_dcid_copy(ngtcp2_dcid *dest, const ngtcp2_dcid *src); |
| 149 | |
| 150 | /* |
| 151 | * ngtcp2_dcid_copy_cid_token behaves like ngtcp2_dcid_copy, but it |
| 152 | * only copies cid, seq, and token. dest->flags should be initialized |
| 153 | * before this call because NGTCP2_DCID_FLAG_TOKEN_PRESENT is set or |
| 154 | * unset. |
| 155 | */ |
| 156 | void ngtcp2_dcid_copy_cid_token(ngtcp2_dcid *dest, const ngtcp2_dcid *src); |
| 157 | |
| 158 | /* |
| 159 | * ngtcp2_dcid_verify_uniqueness verifies uniqueness of (|seq|, |cid|, |
| 160 | * |token|) tuple against |dcid|. |
| 161 | */ |
| 162 | int ngtcp2_dcid_verify_uniqueness(const ngtcp2_dcid *dcid, uint64_t seq, |
| 163 | const ngtcp2_cid *cid, const uint8_t *token); |
| 164 | |
| 165 | /* |
| 166 | * ngtcp2_dcid_verify_stateless_reset_token verifies stateless reset |
| 167 | * token |token| received on |path| against the one included in |
| 168 | * |dcid|. Tokens are compared in constant time. This function |
| 169 | * returns 0 if the verification succeeds, or one of the following |
| 170 | * negative error codes: |
| 171 | * |
| 172 | * NGTCP2_ERR_INVALID_ARGUMENT |
| 173 | * Tokens do not match; or |dcid| does not contain a token. |
| 174 | */ |
| 175 | int ngtcp2_dcid_verify_stateless_reset_token(const ngtcp2_dcid *dcid, |
| 176 | const ngtcp2_path *path, |
| 177 | const uint8_t *token); |
| 178 | |
| 179 | /* TODO It might be performance win if we store congestion state in |
| 180 | this entry, and restore it when migrate back to this path. */ |
| 181 | typedef struct ngtcp2_path_history_entry { |
| 182 | /* ps contains path. */ |
| 183 | ngtcp2_path_storage ps; |
| 184 | /* max_udp_payload_size is the maximum size of UDP datagram payload |
| 185 | that is allowed to be sent to this path. */ |
| 186 | size_t max_udp_payload_size; |
| 187 | /* ts is the timestamp when this entry is added to the path history. |
| 188 | It happens when a local endpoint migrates to the another path. */ |
| 189 | ngtcp2_tstamp ts; |
| 190 | } ngtcp2_path_history_entry; |
| 191 | |
| 192 | /* |
| 193 | * ngtcp2_dcid_apply_validated_path applies the defaults from |ent| |
| 194 | * which contains the validated path and its stored configurations. |
| 195 | */ |
| 196 | void ngtcp2_dcid_apply_validated_path(ngtcp2_dcid *dcid, |
| 197 | const ngtcp2_path_history_entry *ent); |
| 198 | |
| 199 | #endif /* !defined(NGTCP2_CID_H) */ |