144d362409d5469aed47d19e7908d19bd194493aThomas Graf/*
244d362409d5469aed47d19e7908d19bd194493aThomas Graf * src/nl-addr-delete.c     Delete addresses
344d362409d5469aed47d19e7908d19bd194493aThomas Graf *
444d362409d5469aed47d19e7908d19bd194493aThomas Graf *	This library is free software; you can redistribute it and/or
55e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf *	modify it under the terms of the GNU General Public License as
65e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf *	published by the Free Software Foundation version 2 of the License.
744d362409d5469aed47d19e7908d19bd194493aThomas Graf *
88808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
944d362409d5469aed47d19e7908d19bd194493aThomas Graf */
1044d362409d5469aed47d19e7908d19bd194493aThomas Graf
118808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf#include <netlink/cli/utils.h>
128808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf#include <netlink/cli/addr.h>
138808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf#include <netlink/cli/link.h>
1444d362409d5469aed47d19e7908d19bd194493aThomas Graf
15b624b9ed936f4c23bc4f30d8dce34da49775dd15Thomas Grafstatic struct nl_sock *sock;
165e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Grafstatic int interactive = 0, default_yes = 0, quiet = 0;
175e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Grafstatic int deleted = 0;
185e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf
195e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Grafstatic void print_usage(void)
2044d362409d5469aed47d19e7908d19bd194493aThomas Graf{
215e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	printf(
22ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf"Usage: nl-addr-delete [OPTION]... [ADDRESS]\n"
23ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf"\n"
24ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf"Options\n"
25ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf" -i, --interactive         Run interactively.\n"
26ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf"     --yes                 Set default answer to yes.\n"
27ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf" -q, --quiet               Do not print informal notifications.\n"
28ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf" -h, --help                Show this help.\n"
29ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf" -v, --version             Show versioning information.\n"
30ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf"\n"
31ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf"Address Options\n"
32ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf" -a, --local=ADDR          Local address.\n"
33ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf" -d, --dev=DEV             Associated network device.\n"
34ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf"     --family=FAMILY       Family of local address.\n"
35ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf"     --label=STRING        Address label (IPv4).\n"
36ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf"     --peer=ADDR           Peer address (IPv4).\n"
37ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf"     --scope=SCOPE         Address scope (IPv4).\n"
38ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf"     --broadcast=ADDR      Broadcast address of network (IPv4).\n"
39ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf"     --valid-lifetime=TS   Valid lifetime before route expires (IPv6).\n"
40ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf"     --preferred=TIME      Preferred lifetime (IPv6).\n"
41ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf"     --valid=TIME          Valid lifetime (IPv6).\n"
425e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	);
4344d362409d5469aed47d19e7908d19bd194493aThomas Graf
445e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	exit(0);
455e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf}
465e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf
475e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Grafstatic void delete_cb(struct nl_object *obj, void *arg)
485e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf{
495e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	struct rtnl_addr *addr = nl_object_priv(obj);
505e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	struct nl_dump_params params = {
51d84430702496f617c01c5e2d27d0e82e02390bb7Thomas Graf		.dp_type = NL_DUMP_LINE,
525e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		.dp_fd = stdout,
535e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	};
545e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	int err;
555e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf
568808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	if (interactive && !nl_cli_confirm(obj, &params, default_yes))
575e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		return;
5844d362409d5469aed47d19e7908d19bd194493aThomas Graf
595e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	if ((err = rtnl_addr_delete(sock, addr, 0)) < 0)
608808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		nl_cli_fatal(err, "Unable to delete address: %s\n",
618808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			     nl_geterror(err));
6244d362409d5469aed47d19e7908d19bd194493aThomas Graf
635e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	if (!quiet) {
645e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		printf("Deleted ");
655e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		nl_object_dump(obj, &params);
665e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	}
675e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf
685e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	deleted++;
695e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf}
7044d362409d5469aed47d19e7908d19bd194493aThomas Graf
715e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Grafint main(int argc, char *argv[])
725e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf{
735e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	struct rtnl_addr *addr;
745e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	struct nl_cache *link_cache, *addr_cache;
7544d362409d5469aed47d19e7908d19bd194493aThomas Graf
768808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	sock = nl_cli_alloc_socket();
778808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	nl_cli_connect(sock, NETLINK_ROUTE);
788808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	link_cache = nl_cli_link_alloc_cache(sock);
798808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	addr_cache = nl_cli_addr_alloc_cache(sock);
808808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	addr = nl_cli_addr_alloc();
8144d362409d5469aed47d19e7908d19bd194493aThomas Graf
825e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	for (;;) {
835e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		int c, optidx = 0;
845e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		enum {
855e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			ARG_FAMILY = 257,
865e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			ARG_LABEL = 258,
875e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			ARG_YES,
885e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			ARG_PEER,
895e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			ARG_SCOPE,
905e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			ARG_BROADCAST,
91ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf			ARG_PREFERRED,
92ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf			ARG_VALID,
935e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		};
945e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		static struct option long_opts[] = {
955e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			{ "interactive", 0, 0, 'i' },
965e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			{ "yes", 0, 0, ARG_YES },
975e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			{ "quiet", 0, 0, 'q' },
985e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			{ "help", 0, 0, 'h' },
995e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			{ "version", 0, 0, 'v' },
1005e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			{ "local", 1, 0, 'a' },
1015e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			{ "dev", 1, 0, 'd' },
1025e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			{ "family", 1, 0, ARG_FAMILY },
1035e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			{ "label", 1, 0, ARG_LABEL },
1045e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			{ "peer", 1, 0, ARG_PEER },
1055e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			{ "scope", 1, 0, ARG_SCOPE },
1065e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			{ "broadcast", 1, 0, ARG_BROADCAST },
107ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf			{ "preferred", 1, 0, ARG_PREFERRED },
108ab578b9ca00b19ad6b66283ec08d004f9fc391d6Thomas Graf			{ "valid", 1, 0, ARG_VALID },
1095e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			{ 0, 0, 0, 0 }
1105e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		};
1115e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf
1125e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		c = getopt_long(argc, argv, "iqhva:d:", long_opts, &optidx);
1135e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		if (c == -1)
1145e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf			break;
11544d362409d5469aed47d19e7908d19bd194493aThomas Graf
1165e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		switch (c) {
1175e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		case 'i': interactive = 1; break;
1185e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		case ARG_YES: default_yes = 1; break;
1195e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		case 'q': quiet = 1; break;
1205e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		case 'h': print_usage(); break;
1218808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		case 'v': nl_cli_print_version(); break;
1228808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		case 'a': nl_cli_addr_parse_local(addr, optarg); break;
1238808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		case 'd': nl_cli_addr_parse_dev(addr, link_cache, optarg); break;
1248808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		case ARG_FAMILY: nl_cli_addr_parse_family(addr, optarg); break;
1258808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		case ARG_LABEL: nl_cli_addr_parse_label(addr, optarg); break;
1268808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		case ARG_PEER: nl_cli_addr_parse_peer(addr, optarg); break;
1278808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		case ARG_SCOPE: nl_cli_addr_parse_scope(addr, optarg); break;
1288808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		case ARG_BROADCAST: nl_cli_addr_parse_broadcast(addr, optarg); break;
1298808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		case ARG_PREFERRED: nl_cli_addr_parse_preferred(addr, optarg); break;
1308808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		case ARG_VALID: nl_cli_addr_parse_valid(addr, optarg); break;
1315e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		}
1325e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf 	}
13344d362409d5469aed47d19e7908d19bd194493aThomas Graf
1345e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr), delete_cb, NULL);
13544d362409d5469aed47d19e7908d19bd194493aThomas Graf
1365e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	if (!quiet)
1375e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf		printf("Deleted %d addresses\n", deleted);
13844d362409d5469aed47d19e7908d19bd194493aThomas Graf
1395e732ad075e54ceb9f9e94fadbcec2e83d971b52Thomas Graf	return 0;
14044d362409d5469aed47d19e7908d19bd194493aThomas Graf}
141