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

libnl 3.7.0

nl-addr-list.c
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
4 */
5
6 #include <netlink/cli/utils.h>
7 #include <netlink/cli/addr.h>
8 #include <netlink/cli/link.h>
9
10 #include <linux/netlink.h>
11
12 static void print_usage(void)
13 {
14 printf(
15 "Usage: nl-addr-list [OPTION]... [ADDRESS]\n"
16 "\n"
17 "Options\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"
23 "\n"
24 "Address Selection\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"
35 );
36 exit(0);
37 }
38
39 static char *prefix;
40
41 static void print_prefix(struct nl_dump_params *p, int line)
42 {
43 if (prefix)
44 nl_dump(p, "%s", prefix);
45 }
46
47 static void env_dump(struct nl_object *obj, void *arg)
48 {
49 struct nl_dump_params *p = arg;
50 struct rtnl_addr *addr = (struct rtnl_addr *) obj;
51 struct nl_cache *link_cache;
52 struct nl_addr *a;
53 static int index = 0;
54 char buf[128], pfx[32], *s;
55
56 snprintf(pfx, sizeof(pfx), "ADDR%d", index++);
57
58 nl_dump_line(p, "%s_FAMILY=%s\n", pfx,
59 nl_af2str(rtnl_addr_get_family(addr), buf, sizeof(buf)));
60
61 nl_dump_line(p, "%s_LOCAL=%s\n", pfx,
62 nl_addr2str(rtnl_addr_get_local(addr), buf, sizeof(buf)));
63
64 nl_dump_line(p, "%s_IFINDEX=%u\n", pfx, rtnl_addr_get_ifindex(addr));
65 link_cache = nl_cache_mngt_require_safe("route/link");
66 if (link_cache)
67 nl_dump_line(p, "%s_IFNAME=%s\n", pfx,
68 rtnl_link_i2name(link_cache,
69 rtnl_addr_get_ifindex(addr),
70 buf, sizeof(buf)));
71
72 if ((a = rtnl_addr_get_peer(addr)))
73 nl_dump_line(p, "%s_PEER=%s\n", pfx,
74 nl_addr2str(a, buf, sizeof(buf)));
75
76 if ((a = rtnl_addr_get_broadcast(addr)))
77 nl_dump_line(p, "%s_BROADCAST=%s\n", pfx,
78 nl_addr2str(a, buf, sizeof(buf)));
79
80 nl_dump_line(p, "%s_SCOPE=%s\n", pfx,
81 rtnl_scope2str(rtnl_addr_get_scope(addr),
82 buf, sizeof(buf)));
83
84 if ((s = rtnl_addr_get_label(addr)))
85 nl_dump_line(p, "%s_LABEL=%s\n", pfx, s);
86
87 rtnl_addr_flags2str(rtnl_addr_get_flags(addr), buf, sizeof(buf));
88 if (buf[0])
89 nl_dump_line(p, "%s_FLAGS=%s\n", pfx, buf);
90
91 nl_dump_line(p, "%s_CACHEINFO_VALID=%u\n", pfx,
92 rtnl_addr_get_valid_lifetime(addr));
93
94 if (link_cache)
95 nl_cache_put(link_cache);
96
97 #if 0
98 if (addr->ce_mask & ADDR_ATTR_CACHEINFO) {
99 struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo;
100
101 nl_dump_line(p, "ADDR_CACHEINFO_PREFERRED=%u\n",
102 ci->aci_prefered);
103
104 nl_dump_line(p, "ADDR_CACHEINFO_CREATED=%u\n", ci->aci_cstamp);
105 nl_dump_line(p, "ADDR_CACHEINFO_LASTUPDATE=%u\n",
106 ci->aci_tstamp);
107 }
108 #endif
109 }
110
111 int main(int argc, char *argv[])
112 {
113 struct nl_sock *sock;
114 struct rtnl_addr *addr;
115 struct nl_cache *link_cache, *addr_cache;
116 struct nl_dump_params params = {
117 .dp_type = NL_DUMP_LINE,
118 .dp_nl_cb = print_prefix,
119 .dp_fd = stdout,
120 };
121 int dump_env = 0;
122
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();
128
129 for (;;) {
130 int c, optidx = 0;
131 enum {
132 ARG_FAMILY = 257,
133 ARG_LABEL = 258,
134 ARG_PEER,
135 ARG_SCOPE,
136 ARG_BROADCAST,
137 ARG_DETAILS,
138 ARG_ENV,
139 ARG_PREFIX,
140 ARG_PREFERRED,
141 ARG_VALID,
142 };
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 },
158 { 0, 0, 0, 0 }
159 };
160
161 c = getopt_long(argc, argv, "46hva:d:", long_opts, &optidx);
162 if (c == -1)
163 break;
164
165 switch (c) {
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;
169 case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; 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;
183 }
184 }
185
186 if (dump_env)
187 nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr), env_dump,
188 &params);
189 else
190 nl_cache_dump_filter(addr_cache, &params, OBJ_CAST(addr));
191
192 return 0;
193 }
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:993
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:424
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:1211
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:1294
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:1473
nl_dump
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:955
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
NL_DUMP_DETAILS
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:17
nl_dump_params
Dumping parameters.
Definition: types.h:28
nl_dump_params::dp_type
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:32