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

libnl 3.12.0

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