[LEDE-DEV] [PATCH odhcpd] dhcpv4: notify DHCP ack messages via ubus

Borja Salazar borja.salazar at fon.com
Fri Nov 24 02:56:33 PST 2017


Signed-off-by: Borja Salazar <borja.salazar at fon.com>
---
 src/dhcpv4.c |  8 ++++++++
 src/odhcpd.h |  1 +
 src/ubus.c   | 19 +++++++++++++++++++
 3 files changed, 28 insertions(+)

diff --git a/src/dhcpv4.c b/src/dhcpv4.c
index 166582e..ad8d338 100644
--- a/src/dhcpv4.c
+++ b/src/dhcpv4.c
@@ -870,6 +870,14 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
 				req->chaddr[3],req->chaddr[4],req->chaddr[5],
 				inet_ntoa(dest.sin_addr));
 
+#ifdef WITH_UBUS
+	if (msg == DHCPV4_MSG_ACK) {
+		char mac_str[13];
+		memset(&mac_str, 0, sizeof(mac_str));
+		odhcpd_hexlify(mac_str, reply.chaddr, ETH_ALEN);
+		ubus_event_bcast("dhcp.ack", mac_str, inet_ntoa(reply.yiaddr), hostname, iface->ifname);
+	}
+#endif
 	sendto(sock, &reply, sizeof(reply), MSG_DONTWAIT,
 			(struct sockaddr*)&dest, sizeof(dest));
 }
diff --git a/src/odhcpd.h b/src/odhcpd.h
index 45badd3..73e5a49 100644
--- a/src/odhcpd.h
+++ b/src/odhcpd.h
@@ -296,6 +296,7 @@ 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_event_bcast(const char *type, const char *mac, const char *ip, 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..e988b8e 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_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface)
+{
+
+	if (!ubus)
+		return;
+
+	blob_buf_init(&b, 0);
+	if (mac)
+		blobmsg_add_string(&b, "mac", mac);
+	if (ip)
+		blobmsg_add_string(&b, "ip", ip);
+	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)
-- 
2.11.0




More information about the Lede-dev mailing list