* Print a description of the network interfaces. */
| 269 | * Print a description of the network interfaces. |
| 270 | */ |
| 271 | void |
| 272 | intpr(void (*pfunc)(char *), int af) |
| 273 | { |
| 274 | struct ifaddrs *ifap, *ifa; |
| 275 | struct ifmaddrs *ifmap, *ifma; |
| 276 | u_int ifn_len_max = 5, ifn_len; |
| 277 | u_int has_ipv6 = 0, net_len = 13, addr_len = 17; |
| 278 | |
| 279 | if (interval) |
| 280 | return sidewaysintpr(); |
| 281 | |
| 282 | if (getifaddrs(&ifap) != 0) |
| 283 | err(EX_OSERR, "getifaddrs"); |
| 284 | if (aflag && getifmaddrs(&ifmap) != 0) |
| 285 | err(EX_OSERR, "getifmaddrs"); |
| 286 | |
| 287 | if (Wflag) { |
| 288 | for (ifa = ifap; ifa; ifa = ifa->ifa_next) { |
| 289 | if (interface != NULL && |
| 290 | strcmp(ifa->ifa_name, interface) != 0) |
| 291 | continue; |
| 292 | if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af) |
| 293 | continue; |
| 294 | ifn_len = strlen(ifa->ifa_name); |
| 295 | if ((ifa->ifa_flags & IFF_UP) == 0) |
| 296 | ++ifn_len; |
| 297 | ifn_len_max = MAX(ifn_len_max, ifn_len); |
| 298 | if (ifa->ifa_addr->sa_family == AF_INET6) |
| 299 | has_ipv6 = 1; |
| 300 | } |
| 301 | if (has_ipv6) { |
| 302 | net_len = 24; |
| 303 | addr_len = 39; |
| 304 | } else |
| 305 | net_len = 18; |
| 306 | } |
| 307 | |
| 308 | xo_open_list("interface"); |
| 309 | if (!pfunc) { |
| 310 | xo_emit("{T:/%-*.*s}", ifn_len_max, ifn_len_max, "Name"); |
| 311 | xo_emit(" {T:/%5.5s} {T:/%-*.*s} {T:/%-*.*s} {T:/%8.8s} " |
| 312 | "{T:/%5.5s} {T:/%5.5s}", |
| 313 | "Mtu", net_len, net_len, "Network", addr_len, addr_len, |
| 314 | "Address", "Ipkts", "Ierrs", "Idrop"); |
| 315 | if (bflag) |
| 316 | xo_emit(" {T:/%10.10s}","Ibytes"); |
| 317 | xo_emit(" {T:/%8.8s} {T:/%5.5s}", "Opkts", "Oerrs"); |
| 318 | if (bflag) |
| 319 | xo_emit(" {T:/%10.10s}","Obytes"); |
| 320 | xo_emit(" {T:/%5s}", "Coll"); |
| 321 | if (dflag) |
| 322 | xo_emit(" {T:/%5.5s}", "Drop"); |
| 323 | xo_emit("\n"); |
| 324 | } |
| 325 | |
| 326 | for (ifa = ifap; ifa; ifa = ifa->ifa_next) { |
| 327 | bool network = false, link = false; |
| 328 | char *name, *xname, buf[IFNAMSIZ+1]; |
no test coverage detected