1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * src/nl-addr-list.c List addresses
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation version 2 of the License.
8 *
9 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
10 */
11
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/addr.h>
14 #include <netlink/cli/link.h>
15
16 #include <linux/netlink.h>
17
print_usage(void)18 static void print_usage(void)
19 {
20 printf(
21 "Usage: nl-addr-list [OPTION]... [ADDRESS]\n"
22 "\n"
23 "Options\n"
24 " --details Show details on multiple lines.\n"
25 " --env Print address details in sh env variable syntax.\n"
26 " --prefix=STRING Prefix each printed line.\n"
27 " -h, --help Show this help.\n"
28 " -v, --version Show versioning information.\n"
29 "\n"
30 "Address Selection\n"
31 " -a, --local=ADDR Local address.\n"
32 " -d, --dev=DEV Associated network device.\n"
33 " --family=FAMILY Family of local address.\n"
34 " --label=STRING Address label (IPv4).\n"
35 " --peer=ADDR Peer address (IPv4).\n"
36 " --scope=SCOPE Address scope (IPv4).\n"
37 " --broadcast=ADDR Broadcast address of network (IPv4).\n"
38 " --valid-lifetime=TS Valid lifetime before route expires (IPv6).\n"
39 " --preferred=TIME Preferred lifetime (IPv6).\n"
40 " --valid=TIME Valid lifetime (IPv6).\n"
41 );
42 exit(0);
43 }
44
45 static char *prefix;
46
print_prefix(struct nl_dump_params * p,int line)47 static void print_prefix(struct nl_dump_params *p, int line)
48 {
49 if (prefix)
50 nl_dump(p, "%s", prefix);
51 }
52
env_dump(struct nl_object * obj,void * arg)53 static void env_dump(struct nl_object *obj, void *arg)
54 {
55 struct nl_dump_params *p = arg;
56 struct rtnl_addr *addr = (struct rtnl_addr *) obj;
57 struct nl_cache *link_cache;
58 struct nl_addr *a;
59 static int index = 0;
60 char buf[128], pfx[32], *s;
61
62 snprintf(pfx, sizeof(pfx), "ADDR%d", index++);
63
64 nl_dump_line(p, "%s_FAMILY=%s\n", pfx,
65 nl_af2str(rtnl_addr_get_family(addr), buf, sizeof(buf)));
66
67 nl_dump_line(p, "%s_LOCAL=%s\n", pfx,
68 nl_addr2str(rtnl_addr_get_local(addr), buf, sizeof(buf)));
69
70 nl_dump_line(p, "%s_IFINDEX=%u\n", pfx, rtnl_addr_get_ifindex(addr));
71 link_cache = nl_cache_mngt_require_safe("route/link");
72 if (link_cache)
73 nl_dump_line(p, "%s_IFNAME=%s\n", pfx,
74 rtnl_link_i2name(link_cache,
75 rtnl_addr_get_ifindex(addr),
76 buf, sizeof(buf)));
77
78 if ((a = rtnl_addr_get_peer(addr)))
79 nl_dump_line(p, "%s_PEER=%s\n", pfx,
80 nl_addr2str(a, buf, sizeof(buf)));
81
82 if ((a = rtnl_addr_get_broadcast(addr)))
83 nl_dump_line(p, "%s_BROADCAST=%s\n", pfx,
84 nl_addr2str(a, buf, sizeof(buf)));
85
86 nl_dump_line(p, "%s_SCOPE=%s\n", pfx,
87 rtnl_scope2str(rtnl_addr_get_scope(addr),
88 buf, sizeof(buf)));
89
90 if ((s = rtnl_addr_get_label(addr)))
91 nl_dump_line(p, "%s_LABEL=%s\n", pfx, s);
92
93 rtnl_addr_flags2str(rtnl_addr_get_flags(addr), buf, sizeof(buf));
94 if (buf[0])
95 nl_dump_line(p, "%s_FLAGS=%s\n", pfx, buf);
96
97 nl_dump_line(p, "%s_CACHEINFO_VALID=%u\n", pfx,
98 rtnl_addr_get_valid_lifetime(addr));
99
100 if (link_cache)
101 nl_cache_put(link_cache);
102
103 #if 0
104 if (addr->ce_mask & ADDR_ATTR_CACHEINFO) {
105 struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo;
106
107 nl_dump_line(p, "ADDR_CACHEINFO_PREFERRED=%u\n",
108 ci->aci_prefered);
109
110 nl_dump_line(p, "ADDR_CACHEINFO_CREATED=%u\n", ci->aci_cstamp);
111 nl_dump_line(p, "ADDR_CACHEINFO_LASTUPDATE=%u\n",
112 ci->aci_tstamp);
113 }
114 #endif
115 }
116
main(int argc,char * argv[])117 int main(int argc, char *argv[])
118 {
119 struct nl_sock *sock;
120 struct rtnl_addr *addr;
121 struct nl_cache *link_cache, *addr_cache;
122 struct nl_dump_params params = {
123 .dp_type = NL_DUMP_LINE,
124 .dp_nl_cb = print_prefix,
125 .dp_fd = stdout,
126 };
127 int dump_env = 0;
128
129 sock = nl_cli_alloc_socket();
130 nl_cli_connect(sock, NETLINK_ROUTE);
131 link_cache = nl_cli_link_alloc_cache(sock);
132 addr_cache = nl_cli_addr_alloc_cache(sock);
133 addr = nl_cli_addr_alloc();
134
135 for (;;) {
136 int c, optidx = 0;
137 enum {
138 ARG_FAMILY = 257,
139 ARG_LABEL = 258,
140 ARG_PEER,
141 ARG_SCOPE,
142 ARG_BROADCAST,
143 ARG_DETAILS,
144 ARG_ENV,
145 ARG_PREFIX,
146 ARG_PREFERRED,
147 ARG_VALID,
148 };
149 static struct option long_opts[] = {
150 { "details", 0, 0, ARG_DETAILS },
151 { "env", 0, 0, ARG_ENV },
152 { "prefix", 1, 0, ARG_PREFIX },
153 { "help", 0, 0, 'h' },
154 { "version", 0, 0, 'v' },
155 { "local", 1, 0, 'a' },
156 { "dev", 1, 0, 'd' },
157 { "family", 1, 0, ARG_FAMILY },
158 { "label", 1, 0, ARG_LABEL },
159 { "peer", 1, 0, ARG_PEER },
160 { "scope", 1, 0, ARG_SCOPE },
161 { "broadcast", 1, 0, ARG_BROADCAST },
162 { "preferred", 1, 0, ARG_PREFERRED },
163 { "valid", 1, 0, ARG_VALID },
164 { 0, 0, 0, 0 }
165 };
166
167 c = getopt_long(argc, argv, "46hva:d:", long_opts, &optidx);
168 if (c == -1)
169 break;
170
171 switch (c) {
172 case '?': exit(NLE_INVAL);
173 case '4': rtnl_addr_set_family(addr, AF_INET); break;
174 case '6': rtnl_addr_set_family(addr, AF_INET6); break;
175 case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; break;
176 case ARG_ENV: dump_env = 1; break;
177 case ARG_PREFIX: prefix = strdup(optarg); break;
178 case 'h': print_usage(); break;
179 case 'v': nl_cli_print_version(); break;
180 case 'a': nl_cli_addr_parse_local(addr, optarg); break;
181 case 'd': nl_cli_addr_parse_dev(addr, link_cache, optarg); break;
182 case ARG_FAMILY: nl_cli_addr_parse_family(addr, optarg); break;
183 case ARG_LABEL: nl_cli_addr_parse_label(addr, optarg); break;
184 case ARG_PEER: nl_cli_addr_parse_peer(addr, optarg); break;
185 case ARG_SCOPE: nl_cli_addr_parse_scope(addr, optarg); break;
186 case ARG_BROADCAST: nl_cli_addr_parse_broadcast(addr, optarg); break;
187 case ARG_PREFERRED: nl_cli_addr_parse_preferred(addr, optarg); break;
188 case ARG_VALID: nl_cli_addr_parse_valid(addr, optarg); break;
189 }
190 }
191
192 if (dump_env)
193 nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr), env_dump,
194 ¶ms);
195 else
196 nl_cache_dump_filter(addr_cache, ¶ms, OBJ_CAST(addr));
197
198 return 0;
199 }
200