libnl  3.4.0
nl-link-ifindex2name.c
1 /*
2  * src/nl-link-ifindex2name.c Transform a interface index to its name
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/link.h>
14 
15 #include <linux/netlink.h>
16 
17 static void print_usage(void)
18 {
19  printf("Usage: nl-link-ifindex2name <ifindex>\n");
20  exit(0);
21 }
22 
23 int main(int argc, char *argv[])
24 {
25  struct nl_sock *sock;
26  struct nl_cache *link_cache;
27  char name[IFNAMSIZ];
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  ifindex = nl_cli_parse_u32(argv[1]);
38 
39  if (!rtnl_link_i2name(link_cache, ifindex, name, sizeof(name)))
40  nl_cli_fatal(ENOENT, "Interface index %d does not exist",
41  ifindex);
42 
43  printf("%s\n", name);
44 
45  return 0;
46 }
uint32_t nl_cli_parse_u32(const char *arg)
Parse a text based 32 bit unsigned integer argument.
Definition: utils.c:42
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:77