summaryrefslogtreecommitdiffstats
path: root/src/nl-link-name2ifindex.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nl-link-name2ifindex.c')
-rw-r--r--src/nl-link-name2ifindex.c52
1 files changed, 21 insertions, 31 deletions
diff --git a/src/nl-link-name2ifindex.c b/src/nl-link-name2ifindex.c
index 993397d..b04af04 100644
--- a/src/nl-link-name2ifindex.c
+++ b/src/nl-link-name2ifindex.c
@@ -6,46 +6,36 @@
* License as published by the Free Software Foundation version 2.1
* of the License.
*
- * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
*/
-#include "utils.h"
+#include <netlink/cli/utils.h>
+#include <netlink/cli/link.h>
+
+static void print_usage(void)
+{
+ printf("Usage: nl-link-ifindex2name <ifindex>\n");
+ exit(0);
+}
int main(int argc, char *argv[])
{
- struct nl_handle *nlh;
+ struct nl_sock *sock;
struct nl_cache *link_cache;
- int err = -1, ifindex;
-
- if (nltool_init(argc, argv) < 0)
- return -1;
-
- if (argc < 2 || !strcmp(argv[1], "-h")) {
- printf("Usage: nl-link-name2ifindex <name>\n");
- return -1;
- }
-
- nlh = nltool_alloc_handle();
- if (!nlh)
- return -1;
+ uint32_t ifindex;
- if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
- goto errout;
+ if (argc < 2)
+ print_usage();
- link_cache = nltool_alloc_link_cache(nlh);
- if (!link_cache)
- goto errout;
+ sock = nl_cli_alloc_socket();
+ nl_cli_connect(sock, NETLINK_ROUTE);
+ link_cache = nl_cli_link_alloc_cache(sock);
- if ((ifindex = rtnl_link_name2i(link_cache, argv[1])) == RTNL_LINK_NOT_FOUND)
- fprintf(stderr, "Interface %s does not exist\n", argv[1]);
- else
- printf("%d\n", ifindex);
+ if (!(ifindex = rtnl_link_name2i(link_cache, argv[1])))
+ nl_cli_fatal(ENOENT, "Interface \"%s\" does not exist",
+ argv[1]);
- nl_cache_free(link_cache);
- err = 0;
-errout:
- nl_close(nlh);
- nl_handle_destroy(nlh);
+ printf("%u\n", ifindex);
- return err;
+ return 0;
}