From: Yu Watanabe Date: Thu, 14 Mar 2024 10:35:46 +0000 (+0900) Subject: sd-dhcp-server: also save the server address and netmask to the leases file X-Git-Tag: v256-rc1~508^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F31791%2Fhead;p=thirdparty%2Fsystemd.git sd-dhcp-server: also save the server address and netmask to the leases file And introduce a tiny helper to retrieve these information. The function is not used at this time, but will be used later. --- diff --git a/src/libsystemd-network/dhcp-server-lease-internal.h b/src/libsystemd-network/dhcp-server-lease-internal.h index 518c1510941..7626552811d 100644 --- a/src/libsystemd-network/dhcp-server-lease-internal.h +++ b/src/libsystemd-network/dhcp-server-lease-internal.h @@ -38,3 +38,8 @@ int dhcp_server_static_leases_append_json(sd_dhcp_server *server, JsonVariant ** int dhcp_server_save_leases(sd_dhcp_server *server); int dhcp_server_load_leases(sd_dhcp_server *server); +int dhcp_server_leases_file_get_server_address( + int dir_fd, + const char *path, + struct in_addr *ret_address, + uint8_t *ret_prefixlen); diff --git a/src/libsystemd-network/sd-dhcp-server-lease.c b/src/libsystemd-network/sd-dhcp-server-lease.c index b518b474091..2f84d51e651 100644 --- a/src/libsystemd-network/sd-dhcp-server-lease.c +++ b/src/libsystemd-network/sd-dhcp-server-lease.c @@ -307,7 +307,11 @@ int dhcp_server_save_leases(sd_dhcp_server *server) { if (r < 0) return r; - r = json_build(&v, JSON_BUILD_OBJECT(JSON_BUILD_PAIR_ID128("BootID", boot_id))); + r = json_build(&v, JSON_BUILD_OBJECT( + JSON_BUILD_PAIR_ID128("BootID", boot_id), + JSON_BUILD_PAIR_IN4_ADDR("Address", &(struct in_addr) { .s_addr = server->address }), + JSON_BUILD_PAIR_UNSIGNED("PrefixLength", + in4_addr_netmask_to_prefixlen(&(struct in_addr) { .s_addr = server->netmask })))); if (r < 0) return r; @@ -408,6 +412,8 @@ static int json_dispatch_dhcp_lease(sd_dhcp_server *server, JsonVariant *v, bool typedef struct SavedInfo { sd_id128_t boot_id; + struct in_addr address; + uint8_t prefixlen; JsonVariant *leases; } SavedInfo; @@ -439,6 +445,8 @@ static int load_leases_file(int dir_fd, const char *path, SavedInfo *ret) { static const JsonDispatch dispatch_lease_file_table[] = { { "BootID", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(SavedInfo, boot_id), JSON_MANDATORY }, + { "Address", JSON_VARIANT_ARRAY, json_dispatch_in_addr, offsetof(SavedInfo, address), JSON_MANDATORY }, + { "PrefixLength", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint8, offsetof(SavedInfo, prefixlen), JSON_MANDATORY }, { "Leases", JSON_VARIANT_ARRAY, json_dispatch_variant, offsetof(SavedInfo, leases), JSON_MANDATORY }, {} }; @@ -480,3 +488,26 @@ int dhcp_server_load_leases(sd_dhcp_server *server) { return r; } + +int dhcp_server_leases_file_get_server_address( + int dir_fd, + const char *path, + struct in_addr *ret_address, + uint8_t *ret_prefixlen) { + + _cleanup_(saved_info_done) SavedInfo info = {}; + int r; + + if (!ret_address && !ret_prefixlen) + return 0; + + r = load_leases_file(dir_fd, path, &info); + if (r < 0) + return r; + + if (ret_address) + *ret_address = info.address; + if (ret_prefixlen) + *ret_prefixlen = info.prefixlen; + return 0; +}