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