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