+static struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new)
+{
+ size_t len, cmplen = offsetof(struct odhcp6c_entry, target) + ((new->length + 7) / 8);
+ uint8_t *start = odhcp6c_get_state(state, &len);
+
+ for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start;
+ (uint8_t*)c < &start[len] &&
+ (uint8_t*)odhcp6c_next_entry(c) <= &start[len];
+ c = odhcp6c_next_entry(c))
+ if (!memcmp(c, new, cmplen) && !memcmp(c->auxtarget, new->auxtarget, new->auxlen))
+ return c;
+
+ return NULL;
+}
+
+
+bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new,
+ uint32_t safe, bool filterexcess)
+{
+ size_t len;
+ struct odhcp6c_entry *x = odhcp6c_find_entry(state, new);
+ uint8_t *start = odhcp6c_get_state(state, &len);
+
+ if (x && x->valid > new->valid && new->valid < safe)
+ new->valid = safe;
+
+ if (new->valid > 0) {
+ if (x) {
+ if (filterexcess && new->valid >= x->valid &&
+ new->valid != UINT32_MAX &&
+ new->valid - x->valid < min_update_interval &&
+ new->preferred >= x->preferred &&
+ new->preferred != UINT32_MAX &&
+ new->preferred - x->preferred < min_update_interval)
+ return false;
+ x->valid = new->valid;
+ x->preferred = new->preferred;
+ x->t1 = new->t1;
+ x->t2 = new->t2;
+ x->iaid = new->iaid;
+ } else {
+ odhcp6c_add_state(state, new, odhcp6c_entry_size(new));
+ }
+ } else if (x) {
+ odhcp6c_remove_state(state, ((uint8_t*)x) - start, odhcp6c_entry_size(x));
+ }
+ return true;
+}
+
+
+static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed)
+{
+ size_t len;
+ uint8_t *start = odhcp6c_get_state(state, &len);
+ for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start;
+ (uint8_t*)c < &start[len] &&
+ (uint8_t*)odhcp6c_next_entry(c) <= &start[len];
+ ) {
+ if (c->t1 < elapsed)
+ c->t1 = 0;
+ else if (c->t1 != UINT32_MAX)
+ c->t1 -= elapsed;
+
+ if (c->t2 < elapsed)
+ c->t2 = 0;
+ else if (c->t2 != UINT32_MAX)
+ c->t2 -= elapsed;
+
+ if (c->preferred < elapsed)
+ c->preferred = 0;
+ else if (c->preferred != UINT32_MAX)
+ c->preferred -= elapsed;
+
+ if (c->valid < elapsed)
+ c->valid = 0;
+ else if (c->valid != UINT32_MAX)
+ c->valid -= elapsed;
+
+ if (!c->valid) {
+ odhcp6c_remove_state(state, ((uint8_t*)c) - start, odhcp6c_entry_size(c));
+ start = odhcp6c_get_state(state, &len);
+ } else {
+ c = odhcp6c_next_entry(c);
+ }
+ }
+}
+
+
+void odhcp6c_expire(void)
+{
+ time_t now = odhcp6c_get_milli_time() / 1000;
+ uint32_t elapsed = (last_update > 0) ? now - last_update : 0;
+ last_update = now;
+
+ odhcp6c_expire_list(STATE_RA_PREFIX, elapsed);
+ odhcp6c_expire_list(STATE_RA_ROUTE, elapsed);
+ odhcp6c_expire_list(STATE_RA_DNS, elapsed);
+ odhcp6c_expire_list(STATE_RA_SEARCH, elapsed);
+ odhcp6c_expire_list(STATE_IA_NA, elapsed);
+ odhcp6c_expire_list(STATE_IA_PD, elapsed);
+}
+
+
+uint32_t odhcp6c_elapsed(void)
+{
+ return odhcp6c_get_milli_time() / 1000 - last_update;
+}
+
+
+int odhcp6c_random(void *buf, size_t len)
+{
+ return read(urandom_fd, buf, len);
+}
+
+bool odhcp6c_is_bound(void)
+{
+ return bound;
+}
+