6#include <netlink/cli/utils.h>
7#include <netlink/cli/addr.h>
8#include <netlink/cli/link.h>
10#include <linux/netlink.h>
12static void print_usage(
void)
15"Usage: nl-addr-list [OPTION]... [ADDRESS]\n"
18" --details Show details on multiple lines.\n"
19" --env Print address details in sh env variable syntax.\n"
20" --prefix=STRING Prefix each printed line.\n"
21" -h, --help Show this help.\n"
22" -v, --version Show versioning information.\n"
25" -a, --local=ADDR Local address.\n"
26" -d, --dev=DEV Associated network device.\n"
27" --family=FAMILY Family of local address.\n"
28" --label=STRING Address label (IPv4).\n"
29" --peer=ADDR Peer address (IPv4).\n"
30" --scope=SCOPE Address scope (IPv4).\n"
31" --broadcast=ADDR Broadcast address of network (IPv4).\n"
32" --valid-lifetime=TS Valid lifetime before route expires (IPv6).\n"
33" --preferred=TIME Preferred lifetime (IPv6).\n"
34" --valid=TIME Valid lifetime (IPv6).\n"
47static void env_dump(
struct nl_object *obj,
void *arg)
50 struct rtnl_addr *addr = (
struct rtnl_addr *) obj;
51 struct nl_cache *link_cache;
54 char buf[128], pfx[32], *s;
56 snprintf(pfx,
sizeof(pfx),
"ADDR%d", index++);
58 nl_dump_line(p,
"%s_FAMILY=%s\n", pfx,
59 nl_af2str(rtnl_addr_get_family(addr), buf,
sizeof(buf)));
61 nl_dump_line(p,
"%s_LOCAL=%s\n", pfx,
62 nl_addr2str(rtnl_addr_get_local(addr), buf,
sizeof(buf)));
64 nl_dump_line(p,
"%s_IFINDEX=%u\n", pfx, rtnl_addr_get_ifindex(addr));
67 nl_dump_line(p,
"%s_IFNAME=%s\n", pfx,
69 rtnl_addr_get_ifindex(addr),
72 if ((a = rtnl_addr_get_peer(addr)))
73 nl_dump_line(p,
"%s_PEER=%s\n", pfx,
76 if ((a = rtnl_addr_get_broadcast(addr)))
77 nl_dump_line(p,
"%s_BROADCAST=%s\n", pfx,
80 nl_dump_line(p,
"%s_SCOPE=%s\n", pfx,
81 rtnl_scope2str(rtnl_addr_get_scope(addr),
84 if ((s = rtnl_addr_get_label(addr)))
85 nl_dump_line(p,
"%s_LABEL=%s\n", pfx, s);
87 rtnl_addr_flags2str(rtnl_addr_get_flags(addr), buf,
sizeof(buf));
89 nl_dump_line(p,
"%s_FLAGS=%s\n", pfx, buf);
91 nl_dump_line(p,
"%s_CACHEINFO_VALID=%u\n", pfx,
92 rtnl_addr_get_valid_lifetime(addr));
95 nl_cache_put(link_cache);
98 if (addr->ce_mask & ADDR_ATTR_CACHEINFO) {
99 struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo;
101 nl_dump_line(p,
"ADDR_CACHEINFO_PREFERRED=%u\n",
104 nl_dump_line(p,
"ADDR_CACHEINFO_CREATED=%u\n", ci->aci_cstamp);
105 nl_dump_line(p,
"ADDR_CACHEINFO_LASTUPDATE=%u\n",
111int main(
int argc,
char *argv[])
113 struct nl_sock *sock;
114 struct rtnl_addr *addr;
115 struct nl_cache *link_cache, *addr_cache;
118 .dp_nl_cb = print_prefix,
123 sock = nl_cli_alloc_socket();
124 nl_cli_connect(sock, NETLINK_ROUTE);
125 link_cache = nl_cli_link_alloc_cache(sock);
126 addr_cache = nl_cli_addr_alloc_cache(sock);
127 addr = nl_cli_addr_alloc();
143 static struct option long_opts[] = {
144 {
"details", 0, 0, ARG_DETAILS },
145 {
"env", 0, 0, ARG_ENV },
146 {
"prefix", 1, 0, ARG_PREFIX },
147 {
"help", 0, 0,
'h' },
148 {
"version", 0, 0,
'v' },
149 {
"local", 1, 0,
'a' },
150 {
"dev", 1, 0,
'd' },
151 {
"family", 1, 0, ARG_FAMILY },
152 {
"label", 1, 0, ARG_LABEL },
153 {
"peer", 1, 0, ARG_PEER },
154 {
"scope", 1, 0, ARG_SCOPE },
155 {
"broadcast", 1, 0, ARG_BROADCAST },
156 {
"preferred", 1, 0, ARG_PREFERRED },
157 {
"valid", 1, 0, ARG_VALID },
161 c = getopt_long(argc, argv,
"46hva:d:", long_opts, &optidx);
166 case '?': exit(NLE_INVAL);
167 case '4': rtnl_addr_set_family(addr, AF_INET);
break;
168 case '6': rtnl_addr_set_family(addr, AF_INET6);
break;
170 case ARG_ENV: dump_env = 1;
break;
171 case ARG_PREFIX: prefix = strdup(optarg);
break;
172 case 'h': print_usage();
break;
173 case 'v': nl_cli_print_version();
break;
174 case 'a': nl_cli_addr_parse_local(addr, optarg);
break;
175 case 'd': nl_cli_addr_parse_dev(addr, link_cache, optarg);
break;
176 case ARG_FAMILY: nl_cli_addr_parse_family(addr, optarg);
break;
177 case ARG_LABEL: nl_cli_addr_parse_label(addr, optarg);
break;
178 case ARG_PEER: nl_cli_addr_parse_peer(addr, optarg);
break;
179 case ARG_SCOPE: nl_cli_addr_parse_scope(addr, optarg);
break;
180 case ARG_BROADCAST: nl_cli_addr_parse_broadcast(addr, optarg);
break;
181 case ARG_PREFERRED: nl_cli_addr_parse_preferred(addr, optarg);
break;
182 case ARG_VALID: nl_cli_addr_parse_valid(addr, optarg);
break;
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
struct nl_cache * nl_cache_mngt_require_safe(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
void nl_cache_dump_filter(struct nl_cache *cache, struct nl_dump_params *params, struct nl_object *filter)
Dump all elements of a cache (filtered).
void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback on each element of the cache (filtered).
char * rtnl_link_i2name(struct nl_cache *cache, int ifindex, char *dst, size_t len)
Translate interface index to corresponding link name.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
@ NL_DUMP_LINE
Dump object briefly on one line.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
enum nl_dump_type dp_type
Specifies the type of dump that is requested.