libnl  3.2.24-rc1
nl-route-list.c
1 /*
2  * src/nl-route-list.c List route 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/route.h>
14 #include <netlink/cli/link.h>
15 
16 static void print_usage(void)
17 {
18  printf(
19  "Usage: nl-route-list [OPTION]... [ROUTE]\n"
20  "\n"
21  "Options\n"
22  " -c, --cache List the contents of the route cache\n"
23  " -f, --format=TYPE Output format { brief | details | stats }\n"
24  " -h, --help Show this help\n"
25  " -v, --version Show versioning information\n"
26  "\n"
27  "Route Options\n"
28  " -d, --dst=ADDR destination prefix, e.g. 10.10.0.0/16\n"
29  " -n, --nexthop=NH nexthop configuration:\n"
30  " dev=DEV route via device\n"
31  " weight=WEIGHT weight of nexthop\n"
32  " flags=FLAGS\n"
33  " via=GATEWAY route via other node\n"
34  " realms=REALMS\n"
35  " e.g. dev=eth0,via=192.168.1.12\n"
36  " -t, --table=TABLE Routing table\n"
37  " --family=FAMILY Address family\n"
38  " --src=ADDR Source prefix\n"
39  " --iif=DEV Incomming interface\n"
40  " --pref-src=ADDR Preferred source address\n"
41  " --metrics=OPTS Metrics configurations\n"
42  " --priority=NUM Priotity\n"
43  " --scope=SCOPE Scope\n"
44  " --protocol=PROTO Protocol\n"
45  " --type=TYPE { unicast | local | broadcast | multicast }\n"
46  );
47  exit(0);
48 }
49 
50 int main(int argc, char *argv[])
51 {
52  struct nl_sock *sock;
53  struct nl_cache *link_cache, *route_cache;
54  struct rtnl_route *route;
55  struct nl_dump_params params = {
56  .dp_fd = stdout,
57  .dp_type = NL_DUMP_LINE,
58  };
59  int print_cache = 0;
60 
61  sock = nl_cli_alloc_socket();
62  nl_cli_connect(sock, NETLINK_ROUTE);
63  link_cache = nl_cli_link_alloc_cache(sock);
64  route = nl_cli_route_alloc();
65 
66  for (;;) {
67  int c, optidx = 0;
68  enum {
69  ARG_FAMILY = 257,
70  ARG_SRC = 258,
71  ARG_IIF,
72  ARG_PREF_SRC,
73  ARG_METRICS,
74  ARG_PRIORITY,
75  ARG_SCOPE,
76  ARG_PROTOCOL,
77  ARG_TYPE,
78  };
79  static struct option long_opts[] = {
80  { "cache", 0, 0, 'c' },
81  { "format", 1, 0, 'f' },
82  { "help", 0, 0, 'h' },
83  { "version", 0, 0, 'v' },
84  { "dst", 1, 0, 'd' },
85  { "nexthop", 1, 0, 'n' },
86  { "table", 1, 0, 't' },
87  { "family", 1, 0, ARG_FAMILY },
88  { "src", 1, 0, ARG_SRC },
89  { "iif", 1, 0, ARG_IIF },
90  { "pref-src", 1, 0, ARG_PREF_SRC },
91  { "metrics", 1, 0, ARG_METRICS },
92  { "priority", 1, 0, ARG_PRIORITY },
93  { "scope", 1, 0, ARG_SCOPE },
94  { "protocol", 1, 0, ARG_PROTOCOL },
95  { "type", 1, 0, ARG_TYPE },
96  { 0, 0, 0, 0 }
97  };
98 
99  c = getopt_long(argc, argv, "cf:hvd:n:t:", long_opts, &optidx);
100  if (c == -1)
101  break;
102 
103  switch (c) {
104  case 'c': print_cache = 1; break;
105  case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
106  case 'h': print_usage(); break;
107  case 'v': nl_cli_print_version(); break;
108  case 'd': nl_cli_route_parse_dst(route, optarg); break;
109  case 'n': nl_cli_route_parse_nexthop(route, optarg, link_cache); break;
110  case 't': nl_cli_route_parse_table(route, optarg); break;
111  case ARG_FAMILY: nl_cli_route_parse_family(route, optarg); break;
112  case ARG_SRC: nl_cli_route_parse_src(route, optarg); break;
113  case ARG_IIF: nl_cli_route_parse_iif(route, optarg, link_cache); break;
114  case ARG_PREF_SRC: nl_cli_route_parse_pref_src(route, optarg); break;
115  case ARG_METRICS: nl_cli_route_parse_metric(route, optarg); break;
116  case ARG_PRIORITY: nl_cli_route_parse_prio(route, optarg); break;
117  case ARG_SCOPE: nl_cli_route_parse_scope(route, optarg); break;
118  case ARG_PROTOCOL: nl_cli_route_parse_protocol(route, optarg); break;
119  case ARG_TYPE: nl_cli_route_parse_type(route, optarg); break;
120  }
121  }
122 
123  route_cache = nl_cli_route_alloc_cache(sock,
124  print_cache ? ROUTE_CACHE_CONTENT : 0);
125 
126  nl_cache_dump_filter(route_cache, &params, OBJ_CAST(route));
127 
128  return 0;
129 }