File indexing completed on 2024-04-14 14:30:21

0001 /*
0002     SPDX-FileCopyrightText: 2011-2013 Lamarque V. Souza <lamarque@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include <arpa/inet.h>
0008 
0009 #include <QTextStream>
0010 
0011 #include <NetworkManagerQt/ActiveConnection>
0012 #include <NetworkManagerQt/Connection>
0013 #include <NetworkManagerQt/Device>
0014 #include <NetworkManagerQt/Manager>
0015 
0016 QString typeAsString(const int type)
0017 {
0018     switch (type) {
0019     case 0x0:
0020         return QString("Unknown");
0021     case 0x1:
0022         return QString("Ethernet");
0023     case 0x2:
0024         return QString("Wifi");
0025     case 0x3:
0026         return QString("Unused1");
0027     case 0x4:
0028         return QString("Unused2");
0029     case 0x5:
0030         return QString("Bluetooth");
0031     case 0x6:
0032         return QString("OlpcMesh");
0033     case 0x7:
0034         return QString("Wimax");
0035     case 0x8:
0036         return QString("Modem");
0037     }
0038     return QString("Unknown");
0039 }
0040 
0041 int main()
0042 {
0043     QTextStream qout(stdout, QIODevice::WriteOnly);
0044 
0045     const NetworkManager::Device::List list = NetworkManager::networkInterfaces();
0046 
0047     // List device configuration, not including vpn connections, which do not
0048     // have a real device tied to them.
0049     for (const NetworkManager::Device::Ptr &dev : list) {
0050         qout << "\n=====\n";
0051         qout << dev->uni() << "\n";
0052         qout << "type: " << typeAsString(dev->type()) << "\n";
0053         qout << "managed: " << dev->managed() << "\n";
0054         qout << "interface name: " << dev->interfaceName() << "\n";
0055 
0056         NetworkManager::IpConfig ipConfig = dev->ipV4Config();
0057         if (ipConfig.isValid()) {
0058             // static IPv4 configuration.
0059             if (ipConfig.addresses().isEmpty()) {
0060                 qout << "ip address: <not set>\n";
0061             } else {
0062                 NetworkManager::IpAddress address = ipConfig.addresses().at(0);
0063                 qout << "ip address: " << address.ip().toString() << "\n";
0064                 qout << "gateway: " << address.gateway().toString() << "\n";
0065                 qout << "ip address (raw): " << dev->ipV4Address().toString() << "\n";
0066 
0067                 // Static routes.
0068                 if (ipConfig.routes().isEmpty()) {
0069                     qout << "routers: <not set>\n";
0070                 } else {
0071                     qout << "routers: " << ipConfig.routes().at(0).ip().toString() << "\n";
0072                 }
0073 
0074                 if (ipConfig.nameservers().isEmpty()) {
0075                     qout << "nameserver: <not set>\n";
0076                 } else {
0077                     qout << "nameserver: " << ipConfig.nameservers().at(0).toString() << "\n";
0078                 }
0079             }
0080             // DHCPv4 configuration.
0081             NetworkManager::Dhcp4Config::Ptr dhcp4Config = dev->dhcp4Config();
0082             if (!dhcp4Config) {
0083                 qout << "dhcp info unavailable\n";
0084             } else {
0085                 qout << "Dhcp4 options (" << dhcp4Config->path() << "): ";
0086                 QVariantMap options = dhcp4Config->options();
0087                 QVariantMap::ConstIterator it = options.constBegin();
0088                 QVariantMap::ConstIterator end = options.constEnd();
0089                 for (; it != end; ++it) {
0090                     qout << it.key() << "=" << it.value().toString() << " ";
0091                 }
0092                 qout << "\n";
0093 
0094                 qout << "(dhcp) ip address: " << dhcp4Config->optionValue("ip_address") << "\n";
0095                 qout << "(dhcp) network: " << dhcp4Config->optionValue("network_number") << '/' << dhcp4Config->optionValue("subnet_cidr") << " ("
0096                      << dhcp4Config->optionValue("subnet_mask") << ")\n";
0097 
0098                 if (dhcp4Config->optionValue("routers").isEmpty()) {
0099                     qout << "(dhcp) gateway(s): <not set>\n";
0100                 } else {
0101                     qout << "(dhcp) gateway(s): " << dhcp4Config->optionValue("routers") << "\n";
0102                 }
0103 
0104                 if (dhcp4Config->optionValue("domain_name_servers").isEmpty()) {
0105                     qout << "(dhcp) domain name server(s): <not set>\n";
0106                 } else {
0107                     qout << "(dhcp) domain name server(s): " << dhcp4Config->optionValue("domain_name_servers") << "\n";
0108                 }
0109             }
0110         }
0111 
0112         const NetworkManager::Connection::List connections = dev->availableConnections();
0113 
0114         qout << "available connections: ";
0115 
0116         for (const NetworkManager::Connection::Ptr &con : connections) {
0117             qout << "con";
0118             NetworkManager::ConnectionSettings::Ptr settings = con->settings();
0119             qout << "\"" << settings->id() << "\" ";
0120         }
0121     }
0122     qout << "\n";
0123 }