From: Yu Watanabe Date: Tue, 2 Jan 2024 21:06:36 +0000 (+0900) Subject: dhcp: move sd_dhcp_client_id_to_string() to sd-dhcp-client-id.[ch] X-Git-Tag: v256-rc1~1339^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1809132064d2fd3479e316b615cd05698984852c;p=thirdparty%2Fsystemd.git dhcp: move sd_dhcp_client_id_to_string() to sd-dhcp-client-id.[ch] Then, this makes it take sd_dhcp_client_id object, and introduce sd_dhcp_client_id_to_string_from_raw(). --- diff --git a/src/libsystemd-network/sd-dhcp-client-id.c b/src/libsystemd-network/sd-dhcp-client-id.c index 404ca100515..03dca438222 100644 --- a/src/libsystemd-network/sd-dhcp-client-id.c +++ b/src/libsystemd-network/sd-dhcp-client-id.c @@ -93,3 +93,67 @@ int sd_dhcp_client_id_set_iaid_duid( client_id->size = offsetof(typeof(client_id->id), ns.duid) + duid->size; return 0; } + +int sd_dhcp_client_id_to_string(const sd_dhcp_client_id *client_id, char **ret) { + _cleanup_free_ char *t = NULL; + size_t len; + int r; + + assert_return(sd_dhcp_client_id_is_set(client_id), -EINVAL); + assert_return(ret, -EINVAL); + + len = client_id->size - offsetof(typeof(client_id->id), data); + + switch (client_id->id.type) { + case 0: + if (utf8_is_printable((char *) client_id->id.gen.data, len)) + r = asprintf(&t, "%.*s", (int) len, client_id->id.gen.data); + else + r = asprintf(&t, "DATA"); + break; + case 1: + if (len == sizeof_field(sd_dhcp_client_id, id.eth)) + r = asprintf(&t, "%02x:%02x:%02x:%02x:%02x:%02x", + client_id->id.eth.haddr[0], + client_id->id.eth.haddr[1], + client_id->id.eth.haddr[2], + client_id->id.eth.haddr[3], + client_id->id.eth.haddr[4], + client_id->id.eth.haddr[5]); + else + r = asprintf(&t, "ETHER"); + break; + case 2 ... 254: + r = asprintf(&t, "ARP/LL"); + break; + case 255: + if (len < sizeof(uint32_t)) + r = asprintf(&t, "IAID/DUID"); + else { + uint32_t iaid = be32toh(client_id->id.ns.iaid); + /* TODO: check and stringify DUID */ + r = asprintf(&t, "IAID:0x%x/DUID", iaid); + } + break; + } + if (r < 0) + return -ENOMEM; + + *ret = TAKE_PTR(t); + return 0; +} + +int sd_dhcp_client_id_to_string_from_raw(const void *data, size_t data_size, char **ret) { + sd_dhcp_client_id client_id; + int r; + + assert_return(data, -EINVAL); + assert_return(client_id_size_is_valid(data_size), -EINVAL); + assert_return(ret, -EINVAL); + + r = sd_dhcp_client_id_set_raw(&client_id, data, data_size); + if (r < 0) + return r; + + return sd_dhcp_client_id_to_string(&client_id, ret); +} diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index bdfefa3d9f8..e901e99a3b7 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -150,60 +150,6 @@ static int client_receive_message_udp( void *userdata); static void client_stop(sd_dhcp_client *client, int error); -int sd_dhcp_client_id_to_string(const void *data, size_t len, char **ret) { - _cleanup_free_ char *t = NULL; - sd_dhcp_client_id client_id; - int r; - - assert_return(data, -EINVAL); - assert_return(client_id_size_is_valid(len), -EINVAL); - assert_return(ret, -EINVAL); - - r = sd_dhcp_client_id_set_raw(&client_id, data, len); - if (r < 0) - return r; - - len--; - - switch (client_id.id.type) { - case 0: - if (utf8_is_printable((char *) client_id.id.gen.data, len)) - r = asprintf(&t, "%.*s", (int) len, client_id.id.gen.data); - else - r = asprintf(&t, "DATA"); - break; - case 1: - if (len == sizeof_field(sd_dhcp_client_id, id.eth)) - r = asprintf(&t, "%02x:%02x:%02x:%02x:%02x:%02x", - client_id.id.eth.haddr[0], - client_id.id.eth.haddr[1], - client_id.id.eth.haddr[2], - client_id.id.eth.haddr[3], - client_id.id.eth.haddr[4], - client_id.id.eth.haddr[5]); - else - r = asprintf(&t, "ETHER"); - break; - case 2 ... 254: - r = asprintf(&t, "ARP/LL"); - break; - case 255: - if (len < sizeof(uint32_t)) - r = asprintf(&t, "IAID/DUID"); - else { - uint32_t iaid = be32toh(client_id.id.ns.iaid); - /* TODO: check and stringify DUID */ - r = asprintf(&t, "IAID:0x%x/DUID", iaid); - } - break; - } - if (r < 0) - return -ENOMEM; - - *ret = TAKE_PTR(t); - return 0; -} - int dhcp_client_set_state_callback( sd_dhcp_client *client, sd_dhcp_client_callback_t cb, diff --git a/src/network/networkctl.c b/src/network/networkctl.c index a280bfbf8e9..d72eac0dda1 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -1458,7 +1458,7 @@ static int dump_dhcp_leases(Table *table, const char *prefix, sd_bus *bus, const if (r < 0) return bus_log_parse_error(r); - r = sd_dhcp_client_id_to_string(client_id, client_id_sz, &id); + r = sd_dhcp_client_id_to_string_from_raw(client_id, client_id_sz, &id); if (r < 0) return bus_log_parse_error(r); @@ -2288,7 +2288,7 @@ static int link_status_one( if (r >= 0) { _cleanup_free_ char *id = NULL; - r = sd_dhcp_client_id_to_string(client_id, client_id_len, &id); + r = sd_dhcp_client_id_to_string_from_raw(client_id, client_id_len, &id); if (r >= 0) { r = table_add_many(table, TABLE_FIELD, "DHCP4 Client ID", diff --git a/src/systemd/sd-dhcp-client-id.h b/src/systemd/sd-dhcp-client-id.h index 9f165652c59..c128efc84d7 100644 --- a/src/systemd/sd-dhcp-client-id.h +++ b/src/systemd/sd-dhcp-client-id.h @@ -54,6 +54,9 @@ int sd_dhcp_client_id_set_iaid_duid( uint32_t iaid, sd_dhcp_duid *duid); +int sd_dhcp_client_id_to_string(const sd_dhcp_client_id *client_id, char **ret); +int sd_dhcp_client_id_to_string_from_raw(const void *data, size_t data_size, char **ret); + _SD_END_DECLARATIONS; #endif diff --git a/src/systemd/sd-dhcp-client.h b/src/systemd/sd-dhcp-client.h index 2c1c4fb9dde..dcbd79e6b4d 100644 --- a/src/systemd/sd-dhcp-client.h +++ b/src/systemd/sd-dhcp-client.h @@ -164,8 +164,6 @@ sd_dhcp_client *sd_dhcp_client_unref(sd_dhcp_client *client); * options when using RFC7844 Anonymity Profiles */ int sd_dhcp_client_new(sd_dhcp_client **ret, int anonymize); -int sd_dhcp_client_id_to_string(const void *data, size_t len, char **ret); - int sd_dhcp_client_attach_event( sd_dhcp_client *client, sd_event *event,