]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
dhcp: move sd_dhcp_client_id_to_string() to sd-dhcp-client-id.[ch]
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 2 Jan 2024 21:06:36 +0000 (06:06 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 2 Jan 2024 21:06:36 +0000 (06:06 +0900)
Then, this makes it take sd_dhcp_client_id object, and introduce
sd_dhcp_client_id_to_string_from_raw().

src/libsystemd-network/sd-dhcp-client-id.c
src/libsystemd-network/sd-dhcp-client.c
src/network/networkctl.c
src/systemd/sd-dhcp-client-id.h
src/systemd/sd-dhcp-client.h

index 404ca1005151c29d639c3e9187e3d626585b59e0..03dca4382223ea806f0db0c7d1dd47cb67aa404e 100644 (file)
@@ -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);
+}
index bdfefa3d9f81f709f06d8ec27c964247840bf22d..e901e99a3b71a1b21e618b68981b5700b4e0b6e6 100644 (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,
index a280bfbf8e90526f1922c4a6d76f9fb40280a270..d72eac0dda174cc8c3b3d6f6bc5933750c1415b4 100644 (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",
index 9f165652c59627a867f534284630c97c01ddf594..c128efc84d78304df543a6037687c4e3e5e762c6 100644 (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
index 2c1c4fb9dde9f43936f2be1ce2cca222d2acb558..dcbd79e6b4ddb4515face15b10e639da13c7a2d3 100644 (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,