libnl  3.7.0
nl-link-name2ifindex.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2003-2008 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-name2ifindex <name>\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  uint32_t ifindex;
22 
23  if (argc < 2)
24  print_usage();
25 
26  sock = nl_cli_alloc_socket();
27  nl_cli_connect(sock, NETLINK_ROUTE);
28  link_cache = nl_cli_link_alloc_cache(sock);
29 
30  if (!(ifindex = rtnl_link_name2i(link_cache, argv[1])))
31  nl_cli_fatal(ENOENT, "Interface \"%s\" does not exist",
32  argv[1]);
33 
34  printf("%u\n", ifindex);
35 
36  return 0;
37 }
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71