mirror of
https://github.com/systemd/systemd-stable.git
synced 2024-12-22 13:33:56 +03:00
Merge pull request #21272 from yuwata/netif-util-split
netif-util: move several functions from network-util.c to netif-util.c
This commit is contained in:
commit
ed8ba68f3b
@ -1,25 +0,0 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <errno.h>
|
||||
#include <linux/if_arp.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "arphrd-list.h"
|
||||
#include "macro.h"
|
||||
|
||||
static const struct arphrd_name* lookup_arphrd(register const char *str, register GPERF_LEN_TYPE len);
|
||||
|
||||
#include "arphrd-from-name.h"
|
||||
#include "arphrd-to-name.h"
|
||||
|
||||
int arphrd_from_name(const char *name) {
|
||||
const struct arphrd_name *sc;
|
||||
|
||||
assert(name);
|
||||
|
||||
sc = lookup_arphrd(name, strlen(name));
|
||||
if (!sc)
|
||||
return -EINVAL;
|
||||
|
||||
return sc->id;
|
||||
}
|
45
src/basic/arphrd-util.c
Normal file
45
src/basic/arphrd-util.c
Normal file
@ -0,0 +1,45 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
#include <linux/if_arp.h>
|
||||
#include <linux/if_infiniband.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "arphrd-util.h"
|
||||
#include "macro.h"
|
||||
|
||||
static const struct arphrd_name* lookup_arphrd(register const char *str, register GPERF_LEN_TYPE len);
|
||||
|
||||
#include "arphrd-from-name.h"
|
||||
#include "arphrd-to-name.h"
|
||||
|
||||
int arphrd_from_name(const char *name) {
|
||||
const struct arphrd_name *sc;
|
||||
|
||||
assert(name);
|
||||
|
||||
sc = lookup_arphrd(name, strlen(name));
|
||||
if (!sc)
|
||||
return -EINVAL;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,5 +1,10 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stddef.h>
|
||||
|
||||
const char *arphrd_to_name(int id);
|
||||
int arphrd_from_name(const char *name);
|
||||
|
||||
size_t arphrd_to_hw_addr_len(uint16_t arphrd);
|
@ -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
|
||||
|
@ -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"
|
||||
@ -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)
|
||||
|
@ -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-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"
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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"
|
||||
@ -845,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();
|
||||
|
||||
@ -1568,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();
|
||||
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
@ -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;
|
||||
|
||||
|
@ -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
|
||||
|
@ -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"
|
||||
@ -137,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;
|
||||
|
||||
|
103
src/shared/netif-util.c
Normal file
103
src/shared/netif-util.c
Normal file
@ -0,0 +1,103 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "arphrd-util.h"
|
||||
#include "device-util.h"
|
||||
#include "netif-util.h"
|
||||
#include "siphash24.h"
|
||||
#include "sparse-endian.h"
|
||||
#include "strv.h"
|
||||
|
||||
int net_get_type_string(sd_device *device, uint16_t 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_persistent_name(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;
|
||||
}
|
||||
|
||||
/* 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) {
|
||||
const char *name;
|
||||
|
||||
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. */
|
||||
name = net_get_persistent_name(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);
|
||||
|
||||
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);
|
||||
|
||||
/* 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, key->bytes));
|
||||
return 0;
|
||||
}
|
13
src/shared/netif-util.h
Normal file
13
src/shared/netif-util.h
Normal file
@ -0,0 +1,13 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#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);
|
@ -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',
|
||||
|
@ -2,11 +2,10 @@
|
||||
|
||||
#include <linux/if_arp.h>
|
||||
|
||||
#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);
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user