From 940ecbb3916162cf734ffe95e87363199a0c119b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20H=C3=A4rdeman?= Date: Sat, 8 Nov 2025 17:48:00 +0100 Subject: [PATCH] statefiles: write straight to file in statefiles_write_state4() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Instead of writing to a buffer, then to a file, just write straight to file. This also makes the order of the steps in the function more logical (i.e. matches the order in which things are actually written to file). Signed-off-by: David Härdeman Link: https://github.com/openwrt/odhcpd/pull/302 Signed-off-by: Álvaro Fernández Rojas --- src/statefiles.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/statefiles.c b/src/statefiles.c index c392e34..2c41032 100644 --- a/src/statefiles.c +++ b/src/statefiles.c @@ -213,26 +213,25 @@ static void statefiles_write_state4(struct write_ctxt *ctxt, struct dhcpv4_lease struct in_addr addr = { .s_addr = lease->addr }; char ipbuf[INET6_ADDRSTRLEN]; - odhcpd_hexlify(hexhwaddr, lease->hwaddr, sizeof(lease->hwaddr)); inet_ntop(AF_INET, &addr, ipbuf, sizeof(ipbuf)); - /* # "ipv4" "32" "/32" */ - ctxt->buf_idx = snprintf(ctxt->buf, ctxt->buf_len, - "# %s %s ipv4 %s%s %" PRId64 " %x 32 %s/32\n", - ctxt->iface->ifname, hexhwaddr, - (lease->flags & OAF_BROKEN_HOSTNAME) ? "broken\\x20" : "", - (lease->hostname ? lease->hostname : "-"), - (lease->valid_until > ctxt->now ? - (int64_t)(lease->valid_until - ctxt->now + ctxt->wall_time) : - (INFINITE_VALID(lease->valid_until) ? -1 : 0)), - ntohl(lease->addr), ipbuf); - if (statefiles_write_host4(ctxt, lease)) { md5_hash(ipbuf, strlen(ipbuf), &ctxt->md5); md5_hash(lease->hostname, strlen(lease->hostname), &ctxt->md5); } - fwrite(ctxt->buf, 1, ctxt->buf_idx, ctxt->fp); + odhcpd_hexlify(hexhwaddr, lease->hwaddr, sizeof(lease->hwaddr)); + + /* # "ipv4" "32" "/32" */ + fprintf(ctxt->fp, + "# %s %s ipv4 %s%s %" PRId64 " %x 32 %s/32\n", + ctxt->iface->ifname, hexhwaddr, + (lease->flags & OAF_BROKEN_HOSTNAME) ? "broken\\x20" : "", + (lease->hostname ? lease->hostname : "-"), + (lease->valid_until > ctxt->now ? + (int64_t)(lease->valid_until - ctxt->now + ctxt->wall_time) : + (INFINITE_VALID(lease->valid_until) ? -1 : 0)), + ntohl(lease->addr), ipbuf); } static bool statefiles_write_state(time_t now) -- 2.30.2