libnl  3.6.0
nl-rule-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/rule.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-rule-list [OPTION]... [ROUTE]\n"
16  "\n"
17  "Options\n"
18  " -c, --cache List the contents of the route cache\n"
19  " -f, --format=TYPE Output format { brief | details | stats }\n"
20  " -h, --help Show this help\n"
21  " -v, --version Show versioning information\n"
22  "\n"
23  "Rule Options\n"
24  " --family Address family\n"
25  );
26  exit(0);
27 }
28 
29 int main(int argc, char *argv[])
30 {
31  struct nl_sock *sock;
32  struct rtnl_rule *rule;
33  struct nl_cache *rule_cache;
34  struct nl_dump_params params = {
35  .dp_fd = stdout,
36  .dp_type = NL_DUMP_LINE,
37  };
38 
39  sock = nl_cli_alloc_socket();
40  nl_cli_connect(sock, NETLINK_ROUTE);
41  nl_cli_link_alloc_cache(sock);
42  rule_cache = nl_cli_rule_alloc_cache(sock);
43  rule = nl_cli_rule_alloc();
44 
45  for (;;) {
46  int c, optidx = 0;
47  enum {
48  ARG_FAMILY = 257,
49  };
50  static struct option long_opts[] = {
51  { "format", 1, 0, 'f' },
52  { "help", 0, 0, 'h' },
53  { "version", 0, 0, 'v' },
54  { "family", 1, 0, ARG_FAMILY },
55  { 0, 0, 0, 0 }
56  };
57 
58  c = getopt_long(argc, argv, "f:hv", long_opts, &optidx);
59  if (c == -1)
60  break;
61 
62  switch (c) {
63  case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
64  case 'h': print_usage(); break;
65  case 'v': nl_cli_print_version(); break;
66  case ARG_FAMILY: nl_cli_rule_parse_family(rule, optarg); break;
67  }
68  }
69 
70  nl_cache_dump_filter(rule_cache, &params, OBJ_CAST(rule));
71 
72  return 0;
73 }
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
@ 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
FILE * dp_fd
File descriptor the dumping output should go to.
Definition: types.h:77