• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* 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 a particular Connection ID. */
41 #define NGTCP2_SCID_FLAG_USED 0x01u
42 /* NGTCP2_SCID_FLAG_RETIRED indicates that a particular Connection ID
43    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 CID. */
49   uint64_t seq;
50   /* cid is a connection ID */
51   ngtcp2_cid cid;
52   /* retired_ts is the timestamp when peer tells that this CID is
53      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 CID. */
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 peer tells that this CID 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 payload that is
89      allowed to send 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 associated to this CID.
94      Actually, the stateless reset token is tied to the connection,
95      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 it 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 payload size supported
141  * 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 path.
153  */
154 void ngtcp2_dcid_copy_cid_token(ngtcp2_dcid *dest, const ngtcp2_dcid *src);
155 
156 /*
157  * ngtcp2_dcid_verify_uniqueness verifies uniqueness of (|seq|, |cid|,
158  * |token|) tuple against |dcid|.
159  */
160 int ngtcp2_dcid_verify_uniqueness(ngtcp2_dcid *dcid, uint64_t seq,
161                                   const ngtcp2_cid *cid, const uint8_t *token);
162 
163 /*
164  * ngtcp2_dcid_verify_stateless_reset_token verifies stateless reset
165  * token |token| against the one included in |dcid|.  This function
166  * returns 0 if the verification succeeds, or one of the following
167  * negative error codes:
168  *
169  * NGTCP2_ERR_INVALID_ARGUMENT
170  *     Tokens do not match; or |dcid| does not contain a token.
171  */
172 int ngtcp2_dcid_verify_stateless_reset_token(const ngtcp2_dcid *dcid,
173                                              const uint8_t *token);
174 
175 #endif /* NGTCP2_CID_H */
176