libnl  3.5.0
nl-link-ifindex2name.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * src/nl-link-ifindex2name.c Transform a interface index to its name
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-2009 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-ifindex2name <ifindex>\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  char name[IFNAMSIZ];
29  uint32_t ifindex;
30 
31  if (argc < 2)
32  print_usage();
33 
34  sock = nl_cli_alloc_socket();
35  nl_cli_connect(sock, NETLINK_ROUTE);
36  link_cache = nl_cli_link_alloc_cache(sock);
37 
38  ifindex = nl_cli_parse_u32(argv[1]);
39 
40  if (!rtnl_link_i2name(link_cache, ifindex, name, sizeof(name)))
41  nl_cli_fatal(ENOENT, "Interface index %d does not exist",
42  ifindex);
43 
44  printf("%s\n", name);
45 
46  return 0;
47 }
uint32_t nl_cli_parse_u32(const char *arg)
Parse a text based 32 bit unsigned integer argument.
Definition: utils.c:43
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:78