diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2020-10-29 01:04:08 +0900 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2020-10-29 14:23:49 +0900 |
commit | dc7f6c9b94183ea555dc307e1d04209730749af0 (patch) | |
tree | 5c05aca3b82d3bf85f492b6f7c5913bb68947687 /src | |
parent | c2f781bc15e98ac82fca16aa72cd332a24a6ce08 (diff) | |
download | systemd-dc7f6c9b94183ea555dc307e1d04209730749af0.tar.gz |
sd-network: move net_get_unique_predictable_data() and net_get_name_persisten()
Diffstat (limited to 'src')
-rw-r--r-- | src/libsystemd-network/dhcp-identifier.c | 3 | ||||
-rw-r--r-- | src/libsystemd-network/network-internal.c | 56 | ||||
-rw-r--r-- | src/libsystemd-network/network-internal.h | 4 | ||||
-rw-r--r-- | src/libsystemd/sd-network/network-util.c | 54 | ||||
-rw-r--r-- | src/libsystemd/sd-network/network-util.h | 5 | ||||
-rw-r--r-- | src/udev/net/link-config.c | 2 |
6 files changed, 63 insertions, 61 deletions
diff --git a/src/libsystemd-network/dhcp-identifier.c b/src/libsystemd-network/dhcp-identifier.c index e7dbec1511..33e5c00b06 100644 --- a/src/libsystemd-network/dhcp-identifier.c +++ b/src/libsystemd-network/dhcp-identifier.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include <linux/if_infiniband.h> +#include <net/ethernet.h> #include <net/if_arp.h> #include "sd-device.h" @@ -8,7 +9,7 @@ #include "dhcp-identifier.h" #include "dhcp6-protocol.h" -#include "network-internal.h" +#include "network-util.h" #include "siphash24.h" #include "sparse-endian.h" #include "stdio-util.h" diff --git a/src/libsystemd-network/network-internal.c b/src/libsystemd-network/network-internal.c index 4abe7298be..f64c3584ad 100644 --- a/src/libsystemd-network/network-internal.c +++ b/src/libsystemd-network/network-internal.c @@ -4,69 +4,15 @@ #include <linux/if.h> #include <netinet/ether.h> -#include "sd-id128.h" #include "sd-ndisc.h" #include "alloc-util.h" -#include "device-util.h" #include "dhcp-lease-internal.h" +#include "extract-word.h" #include "hexdecoct.h" #include "log.h" #include "network-internal.h" -#include "network-util.h" #include "parse-util.h" -#include "siphash24.h" -#include "strv.h" -#include "util.h" - -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; -} size_t serialize_in_addrs(FILE *f, const struct in_addr *addresses, diff --git a/src/libsystemd-network/network-internal.h b/src/libsystemd-network/network-internal.h index 8bdaa29c78..c4e89ade9a 100644 --- a/src/libsystemd-network/network-internal.h +++ b/src/libsystemd-network/network-internal.h @@ -4,12 +4,8 @@ #include <stdbool.h> #include <stdio.h> -#include "sd-device.h" #include "sd-dhcp-lease.h" -int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_t *result); -const char *net_get_name_persistent(sd_device *device); - size_t serialize_in_addrs(FILE *f, const struct in_addr *addresses, size_t size, diff --git a/src/libsystemd/sd-network/network-util.c b/src/libsystemd/sd-network/network-util.c index f3d6061578..7c32a8ead1 100644 --- a/src/libsystemd/sd-network/network-util.c +++ b/src/libsystemd/sd-network/network-util.c @@ -1,9 +1,14 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ +#include "sd-id128.h" + #include "alloc-util.h" #include "arphrd-list.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" @@ -124,3 +129,52 @@ char *link_get_type_string(sd_device *device, unsigned short iftype) { return ascii_strlower(p); } + +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 1422a57279..2d05b666cb 100644 --- a/src/libsystemd/sd-network/network-util.h +++ b/src/libsystemd/sd-network/network-util.h @@ -1,6 +1,9 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once +#include <inttypes.h> +#include <stdbool.h> + #include "sd-device.h" #include "sd-network.h" @@ -61,3 +64,5 @@ typedef struct LinkOperationalStateRange { int parse_operational_state_range(const char *str, LinkOperationalStateRange *out); char *link_get_type_string(sd_device *device, unsigned short iftype); +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/udev/net/link-config.c b/src/udev/net/link-config.c index c0b74ed3d4..459294bf58 100644 --- a/src/udev/net/link-config.c +++ b/src/udev/net/link-config.c @@ -21,7 +21,7 @@ #include "net-condition.h" #include "netif-naming-scheme.h" #include "netlink-util.h" -#include "network-internal.h" +#include "network-util.h" #include "parse-util.h" #include "path-lookup.h" #include "path-util.h" |