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