|
Packit Service |
f991dd |
/* SPDX-License-Identifier: LGPL-2.1-only */
|
|
Packit Service |
f991dd |
/*
|
|
Packit Service |
f991dd |
* src/nl-addr-list.c List 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 void print_usage(void)
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
printf(
|
|
Packit Service |
f991dd |
"Usage: nl-addr-list [OPTION]... [ADDRESS]\n"
|
|
Packit Service |
f991dd |
"\n"
|
|
Packit Service |
f991dd |
"Options\n"
|
|
Packit Service |
f991dd |
" --details Show details on multiple lines.\n"
|
|
Packit Service |
f991dd |
" --env Print address details in sh env variable syntax.\n"
|
|
Packit Service |
f991dd |
" --prefix=STRING Prefix each printed line.\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 Selection\n"
|
|
Packit Service |
f991dd |
" -a, --local=ADDR Local address.\n"
|
|
Packit Service |
f991dd |
" -d, --dev=DEV Associated network device.\n"
|
|
Packit Service |
f991dd |
" --family=FAMILY Family of local address.\n"
|
|
Packit Service |
f991dd |
" --label=STRING Address label (IPv4).\n"
|
|
Packit Service |
f991dd |
" --peer=ADDR Peer address (IPv4).\n"
|
|
Packit Service |
f991dd |
" --scope=SCOPE Address scope (IPv4).\n"
|
|
Packit Service |
f991dd |
" --broadcast=ADDR Broadcast address of network (IPv4).\n"
|
|
Packit Service |
f991dd |
" --valid-lifetime=TS Valid lifetime before route expires (IPv6).\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 |
exit(0);
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static char *prefix;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static void print_prefix(struct nl_dump_params *p, int line)
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
if (prefix)
|
|
Packit Service |
f991dd |
nl_dump(p, "%s", prefix);
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static void env_dump(struct nl_object *obj, void *arg)
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
struct nl_dump_params *p = arg;
|
|
Packit Service |
f991dd |
struct rtnl_addr *addr = (struct rtnl_addr *) obj;
|
|
Packit Service |
f991dd |
struct nl_cache *link_cache;
|
|
Packit Service |
f991dd |
struct nl_addr *a;
|
|
Packit Service |
f991dd |
static int index = 0;
|
|
Packit Service |
f991dd |
char buf[128], pfx[32], *s;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
snprintf(pfx, sizeof(pfx), "ADDR%d", index++);
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
nl_dump_line(p, "%s_FAMILY=%s\n", pfx,
|
|
Packit Service |
f991dd |
nl_af2str(rtnl_addr_get_family(addr), buf, sizeof(buf)));
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
nl_dump_line(p, "%s_LOCAL=%s\n", pfx,
|
|
Packit Service |
f991dd |
nl_addr2str(rtnl_addr_get_local(addr), buf, sizeof(buf)));
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
nl_dump_line(p, "%s_IFINDEX=%u\n", pfx, rtnl_addr_get_ifindex(addr));
|
|
Packit Service |
f991dd |
link_cache = nl_cache_mngt_require_safe("route/link");
|
|
Packit Service |
f991dd |
if (link_cache)
|
|
Packit Service |
f991dd |
nl_dump_line(p, "%s_IFNAME=%s\n", pfx,
|
|
Packit Service |
f991dd |
rtnl_link_i2name(link_cache,
|
|
Packit Service |
f991dd |
rtnl_addr_get_ifindex(addr),
|
|
Packit Service |
f991dd |
buf, sizeof(buf)));
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if ((a = rtnl_addr_get_peer(addr)))
|
|
Packit Service |
f991dd |
nl_dump_line(p, "%s_PEER=%s\n", pfx,
|
|
Packit Service |
f991dd |
nl_addr2str(a, buf, sizeof(buf)));
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if ((a = rtnl_addr_get_broadcast(addr)))
|
|
Packit Service |
f991dd |
nl_dump_line(p, "%s_BROADCAST=%s\n", pfx,
|
|
Packit Service |
f991dd |
nl_addr2str(a, buf, sizeof(buf)));
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
nl_dump_line(p, "%s_SCOPE=%s\n", pfx,
|
|
Packit Service |
f991dd |
rtnl_scope2str(rtnl_addr_get_scope(addr),
|
|
Packit Service |
f991dd |
buf, sizeof(buf)));
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if ((s = rtnl_addr_get_label(addr)))
|
|
Packit Service |
f991dd |
nl_dump_line(p, "%s_LABEL=%s\n", pfx, s);
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
rtnl_addr_flags2str(rtnl_addr_get_flags(addr), buf, sizeof(buf));
|
|
Packit Service |
f991dd |
if (buf[0])
|
|
Packit Service |
f991dd |
nl_dump_line(p, "%s_FLAGS=%s\n", pfx, buf);
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
nl_dump_line(p, "%s_CACHEINFO_VALID=%u\n", pfx,
|
|
Packit Service |
f991dd |
rtnl_addr_get_valid_lifetime(addr));
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if (link_cache)
|
|
Packit Service |
f991dd |
nl_cache_put(link_cache);
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
#if 0
|
|
Packit Service |
f991dd |
if (addr->ce_mask & ADDR_ATTR_CACHEINFO) {
|
|
Packit Service |
f991dd |
struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
nl_dump_line(p, "ADDR_CACHEINFO_PREFERRED=%u\n",
|
|
Packit Service |
f991dd |
ci->aci_prefered);
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
nl_dump_line(p, "ADDR_CACHEINFO_CREATED=%u\n", ci->aci_cstamp);
|
|
Packit Service |
f991dd |
nl_dump_line(p, "ADDR_CACHEINFO_LASTUPDATE=%u\n",
|
|
Packit Service |
f991dd |
ci->aci_tstamp);
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
#endif
|
|
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, *addr_cache;
|
|
Packit Service |
f991dd |
struct nl_dump_params params = {
|
|
Packit Service |
f991dd |
.dp_type = NL_DUMP_LINE,
|
|
Packit Service |
f991dd |
.dp_nl_cb = print_prefix,
|
|
Packit Service |
f991dd |
.dp_fd = stdout,
|
|
Packit Service |
f991dd |
};
|
|
Packit Service |
f991dd |
int dump_env = 0;
|
|
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_cache = nl_cli_addr_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_DETAILS,
|
|
Packit Service |
f991dd |
ARG_ENV,
|
|
Packit Service |
f991dd |
ARG_PREFIX,
|
|
Packit Service |
f991dd |
ARG_PREFERRED,
|
|
Packit Service |
f991dd |
ARG_VALID,
|
|
Packit Service |
f991dd |
};
|
|
Packit Service |
f991dd |
static struct option long_opts[] = {
|
|
Packit Service |
f991dd |
{ "details", 0, 0, ARG_DETAILS },
|
|
Packit Service |
f991dd |
{ "env", 0, 0, ARG_ENV },
|
|
Packit Service |
f991dd |
{ "prefix", 1, 0, ARG_PREFIX },
|
|
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, "46hva: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 '4': rtnl_addr_set_family(addr, AF_INET); break;
|
|
Packit Service |
f991dd |
case '6': rtnl_addr_set_family(addr, AF_INET6); break;
|
|
Packit Service |
f991dd |
case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; break;
|
|
Packit Service |
f991dd |
case ARG_ENV: dump_env = 1; break;
|
|
Packit Service |
f991dd |
case ARG_PREFIX: prefix = strdup(optarg); 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 (dump_env)
|
|
Packit Service |
f991dd |
nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr), env_dump,
|
|
Packit Service |
f991dd |
¶ms);
|
|
Packit Service |
f991dd |
else
|
|
Packit Service |
f991dd |
nl_cache_dump_filter(addr_cache, ¶ms, OBJ_CAST(addr));
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
return 0;
|
|
Packit Service |
f991dd |
}
|