]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
netif-util: move several functions from network-util.[ch] to shared/netif-util.[ch]
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 6 Nov 2021 01:24:09 +0000 (10:24 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 8 Nov 2021 23:24:10 +0000 (08:24 +0900)
These functions are not relevant to sd-network, and only used by
networkd, networkctl, and udevd.

src/libsystemd-network/dhcp-identifier.c
src/libsystemd/sd-network/network-util.c
src/libsystemd/sd-network/network-util.h
src/network/networkctl.c
src/network/networkd-ipv4ll.c
src/network/networkd-json.c
src/shared/meson.build
src/shared/net-condition.c
src/shared/netif-util.c [new file with mode: 0644]
src/shared/netif-util.h [new file with mode: 0644]
src/udev/net/link-config.c

index 23021f7bad8c38ccf8e6b9abfd2c47a25c3f75fb..eb313caf81623e502d98e4c057606994173f729b 100644 (file)
@@ -9,7 +9,7 @@
 
 #include "dhcp-identifier.h"
 #include "dhcp6-protocol.h"
-#include "network-util.h"
+#include "netif-util.h"
 #include "siphash24.h"
 #include "sparse-endian.h"
 #include "stat-util.h"
index 5632debdebea6b8e0f89207877b4f9dd4e9e0476..971247714ac5bc6192b155fd0d2d3e32c06afdf7 100644 (file)
@@ -1,14 +1,9 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
-#include "sd-id128.h"
+#include "sd-network.h"
 
 #include "alloc-util.h"
-#include "arphrd-util.h"
-#include "device-util.h"
-#include "fd-util.h"
 #include "network-util.h"
-#include "siphash24.h"
-#include "sparse-endian.h"
 #include "string-table.h"
 #include "strv.h"
 
@@ -140,79 +135,3 @@ int parse_operational_state_range(const char *str, LinkOperationalStateRange *ou
 
         return 0;
 }
-
-int link_get_type_string(sd_device *device, unsigned short iftype, char **ret) {
-        const char *t;
-        char *p;
-
-        if (device &&
-            sd_device_get_devtype(device, &t) >= 0 &&
-            !isempty(t)) {
-                p = strdup(t);
-                if (!p)
-                        return -ENOMEM;
-
-                *ret = p;
-                return 0;
-        }
-
-        t = arphrd_to_name(iftype);
-        if (!t)
-                return -ENOENT;
-
-        p = strdup(t);
-        if (!p)
-                return -ENOMEM;
-
-        *ret = ascii_strlower(p);
-        return 0;
-}
-
-const char *net_get_name_persistent(sd_device *device) {
-        const char *name, *field;
-
-        assert(device);
-
-        /* fetch some persistent data unique (on this machine) to this device */
-        FOREACH_STRING(field, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC")
-                if (sd_device_get_property_value(device, field, &name) >= 0)
-                        return name;
-
-        return NULL;
-}
-
-#define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
-
-int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_t *result) {
-        size_t l, sz = 0;
-        const char *name;
-        int r;
-        uint8_t *v;
-
-        assert(device);
-
-        /* net_get_name_persistent() will return one of the device names based on stable information about
-         * the device. If this is not available, we fall back to using the actual device name. */
-        name = net_get_name_persistent(device);
-        if (!name && use_sysname)
-                (void) sd_device_get_sysname(device, &name);
-        if (!name)
-                return log_device_debug_errno(device, SYNTHETIC_ERRNO(ENODATA),
-                                              "No stable identifying information found");
-
-        log_device_debug(device, "Using \"%s\" as stable identifying information", name);
-        l = strlen(name);
-        sz = sizeof(sd_id128_t) + l;
-        v = newa(uint8_t, sz);
-
-        /* Fetch some persistent data unique to this machine */
-        r = sd_id128_get_machine((sd_id128_t*) v);
-        if (r < 0)
-                 return r;
-        memcpy(v + sizeof(sd_id128_t), name, l);
-
-        /* Let's hash the machine ID plus the device name. We use
-         * a fixed, but originally randomly created hash key here. */
-        *result = htole64(siphash24(v, sz, HASH_KEY.bytes));
-        return 0;
-}
index 3a2e4a7f6c6d38d68b0bb4b1ec1b8a76a0ef451c..6e2ad28426302de4cad6f4afcccce1122df41a1a 100644 (file)
@@ -1,12 +1,9 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 #pragma once
 
-#include <inttypes.h>
+#include <errno.h>
 #include <stdbool.h>
 
-#include "sd-device.h"
-#include "sd-network.h"
-
 #include "macro.h"
 
 bool network_is_online(void);
@@ -86,7 +83,3 @@ typedef struct LinkOperationalStateRange {
                                                                    LINK_OPERSTATE_ROUTABLE }
 
 int parse_operational_state_range(const char *str, LinkOperationalStateRange *out);
-
-int link_get_type_string(sd_device *device, unsigned short iftype, char **ret);
-int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_t *result);
-const char *net_get_name_persistent(sd_device *device);
index 6dd05145c95c609624d4fb1dc699f29dcb5a376b..ec86391cd362ee7ad75c3b81692fd6d253153fcc 100644 (file)
@@ -42,6 +42,7 @@
 #include "macro.h"
 #include "macvlan-util.h"
 #include "main-func.h"
+#include "netif-util.h"
 #include "netlink-util.h"
 #include "network-internal.h"
 #include "network-util.h"
index a3e5b4ba74c2feeae2f26895e8039b84eb179476..d05182fde7d5e516a5709cfd04038827035a485e 100644 (file)
@@ -3,7 +3,7 @@
 #include <netinet/in.h>
 #include <linux/if.h>
 
-#include "network-internal.h"
+#include "netif-util.h"
 #include "networkd-address.h"
 #include "networkd-ipv4ll.h"
 #include "networkd-link.h"
index 2bae2f2913d3972f56fb5592a69a9e3ea7cc0293..6a40e4f8e67dcb2632dfb493afb31540b996f468 100644 (file)
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
-#include "network-util.h"
+#include "netif-util.h"
 #include "networkd-json.h"
 #include "networkd-link.h"
 #include "networkd-manager.h"
index 6e09ef84795ffda5c9821c94245d3e155d0f55d1..a23d43ce09aac2a5201f145d690f20dc3d9094df 100644 (file)
@@ -218,6 +218,8 @@ shared_sources = files('''
         net-condition.h
         netif-naming-scheme.c
         netif-naming-scheme.h
+        netif-util.c
+        netif-util.h
         nscd-flush.h
         nsflags.c
         nsflags.h
index 1da60810ee0ba30833399741a4978b91fbb28c6e..7cfac18d651df6fccaebe708bf4660595bd7de07 100644 (file)
@@ -6,6 +6,7 @@
 #include "env-util.h"
 #include "log.h"
 #include "net-condition.h"
+#include "netif-util.h"
 #include "network-util.h"
 #include "socket-util.h"
 #include "string-table.h"
diff --git a/src/shared/netif-util.c b/src/shared/netif-util.c
new file mode 100644 (file)
index 0000000..fb81460
--- /dev/null
@@ -0,0 +1,86 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "sd-id128.h"
+
+#include "arphrd-util.h"
+#include "device-util.h"
+#include "netif-util.h"
+#include "siphash24.h"
+#include "sparse-endian.h"
+#include "strv.h"
+
+int link_get_type_string(sd_device *device, unsigned short iftype, char **ret) {
+        const char *t;
+        char *p;
+
+        if (device &&
+            sd_device_get_devtype(device, &t) >= 0 &&
+            !isempty(t)) {
+                p = strdup(t);
+                if (!p)
+                        return -ENOMEM;
+
+                *ret = p;
+                return 0;
+        }
+
+        t = arphrd_to_name(iftype);
+        if (!t)
+                return -ENOENT;
+
+        p = strdup(t);
+        if (!p)
+                return -ENOMEM;
+
+        *ret = ascii_strlower(p);
+        return 0;
+}
+
+const char *net_get_name_persistent(sd_device *device) {
+        const char *name, *field;
+
+        assert(device);
+
+        /* fetch some persistent data unique (on this machine) to this device */
+        FOREACH_STRING(field, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC")
+                if (sd_device_get_property_value(device, field, &name) >= 0)
+                        return name;
+
+        return NULL;
+}
+
+#define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
+
+int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_t *ret) {
+        size_t l, sz = 0;
+        const char *name;
+        int r;
+        uint8_t *v;
+
+        assert(device);
+
+        /* net_get_name_persistent() will return one of the device names based on stable information about
+         * the device. If this is not available, we fall back to using the actual device name. */
+        name = net_get_name_persistent(device);
+        if (!name && use_sysname)
+                (void) sd_device_get_sysname(device, &name);
+        if (!name)
+                return log_device_debug_errno(device, SYNTHETIC_ERRNO(ENODATA),
+                                              "No stable identifying information found");
+
+        log_device_debug(device, "Using \"%s\" as stable identifying information", name);
+        l = strlen(name);
+        sz = sizeof(sd_id128_t) + l;
+        v = newa(uint8_t, sz);
+
+        /* Fetch some persistent data unique to this machine */
+        r = sd_id128_get_machine((sd_id128_t*) v);
+        if (r < 0)
+                 return r;
+        memcpy(v + sizeof(sd_id128_t), name, l);
+
+        /* Let's hash the machine ID plus the device name. We use
+         * a fixed, but originally randomly created hash key here. */
+        *ret = htole64(siphash24(v, sz, HASH_KEY.bytes));
+        return 0;
+}
diff --git a/src/shared/netif-util.h b/src/shared/netif-util.h
new file mode 100644 (file)
index 0000000..7a2cf1b
--- /dev/null
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#include <inttypes.h>
+#include <stdbool.h>
+
+#include "sd-device.h"
+
+int link_get_type_string(sd_device *device, unsigned short iftype, char **ret);
+const char *net_get_name_persistent(sd_device *device);
+int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_t *ret);
index 1ce8e471e7697516d116e5bd48ecdc54590f9704..0fd5f845d2d278df93f8ee32f7f39f852e3576dc 100644 (file)
@@ -22,8 +22,8 @@
 #include "memory-util.h"
 #include "net-condition.h"
 #include "netif-naming-scheme.h"
+#include "netif-util.h"
 #include "netlink-util.h"
-#include "network-util.h"
 #include "parse-util.h"
 #include "path-lookup.h"
 #include "path-util.h"