Route: Redesign command output

* Route flags now single characters to save
  space.
* Align addresses to columns based on maximum
  size of family address. We can easily pre-loop
  over routes at a future date and choose
  the smallest address width if needed.
* Moved interface device to last column as width
  can vary a lot.
This commit is contained in:
Alexander von Gluck IV 2013-06-16 12:54:23 -05:00
parent 47f91726cd
commit cf671c0cf8

View File

@ -44,7 +44,7 @@ struct address_family {
int family; int family;
const char* name; const char* name;
const char* identifiers[4]; const char* identifiers[4];
uint32 maxAddressLength; int maxAddressLength;
}; };
static const address_family kFamilies[] = { static const address_family kFamilies[] = {
@ -189,8 +189,13 @@ list_routes(int socket, const char *interfaceName, route_entry &route)
} }
} }
int addressLength = family->maxAddressLength;
printf("%s routing table:\n", family->name); printf("%s routing table:\n", family->name);
printf("%*s %*s Flags Interface\n", addressLength, "Destination",
addressLength, "Next Hop");
while (interface < end) { while (interface < end) {
route_entry& route = interface->ifr_route; route_entry& route = interface->ifr_route;
@ -200,53 +205,49 @@ list_routes(int socket, const char *interfaceName, route_entry &route)
if (family != NULL) { if (family != NULL) {
BNetworkAddress destination(*route.destination); BNetworkAddress destination(*route.destination);
printf("%15s", destination.ToString().String()); printf("%*s", addressLength, destination.ToString().String());
if (route.mask != NULL) { if (route.mask != NULL) {
BNetworkAddress mask; BNetworkAddress mask;
mask.SetTo(*route.mask); mask.SetTo(*route.mask);
printf("/%zd\t", mask.PrefixLength()); printf("/%-3zd ", mask.PrefixLength());
} else } else
printf(" \t"); printf(" ");
if ((route.flags & RTF_GATEWAY) != 0) { if ((route.flags & RTF_GATEWAY) != 0) {
BNetworkAddress gateway; BNetworkAddress gateway;
if (route.gateway != NULL) if (route.gateway != NULL)
gateway.SetTo(*route.gateway); gateway.SetTo(*route.gateway);
printf("gateway %-15s ", gateway.ToString().String()); printf("%*s ", addressLength, gateway.ToString().String());
} } else
printf("%*s ", family->maxAddressLength, "-");
} else } else
printf("unknown family "); printf("unknown family ");
printf("%s", interface->ifr_name);
if (route.flags != 0) { if (route.flags != 0) {
const struct { const struct {
int value; int value;
const char *name; const char *name;
} kFlags[] = { } kFlags[] = {
{RTF_DEFAULT, "default"}, {RTF_DEFAULT, "D"},
{RTF_REJECT, "reject"}, {RTF_REJECT, "R"},
{RTF_HOST, "host"}, {RTF_HOST, "H"},
{RTF_LOCAL, "local"}, {RTF_LOCAL, "L"},
{RTF_DYNAMIC, "dynamic"}, {RTF_DYNAMIC, "D"},
{RTF_MODIFIED, "modified"}, {RTF_MODIFIED, "M"},
}; };
bool first = true;
for (uint32 i = 0; i < sizeof(kFlags) / sizeof(kFlags[0]); for (uint32 i = 0; i < sizeof(kFlags) / sizeof(kFlags[0]);
i++) { i++) {
if ((route.flags & kFlags[i].value) != 0) { if ((route.flags & kFlags[i].value) != 0) {
if (first) {
printf(", ");
first = false;
} else
putchar(' ');
printf(kFlags[i].name); printf(kFlags[i].name);
} else
putchar('-');
} }
} printf(" ");
} } else
printf("------ ");
printf("%s", interface->ifr_name);
putchar('\n'); putchar('\n');
} }