| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include "ngtcp2_pv.h" |
| 26 |
|
| 27 |
#include <string.h> |
| 28 |
#include <assert.h> |
| 29 |
|
| 30 |
#include "ngtcp2_mem.h" |
| 31 |
#include "ngtcp2_log.h" |
| 32 |
#include "ngtcp2_macro.h" |
| 33 |
#include "ngtcp2_addr.h" |
| 34 |
|
| 35 |
void ngtcp2_pv_entry_init(ngtcp2_pv_entry *pvent, const uint8_t *data, |
| 36 |
ngtcp2_tstamp expiry, uint8_t flags) { |
| 37 |
memcpy(pvent->data, data, sizeof(pvent->data)); |
| 38 |
pvent->expiry = expiry; |
| 39 |
pvent->flags = flags; |
| 40 |
} |
| 41 |
|
| 42 |
int ngtcp2_pv_new(ngtcp2_pv **ppv, const ngtcp2_dcid *dcid, |
| 43 |
ngtcp2_duration timeout, uint8_t flags, ngtcp2_log *log, |
| 44 |
const ngtcp2_mem *mem) { |
| 45 |
(*ppv) = ngtcp2_mem_malloc(mem, sizeof(ngtcp2_pv)); |
| 46 |
if (*ppv == NULL) { |
| 47 |
return NGTCP2_ERR_NOMEM; |
| 48 |
} |
| 49 |
|
| 50 |
ngtcp2_static_ringbuf_pv_ents_init(&(*ppv)->ents); |
| 51 |
|
| 52 |
ngtcp2_dcid_copy(&(*ppv)->dcid, dcid); |
| 53 |
|
| 54 |
(*ppv)->mem = mem; |
| 55 |
(*ppv)->log = log; |
| 56 |
(*ppv)->timeout = timeout; |
| 57 |
(*ppv)->fallback_pto = 0; |
| 58 |
(*ppv)->started_ts = UINT64_MAX; |
| 59 |
(*ppv)->probe_pkt_left = NGTCP2_PV_NUM_PROBE_PKT; |
| 60 |
(*ppv)->round = 0; |
| 61 |
(*ppv)->flags = flags; |
| 62 |
|
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
void ngtcp2_pv_del(ngtcp2_pv *pv) { |
| 67 |
if (pv == NULL) { |
| 68 |
return; |
| 69 |
} |
| 70 |
ngtcp2_mem_free(pv->mem, pv); |
| 71 |
} |
| 72 |
|
| 73 |
void ngtcp2_pv_add_entry(ngtcp2_pv *pv, const uint8_t *data, |
| 74 |
ngtcp2_tstamp expiry, uint8_t flags, |
| 75 |
ngtcp2_tstamp ts) { |
| 76 |
ngtcp2_pv_entry *ent; |
| 77 |
|
| 78 |
assert(pv->probe_pkt_left); |
| 79 |
|
| 80 |
if (ngtcp2_ringbuf_len(&pv->ents.rb) == 0) { |
| 81 |
pv->started_ts = ts; |
| 82 |
} |
| 83 |
|
| 84 |
ent = ngtcp2_ringbuf_push_back(&pv->ents.rb); |
| 85 |
ngtcp2_pv_entry_init(ent, data, expiry, flags); |
| 86 |
|
| 87 |
pv->flags &= (uint8_t)~NGTCP2_PV_FLAG_CANCEL_TIMER; |
| 88 |
--pv->probe_pkt_left; |
| 89 |
} |
| 90 |
|
| 91 |
int ngtcp2_pv_validate(ngtcp2_pv *pv, uint8_t *pflags, const uint8_t *data) { |
| 92 |
size_t len = ngtcp2_ringbuf_len(&pv->ents.rb); |
| 93 |
size_t i; |
| 94 |
ngtcp2_pv_entry *ent; |
| 95 |
|
| 96 |
if (len == 0) { |
| 97 |
return NGTCP2_ERR_INVALID_STATE; |
| 98 |
} |
| 99 |
|
| 100 |
for (i = 0; i < len; ++i) { |
| 101 |
ent = ngtcp2_ringbuf_get(&pv->ents.rb, i); |
| 102 |
if (memcmp(ent->data, data, sizeof(ent->data)) == 0) { |
| 103 |
*pflags = ent->flags; |
| 104 |
ngtcp2_log_info(pv->log, NGTCP2_LOG_EVENT_PTV, "path has been validated"); |
| 105 |
return 0; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
return NGTCP2_ERR_INVALID_ARGUMENT; |
| 110 |
} |
| 111 |
|
| 112 |
void ngtcp2_pv_handle_entry_expiry(ngtcp2_pv *pv, ngtcp2_tstamp ts) { |
| 113 |
ngtcp2_pv_entry *ent; |
| 114 |
|
| 115 |
if (ngtcp2_ringbuf_len(&pv->ents.rb) == 0) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
ent = ngtcp2_ringbuf_get(&pv->ents.rb, ngtcp2_ringbuf_len(&pv->ents.rb) - 1); |
| 120 |
|
| 121 |
if (ent->expiry > ts) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
++pv->round; |
| 126 |
pv->probe_pkt_left = NGTCP2_PV_NUM_PROBE_PKT; |
| 127 |
} |
| 128 |
|
| 129 |
int ngtcp2_pv_should_send_probe(ngtcp2_pv *pv) { |
| 130 |
return pv->probe_pkt_left > 0; |
| 131 |
} |
| 132 |
|
| 133 |
int ngtcp2_pv_validation_timed_out(ngtcp2_pv *pv, ngtcp2_tstamp ts) { |
| 134 |
ngtcp2_tstamp t; |
| 135 |
ngtcp2_pv_entry *ent; |
| 136 |
|
| 137 |
if (pv->started_ts == UINT64_MAX) { |
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
assert(ngtcp2_ringbuf_len(&pv->ents.rb)); |
| 142 |
|
| 143 |
ent = ngtcp2_ringbuf_get(&pv->ents.rb, ngtcp2_ringbuf_len(&pv->ents.rb) - 1); |
| 144 |
|
| 145 |
t = pv->started_ts + pv->timeout; |
| 146 |
t = ngtcp2_max_uint64(t, ent->expiry); |
| 147 |
|
| 148 |
return t <= ts; |
| 149 |
} |
| 150 |
|
| 151 |
ngtcp2_tstamp ngtcp2_pv_next_expiry(ngtcp2_pv *pv) { |
| 152 |
ngtcp2_pv_entry *ent; |
| 153 |
|
| 154 |
if ((pv->flags & NGTCP2_PV_FLAG_CANCEL_TIMER) || |
| 155 |
ngtcp2_ringbuf_len(&pv->ents.rb) == 0) { |
| 156 |
return UINT64_MAX; |
| 157 |
} |
| 158 |
|
| 159 |
ent = ngtcp2_ringbuf_get(&pv->ents.rb, ngtcp2_ringbuf_len(&pv->ents.rb) - 1); |
| 160 |
|
| 161 |
return ent->expiry; |
| 162 |
} |
| 163 |
|
| 164 |
void ngtcp2_pv_cancel_expired_timer(ngtcp2_pv *pv, ngtcp2_tstamp ts) { |
| 165 |
ngtcp2_tstamp expiry = ngtcp2_pv_next_expiry(pv); |
| 166 |
|
| 167 |
if (expiry > ts) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
pv->flags |= NGTCP2_PV_FLAG_CANCEL_TIMER; |
| 172 |
} |
| 173 |
|
| 174 |
void ngtcp2_pv_set_fallback(ngtcp2_pv *pv, const ngtcp2_dcid *dcid, |
| 175 |
ngtcp2_duration pto) { |
| 176 |
pv->flags |= NGTCP2_PV_FLAG_FALLBACK_PRESENT; |
| 177 |
ngtcp2_dcid_copy(&pv->fallback_dcid, dcid); |
| 178 |
pv->fallback_pto = pto; |
| 179 |
} |