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