libnl 3.5.0

nl-addr-delete.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * src/nl-addr-delete.c Delete addresses
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation version 2 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/addr.h>
14 #include <netlink/cli/link.h>
15
16 #include <linux/netlink.h>
17
18 static struct nl_sock *sock;
19 static int interactive = 0, default_yes = 0, quiet = 0;
20 static int deleted = 0;
21
22 static void print_usage(void)
23 {
24 printf(
25 "Usage: nl-addr-delete [OPTION]... [ADDRESS]\n"
26 "\n"
27 "Options\n"
28 " -i, --interactive Run interactively.\n"
29 " --yes Set default answer to yes.\n"
30 " -q, --quiet Do not print informal notifications.\n"
31 " -h, --help Show this help.\n"
32 " -v, --version Show versioning information.\n"
33 "\n"
34 "Address Options\n"
35 " -a, --local=ADDR Local address.\n"
36 " -d, --dev=DEV Associated network device.\n"
37 " --family=FAMILY Family of local address.\n"
38 " --label=STRING Address label (IPv4).\n"
39 " --peer=ADDR Peer address (IPv4).\n"
40 " --scope=SCOPE Address scope (IPv4).\n"
41 " --broadcast=ADDR Broadcast address of network (IPv4).\n"
42 " --valid-lifetime=TS Valid lifetime before route expires (IPv6).\n"
43 " --preferred=TIME Preferred lifetime (IPv6).\n"
44 " --valid=TIME Valid lifetime (IPv6).\n"
45 );
46
47 exit(0);
48 }
49
50 static void delete_cb(struct nl_object *obj, void *arg)
51 {
52 struct rtnl_addr *addr = nl_object_priv(obj);
53 struct nl_dump_params params = {
54 .dp_type = NL_DUMP_LINE,
55 .dp_fd = stdout,
56 };
57 int err;
58
59 if (interactive && !nl_cli_confirm(obj, &params, default_yes))
60 return;
61
62 if ((err = rtnl_addr_delete(sock, addr, 0)) < 0)
63 nl_cli_fatal(err, "Unable to delete address: %s\n",
64 nl_geterror(err));
65
66 if (!quiet) {
67 printf("Deleted ");
68 nl_object_dump(obj, &params);
69 }
70
71 deleted++;
72 }
73
74 int main(int argc, char *argv[])
75 {
76 struct rtnl_addr *addr;
77 struct nl_cache *link_cache, *addr_cache;
78
79 sock = nl_cli_alloc_socket();
80 nl_cli_connect(sock, NETLINK_ROUTE);
81 link_cache = nl_cli_link_alloc_cache(sock);
82 addr_cache = nl_cli_addr_alloc_cache(sock);
83 addr = nl_cli_addr_alloc();
84
85 for (;;) {
86 int c, optidx = 0;
87 enum {
88 ARG_FAMILY = 257,
89 ARG_LABEL = 258,
90 ARG_YES,
91 ARG_PEER,
92 ARG_SCOPE,
93 ARG_BROADCAST,
94 ARG_PREFERRED,
95 ARG_VALID,
96 };
97 static struct option long_opts[] = {
98 { "interactive", 0, 0, 'i' },
99 { "yes", 0, 0, ARG_YES },
100 { "quiet", 0, 0, 'q' },
101 { "help", 0, 0, 'h' },
102 { "version", 0, 0, 'v' },
103 { "local", 1, 0, 'a' },
104 { "dev", 1, 0, 'd' },
105 { "family", 1, 0, ARG_FAMILY },
106 { "label", 1, 0, ARG_LABEL },
107 { "peer", 1, 0, ARG_PEER },
108 { "scope", 1, 0, ARG_SCOPE },
109 { "broadcast", 1, 0, ARG_BROADCAST },
110 { "preferred", 1, 0, ARG_PREFERRED },
111 { "valid", 1, 0, ARG_VALID },
112 { 0, 0, 0, 0 }
113 };
114
115 c = getopt_long(argc, argv, "iqhva:d:", long_opts, &optidx);
116 if (c == -1)
117 break;
118
119 switch (c) {
120 case 'i': interactive = 1; break;
121 case ARG_YES: default_yes = 1; break;
122 case 'q': quiet = 1; break;
123 case 'h': print_usage(); break;
124 case 'v': nl_cli_print_version(); break;
125 case 'a': nl_cli_addr_parse_local(addr, optarg); break;
126 case 'd': nl_cli_addr_parse_dev(addr, link_cache, optarg); break;
127 case ARG_FAMILY: nl_cli_addr_parse_family(addr, optarg); break;
128 case ARG_LABEL: nl_cli_addr_parse_label(addr, optarg); break;
129 case ARG_PEER: nl_cli_addr_parse_peer(addr, optarg); break;
130 case ARG_SCOPE: nl_cli_addr_parse_scope(addr, optarg); break;
131 case ARG_BROADCAST: nl_cli_addr_parse_broadcast(addr, optarg); break;
132 case ARG_PREFERRED: nl_cli_addr_parse_preferred(addr, optarg); break;
133 case ARG_VALID: nl_cli_addr_parse_valid(addr, optarg); break;
134 }
135 }
136
137 nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr), delete_cb, NULL);
138
139 if (!quiet)
140 printf("Deleted %d addresses\n", deleted);
141
142 return 0;
143 }
NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:22
nl_dump_params::dp_type
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:38
nl_cache_foreach_filter
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:1283
rtnl_addr_delete
int rtnl_addr_delete(struct nl_sock *sk, struct rtnl_addr *addr, int flags)
Request deletion of an address.
Definition: addr.c:820
nl_object_dump
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:289
nl_cli_fatal
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:78
nl_dump_params
Dumping parameters.
Definition: types.h:33