Skip to content

Commit fc2ea97

Browse files
committed
util-lib: add function to resolve "alternative" names
Calls to if_nametoindex() are expected to use resolve_ifname() instead.
1 parent 5c3fa98 commit fc2ea97

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

src/network/networkd-manager-bus.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
#include "alloc-util.h"
88
#include "bus-common-errors.h"
99
#include "bus-util.h"
10-
#include "netlink-util.h"
1110
#include "networkd-link-bus.h"
1211
#include "networkd-link.h"
1312
#include "networkd-manager-bus.h"
1413
#include "networkd-manager.h"
1514
#include "path-util.h"
15+
#include "socket-netlink.h"
1616
#include "strv.h"
1717
#include "user-util.h"
1818

@@ -66,12 +66,9 @@ static int method_get_link_by_name(sd_bus_message *message, void *userdata, sd_b
6666
if (r < 0)
6767
return r;
6868

69-
index = if_nametoindex(name);
70-
if (index <= 0) {
71-
index = rtnl_resolve_link_alternative_name(&manager->rtnl, name);
72-
if (index < 0)
73-
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %s not known", name);
74-
}
69+
index = resolve_ifname(&manager->rtnl, name);
70+
if (index < 0)
71+
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %s cannot be resolved", name);
7572

7673
link = hashmap_get(manager->links, INT_TO_PTR(index));
7774
if (!link)

src/shared/socket-netlink.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,26 @@
1010
#include "extract-word.h"
1111
#include "log.h"
1212
#include "memory-util.h"
13+
#include "netlink-util.h"
1314
#include "parse-util.h"
1415
#include "socket-netlink.h"
1516
#include "socket-util.h"
1617
#include "string-util.h"
1718

19+
int resolve_ifname(sd_netlink **rtnl, const char *name) {
20+
int r;
21+
22+
/* Like if_nametoindex, but resolves "alternative names" too. */
23+
24+
assert(name);
25+
26+
r = if_nametoindex(name);
27+
if (r > 0)
28+
return r;
29+
30+
return rtnl_resolve_link_alternative_name(rtnl, name);
31+
}
32+
1833
int socket_address_parse(SocketAddress *a, const char *s) {
1934
_cleanup_free_ char *n = NULL;
2035
char *e;
@@ -140,15 +155,12 @@ int socket_address_parse(SocketAddress *a, const char *s) {
140155
a->sockaddr.in.sin_port = htobe16(port);
141156
a->size = sizeof(struct sockaddr_in);
142157
} else {
143-
unsigned idx;
144-
145-
if (strlen(n) > IF_NAMESIZE-1)
146-
return -EINVAL;
158+
int idx;
147159

148160
/* Uh, our last resort, an interface name */
149-
idx = if_nametoindex(n);
150-
if (idx == 0)
151-
return -EINVAL;
161+
idx = resolve_ifname(NULL, n);
162+
if (idx < 0)
163+
return idx;
152164

153165
a->sockaddr.in6.sin6_family = AF_INET6;
154166
a->sockaddr.in6.sin6_port = htobe16(port);

src/shared/socket-netlink.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
/* SPDX-License-Identifier: LGPL-2.1+ */
22
#pragma once
33

4+
#include "sd-netlink.h"
5+
46
#include "in-addr-util.h"
57
#include "macro.h"
68
#include "socket-util.h"
79

10+
int resolve_ifname(sd_netlink **rtnl, const char *name);
11+
812
int make_socket_fd(int log_level, const char* address, int type, int flags);
913

1014
int socket_address_parse(SocketAddress *a, const char *s);

0 commit comments

Comments
 (0)