Add option to ignore default lifetime for RDNSS records
[project/odhcp6c.git] / src / odhcp6c.c
index 133ade707a788efa57e9431d5c094f59728b3ca2..e97ad267f192e5b6e421658f8dae2e92eea61d46 100644 (file)
@@ -74,8 +74,9 @@ int main(_unused int argc, char* const argv[])
        int logopt = LOG_PID;
        int c;
        unsigned int client_options = DHCPV6_CLIENT_FQDN | DHCPV6_ACCEPT_RECONFIGURE;
+       unsigned int ra_options = RA_RDNSS_DEFAULT_LIFETIME;
 
-       while ((c = getopt(argc, argv, "S::N:V:P:FB:c:i:r:Ru:s:kt:m:hedp:fav")) != -1) {
+       while ((c = getopt(argc, argv, "S::N:V:P:FB:c:i:r:Ru:s:kt:m:Lhedp:fav")) != -1) {
                switch (c) {
                case 'S':
                        allow_slaac_only = (optarg) ? atoi(optarg) : -1;
@@ -193,6 +194,10 @@ int main(_unused int argc, char* const argv[])
                        min_update_interval = atoi(optarg);
                        break;
 
+               case 'L':
+                       ra_options &= ~RA_RDNSS_DEFAULT_LIFETIME;
+                       break;
+
                case 'e':
                        logopt |= LOG_PERROR;
                        break;
@@ -244,7 +249,8 @@ int main(_unused int argc, char* const argv[])
 
        if ((urandom_fd = open("/dev/urandom", O_CLOEXEC | O_RDONLY)) < 0 ||
                        init_dhcpv6(ifname, client_options, sol_timeout) ||
-                       ra_init(ifname, &ifid) || script_init(script, ifname)) {
+                       ra_init(ifname, &ifid, ra_options) ||
+                       script_init(script, ifname)) {
                syslog(LOG_ERR, "failed to initialize: %s", strerror(errno));
                return 3;
        }
@@ -443,6 +449,7 @@ static int usage(void)
        "       -k              Don't send a RELEASE when stopping\n"
        "       -t <seconds>    Maximum timeout for DHCPv6-SOLICIT (120)\n"
        "       -m <seconds>    Minimum time between accepting updates (30)\n"
+       "       -L              Ignore default lifetime for RDNSS records\n"
        "\nInvocation options:\n"
        "       -p <pidfile>    Set pidfile (/var/run/odhcp6c.pid)\n"
        "       -d              Daemonize\n"
@@ -516,11 +523,11 @@ void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len)
                memcpy(n, data, len);
 }
 
-void odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *data, size_t len)
+int odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *data, size_t len)
 {
        ssize_t len_after = state_len[state] - offset;
        if (len_after < 0)
-               return;
+               return -1;
 
        uint8_t *n = odhcp6c_resize_state(state, len);
        if (n) {
@@ -529,6 +536,8 @@ void odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *d
                memmove(sdata + offset + len, sdata + offset, len_after);
                memcpy(sdata + offset, data, len);
        }
+
+       return 0;
 }
 
 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len)
@@ -568,8 +577,9 @@ static struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const
        uint8_t *start = odhcp6c_get_state(state, &len);
 
        for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start;
-                       (uint8_t*)c < &start[len] && &c->auxtarget[c->auxlen] <= &start[len];
-                       c = (struct odhcp6c_entry*)(&c->auxtarget[c->auxlen]))
+                       (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;
 
@@ -602,10 +612,10 @@ bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new,
                        x->t2 = new->t2;
                        x->iaid = new->iaid;
                } else {
-                       odhcp6c_add_state(state, new, sizeof(*new) + new->auxlen);
+                       odhcp6c_add_state(state, new, odhcp6c_entry_size(new));
                }
        } else if (x) {
-               odhcp6c_remove_state(state, ((uint8_t*)x) - start, sizeof(*x) + x->auxlen);
+               odhcp6c_remove_state(state, ((uint8_t*)x) - start, odhcp6c_entry_size(x));
        }
        return true;
 }
@@ -616,8 +626,9 @@ 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] && &c->auxtarget[c->auxlen] <= &start[len];
-                       c = (struct odhcp6c_entry*)(&c->auxtarget[c->auxlen])) {
+                       (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)
@@ -638,8 +649,12 @@ static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed)
                else if (c->valid != UINT32_MAX)
                        c->valid -= elapsed;
 
-               if (!c->valid)
-                       odhcp6c_remove_state(state, ((uint8_t*)c) - start, sizeof(*c) + c->auxlen);
+               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);
+               }
        }
 }