Skip to content

Commit

Permalink
odhcpcd: enable to set absolute lifetimes
Browse files Browse the repository at this point in the history
UNDER WORK!!!
  • Loading branch information
PolynomialDivision committed Jan 6, 2021
1 parent 3bda900 commit 306080c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 25 deletions.
73 changes: 52 additions & 21 deletions src/config.c
Expand Up @@ -8,6 +8,7 @@
#include <string.h>
#include <sys/stat.h>
#include <syslog.h>
#include <time.h>

#include <uci.h>
#include <uci_blob.h>
Expand Down Expand Up @@ -83,6 +84,7 @@ enum {
IFACE_ATTR_NDPROXY_SLAVE,
IFACE_ATTR_PREFIX_FILTER,
IFACE_ATTR_PREFERRED_LIFETIME,
IFACE_ATTR_ABSOLUTE_LIFETIME,
IFACE_ATTR_MAX
};

Expand Down Expand Up @@ -132,6 +134,7 @@ static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
[IFACE_ATTR_NDPROXY_SLAVE] = { .name = "ndproxy_slave", .type = BLOBMSG_TYPE_BOOL },
[IFACE_ATTR_PREFIX_FILTER] = { .name = "prefix_filter", .type = BLOBMSG_TYPE_STRING },
[IFACE_ATTR_PREFERRED_LIFETIME] = { .name = "preferred_lifetime", .type = BLOBMSG_TYPE_STRING },
[IFACE_ATTR_ABSOLUTE_LIFETIME] = { .name = "absolute_lifetime", .type = BLOBMSG_TYPE_BOOL },
};

static const struct uci_blob_param_info iface_attr_info[IFACE_ATTR_MAX] = {
Expand Down Expand Up @@ -212,6 +215,7 @@ static void set_interface_defaults(struct interface *iface)
iface->ra_mininterval = iface->ra_maxinterval/3;
iface->ra_lifetime = -1;
iface->ra_dns = true;
iface->absolute_lifetime = false;
}

static void clean_interface(struct interface *iface)
Expand Down Expand Up @@ -321,29 +325,52 @@ static void set_config(struct uci_section *s)
}
}

static double parse_leasetime(struct blob_attr *c) {
static double parse_leasetime(struct blob_attr *c, bool absolute) {
char *val = blobmsg_get_string(c), *endptr = NULL;
double time = strcmp(val, "infinite") ? strtod(val, &endptr) : UINT32_MAX;

if (time && endptr && endptr[0]) {
if (endptr[0] == 's')
time *= 1;
else if (endptr[0] == 'm')
time *= 60;
else if (endptr[0] == 'h')
time *= 3600;
else if (endptr[0] == 'd')
time *= 24 * 3600;
else if (endptr[0] == 'w')
time *= 7 * 24 * 3600;
else
double ret_time = strcmp(val, "infinite") ? strtod(val, &endptr) : UINT32_MAX;

if (absolute)
{
struct tm tm = {0};
// "10 Jan 2020 00:00:00"
// Parse absolut time
char *s = strptime(val, "%d %b %Y %H:%M:%S", &tm);
if (s == NULL) {
syslog(LOG_ERR, "Failed to Parse Date: %s", val);
goto err;
}

// ToDo: Convert in odhcpcd_time
time_t now = odhcpd_time();
time_t wall_time = time(NULL);
time_t t = mktime(&tm);

double diff = difftime(t,wall_time);
ret_time += now + diff;

syslog(LOG_ERR, "Setting time to: %f", ret_time);
syslog(LOG_ERR, "Time values. Now: %ld, Wall-Time: %ld, Lifetime: %ld", now, wall_time, t);
} else {
if (ret_time && endptr && endptr[0]) {
if (endptr[0] == 's')
ret_time *= 1;
else if (endptr[0] == 'm')
ret_time *= 60;
else if (endptr[0] == 'h')
ret_time *= 3600;
else if (endptr[0] == 'd')
ret_time *= 24 * 3600;
else if (endptr[0] == 'w')
ret_time *= 7 * 24 * 3600;
else
goto err;
}
}

if (time < 60)
time = 60;
if (ret_time < 60)
ret_time = 60;

return time;
return ret_time;

err:
return -1;
Expand Down Expand Up @@ -409,7 +436,7 @@ int set_lease_from_blobmsg(struct blob_attr *ba)
}

if ((c = tb[LEASE_ATTR_LEASETIME])) {
double time = parse_leasetime(c);
double time = parse_leasetime(c, false); // do not support absolute timestamps for now
if (time < 0)
goto err;

Expand Down Expand Up @@ -520,16 +547,20 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
if ((c = tb[IFACE_ATTR_DYNAMICDHCP]))
iface->no_dynamic_dhcp = !blobmsg_get_bool(c);


if ((c = tb[IFACE_ATTR_ABSOLUTE_LIFETIME]))
iface->absolute_lifetime = blobmsg_get_bool(c);

if ((c = tb[IFACE_ATTR_LEASETIME])) {
double time = parse_leasetime(c);
double time = parse_leasetime(c, iface->absolute_lifetime);
if (time < 0)
goto err;

iface->dhcp_leasetime = time;
}

if ((c = tb[IFACE_ATTR_PREFERRED_LIFETIME])) {
double time = parse_leasetime(c);
double time = parse_leasetime(c, iface->absolute_lifetime);
if (time < 0)
goto err;

Expand Down
25 changes: 21 additions & 4 deletions src/dhcpv6-ia.c
Expand Up @@ -17,6 +17,7 @@
#include "dhcpv6.h"
#include "dhcpv4.h"

#include <stdint.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
Expand Down Expand Up @@ -919,13 +920,29 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
}
}

if (!INFINITE_VALID(a->valid_until))

if (!INFINITE_VALID(a->valid_until)) {
uint32_t tmp_valid;
if (iface->absolute_lifetime)
//TODO
tmp_valid = valid; // ToDO: check that
else
tmp_valid = valid + now;

/* UINT32_MAX is considered as infinite leasetime */
a->valid_until = (valid == UINT32_MAX) ? 0 : valid + now;
a->valid_until = (valid == UINT32_MAX) ? 0 : tmp_valid;
}
if (!INFINITE_VALID(a->preferred_until)) {
uint32_t tmp_pref;
if (iface->absolute_lifetime)
//TODO
tmp_pref = pref; // ToDO: check that
else
tmp_pref = pref + now;

if (!INFINITE_VALID(a->preferred_until))
/* UINT32_MAX is considered as infinite leasetime */
a->preferred_until = (pref == UINT32_MAX) ? 0 : pref + now;
a->preferred_until = (pref == UINT32_MAX) ? 0 : tmp_pref;
}

o_ia.t1 = htonl((pref == UINT32_MAX) ? pref : pref * 5 / 10);
o_ia.t2 = htonl((pref == UINT32_MAX) ? pref : pref * 8 / 10);
Expand Down
1 change: 1 addition & 0 deletions src/odhcpd.h
Expand Up @@ -288,6 +288,7 @@ struct interface {
uint32_t ra_hoplimit;
int ra_mtu;
uint32_t preferred_lifetime;
bool absolute_lifetime;

// DHCP
uint32_t dhcp_leasetime;
Expand Down

0 comments on commit 306080c

Please sign in to comment.