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 15static 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 42static char *prefix; 43 44void print_prefix(struct nl_dump_params *p, int line) 45{ 46 if (prefix) 47 nl_dump(p, "%s", prefix); 48} 49 50static 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("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 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 111int 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 ¶ms); 189 else 190 nl_cache_dump_filter(addr_cache, ¶ms, OBJ_CAST(addr)); 191 192 return 0; 193} 194