diff mbox series

[LEDE-DEV,odhcpd,v2] dhcpv4: notify DHCP ACK and RELEASE via ubus

Message ID 20171128140827.26922-1-borja.salazar@fon.com
State Accepted
Headers show
Series [LEDE-DEV,odhcpd,v2] dhcpv4: notify DHCP ACK and RELEASE via ubus | expand

Commit Message

Borja Salazar Nov. 28, 2017, 2:08 p.m. UTC
if the ubus object has any subscription
notify DHCP ACK and DHCP RELEASE events
using ubus notifications.

Signed-off-by: Borja Salazar <borja.salazar@fon.com>
---
 src/dhcpv4.c | 16 ++++++++++++++++
 src/odhcpd.h |  2 ++
 src/ubus.c   | 24 ++++++++++++++++++++++++
 3 files changed, 42 insertions(+)

Comments

Hans Dedecker Dec. 6, 2017, 12:21 p.m. UTC | #1
On Tue, Nov 28, 2017 at 3:08 PM, Borja Salazar <borja.salazar@fon.com> wrote:
> if the ubus object has any subscription
> notify DHCP ACK and DHCP RELEASE events
> using ubus notifications.
>
> Signed-off-by: Borja Salazar <borja.salazar@fon.com>
> ---
>  src/dhcpv4.c | 16 ++++++++++++++++
>  src/odhcpd.h |  2 ++
>  src/ubus.c   | 24 ++++++++++++++++++++++++
>  3 files changed, 42 insertions(+)
>
> diff --git a/src/dhcpv4.c b/src/dhcpv4.c
> index 166582e..9fd6a8d 100644
> --- a/src/dhcpv4.c
> +++ b/src/dhcpv4.c
> @@ -723,6 +723,14 @@ 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 (ubus_has_subscribers() && reqmsg == DHCPV4_MSG_RELEASE) {
> +               char req_chaddr_str[13];
> +               memset(&req_chaddr_str, 0, sizeof(req_chaddr_str));
> +               odhcpd_hexlify(req_chaddr_str, req->chaddr, ETH_ALEN);
> +               ubus_event_bcast("dhcp.release", req_chaddr_str, inet_ntoa(req->ciaddr), hostname, iface->ifname);
> +       }
> +#endif
>         if (reqmsg == DHCPV4_MSG_DECLINE || reqmsg == DHCPV4_MSG_RELEASE)
>                 return;
>
> @@ -870,6 +878,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 (ubus_has_subscribers() && msg == DHCPV4_MSG_ACK) {
> +               char reply_chaddr_str[13];
> +               memset(&reply_chaddr_str, 0, sizeof(reply_chaddr_str));
> +               odhcpd_hexlify(reply_chaddr_str, reply.chaddr, ETH_ALEN);
> +               ubus_event_bcast("dhcp.ack", reply_chaddr_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..7ee4f44 100644
> --- a/src/odhcpd.h
> +++ b/src/odhcpd.h
> @@ -296,6 +296,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_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface);
> +bool ubus_has_subscribers(void);
>  #endif
>
>  int netlink_add_netevent_handler(struct netevent_handler *hdlr);
> diff --git a/src/ubus.c b/src/ubus.c
> index 95eeff7..741daed 100644
> --- a/src/ubus.c
> +++ b/src/ubus.c
> @@ -311,6 +311,30 @@ static const struct blobmsg_policy obj_attrs[OBJ_ATTR_MAX] = {
>         [OBJ_ATTR_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
>  };
>
> +bool ubus_has_subscribers(void)
> +{
> +       return main_object.has_subscribers;
> +}
> +
> +void ubus_event_bcast(const char *type, const char *mac, const char *ip, 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", 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
Patch re-worked and applied as
https://git.lede-project.org/?p=project/odhcpd.git;a=commit;h=c516801e3dbb992ce1ea5274439b9b30888d9fb1

Hans
>
> _______________________________________________
> Lede-dev mailing list
> Lede-dev@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/lede-dev
diff mbox series

Patch

diff --git a/src/dhcpv4.c b/src/dhcpv4.c
index 166582e..9fd6a8d 100644
--- a/src/dhcpv4.c
+++ b/src/dhcpv4.c
@@ -723,6 +723,14 @@  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 (ubus_has_subscribers() && reqmsg == DHCPV4_MSG_RELEASE) {
+		char req_chaddr_str[13];
+		memset(&req_chaddr_str, 0, sizeof(req_chaddr_str));
+		odhcpd_hexlify(req_chaddr_str, req->chaddr, ETH_ALEN);
+		ubus_event_bcast("dhcp.release", req_chaddr_str, inet_ntoa(req->ciaddr), hostname, iface->ifname);
+	}
+#endif
 	if (reqmsg == DHCPV4_MSG_DECLINE || reqmsg == DHCPV4_MSG_RELEASE)
 		return;
 
@@ -870,6 +878,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 (ubus_has_subscribers() && msg == DHCPV4_MSG_ACK) {
+		char reply_chaddr_str[13];
+		memset(&reply_chaddr_str, 0, sizeof(reply_chaddr_str));
+		odhcpd_hexlify(reply_chaddr_str, reply.chaddr, ETH_ALEN);
+		ubus_event_bcast("dhcp.ack", reply_chaddr_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..7ee4f44 100644
--- a/src/odhcpd.h
+++ b/src/odhcpd.h
@@ -296,6 +296,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_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface);
+bool ubus_has_subscribers(void);
 #endif
 
 int netlink_add_netevent_handler(struct netevent_handler *hdlr);
diff --git a/src/ubus.c b/src/ubus.c
index 95eeff7..741daed 100644
--- a/src/ubus.c
+++ b/src/ubus.c
@@ -311,6 +311,30 @@  static const struct blobmsg_policy obj_attrs[OBJ_ATTR_MAX] = {
 	[OBJ_ATTR_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
 };
 
+bool ubus_has_subscribers(void)
+{
+	return main_object.has_subscribers;
+}
+
+void ubus_event_bcast(const char *type, const char *mac, const char *ip, 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", 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)