|
Packit Service |
f991dd |
/* SPDX-License-Identifier: LGPL-2.1-only */
|
|
Packit Service |
f991dd |
/*
|
|
Packit Service |
f991dd |
* src/nl-addr-add.c Add addresses
|
|
Packit Service |
f991dd |
*
|
|
Packit Service |
f991dd |
* This library is free software; you can redistribute it and/or
|
|
Packit Service |
f991dd |
* modify it under the terms of the GNU General Public License as
|
|
Packit Service |
f991dd |
* published by the Free Software Foundation version 2 of the License.
|
|
Packit Service |
f991dd |
*
|
|
Packit Service |
f991dd |
* Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
|
|
Packit Service |
f991dd |
*/
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
#include <netlink/cli/utils.h>
|
|
Packit Service |
f991dd |
#include <netlink/cli/addr.h>
|
|
Packit Service |
f991dd |
#include <netlink/cli/link.h>
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
#include <linux/netlink.h>
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static int quiet = 0;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static void print_usage(void)
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
printf(
|
|
Packit Service |
f991dd |
"Usage: nl-addr-add [OPTION]... [ADDRESS]\n"
|
|
Packit Service |
f991dd |
"\n"
|
|
Packit Service |
f991dd |
"Options\n"
|
|
Packit Service |
f991dd |
" --replace Replace the address if it exists.\n"
|
|
Packit Service |
f991dd |
" -q, --quiet Do not print informal notifications.\n"
|
|
Packit Service |
f991dd |
" -h, --help Show this help.\n"
|
|
Packit Service |
f991dd |
" -v, --version Show versioning information.\n"
|
|
Packit Service |
f991dd |
"\n"
|
|
Packit Service |
f991dd |
"Address Options\n"
|
|
Packit Service |
f991dd |
" -a, --local=ADDR Address to be considered local.\n"
|
|
Packit Service |
f991dd |
" -d, --dev=DEV Device the address should be assigned to.\n"
|
|
Packit Service |
f991dd |
" --family=FAMILY Address family (normally autodetected).\n"
|
|
Packit Service |
f991dd |
" --broadcast=ADDR Broadcast address of network (IPv4).\n"
|
|
Packit Service |
f991dd |
" --peer=ADDR Peer address (IPv4).\n"
|
|
Packit Service |
f991dd |
" --label=STRING Additional address label (IPv4).\n"
|
|
Packit Service |
f991dd |
" --scope=SCOPE Scope of local address (IPv4).\n"
|
|
Packit Service |
f991dd |
" --preferred=TIME Preferred lifetime (IPv6).\n"
|
|
Packit Service |
f991dd |
" --valid=TIME Valid lifetime (IPv6).\n"
|
|
Packit Service |
f991dd |
);
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
exit(0);
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
int main(int argc, char *argv[])
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
struct nl_sock *sock;
|
|
Packit Service |
f991dd |
struct rtnl_addr *addr;
|
|
Packit Service |
f991dd |
struct nl_cache *link_cache;
|
|
Packit Service |
f991dd |
struct nl_dump_params dp = {
|
|
Packit Service |
f991dd |
.dp_type = NL_DUMP_LINE,
|
|
Packit Service |
f991dd |
.dp_fd = stdout,
|
|
Packit Service |
f991dd |
};
|
|
Packit Service |
f991dd |
int err, nlflags = NLM_F_CREATE;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
sock = nl_cli_alloc_socket();
|
|
Packit Service |
f991dd |
nl_cli_connect(sock, NETLINK_ROUTE);
|
|
Packit Service |
f991dd |
link_cache = nl_cli_link_alloc_cache(sock);
|
|
Packit Service |
f991dd |
addr = nl_cli_addr_alloc();
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
for (;;) {
|
|
Packit Service |
f991dd |
int c, optidx = 0;
|
|
Packit Service |
f991dd |
enum {
|
|
Packit Service |
f991dd |
ARG_FAMILY = 257,
|
|
Packit Service |
f991dd |
ARG_LABEL = 258,
|
|
Packit Service |
f991dd |
ARG_PEER,
|
|
Packit Service |
f991dd |
ARG_SCOPE,
|
|
Packit Service |
f991dd |
ARG_BROADCAST,
|
|
Packit Service |
f991dd |
ARG_REPLACE,
|
|
Packit Service |
f991dd |
ARG_PREFERRED,
|
|
Packit Service |
f991dd |
ARG_VALID,
|
|
Packit Service |
f991dd |
};
|
|
Packit Service |
f991dd |
static struct option long_opts[] = {
|
|
Packit Service |
f991dd |
{ "replace", 0, 0, ARG_REPLACE },
|
|
Packit Service |
f991dd |
{ "quiet", 0, 0, 'q' },
|
|
Packit Service |
f991dd |
{ "help", 0, 0, 'h' },
|
|
Packit Service |
f991dd |
{ "version", 0, 0, 'v' },
|
|
Packit Service |
f991dd |
{ "local", 1, 0, 'a' },
|
|
Packit Service |
f991dd |
{ "dev", 1, 0, 'd' },
|
|
Packit Service |
f991dd |
{ "family", 1, 0, ARG_FAMILY },
|
|
Packit Service |
f991dd |
{ "label", 1, 0, ARG_LABEL },
|
|
Packit Service |
f991dd |
{ "peer", 1, 0, ARG_PEER },
|
|
Packit Service |
f991dd |
{ "scope", 1, 0, ARG_SCOPE },
|
|
Packit Service |
f991dd |
{ "broadcast", 1, 0, ARG_BROADCAST },
|
|
Packit Service |
f991dd |
{ "preferred", 1, 0, ARG_PREFERRED },
|
|
Packit Service |
f991dd |
{ "valid", 1, 0, ARG_VALID },
|
|
Packit Service |
f991dd |
{ 0, 0, 0, 0 }
|
|
Packit Service |
f991dd |
};
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
c = getopt_long(argc, argv, "qhva:d:", long_opts, &optidx);
|
|
Packit Service |
f991dd |
if (c == -1)
|
|
Packit Service |
f991dd |
break;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
switch (c) {
|
|
Packit Service |
f991dd |
case '?': exit(NLE_INVAL);
|
|
Packit Service |
f991dd |
case ARG_REPLACE: nlflags |= NLM_F_REPLACE; break;
|
|
Packit Service |
f991dd |
case 'q': quiet = 1; break;
|
|
Packit Service |
f991dd |
case 'h': print_usage(); break;
|
|
Packit Service |
f991dd |
case 'v': nl_cli_print_version(); break;
|
|
Packit Service |
f991dd |
case 'a': nl_cli_addr_parse_local(addr, optarg); break;
|
|
Packit Service |
f991dd |
case 'd': nl_cli_addr_parse_dev(addr, link_cache, optarg); break;
|
|
Packit Service |
f991dd |
case ARG_FAMILY: nl_cli_addr_parse_family(addr, optarg); break;
|
|
Packit Service |
f991dd |
case ARG_LABEL: nl_cli_addr_parse_label(addr, optarg); break;
|
|
Packit Service |
f991dd |
case ARG_PEER: nl_cli_addr_parse_peer(addr, optarg); break;
|
|
Packit Service |
f991dd |
case ARG_SCOPE: nl_cli_addr_parse_scope(addr, optarg); break;
|
|
Packit Service |
f991dd |
case ARG_BROADCAST: nl_cli_addr_parse_broadcast(addr, optarg); break;
|
|
Packit Service |
f991dd |
case ARG_PREFERRED: nl_cli_addr_parse_preferred(addr, optarg); break;
|
|
Packit Service |
f991dd |
case ARG_VALID: nl_cli_addr_parse_valid(addr, optarg); break;
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if ((err = rtnl_addr_add(sock, addr, nlflags)) < 0)
|
|
Packit Service |
f991dd |
nl_cli_fatal(err, "Unable to add address: %s",
|
|
Packit Service |
f991dd |
nl_geterror(err));
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if (!quiet) {
|
|
Packit Service |
f991dd |
printf("Added ");
|
|
Packit Service |
f991dd |
nl_object_dump(OBJ_CAST(addr), &dp;;
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
return 0;
|
|
Packit Service |
f991dd |
}
|