libnl  3.3.0
nl-neigh-list.c
1 /*
2  * src/nl-neigh-list.c List Neighbours
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/neigh.h>
14 #include <netlink/cli/link.h>
15 
16 #include <linux/netlink.h>
17 
18 static void print_usage(void)
19 {
20  printf(
21  "Usage: nl-neigh-list [OPTION]... [NEIGHBOUR]\n"
22  "\n"
23  "Options\n"
24  " -f, --format=TYPE Output format { brief | details | stats }\n"
25  " -h, --help Show this help\n"
26  " -v, --version Show versioning information\n"
27  "\n"
28  "Neighbour Options\n"
29  " -a, --addr=ADDR Destination address of neighbour\n"
30  " -l, --lladdr=ADDR Link layer address of neighbour\n"
31  " -d, --dev=DEV Device the neighbour is connected to\n"
32  " --family=FAMILY Destination address family\n"
33  " --state=STATE Neighbour state, (default = permanent)\n"
34  );
35  exit(0);
36 }
37 
38 int main(int argc, char *argv[])
39 {
40  struct nl_sock *sock;
41  struct rtnl_neigh *neigh;
42  struct nl_cache *link_cache, *neigh_cache;
43  struct nl_dump_params params = {
45  .dp_fd = stdout,
46  };
47 
48  sock = nl_cli_alloc_socket();
49  nl_cli_connect(sock, NETLINK_ROUTE);
50  link_cache = nl_cli_link_alloc_cache_flags(sock, NL_CACHE_AF_ITER);
51  neigh_cache = nl_cli_neigh_alloc_cache(sock);
52  neigh = nl_cli_neigh_alloc();
53 
54  for (;;) {
55  int c, optidx = 0;
56  enum {
57  ARG_FAMILY = 257,
58  ARG_STATE = 258,
59  };
60  static struct option long_opts[] = {
61  { "format", 1, 0, 'f' },
62  { "help", 0, 0, 'h' },
63  { "version", 0, 0, 'v' },
64  { "addr", 1, 0, 'a' },
65  { "lladdr", 1, 0, 'l' },
66  { "dev", 1, 0, 'd' },
67  { "family", 1, 0, ARG_FAMILY },
68  { "state", 1, 0, ARG_STATE },
69  { 0, 0, 0, 0 }
70  };
71 
72  c = getopt_long(argc, argv, "f:hva:l:d:", long_opts, &optidx);
73  if (c == -1)
74  break;
75 
76  switch (c) {
77  case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
78  case 'h': print_usage(); break;
79  case 'v': nl_cli_print_version(); break;
80  case 'a': nl_cli_neigh_parse_dst(neigh, optarg); break;
81  case 'l': nl_cli_neigh_parse_lladdr(neigh, optarg); break;
82  case 'd': nl_cli_neigh_parse_dev(neigh, link_cache, optarg); break;
83  case ARG_FAMILY: nl_cli_neigh_parse_family(neigh, optarg); break;
84  case ARG_STATE: nl_cli_neigh_parse_state(neigh, optarg); break;
85  }
86  }
87 
88  nl_cache_dump_filter(neigh_cache, &params, OBJ_CAST(neigh));
89 
90  return 0;
91 }
Dump object briefly on one line.
Definition: types.h:22
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:38
void nl_cache_dump_filter(struct nl_cache *cache, struct nl_dump_params *params, struct nl_object *filter)
Dump all elements of a cache (filtered).
Definition: cache.c:1216
Dumping parameters.
Definition: types.h:33
#define NL_CACHE_AF_ITER
Explicitely iterate over all address families when updating the cache.
Definition: cache.h:45