[From nobody Thu Jun 25 05:54:43 2020
Received: from mail-lj1-x231.google.com ([2a00:1450:4864:20::231])
 by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux))
 id 1fXuf4-0006ob-U2
 for openwrt-devel@lists.openwrt.org; Tue, 26 Jun 2018 20:34:04 +0000
Received: by mail-lj1-x231.google.com with SMTP id i125-v6so9813179lji.2
 for &lt;openwrt-devel@lists.openwrt.org&gt;; Tue, 26 Jun 2018 13:33:51 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kempniu.pl; s=google;
 h=sender:from:to:subject:date:message-id:mime-version
 :content-transfer-encoding;
 bh=yqda31em1Smky1v3YYOG4EXIjLn/bromU3OWfEzAer8=;
 b=rIle/MMz1mvp98S0UmW/91gFz66vgmkikCpyrBXppyLsnPYHD1fHgU9wzpWpu+05fk
 a404qWGGfjgCZH5fUq5sjPqZPbNNdBEjq3Vue5oFC1G4aIM+AaXAblSZCBT3owU5ROxN
 1IxO7dRiuDar7oIvASKHX+lQm9SzMC9NwLAeg=
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20161025;
 h=x-gm-message-state:sender:from:to:subject:date:message-id
 :mime-version:content-transfer-encoding;
 bh=yqda31em1Smky1v3YYOG4EXIjLn/bromU3OWfEzAer8=;
 b=kR7nNrOLjNhb7DJ55gW9IuU4KVIFtOa8sdQPc7Di9xLBvL9kfNlxQ8HzBhw885+rvg
 38Pxi6LpVMNOjk7vsHI8dwoGHW07Eo547wSA+7qgRS8ecpv+djaP5cNmvxGx+/Und5pd
 O8krcIiOIcU7xOm54h7hg8S1tnfwDGydoNiz5CF0O1feu01EMsBSWlsO2Tn5qccTLNr+
 bvTDI0T7TYAAPL+PXdjwKXSV+ltReQiEWEECEkpXqzfpJphAIxlM4Ob0Ntuqo5lrTNjY
 m2H8bArc2inlLc8MhSCbP/AzLZtp3LNrmtJRNJ4v/LKeRTU3p2fhowX5I2aqFptW2tSY
 lqcg==
X-Gm-Message-State: APt69E0w9JkRF98vtsT6H3dIYrVmtOK6dZWQ/pZhje/PtdK0fBNP58nJ
 LOouNQV4NIwHs51ErOn7RHP4nTNSpaM=
X-Google-Smtp-Source: AAOMgpdt1I0jIrW1IjhwpvyP1YrP4sc5TquoRm5NGMZ33TTZnaG8CR48gYaZWTT2/YR+0DVfCExPPA==
X-Received: by 2002:a2e:8654:: with SMTP id
 i20-v6mr2088059ljj.43.1530045229222; 
 Tue, 26 Jun 2018 13:33:49 -0700 (PDT)
Received: from larwa.hq.kempniu.pl ([2001:470:64df:111::ae4])
 by smtp.gmail.com with ESMTPSA id j7-v6sm387670ljg.48.2018.06.26.13.33.48
 for &lt;openwrt-devel@lists.openwrt.org&gt;
 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
 Tue, 26 Jun 2018 13:33:48 -0700 (PDT)
Sender: =?UTF-8?B?TWljaGHFgiBLxJlwaWXFhA==?= &lt;kempniu@kempniu.pl&gt;
From: =?UTF-8?q?Micha=C5=82=20K=C4=99pie=C5=84?= &lt;lede@kempniu.pl&gt;
To: openwrt-devel@lists.openwrt.org
Subject: [PATCH] odhcpd: allow specifying lease trigger mode
Date: Tue, 26 Jun 2018 22:33:40 +0200
Message-Id: &lt;20180626203340.8502-1-lede@kempniu.pl&gt;
X-Mailer: git-send-email 2.18.0
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 
X-CRM114-CacheID: sfid-20180626_133403_073495_32445AD9 
X-CRM114-Status: GOOD (  14.07  )
X-Spam-Score: -0.1 (/)
X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary:
 Content analysis details:   (-0.1 points)
 pts rule name              description
 ---- ---------------------- --------------------------------------------------
 -0.0 RCVD_IN_DNSWL_NONE     RBL: Sender listed at http://www.dnswl.org/, no
 trust [2a00:1450:4864:20:0:0:0:231 listed in] [list.dnswl.org]
 -0.0 SPF_PASS               SPF: sender matches SPF record
 0.1 DKIM_SIGNED            Message has a DKIM or DK signature,
 not necessarily valid
 -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's
 domain
 -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature

odhcpd only calls the lease trigger script when a lease with an
associated hostname is changed.  Enabling the lease trigger script to
also be called when a lease without an associated hostname is changed
allows for greater flexibility.

Add a new global odhcpd configuration parameter, &quot;leasetrigger_mode&quot;,
with a default value of 0 which retains current behavior.  Setting the
parameter to 1 causes the lease trigger script to instead be called
every time any lease is changed, no matter whether it is associated with
a hostname or not.

Signed-off-by: Michał Kępień &lt;lede@kempniu.pl&gt;
---
The configuration parameter added by this patch could obviously be a
boolean, but it occurred to me that if the flexibility of the lease
trigger script is further improved in the future [1], more lease trigger
modes may be added (e.g. upon every single DHCP event, even if one does
not cause any lease to be modified).

[1] see e.g. https://bugs.openwrt.org/index.php?do=details&amp;task_id=401

 README          | 18 ++++++++++--------
 src/config.c    | 10 ++++++++--
 src/dhcpv6-ia.c |  6 ++++++
 src/odhcpd.h    |  1 +
 4 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/README b/README
index 0c562e6..a43f2e7 100644
--- a/README
+++ b/README
@@ -57,14 +57,16 @@ and may also receive information from ubus
 
 Section of type odhcpd
 
-Option		Type	Default			Description
-legacy		bool	0			Enable DHCPv4 if start but
-						no dhcpv4 option set
-maindhcp	bool	0			Use odhcpd as the main DHCPv4
-						service
-leasefile	string				DHCP/v6 lease/hostfile
-leasetrigger	string				Lease trigger script
-loglevel	integer 6			Syslog level priority (0-7)
+Option			Type	Default			Description
+legacy			bool	0			Enable DHCPv4 if start but
+							no dhcpv4 option set
+maindhcp		bool	0			Use odhcpd as the main DHCPv4
+							service
+leasefile		string				DHCP/v6 lease/hostfile
+leasetrigger		string				Lease trigger script
+leasetrigger_mode	integer	0			Lease trigger mode
+			0: lease with hostname changed, 1: any lease changed
+loglevel		integer 6			Syslog level priority (0-7)
 
 
 Sections of type dhcp (configure DHCP / DHCPv6 / RA / NDP service)
diff --git a/src/config.c b/src/config.c
index 118349a..78488b7 100644
--- a/src/config.c
+++ b/src/config.c
@@ -20,8 +20,8 @@ static int reload_pipe[2];
 struct list_head leases = LIST_HEAD_INIT(leases);
 struct list_head interfaces = LIST_HEAD_INIT(interfaces);
 struct config config = {.legacy = false, .main_dhcpv4 = false,
-			.dhcp_cb = NULL, .dhcp_statefile = NULL,
-			.log_level = LOG_INFO};
+			.dhcp_cb = NULL, .dhcp_cb_mode = 0,
+			.dhcp_statefile = NULL, .log_level = LOG_INFO};
 
 enum {
 	IFACE_ATTR_INTERFACE,
@@ -149,6 +149,7 @@ enum {
 	ODHCPD_ATTR_MAINDHCP,
 	ODHCPD_ATTR_LEASEFILE,
 	ODHCPD_ATTR_LEASETRIGGER,
+	ODHCPD_ATTR_LEASETRIGGER_MODE,
 	ODHCPD_ATTR_LOGLEVEL,
 	ODHCPD_ATTR_MAX
 };
@@ -158,6 +159,7 @@ static const struct blobmsg_policy odhcpd_attrs[LEASE_ATTR_MAX] = {
 	[ODHCPD_ATTR_MAINDHCP] = { .name = &quot;maindhcp&quot;, .type = BLOBMSG_TYPE_BOOL },
 	[ODHCPD_ATTR_LEASEFILE] = { .name = &quot;leasefile&quot;, .type = BLOBMSG_TYPE_STRING },
 	[ODHCPD_ATTR_LEASETRIGGER] = { .name = &quot;leasetrigger&quot;, .type = BLOBMSG_TYPE_STRING },
+	[ODHCPD_ATTR_LEASETRIGGER_MODE] = { .name = &quot;leasetrigger_mode&quot;, .type = BLOBMSG_TYPE_INT32 },
 	[ODHCPD_ATTR_LOGLEVEL] = { .name = &quot;loglevel&quot;, .type = BLOBMSG_TYPE_INT32 },
 };
 
@@ -290,6 +292,10 @@ static void set_config(struct uci_section *s)
 		config.dhcp_cb = strdup(blobmsg_get_string(c));
 	}
 
+	if ((c = tb[ODHCPD_ATTR_LEASETRIGGER_MODE])) {
+		config.dhcp_cb_mode = blobmsg_get_u32(c);
+	}
+
 	if ((c = tb[ODHCPD_ATTR_LOGLEVEL])) {
 		int log_level = (blobmsg_get_u32(c) &amp; LOG_PRIMASK);
 
diff --git a/src/dhcpv6-ia.c b/src/dhcpv6-ia.c
index 4ee6dd2..e51be79 100644
--- a/src/dhcpv6-ia.c
+++ b/src/dhcpv6-ia.c
@@ -408,6 +408,9 @@ void dhcpv6_write_statefile(void)
 
 					ctxt.buf[ctxt.buf_idx - 1] = '\n';
 					fwrite(ctxt.buf, 1, ctxt.buf_idx, ctxt.fp);
+					if (config.dhcp_cb_mode &gt; 0) {
+						md5_hash(ctxt.buf, ctxt.buf_idx, &amp;ctxt.md5);
+					}
 				}
 			}
 
@@ -453,6 +456,9 @@ void dhcpv6_write_statefile(void)
 									&quot;%s/32 &quot;, ipbuf);
 					ctxt.buf[ctxt.buf_idx - 1] = '\n';
 					fwrite(ctxt.buf, 1, ctxt.buf_idx, ctxt.fp);
+					if (config.dhcp_cb_mode &gt; 0) {
+						md5_hash(ctxt.buf, ctxt.buf_idx, &amp;ctxt.md5);
+					}
 				}
 			}
 		}
diff --git a/src/odhcpd.h b/src/odhcpd.h
index 91fdcbf..0f23667 100644
--- a/src/odhcpd.h
+++ b/src/odhcpd.h
@@ -138,6 +138,7 @@ struct config {
 	bool legacy;
 	bool main_dhcpv4;
 	char *dhcp_cb;
+	int dhcp_cb_mode;
 	char *dhcp_statefile;
 	int log_level;
 } config;
-- 
2.18.0


]