libnl  3.3.0
nl-route-delete.c
1 /*
2  * src/nl-route-delete.c Delete Routes
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 #include <linux/netlink.h>
17 
18 static int interactive = 0, default_yes = 0, quiet = 0;
19 static int deleted = 0;
20 static struct nl_sock *sock;
21 
22 static void print_version(void)
23 {
24  fprintf(stderr, "%s\n", LIBNL_STRING);
25  exit(0);
26 }
27 
28 static void print_usage(void)
29 {
30  printf(
31  "Usage: nl-route-delete [OPTION]... [ROUTE]\n"
32  "\n"
33  "Options\n"
34  " -i, --interactive Run interactively\n"
35  " --yes Set default answer to yes\n"
36  " -q, --quiet Do not print informal notifications\n"
37  " -h, --help Show this help\n"
38  " -v, --version Show versioning information\n"
39  "\n"
40  "Route Options\n"
41  " -d, --dst=ADDR destination prefix, e.g. 10.10.0.0/16\n"
42  " -n, --nexthop=NH nexthop configuration:\n"
43  " dev=DEV route via device\n"
44  " weight=WEIGHT weight of nexthop\n"
45  " flags=FLAGS\n"
46  " via=GATEWAY route via other node\n"
47  " realms=REALMS\n"
48  " e.g. dev=eth0,via=192.168.1.12\n"
49  " -t, --table=TABLE Routing table\n"
50  " --family=FAMILY Address family\n"
51  " --src=ADDR Source prefix\n"
52  " --iif=DEV Incomming interface\n"
53  " --pref-src=ADDR Preferred source address\n"
54  " --metrics=OPTS Metrics configurations\n"
55  " --priority=NUM Priotity\n"
56  " --scope=SCOPE Scope\n"
57  " --protocol=PROTO Protocol\n"
58  " --type=TYPE { unicast | local | broadcast | multicast }\n"
59  );
60  exit(0);
61 }
62 
63 static void delete_cb(struct nl_object *obj, void *arg)
64 {
65  struct rtnl_route *route = (struct rtnl_route *) obj;
66  struct nl_dump_params params = {
68  .dp_fd = stdout,
69  };
70  int err;
71 
72  if (interactive && !nl_cli_confirm(obj, &params, default_yes))
73  return;
74 
75  if ((err = rtnl_route_delete(sock, route, 0)) < 0)
76  nl_cli_fatal(err, "Unable to delete route: %s", nl_geterror(err));
77 
78  if (!quiet) {
79  printf("Deleted ");
80  nl_object_dump(obj, &params);
81  }
82 
83  deleted++;
84 }
85 
86 int main(int argc, char *argv[])
87 {
88  struct nl_cache *link_cache, *route_cache;
89  struct rtnl_route *route;
90  int nf = 0;
91 
92  sock = nl_cli_alloc_socket();
93  nl_cli_connect(sock, NETLINK_ROUTE);
94  link_cache = nl_cli_link_alloc_cache(sock);
95  route_cache = nl_cli_route_alloc_cache(sock, 0);
96  route = nl_cli_route_alloc();
97 
98  for (;;) {
99  int c, optidx = 0;
100  enum {
101  ARG_FAMILY = 257,
102  ARG_SRC = 258,
103  ARG_IIF,
104  ARG_PREF_SRC,
105  ARG_METRICS,
106  ARG_PRIORITY,
107  ARG_SCOPE,
108  ARG_PROTOCOL,
109  ARG_TYPE,
110  ARG_YES,
111  };
112  static struct option long_opts[] = {
113  { "interactive", 0, 0, 'i' },
114  { "yes", 0, 0, ARG_YES },
115  { "quiet", 0, 0, 'q' },
116  { "help", 0, 0, 'h' },
117  { "version", 0, 0, 'v' },
118  { "dst", 1, 0, 'd' },
119  { "nexthop", 1, 0, 'n' },
120  { "table", 1, 0, 't' },
121  { "family", 1, 0, ARG_FAMILY },
122  { "src", 1, 0, ARG_SRC },
123  { "iif", 1, 0, ARG_IIF },
124  { "pref-src", 1, 0, ARG_PREF_SRC },
125  { "metrics", 1, 0, ARG_METRICS },
126  { "priority", 1, 0, ARG_PRIORITY },
127  { "scope", 1, 0, ARG_SCOPE },
128  { "protocol", 1, 0, ARG_PROTOCOL },
129  { "type", 1, 0, ARG_TYPE },
130  { 0, 0, 0, 0 }
131  };
132 
133  c = getopt_long(argc, argv, "iqhvd:n:t:", long_opts, &optidx);
134  if (c == -1)
135  break;
136 
137  switch (c) {
138  case 'i': interactive = 1; break;
139  case ARG_YES: default_yes = 1; break;
140  case 'q': quiet = 1; break;
141  case 'h': print_usage(); break;
142  case 'v': print_version(); break;
143  case 'd': nf++; nl_cli_route_parse_dst(route, optarg); break;
144  case 'n': nf++; nl_cli_route_parse_nexthop(route, optarg, link_cache); break;
145  case 't': nf++; nl_cli_route_parse_table(route, optarg); break;
146  case ARG_FAMILY: nf++; nl_cli_route_parse_family(route, optarg); break;
147  case ARG_SRC: nf++; nl_cli_route_parse_src(route, optarg); break;
148  case ARG_IIF: nf++; nl_cli_route_parse_iif(route, optarg, link_cache); break;
149  case ARG_PREF_SRC: nf++; nl_cli_route_parse_pref_src(route, optarg); break;
150  case ARG_METRICS: nf++; nl_cli_route_parse_metric(route, optarg); break;
151  case ARG_PRIORITY: nf++; nl_cli_route_parse_prio(route, optarg); break;
152  case ARG_SCOPE: nf++; nl_cli_route_parse_scope(route, optarg); break;
153  case ARG_PROTOCOL: nf++; nl_cli_route_parse_protocol(route, optarg); break;
154  case ARG_TYPE: nf++; nl_cli_route_parse_type(route, optarg); break;
155  }
156  }
157 
158  if (nf == 0 && !interactive && !default_yes) {
159  fprintf(stderr, "You attempted to delete all routes in "
160  "non-interactive mode, aborting.\n");
161  exit(0);
162  }
163 
164  nl_cache_foreach_filter(route_cache, OBJ_CAST(route), delete_cb, NULL);
165 
166  if (!quiet)
167  printf("Deleted %d routes\n", deleted);
168 
169  return 0;
170 }
Dump object briefly on one line.
Definition: types.h:22
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:38
void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback on each element of the cache (filtered).
Definition: cache.c:1282
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:288
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
Dumping parameters.
Definition: types.h:33