1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-05 15:21:37 +03:00

util-lib: add function to resolve "alternative" names

Calls to if_nametoindex() are expected to use resolve_ifname() instead.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2020-01-10 21:18:31 +01:00
parent 5c3fa98db6
commit fc2ea97ad0
3 changed files with 27 additions and 14 deletions

View File

@ -7,12 +7,12 @@
#include "alloc-util.h"
#include "bus-common-errors.h"
#include "bus-util.h"
#include "netlink-util.h"
#include "networkd-link-bus.h"
#include "networkd-link.h"
#include "networkd-manager-bus.h"
#include "networkd-manager.h"
#include "path-util.h"
#include "socket-netlink.h"
#include "strv.h"
#include "user-util.h"
@ -66,12 +66,9 @@ static int method_get_link_by_name(sd_bus_message *message, void *userdata, sd_b
if (r < 0)
return r;
index = if_nametoindex(name);
if (index <= 0) {
index = rtnl_resolve_link_alternative_name(&manager->rtnl, name);
if (index < 0)
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %s not known", name);
}
index = resolve_ifname(&manager->rtnl, name);
if (index < 0)
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %s cannot be resolved", name);
link = hashmap_get(manager->links, INT_TO_PTR(index));
if (!link)

View File

@ -10,11 +10,26 @@
#include "extract-word.h"
#include "log.h"
#include "memory-util.h"
#include "netlink-util.h"
#include "parse-util.h"
#include "socket-netlink.h"
#include "socket-util.h"
#include "string-util.h"
int resolve_ifname(sd_netlink **rtnl, const char *name) {
int r;
/* Like if_nametoindex, but resolves "alternative names" too. */
assert(name);
r = if_nametoindex(name);
if (r > 0)
return r;
return rtnl_resolve_link_alternative_name(rtnl, name);
}
int socket_address_parse(SocketAddress *a, const char *s) {
_cleanup_free_ char *n = NULL;
char *e;
@ -140,15 +155,12 @@ int socket_address_parse(SocketAddress *a, const char *s) {
a->sockaddr.in.sin_port = htobe16(port);
a->size = sizeof(struct sockaddr_in);
} else {
unsigned idx;
if (strlen(n) > IF_NAMESIZE-1)
return -EINVAL;
int idx;
/* Uh, our last resort, an interface name */
idx = if_nametoindex(n);
if (idx == 0)
return -EINVAL;
idx = resolve_ifname(NULL, n);
if (idx < 0)
return idx;
a->sockaddr.in6.sin6_family = AF_INET6;
a->sockaddr.in6.sin6_port = htobe16(port);

View File

@ -1,10 +1,14 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once
#include "sd-netlink.h"
#include "in-addr-util.h"
#include "macro.h"
#include "socket-util.h"
int resolve_ifname(sd_netlink **rtnl, const char *name);
int make_socket_fd(int log_level, const char* address, int type, int flags);
int socket_address_parse(SocketAddress *a, const char *s);