From 8f27697b9b82420890cedd429622052c8b67cea1 Mon Sep 17 00:00:00 2001 From: Santiago Piccinini Date: Wed, 2 Dec 2020 19:07:05 -0300 Subject: ubus: add add_lease method Allows sharing leases between odhcpd instances running in multiple hosts. Signed-off-by: Santiago Piccinini Signed-off-by: Hans Dedecker --- src/config.c | 29 +++++++++++++---------------- src/odhcpd.h | 11 +++++++++++ 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); diff --git a/src/ubus.c b/src/ubus.c index 72d47c4..ebc3103 100644 --- a/src/ubus.c +++ b/src/ubus.c @@ -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 = -- cgit v1.2.3