1 /*
2 * src/nl-addr-delete.c Delete addresses
3 *
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.
7 *
8 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
9 */
10
11 #include <netlink/cli/utils.h>
12 #include <netlink/cli/addr.h>
13 #include <netlink/cli/link.h>
14
15 static struct nl_sock *sock;
16 static int interactive = 0, default_yes = 0, quiet = 0;
17 static int deleted = 0;
18
print_usage(void)19 static void print_usage(void)
20 {
21 printf(
22 "Usage: nl-addr-delete [OPTION]... [ADDRESS]\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 "Address Options\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"
42 );
43
44 exit(0);
45 }
46
delete_cb(struct nl_object * obj,void * arg)47 static void delete_cb(struct nl_object *obj, void *arg)
48 {
49 struct rtnl_addr *addr = nl_object_priv(obj);
50 struct nl_dump_params params = {
51 .dp_type = NL_DUMP_LINE,
52 .dp_fd = stdout,
53 };
54 int err;
55
56 if (interactive && !nl_cli_confirm(obj, ¶ms, default_yes))
57 return;
58
59 if ((err = rtnl_addr_delete(sock, addr, 0)) < 0)
60 nl_cli_fatal(err, "Unable to delete address: %s\n",
61 nl_geterror(err));
62
63 if (!quiet) {
64 printf("Deleted ");
65 nl_object_dump(obj, ¶ms);
66 }
67
68 deleted++;
69 }
70
main(int argc,char * argv[])71 int main(int argc, char *argv[])
72 {
73 struct rtnl_addr *addr;
74 struct nl_cache *link_cache, *addr_cache;
75
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();
81
82 for (;;) {
83 int c, optidx = 0;
84 enum {
85 ARG_FAMILY = 257,
86 ARG_LABEL = 258,
87 ARG_YES,
88 ARG_PEER,
89 ARG_SCOPE,
90 ARG_BROADCAST,
91 ARG_PREFERRED,
92 ARG_VALID,
93 };
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 },
109 { 0, 0, 0, 0 }
110 };
111
112 c = getopt_long(argc, argv, "iqhva:d:", long_opts, &optidx);
113 if (c == -1)
114 break;
115
116 switch (c) {
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;
131 }
132 }
133
134 nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr), delete_cb, NULL);
135
136 if (!quiet)
137 printf("Deleted %d addresses\n", deleted);
138
139 return 0;
140 }
141