void NetworkInterfaceTest::testMap() { NetworkInterface::Map m = NetworkInterface::map(false, false); assert (!m.empty()); for (NetworkInterface::Map::const_iterator it = m.begin(); it != m.end(); ++it) { std::cout << std::endl << "=============" << std::endl; std::cout << "Index: " << it->second.index() << std::endl; std::cout << "Name: " << it->second.name() << std::endl; std::cout << "DisplayName: " << it->second.displayName() << std::endl; std::cout << "Status: " << (it->second.isUp() ? "Up" : "Down") << std::endl; NetworkInterface::MACAddress mac(it->second.macAddress()); if (!mac.empty() && (it->second.type() != NetworkInterface::NI_TYPE_SOFTWARE_LOOPBACK)) std::cout << "MAC Address: (" << it->second.type() << ") " << mac << std::endl; typedef NetworkInterface::AddressList List; const List& ipList = it->second.addressList(); List::const_iterator ipIt = ipList.begin(); List::const_iterator ipEnd = ipList.end(); for (int counter = 0; ipIt != ipEnd; ++ipIt, ++counter) { std::cout << std::endl << "----------" << std::endl; std::cout << "Address " << counter << std::endl; std::cout << "----------" << std::endl; std::cout << "Address: " << ipIt->get<NetworkInterface::IP_ADDRESS>() << std::endl; IPAddress addr = ipIt->get<NetworkInterface::SUBNET_MASK>(); if (!addr.isWildcard()) std::cout << "Subnet: " << addr << " (/" << addr.prefixLength() << ")" << std::endl; addr = ipIt->get<NetworkInterface::BROADCAST_ADDRESS>(); if (!addr.isWildcard()) std::cout << "Broadcast: " << addr << std::endl; } std::cout << "=============" << std::endl << std::endl; } }
int main(int argc, char** argv) { if (argc != 1) { Path p(argv[0]); std::cerr << "usage: " << p.getBaseName() << std::endl; return 1; } try { const NetworkInterface::Map map = NetworkInterface::map(); for ( NetworkInterface::Map::const_iterator it = map.begin(); it != map.end(); ++it) { const NetworkInterface& intf = it->second; std::string sep(""); std::cout << intf.name() << " [" << intf.index() << "]: "; std::cout << "<"; if (intf.isUp()) { std::cout << sep << "UP"; sep = ","; } if (intf.isRunning()) { std::cout << sep << "RUNNING"; sep = ","; } if (intf.isLoopback()) { std::cout << sep << "LOOPBACK"; sep = ","; } if (intf.isPointToPoint()) { std::cout << sep << "P2P"; sep = ","; } if (intf.supportsIPv4()) { std::cout << sep << "IPv4"; sep = ","; } if (intf.supportsIPv6()) { std::cout << sep << "IPv6"; sep = ","; } if (intf.supportsBroadcast()) { std::cout << sep << "BCAST"; sep = ","; } if (intf.supportsMulticast()) { std::cout << sep << "MCAST"; sep = ","; } if (!intf.isLoopback()) { std::cout << sep << std::dec << intf.mtu(); sep = ","; } std::cout << ">" << std::endl; const NetworkInterface::AddressList& ipList = intf.addressList(); NetworkInterface::AddressList::const_iterator ipIt = ipList.begin(); NetworkInterface::AddressList::const_iterator ipEnd = ipList.end(); for (; ipIt != ipEnd; ++ipIt) { std::cout << " " << ipIt->get<NetworkInterface::IP_ADDRESS>().toString(); IPAddress addr; addr = ipIt->get<NetworkInterface::SUBNET_MASK>(); if (!addr.isWildcard()) std::cout << '/' << addr.toString() << " (" << addr.prefixLength() << ')'; addr = ipIt->get<NetworkInterface::BROADCAST_ADDRESS>(); if (!addr.isWildcard()) std::cout << (intf.isPointToPoint() ? " dest " : " bcast ") << addr.toString(); std::cout << std::endl; } std::cout << std::endl; } } catch (Exception& exc) { std::cerr << exc.displayText() << std::endl; return 1; } return 0; }