libnl: /data/src/libnl3/src/nl-addr-delete.c Source File

libnl 3.10.0

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