+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 - x->valid < 60 &&
+ new->preferred >= x->preferred &&
+ new->preferred - x->preferred < 60 &&
+ 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;
+ } 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;
+}
+
+
+bool odhcp6c_addr_in_scope(const struct in6_addr *addr)
+{
+ FILE *fd = fopen("/proc/net/if_inet6", "r");
+ int len;
+ char buf[256];
+
+ if (fd == NULL)
+ return false;
+
+ while (fgets(buf, sizeof(buf), fd)) {
+ struct in6_addr inet6_addr;
+ uint32_t flags, dummy;
+ unsigned int i;
+ char name[8], addr_buf[32];
+
+ len = strlen(buf);
+
+ if ((len <= 0) || buf[len - 1] != '\n')
+ return false;
+
+ buf[--len] = '\0';
+
+ if (sscanf(buf, "%s %x %x %x %x %s",
+ addr_buf, &dummy, &dummy, &dummy, &flags, name) != 6)
+ return false;
+
+ if (strcmp(name, ifname) ||
+ (flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE | IFA_F_DEPRECATED)))
+ continue;
+
+ for (i = 0; i < sizeof(addr_buf); i++) {
+ if (!isxdigit(addr_buf[i]) || isupper(addr_buf[i]))
+ return false;
+ }
+
+ memset(&inet6_addr, 0, sizeof(inet6_addr));
+ for (i = 0; i < (sizeof(addr_buf) / 2); i++) {
+ unsigned char byte;
+ static const char hex[] = "0123456789abcdef";
+ byte = ((index(hex, addr_buf[i * 2]) - hex) << 4) |
+ (index(hex, addr_buf[i * 2 + 1]) - hex);
+ inet6_addr.s6_addr[i] = byte;
+ }
+
+ if ((IN6_IS_ADDR_LINKLOCAL(&inet6_addr) == IN6_IS_ADDR_LINKLOCAL(addr)) &&
+ (IN6_IS_ADDR_UNIQUELOCAL(&inet6_addr) == IN6_IS_ADDR_UNIQUELOCAL(addr)))
+ return true;
+ }
+ return false;
+}
+