Home | History | Annotate | Download | only in src
      1 /*
      2  * src/nl-link-name2ifindex.c     Transform a interface name to its index
      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-2008 Thomas Graf <tgraf (at) suug.ch>
     10  */
     11 
     12 #include <netlink/cli/utils.h>
     13 #include <netlink/cli/link.h>
     14 
     15 static void print_usage(void)
     16 {
     17 	printf("Usage: nl-link-name2ifindex <name>\n");
     18 	exit(0);
     19 }
     20 
     21 int main(int argc, char *argv[])
     22 {
     23 	struct nl_sock *sock;
     24 	struct nl_cache *link_cache;
     25 	uint32_t ifindex;
     26 
     27 	if (argc < 2)
     28 		print_usage();
     29 
     30 	sock = nl_cli_alloc_socket();
     31 	nl_cli_connect(sock, NETLINK_ROUTE);
     32 	link_cache = nl_cli_link_alloc_cache(sock);
     33 
     34 	if (!(ifindex = rtnl_link_name2i(link_cache, argv[1])))
     35 		nl_cli_fatal(ENOENT, "Interface \"%s\" does not exist",
     36 			     argv[1]);
     37 
     38 	printf("%u\n", ifindex);
     39 
     40 	return 0;
     41 }
     42