libnl 3.5.0

nl-addr-add.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * src/nl-addr-add.c Add 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 int quiet = 0;
19
20 static void print_usage(void)
21 {
22 printf(
23 "Usage: nl-addr-add [OPTION]... [ADDRESS]\n"
24 "\n"
25 "Options\n"
26 " --replace Replace the address if it exists.\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 Address to be considered local.\n"
33 " -d, --dev=DEV Device the address should be assigned to.\n"
34 " --family=FAMILY Address family (normally autodetected).\n"
35 " --broadcast=ADDR Broadcast address of network (IPv4).\n"
36 " --peer=ADDR Peer address (IPv4).\n"
37 " --label=STRING Additional address label (IPv4).\n"
38 " --scope=SCOPE Scope of local address (IPv4).\n"
39 " --preferred=TIME Preferred lifetime (IPv6).\n"
40 " --valid=TIME Valid lifetime (IPv6).\n"
41 );
42
43 exit(0);
44 }
45
46 int main(int argc, char *argv[])
47 {
48 struct nl_sock *sock;
49 struct rtnl_addr *addr;
50 struct nl_cache *link_cache;
51 struct nl_dump_params dp = {
52 .dp_type = NL_DUMP_LINE,
53 .dp_fd = stdout,
54 };
55 int err, nlflags = NLM_F_CREATE;
56
57 sock = nl_cli_alloc_socket();
58 nl_cli_connect(sock, NETLINK_ROUTE);
59 link_cache = nl_cli_link_alloc_cache(sock);
60 addr = nl_cli_addr_alloc();
61
62 for (;;) {
63 int c, optidx = 0;
64 enum {
65 ARG_FAMILY = 257,
66 ARG_LABEL = 258,
67 ARG_PEER,
68 ARG_SCOPE,
69 ARG_BROADCAST,
70 ARG_REPLACE,
71 ARG_PREFERRED,
72 ARG_VALID,
73 };
74 static struct option long_opts[] = {
75 { "replace", 0, 0, ARG_REPLACE },
76 { "quiet", 0, 0, 'q' },
77 { "help", 0, 0, 'h' },
78 { "version", 0, 0, 'v' },
79 { "local", 1, 0, 'a' },
80 { "dev", 1, 0, 'd' },
81 { "family", 1, 0, ARG_FAMILY },
82 { "label", 1, 0, ARG_LABEL },
83 { "peer", 1, 0, ARG_PEER },
84 { "scope", 1, 0, ARG_SCOPE },
85 { "broadcast", 1, 0, ARG_BROADCAST },
86 { "preferred", 1, 0, ARG_PREFERRED },
87 { "valid", 1, 0, ARG_VALID },
88 { 0, 0, 0, 0 }
89 };
90
91 c = getopt_long(argc, argv, "qhva:d:", long_opts, &optidx);
92 if (c == -1)
93 break;
94
95 switch (c) {
96 case '?': exit(NLE_INVAL);
97 case ARG_REPLACE: nlflags |= NLM_F_REPLACE; break;
98 case 'q': quiet = 1; break;
99 case 'h': print_usage(); break;
100 case 'v': nl_cli_print_version(); break;
101 case 'a': nl_cli_addr_parse_local(addr, optarg); break;
102 case 'd': nl_cli_addr_parse_dev(addr, link_cache, optarg); break;
103 case ARG_FAMILY: nl_cli_addr_parse_family(addr, optarg); break;
104 case ARG_LABEL: nl_cli_addr_parse_label(addr, optarg); break;
105 case ARG_PEER: nl_cli_addr_parse_peer(addr, optarg); break;
106 case ARG_SCOPE: nl_cli_addr_parse_scope(addr, optarg); break;
107 case ARG_BROADCAST: nl_cli_addr_parse_broadcast(addr, optarg); break;
108 case ARG_PREFERRED: nl_cli_addr_parse_preferred(addr, optarg); break;
109 case ARG_VALID: nl_cli_addr_parse_valid(addr, optarg); break;
110 }
111 }
112
113 if ((err = rtnl_addr_add(sock, addr, nlflags)) < 0)
114 nl_cli_fatal(err, "Unable to add address: %s",
115 nl_geterror(err));
116
117 if (!quiet) {
118 printf("Added ");
119 nl_object_dump(OBJ_CAST(addr), &dp);
120 }
121
122 return 0;
123 }
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_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
rtnl_addr_add
int rtnl_addr_add(struct nl_sock *sk, struct rtnl_addr *addr, int flags)
Request addition of new address.
Definition: addr.c:748
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