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