oops
[libnl.git] / src / nl-link-name2ifindex.c
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-2006 Thomas Graf <tgraf@suug.ch>
10  */
11
12 #include "utils.h"
13
14 int main(int argc, char *argv[])
15 {
16         struct nl_handle *nlh;
17         struct nl_cache *link_cache;
18         int err = -1, ifindex;
19
20         if (nltool_init(argc, argv) < 0)
21                 return -1;
22
23         if (argc < 2 || !strcmp(argv[1], "-h")) {
24                 printf("Usage: nl-link-name2ifindex <name>\n");
25                 return -1;
26         }
27
28         nlh = nl_handle_alloc_nondefault(nltool_cbset);
29         if (!nlh)
30                 return -1;
31
32         if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
33                 goto errout;
34
35         link_cache = nltool_alloc_link_cache(nlh);
36         if (!link_cache)
37                 goto errout;
38
39         if ((ifindex = rtnl_link_name2i(link_cache, argv[1])) == RTNL_LINK_NOT_FOUND)
40                 fprintf(stderr, "Interface %s does not exist\n", argv[1]);
41         else
42                 printf("%d\n", ifindex);
43
44         nl_cache_free(link_cache);
45         err = 0;
46 errout:
47         nl_close(nlh);
48         nl_handle_destroy(nlh);
49
50         return err;
51 }