libnl  3.7.0
nl-neigh-delete.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
4  */
5 
6 #include <netlink/cli/utils.h>
7 #include <netlink/cli/neigh.h>
8 #include <netlink/cli/link.h>
9 
10 #include <linux/netlink.h>
11 
12 static int quiet = 0, default_yes = 0, deleted = 0, interactive = 0;
13 static struct nl_sock *sock;
14 
15 static void print_usage(void)
16 {
17  printf(
18  "Usage: nl-neigh-delete [OPTION]... [NEIGHBOUR]\n"
19  "\n"
20  "Options\n"
21  " -i, --interactive Run interactively\n"
22  " --yes Set default answer to yes\n"
23  " -q, --quiet Do not print informal notifications\n"
24  " -h, --help Show this help\n"
25  " -v, --version Show versioning information\n"
26  "\n"
27  "Neighbour Options\n"
28  " -a, --addr=ADDR Destination address of neighbour\n"
29  " -l, --lladdr=ADDR Link layer address of neighbour\n"
30  " -d, --dev=DEV Device the neighbour is connected to\n"
31  " --family=FAMILY Destination address family\n"
32  " --state=STATE Neighbour state, (default = permanent)\n"
33  );
34 
35  exit(0);
36 }
37 
38 static void delete_cb(struct nl_object *obj, void *arg)
39 {
40  struct rtnl_neigh *neigh = nl_object_priv(obj);
41  struct nl_dump_params params = {
43  .dp_fd = stdout,
44  };
45  int err;
46 
47  if (interactive && !nl_cli_confirm(obj, &params, default_yes))
48  return;
49 
50  if ((err = rtnl_neigh_delete(sock, neigh, 0)) < 0)
51  nl_cli_fatal(err, "Unable to delete neighbour: %s\n",
52  nl_geterror(err));
53 
54  if (!quiet) {
55  printf("Deleted ");
56  nl_object_dump(obj, &params);
57  }
58 
59  deleted++;
60 }
61 
62 int main(int argc, char *argv[])
63 {
64  struct rtnl_neigh *neigh;
65  struct nl_cache *link_cache, *neigh_cache;
66 
67  sock = nl_cli_alloc_socket();
68  nl_cli_connect(sock, NETLINK_ROUTE);
69  link_cache = nl_cli_link_alloc_cache(sock);
70  neigh_cache = nl_cli_neigh_alloc_cache(sock);
71  neigh = nl_cli_neigh_alloc();
72 
73  for (;;) {
74  int c, optidx = 0;
75  enum {
76  ARG_FAMILY = 257,
77  ARG_STATE = 258,
78  ARG_YES,
79  };
80  static struct option long_opts[] = {
81  { "interactive", 0, 0, 'i' },
82  { "yes", 0, 0, ARG_YES },
83  { "quiet", 0, 0, 'q' },
84  { "help", 0, 0, 'h' },
85  { "version", 0, 0, 'v' },
86  { "addr", 1, 0, 'a' },
87  { "lladdr", 1, 0, 'l' },
88  { "dev", 1, 0, 'd' },
89  { "family", 1, 0, ARG_FAMILY },
90  { "state", 1, 0, ARG_STATE },
91  { 0, 0, 0, 0 }
92  };
93 
94  c = getopt_long(argc, argv, "qhva:l:d:", long_opts, &optidx);
95  if (c == -1)
96  break;
97 
98  switch (c) {
99  case 'i': interactive = 1; break;
100  case ARG_YES: default_yes = 1; break;
101  case 'q': quiet = 1; break;
102  case 'h': print_usage(); break;
103  case 'v': nl_cli_print_version(); break;
104  case 'a': nl_cli_neigh_parse_dst(neigh, optarg); break;
105  case 'l': nl_cli_neigh_parse_lladdr(neigh, optarg); break;
106  case 'd': nl_cli_neigh_parse_dev(neigh, link_cache, optarg); break;
107  case ARG_FAMILY: nl_cli_neigh_parse_family(neigh, optarg); break;
108  case ARG_STATE: nl_cli_neigh_parse_state(neigh, optarg); break;
109  }
110  }
111 
112  nl_cache_foreach_filter(neigh_cache, OBJ_CAST(neigh), delete_cb, NULL);
113 
114  if (!quiet)
115  printf("Deleted %d neighbours\n", deleted);
116 
117  return 0;
118 }
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:1294
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
int rtnl_neigh_delete(struct nl_sock *sk, struct rtnl_neigh *neigh, int flags)
Delete a neighbour.
Definition: neigh.c:831
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:287
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
Dumping parameters.
Definition: types.h:28
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:32