libnl: /data/src/libnl3/src/nl-link-ifindex2name.c Source File

libnl 3.7.0

nl-link-ifindex2name.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
4 */
5
6 #include <netlink/cli/utils.h>
7 #include <netlink/cli/link.h>
8
9 #include <linux/netlink.h>
10
11 static void print_usage(void)
12 {
13 printf("Usage: nl-link-ifindex2name <ifindex>\n");
14 exit(0);
15 }
16
17 int main(int argc, char *argv[])
18 {
19 struct nl_sock *sock;
20 struct nl_cache *link_cache;
21 char name[IFNAMSIZ];
22 uint32_t ifindex;
23
24 if (argc < 2)
25 print_usage();
26
27 sock = nl_cli_alloc_socket();
28 nl_cli_connect(sock, NETLINK_ROUTE);
29 link_cache = nl_cli_link_alloc_cache(sock);
30
31 ifindex = nl_cli_parse_u32(argv[1]);
32
33 if (!rtnl_link_i2name(link_cache, ifindex, name, sizeof(name)))
34 nl_cli_fatal(ENOENT, "Interface index %d does not exist",
35 ifindex);
36
37 printf("%s\n", name);
38
39 return 0;
40 }
nl_cli_fatal
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
nl_cli_parse_u32
uint32_t nl_cli_parse_u32(const char *arg)
Parse a text based 32 bit unsigned integer argument.
Definition: utils.c:36
rtnl_link_i2name
char * rtnl_link_i2name(struct nl_cache *cache, int ifindex, char *dst, size_t len)
Translate interface index to corresponding link name.
Definition: link.c:1473