From: Hans Dedecker Date: Tue, 28 Nov 2017 14:08:27 +0000 (+0100) Subject: dhcpv4: notify DHCP ACK and RELEASE via ubus X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=c516801e3dbb992ce1ea5274439b9b30888d9fb1;p=project%2Fodhcpd.git dhcpv4: notify DHCP ACK and RELEASE via ubus If the ubus object has any subscription notify DHCP ACK and DHCP RELEASE events using ubus notifications. Signed-off-by: Borja Salazar Signed-off-by: Hans Dedecker --- diff --git a/src/dhcpv4.c b/src/dhcpv4.c index 166582e..c367507 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -723,6 +723,11 @@ static void handle_dhcpv4(void *addr, void *data, size_t len, req->chaddr[0],req->chaddr[1],req->chaddr[2], req->chaddr[3],req->chaddr[4],req->chaddr[5]); +#ifdef WITH_UBUS + if (reqmsg == DHCPV4_MSG_RELEASE) + ubus_bcast_dhcp_event("dhcp.release", req->chaddr, req->hlen, + &req->ciaddr, hostname, iface->ifname); +#endif if (reqmsg == DHCPV4_MSG_DECLINE || reqmsg == DHCPV4_MSG_RELEASE) return; @@ -872,6 +877,12 @@ static void handle_dhcpv4(void *addr, void *data, size_t len, sendto(sock, &reply, sizeof(reply), MSG_DONTWAIT, (struct sockaddr*)&dest, sizeof(dest)); + +#ifdef WITH_UBUS + if (msg == DHCPV4_MSG_ACK) + ubus_bcast_dhcp_event("dhcp.ack", req->chaddr, req->hlen, &reply.yiaddr, + hostname, iface->ifname); +#endif } static bool dhcpv4_assign(struct interface *iface, diff --git a/src/odhcpd.c b/src/odhcpd.c index 13697b7..97a6de9 100644 --- a/src/odhcpd.c +++ b/src/odhcpd.c @@ -466,6 +466,16 @@ void odhcpd_hexlify(char *dst, const uint8_t *src, size_t len) *dst = 0; } +const char *odhcpd_print_mac(const uint8_t *mac, const size_t len) +{ + static char buf[32]; + + snprintf(buf, sizeof(buf), "%02x", mac[0]); + for (size_t i = 1, j = 2; i < len && j < sizeof(buf); i++, j += 3) + snprintf(buf + j, sizeof(buf) - j, ":%02x", mac[i]); + + return buf; +} int odhcpd_bmemcmp(const void *av, const void *bv, size_t bits) { diff --git a/src/odhcpd.h b/src/odhcpd.h index 45badd3..fbfeb67 100644 --- a/src/odhcpd.h +++ b/src/odhcpd.h @@ -282,6 +282,7 @@ void odhcpd_run(void); time_t odhcpd_time(void); ssize_t odhcpd_unhexlify(uint8_t *dst, size_t len, const char *src); void odhcpd_hexlify(char *dst, const uint8_t *src, size_t len); +const char *odhcpd_print_mac(const uint8_t *mac, const size_t len); int odhcpd_bmemcmp(const void *av, const void *bv, size_t bits); void odhcpd_bmemcpy(void *av, const void *bv, size_t bits); @@ -296,6 +297,8 @@ int ubus_init(void); const char* ubus_get_ifname(const char *name); void ubus_apply_network(void); bool ubus_has_prefix(const char *name, const char *ifname); +void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac, const size_t mac_len, + const struct in_addr *addr, const char *name, const char *interface); #endif int netlink_add_netevent_handler(struct netevent_handler *hdlr); diff --git a/src/ubus.c b/src/ubus.c index 95eeff7..19b14be 100644 --- a/src/ubus.c +++ b/src/ubus.c @@ -311,6 +311,25 @@ static const struct blobmsg_policy obj_attrs[OBJ_ATTR_MAX] = { [OBJ_ATTR_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING }, }; +void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac, + const size_t mlen, const struct in_addr *addr, const char *name, + const char *interface) +{ + if (!ubus || !main_object.has_subscribers) + return; + + blob_buf_init(&b, 0); + if (mac) + blobmsg_add_string(&b, "mac", odhcpd_print_mac(mac, mlen)); + if (addr) + blobmsg_add_string(&b, "ip", inet_ntoa(*addr)); + if (name) + blobmsg_add_string(&b, "name", name); + if (interface) + blobmsg_add_string(&b, "interface", interface); + + ubus_notify(ubus, &main_object, type, b.head, -1); +} static void handle_event(_unused struct ubus_context *ctx, _unused struct ubus_event_handler *ev, _unused const char *type, struct blob_attr *msg)