~ubuntu-branches/ubuntu/trusty/libnl3/trusty

« back to all changes in this revision

Viewing changes to src/nl-link-ifindex2name.c

  • Committer: Bazaar Package Importer
  • Author(s): Heiko Stuebner
  • Date: 2011-05-21 19:25:13 UTC
  • Revision ID: james.westby@ubuntu.com-20110521192513-1ieyu9w9kym4bt16
Tags: upstream-3.0
ImportĀ upstreamĀ versionĀ 3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * src/nl-link-ifindex2name.c     Transform a interface index to its name
 
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-2009 Thomas Graf <tgraf@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-ifindex2name <ifindex>\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
        char name[IFNAMSIZ];
 
26
        uint32_t ifindex;
 
27
 
 
28
        if (argc < 2)
 
29
                print_usage();
 
30
 
 
31
        sock = nl_cli_alloc_socket();
 
32
        nl_cli_connect(sock, NETLINK_ROUTE);
 
33
        link_cache = nl_cli_link_alloc_cache(sock);
 
34
 
 
35
        ifindex = nl_cli_parse_u32(argv[1]);
 
36
 
 
37
        if (!rtnl_link_i2name(link_cache, ifindex, name, sizeof(name)))
 
38
                nl_cli_fatal(ENOENT, "Interface index %d does not exist",
 
39
                             ifindex);
 
40
 
 
41
        printf("%s\n", name);
 
42
 
 
43
        return 0;
 
44
}