[Dnsmasq-discuss] [PATCH] Add interface name, vendor class if present and lease expiration time to dbus signals

Victorien Molle victorien.molle at wifirst.fr
Wed Dec 4 12:24:48 GMT 2019


For our usage, we need to have more informations sent over D-Bus such as the interface
name, the vendor class identifier and the lease expiration time.

Note: in order to get leases extradata be populated, we enabled this feature if D-Bus
is enabled in configuration file (this was enabled in the past only if a script was
ran on leases updates).

Signed-off-by: Victorien Molle <victorien.molle at wifirst.fr>
---
 src/dbus.c    | 57 +++++++++++++++++++++++++++++++++++++++++++++------
 src/rfc2131.c |  2 +-
 2 files changed, 52 insertions(+), 7 deletions(-)

diff --git a/src/dbus.c b/src/dbus.c
index c0ce903..88746b2 100644
--- a/src/dbus.c
+++ b/src/dbus.c
@@ -59,16 +59,25 @@ const char* introspection_xml_template =
 "      <arg name=\"ipaddr\" type=\"s\"/>\n"
 "      <arg name=\"hwaddr\" type=\"s\"/>\n"
 "      <arg name=\"hostname\" type=\"s\"/>\n"
+"      <arg name=\"interface\" type=\"s\"/>\n"
+"      <arg name=\"vendor_class\" type=\"s\"/>\n"
+"      <arg name=\"lease_duration\" type=\"u\"/>\n"
 "    </signal>\n"
 "    <signal name=\"DhcpLeaseDeleted\">\n"
 "      <arg name=\"ipaddr\" type=\"s\"/>\n"
 "      <arg name=\"hwaddr\" type=\"s\"/>\n"
 "      <arg name=\"hostname\" type=\"s\"/>\n"
+"      <arg name=\"interface\" type=\"s\"/>\n"
+"      <arg name=\"vendor_class\" type=\"s\"/>\n"
+"      <arg name=\"lease_duration\" type=\"u\"/>\n"
 "    </signal>\n"
 "    <signal name=\"DhcpLeaseUpdated\">\n"
 "      <arg name=\"ipaddr\" type=\"s\"/>\n"
 "      <arg name=\"hwaddr\" type=\"s\"/>\n"
 "      <arg name=\"hostname\" type=\"s\"/>\n"
+"      <arg name=\"interface\" type=\"s\"/>\n"
+"      <arg name=\"vendor_class\" type=\"s\"/>\n"
+"      <arg name=\"lease_duration\" type=\"u\"/>\n"
 "    </signal>\n"
 #ifdef HAVE_DHCP
 "    <method name=\"AddDhcpLease\">\n"
@@ -830,11 +839,15 @@ void check_dbus_listeners()
 #ifdef HAVE_DHCP
 void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname)
 {
+  char *action_str, *mac = daemon->namebuff, *interface, *vendor_class;
   DBusConnection *connection = (DBusConnection *)daemon->dbus;
+  dbus_uint64_t expires = lease->expires;
+  unsigned char *buf = lease->extradata;
   DBusMessage* message = NULL;
   DBusMessageIter args;
-  char *action_str, *mac = daemon->namebuff;
+  int allocated = 0;
   unsigned char *p;
+  struct irec *it;
   int i;
 
   if (!connection)
@@ -842,7 +855,31 @@ void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname)
   
   if (!hostname)
     hostname = "";
-  
+
+  interface = "";
+  for (it = daemon->interfaces; it != NULL; it = it->next)
+    {
+      if (it->index == lease->last_interface)
+        {
+          interface = it->name;
+          break;
+        }
+    }
+
+  vendor_class = "";
+  /* As defined in rfc2131.c, the first data is the vendor class even if it is empty */
+  if (buf && *buf != 0)
+    {
+      vendor_class = (char*)whine_malloc(lease->extradata_len + 1);
+      if (vendor_class)
+      {
+        allocated = 1;
+        strncpy(vendor_class, (const char*)buf, (lease->extradata_len));
+      }
+      else
+        vendor_class = "";
+    }
+
 #ifdef HAVE_DHCP6
    if (lease->flags & (LEASE_TA | LEASE_NA))
      {
@@ -865,19 +902,27 @@ void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname)
   else if (action == ACTION_OLD)
     action_str = "DhcpLeaseUpdated";
   else
-    return;
+    goto out;
 
   if (!(message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, action_str)))
-    return;
-  
+      goto out;
+
   dbus_message_iter_init_append(message, &args);
   
   if (dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &daemon->addrbuff) &&
       dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &mac) &&
-      dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &hostname))
+      dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &hostname) &&
+      dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &interface) &&
+      dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &vendor_class) &&
+      dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT64, &expires)
+     )
     dbus_connection_send(connection, message, NULL);
   
   dbus_message_unref(message);
+
+out:
+  if (vendor_class && allocated)
+    free(vendor_class);
 }
 #endif
 
diff --git a/src/rfc2131.c b/src/rfc2131.c
index 0058747..0cea3c4 100644
--- a/src/rfc2131.c
+++ b/src/rfc2131.c
@@ -1366,7 +1366,7 @@ size_t dhcp_reply(struct dhcp_context *context, char *iface_name, int int_index,
 	      lease->flags |= LEASE_CHANGED;
 
 #ifdef HAVE_SCRIPT
-	      if (daemon->lease_change_command)
+	      if (daemon->lease_change_command || daemon->dbus)
 		{
 		  struct dhcp_netid *n;
 		  
-- 
2.24.0




More information about the Dnsmasq-discuss mailing list