diff options
author | Santiago Piccinini <spiccinini@altermundi.net> | 2020-12-02 19:07:05 -0300 |
---|---|---|
committer | Hans Dedecker <dedeckeh@gmail.com> | 2020-12-06 20:44:29 +0100 |
commit | 8f27697b9b82420890cedd429622052c8b67cea1 (patch) | |
tree | fb8ddcb7580e973de1604d8dc995b00977ba0d90 /src | |
parent | 694148a87fa5a0c964286fdaac1fa490a2fb530b (diff) |
ubus: add add_lease method
Allows sharing leases between odhcpd instances running
in multiple hosts.
Signed-off-by: Santiago Piccinini <spiccinini@altermundi.net>
Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/config.c | 29 | ||||
-rw-r--r-- | src/odhcpd.h | 11 | ||||
-rw-r--r-- | src/ubus.c | 10 |
3 files changed, 34 insertions, 16 deletions
diff --git a/src/config.c b/src/config.c index 8a6f573..95b745c 100644 --- a/src/config.c +++ b/src/config.c @@ -144,17 +144,7 @@ const struct uci_blob_param_list interface_attr_list = { .info = iface_attr_info, }; -enum { - LEASE_ATTR_IP, - LEASE_ATTR_MAC, - LEASE_ATTR_DUID, - LEASE_ATTR_HOSTID, - LEASE_ATTR_LEASETIME, - LEASE_ATTR_NAME, - LEASE_ATTR_MAX -}; - -static const struct blobmsg_policy lease_attrs[LEASE_ATTR_MAX] = { +const struct blobmsg_policy lease_attrs[LEASE_ATTR_MAX] = { [LEASE_ATTR_IP] = { .name = "ip", .type = BLOBMSG_TYPE_STRING }, [LEASE_ATTR_MAC] = { .name = "mac", .type = BLOBMSG_TYPE_STRING }, [LEASE_ATTR_DUID] = { .name = "duid", .type = BLOBMSG_TYPE_STRING }, @@ -387,16 +377,15 @@ static void free_lease(struct lease *l) free(l); } -static int set_lease(struct uci_section *s) + +int set_lease_from_blobmsg(struct blob_attr *ba) { struct blob_attr *tb[LEASE_ATTR_MAX], *c; struct lease *l; size_t duidlen = 0; uint8_t *duid; - blob_buf_init(&b, 0); - uci_to_blob(&b, s, &lease_attr_list); - blobmsg_parse(lease_attrs, LEASE_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head)); + blobmsg_parse(lease_attrs, LEASE_ATTR_MAX, tb, blob_data(ba), blob_len(ba)); if ((c = tb[LEASE_ATTR_DUID])) duidlen = (blobmsg_data_len(c) - 1) / 2; @@ -460,6 +449,14 @@ err: return -1; } +static int set_lease_from_uci(struct uci_section *s) +{ + blob_buf_init(&b, 0); + uci_to_blob(&b, s, &lease_attr_list); + + return set_lease_from_blobmsg(b.head); +} + int config_parse_interface(void *data, size_t len, const char *name, bool overwrite) { struct interface *iface; @@ -1072,7 +1069,7 @@ void odhcpd_reload(void) uci_foreach_element(&dhcp->sections, e) { struct uci_section* s = uci_to_section(e); if (!strcmp(s->type, "host")) - set_lease(s); + set_lease_from_uci(s); } } diff --git a/src/odhcpd.h b/src/odhcpd.h index 0108af1..2f7dd25 100644 --- a/src/odhcpd.h +++ b/src/odhcpd.h @@ -159,6 +159,15 @@ struct lease { char *hostname; }; +enum { + LEASE_ATTR_IP, + LEASE_ATTR_MAC, + LEASE_ATTR_DUID, + LEASE_ATTR_HOSTID, + LEASE_ATTR_LEASETIME, + LEASE_ATTR_NAME, + LEASE_ATTR_MAX +}; struct odhcpd_ref_ip; @@ -315,6 +324,7 @@ struct interface { }; extern struct avl_tree interfaces; +extern const struct blobmsg_policy lease_attrs[LEASE_ATTR_MAX]; inline static void free_assignment(struct dhcp_assignment *a) { @@ -375,6 +385,7 @@ struct lease *config_find_lease_by_duid(const uint8_t *duid, const uint16_t len) struct lease *config_find_lease_by_mac(const uint8_t *mac); struct lease *config_find_lease_by_hostid(const uint32_t hostid); struct lease *config_find_lease_by_ipaddr(const uint32_t ipaddr); +int set_lease_from_blobmsg(struct blob_attr *ba); #ifdef WITH_UBUS int ubus_init(void); @@ -174,10 +174,20 @@ static int handle_dhcpv6_leases(_unused struct ubus_context *ctx, _unused struct return 0; } +static int handle_add_lease(_unused struct ubus_context *ctx, _unused struct ubus_object *obj, + _unused struct ubus_request_data *req, _unused const char *method, + struct blob_attr *msg) +{ + if (!set_lease_from_blobmsg(msg)) + return UBUS_STATUS_OK; + + return UBUS_STATUS_INVALID_ARGUMENT; +} static struct ubus_method main_object_methods[] = { {.name = "ipv4leases", .handler = handle_dhcpv4_leases}, {.name = "ipv6leases", .handler = handle_dhcpv6_leases}, + UBUS_METHOD("add_lease", handle_add_lease, lease_attrs), }; static struct ubus_object_type main_object_type = |