static status_t show_all() { BNetworkRoster& roster = BNetworkRoster::Default(); BNetworkInterface interface; uint32 cookie = 0; while (roster.GetNextInterface(&cookie, interface) == B_OK) { BNetworkAddress linkAddress; status_t status = interface.GetHardwareAddress(linkAddress); if (status == B_OK && linkAddress.LinkLevelType() == IFT_TUN) show_interface(interface.Name()); } return B_OK; }
bool list_interface(const char* name) { printf("%s", name); size_t length = strlen(name); if (length < 8) putchar('\t'); else printf("\n\t"); // get link level interface for this interface BNetworkInterface interface(name); if (!interface.Exists()) { printf("Interface not found!\n"); return false; } BNetworkAddress linkAddress; status_t status = interface.GetHardwareAddress(linkAddress); if (status == B_OK) { const char *type = "unknown"; switch (linkAddress.LinkLevelType()) { case IFT_ETHER: type = "Ethernet"; break; case IFT_LOOP: type = "Local Loopback"; break; case IFT_MODEM: type = "Modem"; break; } BString address = linkAddress.ToString(); if (address.Length() == 0) address = "none"; printf("Hardware type: %s, Address: %s\n", type, address.String()); } else printf("No link level: %s\n", strerror(status)); int media = interface.Media(); if ((media & IFM_ACTIVE) != 0) { // dump media state in case we're linked const char* type = "unknown"; bool show = false; for (int32 i = 0; kMediaTypes[i].type >= 0; i++) { // loopback don't really have a media anyway if (IFM_TYPE(media) == 0/*IFT_LOOP*/) break; // only check for generic or correct subtypes if (kMediaTypes[i].type && kMediaTypes[i].type != IFM_TYPE(media)) continue; for (int32 j = 0; kMediaTypes[i].subtypes[j].subtype >= 0; j++) { if (kMediaTypes[i].subtypes[j].subtype == IFM_SUBTYPE(media)) { // found a match type = kMediaTypes[i].subtypes[j].pretty; show = true; break; } } } if (show) printf("\tMedia type: %s\n", type); } // Print associated wireless network(s) BNetworkDevice device(name); if (device.IsWireless()) { wireless_network network; bool first = true; uint32 cookie = 0; while (device.GetNextAssociatedNetwork(cookie, network) == B_OK) { if (first) { printf("\tNetwork: "); first = false; } else printf("\t\t"); printf("%s, Address: %s, %s", network.name, network.address.ToString().String(), get_authentication_mode(network.authentication_mode, network.flags)); const char* keyMode = get_key_mode(network.key_mode); if (keyMode != NULL) printf(", %s/%s", keyMode, get_cipher(network.cipher)); putchar('\n'); } } uint32 flags = interface.Flags(); list_interface_addresses(interface, flags); // Print MTU, metric, flags printf("\tMTU: %" B_PRId32 ", Metric: %" B_PRId32, interface.MTU(), interface.Metric()); if (flags != 0) { const struct { int value; const char *name; } kFlags[] = { {IFF_UP, "up"}, {IFF_NOARP, "noarp"}, {IFF_BROADCAST, "broadcast"}, {IFF_LOOPBACK, "loopback"}, {IFF_PROMISC, "promiscuous"}, {IFF_ALLMULTI, "allmulti"}, {IFF_AUTOUP, "autoup"}, {IFF_LINK, "link"}, {IFF_AUTO_CONFIGURED, "auto-configured"}, {IFF_CONFIGURING, "configuring"}, }; bool first = true; for (uint32 i = 0; i < sizeof(kFlags) / sizeof(kFlags[0]); i++) { if ((flags & kFlags[i].value) != 0) { if (first) { printf(","); first = false; } putchar(' '); printf(kFlags[i].name); } } } putchar('\n'); // Print statistics ifreq_stats stats; if (interface.GetStats(stats) == B_OK) { printf("\tReceive: %d packets, %d errors, %Ld bytes, %d mcasts, %d " "dropped\n", stats.receive.packets, stats.receive.errors, stats.receive.bytes, stats.receive.multicast_packets, stats.receive.dropped); printf("\tTransmit: %d packets, %d errors, %Ld bytes, %d mcasts, %d " "dropped\n", stats.send.packets, stats.send.errors, stats.send.bytes, stats.send.multicast_packets, stats.send.dropped); printf("\tCollisions: %d\n", stats.collisions); } putchar('\n'); return true; }