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

libnl 3.10.0

nl-addr-list.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 void print_usage(void)
15{
16 printf(
17"Usage: nl-addr-list [OPTION]... [ADDRESS]\n"
18"\n"
19"Options\n"
20" --details Show details on multiple lines.\n"
21" --env Print address details in sh env variable syntax.\n"
22" --prefix=STRING Prefix each printed line.\n"
23" -h, --help Show this help.\n"
24" -v, --version Show versioning information.\n"
25"\n"
26"Address Selection\n"
27" -a, --local=ADDR Local address.\n"
28" -d, --dev=DEV Associated network device.\n"
29" --family=FAMILY Family of local address.\n"
30" --label=STRING Address label (IPv4).\n"
31" --peer=ADDR Peer address (IPv4).\n"
32" --scope=SCOPE Address scope (IPv4).\n"
33" --broadcast=ADDR Broadcast address of network (IPv4).\n"
34" --valid-lifetime=TS Valid lifetime before route expires (IPv6).\n"
35" --preferred=TIME Preferred lifetime (IPv6).\n"
36" --valid=TIME Valid lifetime (IPv6).\n"
37 );
38 exit(0);
39}
40
41static char *prefix;
42
43static void print_prefix(struct nl_dump_params *p, int line)
44{
45 if (prefix)
46 nl_dump(p, "%s", prefix);
47}
48
49static void env_dump(struct nl_object *obj, void *arg)
50{
51 struct nl_dump_params *p = arg;
52 struct rtnl_addr *addr = (struct rtnl_addr *) obj;
53 struct nl_cache *link_cache;
54 struct nl_addr *a;
55 static int index = 0;
56 char buf[128], pfx[32], *s;
57
58 snprintf(pfx, sizeof(pfx), "ADDR%d", index++);
59
60 nl_dump_line(p, "%s_FAMILY=%s\n", pfx,
61 nl_af2str(rtnl_addr_get_family(addr), buf, sizeof(buf)));
62
63 nl_dump_line(p, "%s_LOCAL=%s\n", pfx,
64 nl_addr2str(rtnl_addr_get_local(addr), buf, sizeof(buf)));
65
66 nl_dump_line(p, "%s_IFINDEX=%u\n", pfx, rtnl_addr_get_ifindex(addr));
67 link_cache = nl_cache_mngt_require_safe("route/link");
68 if (link_cache)
69 nl_dump_line(p, "%s_IFNAME=%s\n", pfx,
70 rtnl_link_i2name(link_cache,
71 rtnl_addr_get_ifindex(addr),
72 buf, sizeof(buf)));
73
74 if ((a = rtnl_addr_get_peer(addr)))
75 nl_dump_line(p, "%s_PEER=%s\n", pfx,
76 nl_addr2str(a, buf, sizeof(buf)));
77
78 if ((a = rtnl_addr_get_broadcast(addr)))
79 nl_dump_line(p, "%s_BROADCAST=%s\n", pfx,
80 nl_addr2str(a, buf, sizeof(buf)));
81
82 nl_dump_line(p, "%s_SCOPE=%s\n", pfx,
83 rtnl_scope2str(rtnl_addr_get_scope(addr),
84 buf, sizeof(buf)));
85
86 if ((s = rtnl_addr_get_label(addr)))
87 nl_dump_line(p, "%s_LABEL=%s\n", pfx, s);
88
89 rtnl_addr_flags2str(rtnl_addr_get_flags(addr), buf, sizeof(buf));
90 if (buf[0])
91 nl_dump_line(p, "%s_FLAGS=%s\n", pfx, buf);
92
93 nl_dump_line(p, "%s_CACHEINFO_VALID=%u\n", pfx,
94 rtnl_addr_get_valid_lifetime(addr));
95
96 if (link_cache)
97 nl_cache_put(link_cache);
98
99#if 0
100 if (addr->ce_mask & ADDR_ATTR_CACHEINFO) {
101 struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo;
102
103 nl_dump_line(p, "ADDR_CACHEINFO_PREFERRED=%u\n",
104 ci->aci_prefered);
105
106 nl_dump_line(p, "ADDR_CACHEINFO_CREATED=%u\n", ci->aci_cstamp);
107 nl_dump_line(p, "ADDR_CACHEINFO_LASTUPDATE=%u\n",
108 ci->aci_tstamp);
109 }
110#endif
111}
112
113int main(int argc, char *argv[])
114{
115 struct nl_sock *sock;
116 struct rtnl_addr *addr;
117 struct nl_cache *link_cache, *addr_cache;
118 struct nl_dump_params params = {
119 .dp_type = NL_DUMP_LINE,
120 .dp_nl_cb = print_prefix,
121 .dp_fd = stdout,
122 };
123 int dump_env = 0;
124
125 sock = nl_cli_alloc_socket();
126 nl_cli_connect(sock, NETLINK_ROUTE);
127 link_cache = nl_cli_link_alloc_cache(sock);
128 addr_cache = nl_cli_addr_alloc_cache(sock);
129 addr = nl_cli_addr_alloc();
130
131 for (;;) {
132 int c, optidx = 0;
133 enum {
134 ARG_FAMILY = 257,
135 ARG_LABEL = 258,
136 ARG_PEER,
137 ARG_SCOPE,
138 ARG_BROADCAST,
139 ARG_DETAILS,
140 ARG_ENV,
141 ARG_PREFIX,
142 ARG_PREFERRED,
143 ARG_VALID,
144 };
145 static struct option long_opts[] = {
146 { "details", 0, 0, ARG_DETAILS },
147 { "env", 0, 0, ARG_ENV },
148 { "prefix", 1, 0, ARG_PREFIX },
149 { "help", 0, 0, 'h' },
150 { "version", 0, 0, 'v' },
151 { "local", 1, 0, 'a' },
152 { "dev", 1, 0, 'd' },
153 { "family", 1, 0, ARG_FAMILY },
154 { "label", 1, 0, ARG_LABEL },
155 { "peer", 1, 0, ARG_PEER },
156 { "scope", 1, 0, ARG_SCOPE },
157 { "broadcast", 1, 0, ARG_BROADCAST },
158 { "preferred", 1, 0, ARG_PREFERRED },
159 { "valid", 1, 0, ARG_VALID },
160 { 0, 0, 0, 0 }
161 };
162
163 c = getopt_long(argc, argv, "46hva:d:", long_opts, &optidx);
164 if (c == -1)
165 break;
166
167 switch (c) {
168 case '?': exit(NLE_INVAL);
169 case '4': rtnl_addr_set_family(addr, AF_INET); break;
170 case '6': rtnl_addr_set_family(addr, AF_INET6); break;
171 case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; break;
172 case ARG_ENV: dump_env = 1; break;
173 case ARG_PREFIX: prefix = strdup(optarg); break;
174 case 'h': print_usage(); break;
175 case 'v': nl_cli_print_version(); break;
176 case 'a': nl_cli_addr_parse_local(addr, optarg); break;
177 case 'd': nl_cli_addr_parse_dev(addr, link_cache, optarg); break;
178 case ARG_FAMILY: nl_cli_addr_parse_family(addr, optarg); break;
179 case ARG_LABEL: nl_cli_addr_parse_label(addr, optarg); break;
180 case ARG_PEER: nl_cli_addr_parse_peer(addr, optarg); break;
181 case ARG_SCOPE: nl_cli_addr_parse_scope(addr, optarg); break;
182 case ARG_BROADCAST: nl_cli_addr_parse_broadcast(addr, optarg); break;
183 case ARG_PREFERRED: nl_cli_addr_parse_preferred(addr, optarg); break;
184 case ARG_VALID: nl_cli_addr_parse_valid(addr, optarg); break;
185 }
186 }
187
188 if (dump_env)
189 nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr), env_dump,
190 &params);
191 else
192 nl_cache_dump_filter(addr_cache, &params, OBJ_CAST(addr));
193
194 return 0;
195}
nl_addr2str
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
Definition addr.c:1001
nl_cache_mngt_require_safe
struct nl_cache * nl_cache_mngt_require_safe(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
Definition cache_mngt.c:430
nl_cache_dump_filter
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).
Definition cache.c:1217
nl_cache_foreach_filter
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).
Definition cache.c:1300
rtnl_link_i2name
char * rtnl_link_i2name(struct nl_cache *cache, int ifindex, char *dst, size_t len)
Translate interface index to corresponding link name.
Definition link.c:1483
nl_dump
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition utils.c:1015
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition types.h:20
NL_DUMP_DETAILS
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition types.h:21
nl_dump_params
Dumping parameters.
Definition types.h:32
nl_dump_params::dp_type
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition types.h:36