2
* src/nl-addr-delete.c Delete addresses
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License as
6
* published by the Free Software Foundation version 2 of the License.
8
* Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
11
#include <netlink/cli/utils.h>
12
#include <netlink/cli/addr.h>
13
#include <netlink/cli/link.h>
15
static struct nl_sock *sock;
16
static int interactive = 0, default_yes = 0, quiet = 0;
17
static int deleted = 0;
19
static void print_usage(void)
22
"Usage: nl-addr-delete [OPTION]... [ADDRESS]\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"
32
" -a, --local=ADDR Local address.\n"
33
" -d, --dev=DEV Associated network device.\n"
34
" --family=FAMILY Family of local address.\n"
35
" --label=STRING Address label (IPv4).\n"
36
" --peer=ADDR Peer address (IPv4).\n"
37
" --scope=SCOPE Address scope (IPv4).\n"
38
" --broadcast=ADDR Broadcast address of network (IPv4).\n"
39
" --valid-lifetime=TS Valid lifetime before route expires (IPv6).\n"
40
" --preferred=TIME Preferred lifetime (IPv6).\n"
41
" --valid=TIME Valid lifetime (IPv6).\n"
47
static void delete_cb(struct nl_object *obj, void *arg)
49
struct rtnl_addr *addr = nl_object_priv(obj);
50
struct nl_dump_params params = {
51
.dp_type = NL_DUMP_LINE,
56
if (interactive && !nl_cli_confirm(obj, ¶ms, default_yes))
59
if ((err = rtnl_addr_delete(sock, addr, 0)) < 0)
60
nl_cli_fatal(err, "Unable to delete address: %s\n",
65
nl_object_dump(obj, ¶ms);
71
int main(int argc, char *argv[])
73
struct rtnl_addr *addr;
74
struct nl_cache *link_cache, *addr_cache;
76
sock = nl_cli_alloc_socket();
77
nl_cli_connect(sock, NETLINK_ROUTE);
78
link_cache = nl_cli_link_alloc_cache(sock);
79
addr_cache = nl_cli_addr_alloc_cache(sock);
80
addr = nl_cli_addr_alloc();
94
static struct option long_opts[] = {
95
{ "interactive", 0, 0, 'i' },
96
{ "yes", 0, 0, ARG_YES },
97
{ "quiet", 0, 0, 'q' },
98
{ "help", 0, 0, 'h' },
99
{ "version", 0, 0, 'v' },
100
{ "local", 1, 0, 'a' },
101
{ "dev", 1, 0, 'd' },
102
{ "family", 1, 0, ARG_FAMILY },
103
{ "label", 1, 0, ARG_LABEL },
104
{ "peer", 1, 0, ARG_PEER },
105
{ "scope", 1, 0, ARG_SCOPE },
106
{ "broadcast", 1, 0, ARG_BROADCAST },
107
{ "preferred", 1, 0, ARG_PREFERRED },
108
{ "valid", 1, 0, ARG_VALID },
112
c = getopt_long(argc, argv, "iqhva:d:", long_opts, &optidx);
117
case 'i': interactive = 1; break;
118
case ARG_YES: default_yes = 1; break;
119
case 'q': quiet = 1; break;
120
case 'h': print_usage(); break;
121
case 'v': nl_cli_print_version(); break;
122
case 'a': nl_cli_addr_parse_local(addr, optarg); break;
123
case 'd': nl_cli_addr_parse_dev(addr, link_cache, optarg); break;
124
case ARG_FAMILY: nl_cli_addr_parse_family(addr, optarg); break;
125
case ARG_LABEL: nl_cli_addr_parse_label(addr, optarg); break;
126
case ARG_PEER: nl_cli_addr_parse_peer(addr, optarg); break;
127
case ARG_SCOPE: nl_cli_addr_parse_scope(addr, optarg); break;
128
case ARG_BROADCAST: nl_cli_addr_parse_broadcast(addr, optarg); break;
129
case ARG_PREFERRED: nl_cli_addr_parse_preferred(addr, optarg); break;
130
case ARG_VALID: nl_cli_addr_parse_valid(addr, optarg); break;
134
nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr), delete_cb, NULL);
137
printf("Deleted %d addresses\n", deleted);