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