summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorHans Dedecker <dedeckeh@gmail.com>2017-11-28 15:08:27 +0100
committerHans Dedecker <dedeckeh@gmail.com>2017-12-06 10:30:33 +0100
commitc516801e3dbb992ce1ea5274439b9b30888d9fb1 (patch)
tree057186b8a30ff591ed296e015cf5f8464a9cc06f
parent92e205df59af022e0cfe6e0243b3ef572fc38f5c (diff)
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 <borja.salazar@fon.com> Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
-rw-r--r--src/dhcpv4.c11
-rw-r--r--src/odhcpd.c10
-rw-r--r--src/odhcpd.h3
-rw-r--r--src/ubus.c19
4 files changed, 43 insertions, 0 deletions
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)