From: David Härdeman Date: Sat, 8 Nov 2025 17:09:45 +0000 (+0100) Subject: statefiles: simplify statefiles_write_state6() X-Git-Url: http://git.openwrt.org/?a=commitdiff_plain;h=f6f2ef2de0459ef39baed68aabe2f3266a1a958d;p=project%2Fodhcpd.git statefiles: simplify statefiles_write_state6() 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 Link: https://github.com/openwrt/odhcpd/pull/302 Signed-off-by: Álvaro Fernández Rojas --- diff --git a/src/statefiles.c b/src/statefiles.c index 2c41032..40c0c29 100644 --- a/src/statefiles.c +++ b/src/statefiles.c @@ -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); - - /* # [ ...] */ - 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); + + /* # [ ...] */ + 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)