libnl 3.5.0

nl-link-name2ifindex.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * src/nl-link-name2ifindex.c Transform a interface name to its index
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation version 2.1
8 * of the License.
9 *
10 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
11 */
12
13 #include <netlink/cli/utils.h>
14 #include <netlink/cli/link.h>
15
16 #include <linux/netlink.h>
17
18 static void print_usage(void)
19 {
20 printf("Usage: nl-link-name2ifindex <name>\n");
21 exit(0);
22 }
23
24 int main(int argc, char *argv[])
25 {
26 struct nl_sock *sock;
27 struct nl_cache *link_cache;
28 uint32_t ifindex;
29
30 if (argc < 2)
31 print_usage();
32
33 sock = nl_cli_alloc_socket();
34 nl_cli_connect(sock, NETLINK_ROUTE);
35 link_cache = nl_cli_link_alloc_cache(sock);
36
37 if (!(ifindex = rtnl_link_name2i(link_cache, argv[1])))
38 nl_cli_fatal(ENOENT, "Interface \"%s\" does not exist",
39 argv[1]);
40
41 printf("%u\n", ifindex);
42
43 return 0;
44 }
nl_cli_fatal
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:78
rtnl_link_name2i
int rtnl_link_name2i(struct nl_cache *cache, const char *name)
Translate link name to corresponding interface index.
Definition: link.c:1507