libnl  3.5.0
nl-rule-list.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * src/nl-rule-dump.c Dump rule attributes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation version 2.1
8  * of the License.
9  *
10  * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
11  */
12 
13 #include <netlink/cli/utils.h>
14 #include <netlink/cli/rule.h>
15 #include <netlink/cli/link.h>
16 
17 #include <linux/netlink.h>
18 
19 static void print_usage(void)
20 {
21  printf(
22  "Usage: nl-rule-list [OPTION]... [ROUTE]\n"
23  "\n"
24  "Options\n"
25  " -c, --cache List the contents of the route cache\n"
26  " -f, --format=TYPE Output format { brief | details | stats }\n"
27  " -h, --help Show this help\n"
28  " -v, --version Show versioning information\n"
29  "\n"
30  "Rule Options\n"
31  " --family Address family\n"
32  );
33  exit(0);
34 }
35 
36 int main(int argc, char *argv[])
37 {
38  struct nl_sock *sock;
39  struct rtnl_rule *rule;
40  struct nl_cache *rule_cache;
41  struct nl_dump_params params = {
42  .dp_fd = stdout,
43  .dp_type = NL_DUMP_LINE,
44  };
45 
46  sock = nl_cli_alloc_socket();
47  nl_cli_connect(sock, NETLINK_ROUTE);
48  nl_cli_link_alloc_cache(sock);
49  rule_cache = nl_cli_rule_alloc_cache(sock);
50  rule = nl_cli_rule_alloc();
51 
52  for (;;) {
53  int c, optidx = 0;
54  enum {
55  ARG_FAMILY = 257,
56  };
57  static struct option long_opts[] = {
58  { "format", 1, 0, 'f' },
59  { "help", 0, 0, 'h' },
60  { "version", 0, 0, 'v' },
61  { "family", 1, 0, ARG_FAMILY },
62  { 0, 0, 0, 0 }
63  };
64 
65  c = getopt_long(argc, argv, "f:hv", long_opts, &optidx);
66  if (c == -1)
67  break;
68 
69  switch (c) {
70  case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
71  case 'h': print_usage(); break;
72  case 'v': nl_cli_print_version(); break;
73  case ARG_FAMILY: nl_cli_rule_parse_family(rule, optarg); break;
74  }
75  }
76 
77  nl_cache_dump_filter(rule_cache, &params, OBJ_CAST(rule));
78 
79  return 0;
80 }
Dump object briefly on one line.
Definition: types.h:22
FILE * dp_fd
File descriptor the dumping output should go to.
Definition: types.h:83
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:1217
Dumping parameters.
Definition: types.h:33