diff --git a/src/libsystemd-network/dhcp-duid-internal.h b/src/libsystemd-network/dhcp-duid-internal.h index ae888f789aa37..f8bc15c47eead 100644 --- a/src/libsystemd-network/dhcp-duid-internal.h +++ b/src/libsystemd-network/dhcp-duid-internal.h @@ -74,6 +74,7 @@ static inline bool duid_data_size_is_valid(size_t size) { } const char *duid_type_to_string(DUIDType t) _const_; +int dhcp_duid_to_string_internal(uint16_t type, const void *data, size_t data_size, char **ret); int dhcp_identifier_set_iaid( sd_device *dev, diff --git a/src/libsystemd-network/sd-dhcp-duid.c b/src/libsystemd-network/sd-dhcp-duid.c index 303e332320e82..297deb8a5d60e 100644 --- a/src/libsystemd-network/sd-dhcp-duid.c +++ b/src/libsystemd-network/sd-dhcp-duid.c @@ -5,6 +5,7 @@ #include #include "dhcp-duid-internal.h" +#include "hexdecoct.h" #include "netif-util.h" #include "network-common.h" #include "siphash24.h" @@ -203,6 +204,46 @@ int sd_dhcp_duid_set_uuid(sd_dhcp_duid *duid) { return 0; } +int dhcp_duid_to_string_internal(uint16_t type, const void *data, size_t data_size, char **ret) { + _cleanup_free_ char *p = NULL, *x = NULL; + const char *t; + + assert(data); + assert(duid_data_size_is_valid(data_size)); + assert(ret); + + x = hexmem(data, data_size); + if (!x) + return -ENOMEM; + + t = duid_type_to_string(type); + if (!t) + return asprintf(ret, "%04x:%s", htobe16(type), x); + + p = strjoin(t, ":", x); + if (!p) + return -ENOMEM; + + *ret = TAKE_PTR(p); + return 0; +} + +int sd_dhcp_duid_to_string(const sd_dhcp_duid *duid, char **ret) { + uint16_t type; + const void *data; + size_t data_size; + int r; + + assert_return(sd_dhcp_duid_is_set(duid), -EINVAL); + assert_return(ret, -EINVAL); + + r = sd_dhcp_duid_get(duid, &type, &data, &data_size); + if (r < 0) + return r; + + return dhcp_duid_to_string_internal(type, data, data_size, ret); +} + int dhcp_identifier_set_iaid( sd_device *dev, const struct hw_addr_data *hw_addr, diff --git a/src/libsystemd-network/sd-dhcp6-client.c b/src/libsystemd-network/sd-dhcp6-client.c index 14f397a85ab3a..db4e0f9c5919f 100644 --- a/src/libsystemd-network/sd-dhcp6-client.c +++ b/src/libsystemd-network/sd-dhcp6-client.c @@ -18,7 +18,6 @@ #include "dns-domain.h" #include "event-util.h" #include "fd-util.h" -#include "hexdecoct.h" #include "hostname-util.h" #include "in-addr-util.h" #include "iovec-util.h" @@ -288,39 +287,14 @@ int sd_dhcp6_client_get_duid(sd_dhcp6_client *client, const sd_dhcp_duid **ret) return 0; } -int sd_dhcp6_client_duid_as_string( - sd_dhcp6_client *client, - char **duid) { - _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL; - const char *v; - int r; - +int sd_dhcp6_client_get_duid_as_string(sd_dhcp6_client *client, char **ret) { assert_return(client, -EINVAL); - assert_return(sd_dhcp_duid_is_set(&client->duid), -ENODATA); - assert_return(duid, -EINVAL); - - v = duid_type_to_string(be16toh(client->duid.duid.type)); - if (v) { - s = strdup(v); - if (!s) - return -ENOMEM; - } else { - r = asprintf(&s, "%0x", client->duid.duid.type); - if (r < 0) - return -ENOMEM; - } - - t = hexmem(client->duid.duid.data, client->duid.size - offsetof(struct duid, data)); - if (!t) - return -ENOMEM; - - p = strjoin(s, ":", t); - if (!p) - return -ENOMEM; + assert_return(ret, -EINVAL); - *duid = TAKE_PTR(p); + if (!sd_dhcp_duid_is_set(&client->duid)) + return -ENODATA; - return 0; + return sd_dhcp_duid_to_string(&client->duid, ret); } int sd_dhcp6_client_set_iaid(sd_dhcp6_client *client, uint32_t iaid) { diff --git a/src/network/networkd-dhcp6.c b/src/network/networkd-dhcp6.c index ee20d0ed50a6b..fee9d95c0d4f5 100644 --- a/src/network/networkd-dhcp6.c +++ b/src/network/networkd-dhcp6.c @@ -832,7 +832,7 @@ int link_serialize_dhcp6_client(Link *link, FILE *f) { if (r >= 0) fprintf(f, "DHCP6_CLIENT_IAID=0x%x\n", iaid); - r = sd_dhcp6_client_duid_as_string(link->dhcp6_client, &duid); + r = sd_dhcp6_client_get_duid_as_string(link->dhcp6_client, &duid); if (r >= 0) fprintf(f, "DHCP6_CLIENT_DUID=%s\n", duid); diff --git a/src/systemd/sd-dhcp-duid.h b/src/systemd/sd-dhcp-duid.h index 06e3f8af17184..b1d2772a3eb93 100644 --- a/src/systemd/sd-dhcp-duid.h +++ b/src/systemd/sd-dhcp-duid.h @@ -64,6 +64,8 @@ int sd_dhcp_duid_set_ll( int sd_dhcp_duid_set_en(sd_dhcp_duid *duid); int sd_dhcp_duid_set_uuid(sd_dhcp_duid *duid); +int sd_dhcp_duid_to_string(const sd_dhcp_duid *duid, char **ret); + _SD_END_DECLARATIONS; #endif diff --git a/src/systemd/sd-dhcp6-client.h b/src/systemd/sd-dhcp6-client.h index 65392580da5c9..d551b4dd90182 100644 --- a/src/systemd/sd-dhcp6-client.h +++ b/src/systemd/sd-dhcp6-client.h @@ -71,15 +71,13 @@ int sd_dhcp6_client_set_duid_uuid(sd_dhcp6_client *client); int sd_dhcp6_client_set_duid_raw(sd_dhcp6_client *client, uint16_t duid_type, const uint8_t *duid, size_t duid_len); int sd_dhcp6_client_set_duid(sd_dhcp6_client *client, const sd_dhcp_duid *duid); int sd_dhcp6_client_get_duid(sd_dhcp6_client *client, const sd_dhcp_duid **ret); +int sd_dhcp6_client_get_duid_as_string(sd_dhcp6_client *client, char **ret); int sd_dhcp6_client_set_iaid( sd_dhcp6_client *client, uint32_t iaid); int sd_dhcp6_client_get_iaid( sd_dhcp6_client *client, uint32_t *iaid); -int sd_dhcp6_client_duid_as_string( - sd_dhcp6_client *client, - char **duid); int sd_dhcp6_client_set_fqdn( sd_dhcp6_client *client, const char *fqdn);