libnl 3.11.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 "nl-default.h"
7
8#include <linux/netlink.h>
9
10#include <netlink/cli/utils.h>
11#include <netlink/cli/link.h>
12
13static void print_usage(void)
14{
15 printf("Usage: nl-link-name2ifindex <name>\n");
16 exit(0);
17}
18
19int main(int argc, char *argv[])
20{
21 struct nl_sock *sock;
22 struct nl_cache *link_cache;
23 uint32_t ifindex;
24
25 if (argc < 2)
26 print_usage();
27
28 sock = nl_cli_alloc_socket();
29 nl_cli_connect(sock, NETLINK_ROUTE);
30 link_cache = nl_cli_link_alloc_cache(sock);
31
32 if (!(ifindex = rtnl_link_name2i(link_cache, argv[1])))
33 nl_cli_fatal(ENOENT, "Interface \"%s\" does not exist",
34 argv[1]);
35
36 printf("%u\n", ifindex);
37
38 return 0;
39}
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition utils.c:71