libnl 3.7.0
nl-addr-add.c
1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
4 */
5
6#include <netlink/cli/utils.h>
7#include <netlink/cli/addr.h>
8#include <netlink/cli/link.h>
9
10#include <linux/netlink.h>
11
12static int quiet = 0;
13
14static void print_usage(void)
15{
16 printf(
17"Usage: nl-addr-add [OPTION]... [ADDRESS]\n"
18"\n"
19"Options\n"
20" --replace Replace the address if it exists.\n"
21" -q, --quiet Do not print informal notifications.\n"
22" -h, --help Show this help.\n"
23" -v, --version Show versioning information.\n"
24"\n"
25"Address Options\n"
26" -a, --local=ADDR Address to be considered local.\n"
27" -d, --dev=DEV Device the address should be assigned to.\n"
28" --family=FAMILY Address family (normally autodetected).\n"
29" --broadcast=ADDR Broadcast address of network (IPv4).\n"
30" --peer=ADDR Peer address (IPv4).\n"
31" --label=STRING Additional address label (IPv4).\n"
32" --scope=SCOPE Scope of local address (IPv4).\n"
33" --preferred=TIME Preferred lifetime (IPv6).\n"
34" --valid=TIME Valid lifetime (IPv6).\n"
35 );
36
37 exit(0);
38}
39
40int main(int argc, char *argv[])
41{
42 struct nl_sock *sock;
43 struct rtnl_addr *addr;
44 struct nl_cache *link_cache;
45 struct nl_dump_params dp = {
47 .dp_fd = stdout,
48 };
49 int err, nlflags = NLM_F_CREATE;
50
51 sock = nl_cli_alloc_socket();
52 nl_cli_connect(sock, NETLINK_ROUTE);
53 link_cache = nl_cli_link_alloc_cache(sock);
54 addr = nl_cli_addr_alloc();
55
56 for (;;) {
57 int c, optidx = 0;
58 enum {
59 ARG_FAMILY = 257,
60 ARG_LABEL = 258,
61 ARG_PEER,
62 ARG_SCOPE,
63 ARG_BROADCAST,
64 ARG_REPLACE,
65 ARG_PREFERRED,
66 ARG_VALID,
67 };
68 static struct option long_opts[] = {
69 { "replace", 0, 0, ARG_REPLACE },
70 { "quiet", 0, 0, 'q' },
71 { "help", 0, 0, 'h' },
72 { "version", 0, 0, 'v' },
73 { "local", 1, 0, 'a' },
74 { "dev", 1, 0, 'd' },
75 { "family", 1, 0, ARG_FAMILY },
76 { "label", 1, 0, ARG_LABEL },
77 { "peer", 1, 0, ARG_PEER },
78 { "scope", 1, 0, ARG_SCOPE },
79 { "broadcast", 1, 0, ARG_BROADCAST },
80 { "preferred", 1, 0, ARG_PREFERRED },
81 { "valid", 1, 0, ARG_VALID },
82 { 0, 0, 0, 0 }
83 };
84
85 c = getopt_long(argc, argv, "qhva:d:", long_opts, &optidx);
86 if (c == -1)
87 break;
88
89 switch (c) {
90 case '?': exit(NLE_INVAL);
91 case ARG_REPLACE: nlflags |= NLM_F_REPLACE; break;
92 case 'q': quiet = 1; break;
93 case 'h': print_usage(); break;
94 case 'v': nl_cli_print_version(); break;
95 case 'a': nl_cli_addr_parse_local(addr, optarg); break;
96 case 'd': nl_cli_addr_parse_dev(addr, link_cache, optarg); break;
97 case ARG_FAMILY: nl_cli_addr_parse_family(addr, optarg); break;
98 case ARG_LABEL: nl_cli_addr_parse_label(addr, optarg); break;
99 case ARG_PEER: nl_cli_addr_parse_peer(addr, optarg); break;
100 case ARG_SCOPE: nl_cli_addr_parse_scope(addr, optarg); break;
101 case ARG_BROADCAST: nl_cli_addr_parse_broadcast(addr, optarg); break;
102 case ARG_PREFERRED: nl_cli_addr_parse_preferred(addr, optarg); break;
103 case ARG_VALID: nl_cli_addr_parse_valid(addr, optarg); break;
104 }
105 }
106
107 if ((err = rtnl_addr_add(sock, addr, nlflags)) < 0)
108 nl_cli_fatal(err, "Unable to add address: %s",
109 nl_geterror(err));
110
111 if (!quiet) {
112 printf("Added ");
113 nl_object_dump(OBJ_CAST(addr), &dp);
114 }
115
116 return 0;
117}
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:287
int rtnl_addr_add(struct nl_sock *sk, struct rtnl_addr *addr, int flags)
Request addition of new address.
Definition: addr.c:748
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
Dumping parameters.
Definition: types.h:28
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:32