+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 / 8;
+ struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
+ struct odhcp6c_entry *x = NULL;
+
+ for (struct odhcp6c_entry *c = start; !x && c < &start[len/sizeof(*c)]; ++c)
+ if (!memcmp(c, new, cmplen))
+ return c;
+
+ return NULL;
+}
+
+
+bool odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe)
+{
+ size_t len;
+ struct odhcp6c_entry *x = odhcp6c_find_entry(state, new);
+ struct odhcp6c_entry *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 (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 &&
+ x->class == new->class)
+ return false;
+ x->valid = new->valid;
+ x->preferred = new->preferred;
+ x->t1 = new->t1;
+ x->t2 = new->t2;
+ x->class = new->class;
+ x->iaid = new->iaid;
+ } else {
+ odhcp6c_add_state(state, new, sizeof(*new));
+ }
+ } else if (x) {
+ odhcp6c_remove_state(state, (x - start) * sizeof(*x), sizeof(*x));
+ }
+ return true;
+}
+
+
+bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new)
+{
+ return odhcp6c_update_entry_safe(state, new, 0);
+}
+
+
+static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed)
+{
+ size_t len;
+ struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
+ for (struct odhcp6c_entry *c = start; c < &start[len / sizeof(*c)]; ++c) {
+ 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, (c - start) * sizeof(*c), sizeof(*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_IA_NA, elapsed);
+ odhcp6c_expire_list(STATE_IA_PD, elapsed);
+}
+
+
+uint32_t odhcp6c_elapsed(void)
+{
+ return odhcp6c_get_milli_time() / 1000 - last_update;
+}
+
+
+void odhcp6c_random(void *buf, size_t len)
+{
+ read(urandom_fd, buf, len);
+}
+
+bool odhcp6c_is_bound(void)
+{
+ return bound;
+}
+