From f09d20256a32a87fa65743dd92aaa8118587d1e2 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 7 Nov 2021 15:16:25 +0900 Subject: [PATCH 1/6] arphrd-util: rename arphrd-list.[ch] -> arphrd-util.[ch] --- src/basic/{arphrd-list.c => arphrd-util.c} | 2 +- src/basic/{arphrd-list.h => arphrd-util.h} | 0 src/basic/meson.build | 4 ++-- src/libsystemd/sd-network/network-util.c | 2 +- src/test/meson.build | 2 +- src/test/{test-arphrd-list.c => test-arphrd-util.c} | 3 +-- src/udev/test-udev-netlink.c | 2 +- 7 files changed, 7 insertions(+), 8 deletions(-) rename src/basic/{arphrd-list.c => arphrd-util.c} (95%) rename src/basic/{arphrd-list.h => arphrd-util.h} (100%) rename src/test/{test-arphrd-list.c => test-arphrd-util.c} (96%) diff --git a/src/basic/arphrd-list.c b/src/basic/arphrd-util.c similarity index 95% rename from src/basic/arphrd-list.c rename to src/basic/arphrd-util.c index 99048d20c8..b529347530 100644 --- a/src/basic/arphrd-list.c +++ b/src/basic/arphrd-util.c @@ -4,7 +4,7 @@ #include #include -#include "arphrd-list.h" +#include "arphrd-util.h" #include "macro.h" static const struct arphrd_name* lookup_arphrd(register const char *str, register GPERF_LEN_TYPE len); diff --git a/src/basic/arphrd-list.h b/src/basic/arphrd-util.h similarity index 100% rename from src/basic/arphrd-list.h rename to src/basic/arphrd-util.h diff --git a/src/basic/meson.build b/src/basic/meson.build index cf97f6de1d..042ab86d5e 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -9,8 +9,8 @@ basic_sources = files(''' alloc-util.h architecture.c architecture.h - arphrd-list.c - arphrd-list.h + arphrd-util.c + arphrd-util.h async.c async.h audit-util.c diff --git a/src/libsystemd/sd-network/network-util.c b/src/libsystemd/sd-network/network-util.c index 4a648caa5d..5632debdeb 100644 --- a/src/libsystemd/sd-network/network-util.c +++ b/src/libsystemd/sd-network/network-util.c @@ -3,7 +3,7 @@ #include "sd-id128.h" #include "alloc-util.h" -#include "arphrd-list.h" +#include "arphrd-util.h" #include "device-util.h" #include "fd-util.h" #include "network-util.h" diff --git a/src/test/meson.build b/src/test/meson.build index b22413029d..84811c038f 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -570,7 +570,7 @@ tests += [ [['src/test/test-af-list.c', generated_gperf_headers]], - [['src/test/test-arphrd-list.c', + [['src/test/test-arphrd-util.c', generated_gperf_headers]], [['src/test/test-ip-protocol-list.c', diff --git a/src/test/test-arphrd-list.c b/src/test/test-arphrd-util.c similarity index 96% rename from src/test/test-arphrd-list.c rename to src/test/test-arphrd-util.c index 9e21f32189..f12388dcce 100644 --- a/src/test/test-arphrd-list.c +++ b/src/test/test-arphrd-util.c @@ -2,11 +2,10 @@ #include +#include "arphrd-util.h" #include "string-util.h" #include "tests.h" -#include "arphrd-list.h" - int main(int argc, const char *argv[]) { test_setup_logging(LOG_INFO); diff --git a/src/udev/test-udev-netlink.c b/src/udev/test-udev-netlink.c index d177b09567..c1213b7ca8 100644 --- a/src/udev/test-udev-netlink.c +++ b/src/udev/test-udev-netlink.c @@ -2,7 +2,7 @@ #include "sd-device.h" -#include "arphrd-list.h" +#include "arphrd-util.h" #include "ether-addr-util.h" #include "parse-util.h" #include "tests.h" From 91961fff43d0a3565812e5eeeb237de45242e06a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 7 Nov 2021 15:23:38 +0900 Subject: [PATCH 2/6] arphrd-util: introduce arphrd_to_hw_addr_len() --- src/basic/arphrd-util.c | 20 ++++++++++++++++++++ src/basic/arphrd-util.h | 5 +++++ 2 files changed, 25 insertions(+) diff --git a/src/basic/arphrd-util.c b/src/basic/arphrd-util.c index b529347530..3ea2c9d09a 100644 --- a/src/basic/arphrd-util.c +++ b/src/basic/arphrd-util.c @@ -1,7 +1,9 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include +#include #include +#include #include #include "arphrd-util.h" @@ -23,3 +25,21 @@ int arphrd_from_name(const char *name) { return sc->id; } + +size_t arphrd_to_hw_addr_len(uint16_t arphrd) { + switch (arphrd) { + case ARPHRD_ETHER: + return ETH_ALEN; + case ARPHRD_INFINIBAND: + return INFINIBAND_ALEN; + case ARPHRD_TUNNEL: + case ARPHRD_SIT: + case ARPHRD_IPGRE: + return sizeof(struct in_addr); + case ARPHRD_TUNNEL6: + case ARPHRD_IP6GRE: + return sizeof(struct in6_addr); + default: + return 0; + } +} diff --git a/src/basic/arphrd-util.h b/src/basic/arphrd-util.h index bc95b4507b..33f5694abd 100644 --- a/src/basic/arphrd-util.h +++ b/src/basic/arphrd-util.h @@ -1,5 +1,10 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include +#include + const char *arphrd_to_name(int id); int arphrd_from_name(const char *name); + +size_t arphrd_to_hw_addr_len(uint16_t arphrd); From b5cc5591faec4117162099c92bc84d1120e6e234 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 6 Nov 2021 10:24:09 +0900 Subject: [PATCH 3/6] netif-util: move several functions from network-util.[ch] to shared/netif-util.[ch] These functions are not relevant to sd-network, and only used by networkd, networkctl, and udevd. --- src/libsystemd-network/dhcp-identifier.c | 2 +- src/libsystemd/sd-network/network-util.c | 83 +---------------------- src/libsystemd/sd-network/network-util.h | 9 +-- src/network/networkctl.c | 1 + src/network/networkd-ipv4ll.c | 2 +- src/network/networkd-json.c | 2 +- src/shared/meson.build | 2 + src/shared/net-condition.c | 1 + src/shared/netif-util.c | 86 ++++++++++++++++++++++++ src/shared/netif-util.h | 11 +++ src/udev/net/link-config.c | 2 +- 11 files changed, 107 insertions(+), 94 deletions(-) create mode 100644 src/shared/netif-util.c create mode 100644 src/shared/netif-util.h diff --git a/src/libsystemd-network/dhcp-identifier.c b/src/libsystemd-network/dhcp-identifier.c index 23021f7bad..eb313caf81 100644 --- a/src/libsystemd-network/dhcp-identifier.c +++ b/src/libsystemd-network/dhcp-identifier.c @@ -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" diff --git a/src/libsystemd/sd-network/network-util.c b/src/libsystemd/sd-network/network-util.c index 5632debdeb..971247714a 100644 --- a/src/libsystemd/sd-network/network-util.c +++ b/src/libsystemd/sd-network/network-util.c @@ -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; -} diff --git a/src/libsystemd/sd-network/network-util.h b/src/libsystemd/sd-network/network-util.h index 3a2e4a7f6c..6e2ad28426 100644 --- a/src/libsystemd/sd-network/network-util.h +++ b/src/libsystemd/sd-network/network-util.h @@ -1,12 +1,9 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once -#include +#include #include -#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); diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 6dd05145c9..ec86391cd3 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -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" diff --git a/src/network/networkd-ipv4ll.c b/src/network/networkd-ipv4ll.c index a3e5b4ba74..d05182fde7 100644 --- a/src/network/networkd-ipv4ll.c +++ b/src/network/networkd-ipv4ll.c @@ -3,7 +3,7 @@ #include #include -#include "network-internal.h" +#include "netif-util.h" #include "networkd-address.h" #include "networkd-ipv4ll.h" #include "networkd-link.h" diff --git a/src/network/networkd-json.c b/src/network/networkd-json.c index 2bae2f2913..6a40e4f8e6 100644 --- a/src/network/networkd-json.c +++ b/src/network/networkd-json.c @@ -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" diff --git a/src/shared/meson.build b/src/shared/meson.build index 6e09ef8479..a23d43ce09 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -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 diff --git a/src/shared/net-condition.c b/src/shared/net-condition.c index 1da60810ee..7cfac18d65 100644 --- a/src/shared/net-condition.c +++ b/src/shared/net-condition.c @@ -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 index 0000000000..fb81460c91 --- /dev/null +++ b/src/shared/netif-util.c @@ -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 index 0000000000..7a2cf1be9e --- /dev/null +++ b/src/shared/netif-util.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include +#include + +#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); diff --git a/src/udev/net/link-config.c b/src/udev/net/link-config.c index 1ce8e471e7..0fd5f845d2 100644 --- a/src/udev/net/link-config.c +++ b/src/udev/net/link-config.c @@ -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" From 4eef6fad3150b8b3e63f3284f9f715fb56792709 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 6 Nov 2021 11:28:55 +0900 Subject: [PATCH 4/6] netif-util: rename link_get_type_string() -> net_get_type_string() --- src/network/networkctl.c | 4 ++-- src/network/networkd-json.c | 2 +- src/shared/net-condition.c | 2 +- src/shared/netif-util.c | 2 +- src/shared/netif-util.h | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/network/networkctl.c b/src/network/networkctl.c index ec86391cd3..3056710d78 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -846,7 +846,7 @@ static int list_links(int argc, char *argv[], void *userdata) { setup_state = strdup("unmanaged"); setup_state_to_color(setup_state, &on_color_setup, NULL); - r = link_get_type_string(links[i].sd_device, links[i].iftype, &t); + r = net_get_type_string(links[i].sd_device, links[i].iftype, &t); if (r == -ENOMEM) return log_oom(); @@ -1569,7 +1569,7 @@ static int link_status_one( (void) sd_device_get_property_value(info->sd_device, "ID_MODEL", &model); } - r = link_get_type_string(info->sd_device, info->iftype, &t); + r = net_get_type_string(info->sd_device, info->iftype, &t); if (r == -ENOMEM) return log_oom(); diff --git a/src/network/networkd-json.c b/src/network/networkd-json.c index 6a40e4f8e6..7148c2ee14 100644 --- a/src/network/networkd-json.c +++ b/src/network/networkd-json.c @@ -45,7 +45,7 @@ int link_build_json(Link *link, JsonVariant **ret) { assert(link); assert(ret); - r = link_get_type_string(link->sd_device, link->iftype, &type); + r = net_get_type_string(link->sd_device, link->iftype, &type); if (r == -ENOMEM) return r; diff --git a/src/shared/net-condition.c b/src/shared/net-condition.c index 7cfac18d65..52cac19df3 100644 --- a/src/shared/net-condition.c +++ b/src/shared/net-condition.c @@ -138,7 +138,7 @@ int net_match_config( assert(match); - r = link_get_type_string(device, iftype, &iftype_str); + r = net_get_type_string(device, iftype, &iftype_str); if (r == -ENOMEM) return r; diff --git a/src/shared/netif-util.c b/src/shared/netif-util.c index fb81460c91..069f621103 100644 --- a/src/shared/netif-util.c +++ b/src/shared/netif-util.c @@ -9,7 +9,7 @@ #include "sparse-endian.h" #include "strv.h" -int link_get_type_string(sd_device *device, unsigned short iftype, char **ret) { +int net_get_type_string(sd_device *device, uint16_t iftype, char **ret) { const char *t; char *p; diff --git a/src/shared/netif-util.h b/src/shared/netif-util.h index 7a2cf1be9e..1d4ac5be56 100644 --- a/src/shared/netif-util.h +++ b/src/shared/netif-util.h @@ -6,6 +6,6 @@ #include "sd-device.h" -int link_get_type_string(sd_device *device, unsigned short iftype, char **ret); +int net_get_type_string(sd_device *device, uint16_t 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); From 043b193f6e09058a0aa65c404eb528d70e2fb314 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 8 Nov 2021 10:55:27 +0900 Subject: [PATCH 5/6] netif-util: rename net_get_name_persistent() -> net_get_persistent_name() --- src/libsystemd-network/dhcp-identifier.c | 2 +- src/shared/netif-util.c | 6 +++--- src/shared/netif-util.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libsystemd-network/dhcp-identifier.c b/src/libsystemd-network/dhcp-identifier.c index eb313caf81..3ea6c7ce24 100644 --- a/src/libsystemd-network/dhcp-identifier.c +++ b/src/libsystemd-network/dhcp-identifier.c @@ -193,7 +193,7 @@ int dhcp_identifier_set_iaid( /* device is under renaming */ return -EBUSY; - name = net_get_name_persistent(device); + name = net_get_persistent_name(device); } if (name) diff --git a/src/shared/netif-util.c b/src/shared/netif-util.c index 069f621103..b26a127c38 100644 --- a/src/shared/netif-util.c +++ b/src/shared/netif-util.c @@ -36,7 +36,7 @@ int net_get_type_string(sd_device *device, uint16_t iftype, char **ret) { return 0; } -const char *net_get_name_persistent(sd_device *device) { +const char *net_get_persistent_name(sd_device *device) { const char *name, *field; assert(device); @@ -59,9 +59,9 @@ int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_ assert(device); - /* net_get_name_persistent() will return one of the device names based on stable information about + /* net_get_persistent_name() 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); + name = net_get_persistent_name(device); if (!name && use_sysname) (void) sd_device_get_sysname(device, &name); if (!name) diff --git a/src/shared/netif-util.h b/src/shared/netif-util.h index 1d4ac5be56..d96a64b934 100644 --- a/src/shared/netif-util.h +++ b/src/shared/netif-util.h @@ -7,5 +7,5 @@ #include "sd-device.h" int net_get_type_string(sd_device *device, uint16_t iftype, char **ret); -const char *net_get_name_persistent(sd_device *device); +const char *net_get_persistent_name(sd_device *device); int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_t *ret); From 0295b2fd1d97c68010c7528af13e2952886d52e0 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 8 Nov 2021 11:01:46 +0900 Subject: [PATCH 6/6] netif-util: split net_get_unique_predictable_data() into two --- src/shared/netif-util.c | 29 +++++++++++++++++++++++------ src/shared/netif-util.h | 2 ++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/shared/netif-util.c b/src/shared/netif-util.c index b26a127c38..6500841e7e 100644 --- a/src/shared/netif-util.c +++ b/src/shared/netif-util.c @@ -1,7 +1,5 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ -#include "sd-id128.h" - #include "arphrd-util.h" #include "device-util.h" #include "netif-util.h" @@ -49,15 +47,14 @@ const char *net_get_persistent_name(sd_device *device) { return NULL; } +/* Used when generating hardware address by udev, and IPv4LL seed by networkd. */ #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); + assert(ret); /* net_get_persistent_name() 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. */ @@ -69,6 +66,25 @@ int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_ "No stable identifying information found"); log_device_debug(device, "Using \"%s\" as stable identifying information", name); + + return net_get_unique_predictable_data_from_name(name, NULL, ret); +} + +int net_get_unique_predictable_data_from_name( + const char *name, + const sd_id128_t *key, + uint64_t *ret) { + + size_t l, sz; + uint8_t *v; + int r; + + assert(name); + assert(ret); + + if (!key) + key = &HASH_KEY; + l = strlen(name); sz = sizeof(sd_id128_t) + l; v = newa(uint8_t, sz); @@ -77,10 +93,11 @@ int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_ 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)); + *ret = htole64(siphash24(v, sz, key->bytes)); return 0; } diff --git a/src/shared/netif-util.h b/src/shared/netif-util.h index d96a64b934..f239f042b5 100644 --- a/src/shared/netif-util.h +++ b/src/shared/netif-util.h @@ -5,7 +5,9 @@ #include #include "sd-device.h" +#include "sd-id128.h" int net_get_type_string(sd_device *device, uint16_t iftype, char **ret); const char *net_get_persistent_name(sd_device *device); int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_t *ret); +int net_get_unique_predictable_data_from_name(const char *name, const sd_id128_t *key, uint64_t *ret);