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().
This commit is contained in:
Yu Watanabe
2024-01-03 06:06:36 +09:00
parent 6efa51f862
commit 1809132064
5 changed files with 69 additions and 58 deletions
@@ -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);
}
-54
View File
@@ -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,
+2 -2
View File
@@ -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",
+3
View File
@@ -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
-2
View File
@@ -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,