Skip to content

Commit

Permalink
dhcp: introduce sd_dhcp_duid_to_string()
Browse files Browse the repository at this point in the history
This also renames sd_dhcp6_client_duid_as_string() ->
sd_dhcp6_client_get_duid_as_string(), and make it use
sd_dhcp_duid_to_string().
  • Loading branch information
yuwata committed Dec 22, 2023
1 parent ce9bd20 commit 3a6c222
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/libsystemd-network/dhcp-duid-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 41 additions & 0 deletions src/libsystemd-network/sd-dhcp-duid.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <net/if_arp.h>

#include "dhcp-duid-internal.h"
#include "hexdecoct.h"
#include "netif-util.h"
#include "network-common.h"
#include "siphash24.h"
Expand Down Expand Up @@ -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,
Expand Down
36 changes: 5 additions & 31 deletions src/libsystemd-network/sd-dhcp6-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/network/networkd-dhcp6.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions src/systemd/sd-dhcp-duid.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions src/systemd/sd-dhcp6-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3a6c222

Please sign in to comment.