statefiles: simplify statefiles_write_state6()
authorDavid Härdeman <[email protected]>
Sat, 8 Nov 2025 17:09:45 +0000 (18:09 +0100)
committerÁlvaro Fernández Rojas <[email protected]>
Tue, 11 Nov 2025 07:31:08 +0000 (08:31 +0100)
By collecting the addresses first and writing the hosts entries, we can
write the state line straight to the file, without having to use a
buffer as an intermediary. This also makes the ordering in the function
more logical, as it reflects the order things get written to file.

Signed-off-by: David Härdeman <[email protected]>
Link: https://github.com/openwrt/odhcpd/pull/302
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
src/statefiles.c

index 2c410320cce93e1d211bbdb2a08b3d86437f630a..40c0c29c8fa8ad0a1e8cfffc0715474e4e553eb2 100644 (file)
@@ -163,48 +163,41 @@ static void statefiles_write_state6_addr(struct dhcpv6_lease *lease, struct in6_
 
        inet_ntop(AF_INET6, addr, ipbuf, sizeof(ipbuf));
 
-       ctxt->buf_idx += snprintf(ctxt->buf + ctxt->buf_idx,
-                                 ctxt->buf_len - ctxt->buf_idx,
-                                 "%s/%d ", ipbuf, prefix);
-
        if (statefiles_write_host6(ctxt, lease, addr)) {
                md5_hash(ipbuf, strlen(ipbuf), &ctxt->md5);
                md5_hash(lease->hostname, strlen(lease->hostname), &ctxt->md5);
        }
+
+       ctxt->buf_idx += snprintf(ctxt->buf + ctxt->buf_idx,
+                                 ctxt->buf_len - ctxt->buf_idx,
+                                 " %s/%d", ipbuf, prefix);
 }
 
 static void statefiles_write_state6(struct write_ctxt *ctxt, struct dhcpv6_lease *lease)
 {
        char duidbuf[DUID_HEXSTRLEN];
 
-       odhcpd_hexlify(duidbuf, lease->clid_data, lease->clid_len);
-
-       /* # <iface> <hexduid> <hexiaid> <hostname> <valid_until> <assigned_[host|subnet]_id> <pfx_length> [<addrs> ...] */
-       ctxt->buf_idx = snprintf(ctxt->buf, ctxt->buf_len,
-                                "# %s %s %x %s%s %" PRId64 " ",
-                                ctxt->iface->ifname, duidbuf, ntohl(lease->iaid),
-                                (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)));
-
-       if (lease->flags & OAF_DHCPV6_NA)
-               ctxt->buf_idx += snprintf(ctxt->buf + ctxt->buf_idx,
-                                         ctxt->buf_len - ctxt->buf_idx,
-                                         "%" PRIx64 " %" PRIu8 " ",
-                                         lease->assigned_host_id, lease->length);
-       else
-               ctxt->buf_idx += snprintf(ctxt->buf + ctxt->buf_idx,
-                                         ctxt->buf_len - ctxt->buf_idx,
-                                         "%" PRIx32 " %" PRIu8 " ",
-                                         lease->assigned_subnet_id, lease->length);
+       ctx->buf_idx = 0;
 
        if (INFINITE_VALID(lease->valid_until) || lease->valid_until > ctxt->now)
                odhcpd_enum_addr6(ctxt->iface, lease, ctxt->now, statefiles_write_state6_addr, ctxt);
 
-       ctxt->buf[ctxt->buf_idx - 1] = '\n';
-       fwrite(ctxt->buf, 1, ctxt->buf_idx, ctxt->fp);
+       odhcpd_hexlify(duidbuf, lease->clid_data, lease->clid_len);
+
+       /* # <iface> <hexduid> <hexiaid> <hostname> <valid_until> <assigned_[host|subnet]_id> <pfx_length> [<addrs> ...] */
+       fprintf(ctxt->fp,
+               "# %s %s %x %s%s %" PRId64 " %" PRIx64 " %" PRIu8 "%s\n",
+               ctxt->iface->ifname, duidbuf, ntohl(lease->iaid),
+               (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)),
+               (lease->flags & OAF_DHCPV6_NA ?
+                lease->assigned_host_id :
+                (uint64_t)lease->assigned_subnet_id),
+               lease->length,
+               ctx->buf);
 }
 
 static void statefiles_write_state4(struct write_ctxt *ctxt, struct dhcpv4_lease *lease)