From fd15e95db252cf3bf2fece11419cecfc930028df Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Mon, 7 Mar 2022 05:28:03 +0200 Subject: [PATCH] kinfocenter: reimplement nic module via QNetworkInterface Signed-off-by: Ivailo Monev --- kinfocenter/Modules/nics/CMakeLists.txt | 32 +-- kinfocenter/Modules/nics/config-nic.h.cmake | 15 -- kinfocenter/Modules/nics/nic.cpp | 347 +++++++--------------------- kinfocenter/Modules/nics/nic.h | 17 +- 4 files changed, 106 insertions(+), 305 deletions(-) delete mode 100644 kinfocenter/Modules/nics/config-nic.h.cmake diff --git a/kinfocenter/Modules/nics/CMakeLists.txt b/kinfocenter/Modules/nics/CMakeLists.txt index 2b0ca004..071570bb 100644 --- a/kinfocenter/Modules/nics/CMakeLists.txt +++ b/kinfocenter/Modules/nics/CMakeLists.txt @@ -1,30 +1,22 @@ - -include(CheckStructHasMember) - -check_include_files(sys/sockio.h HAVE_SYS_SOCKIO_H) # nic.cpp -check_symbol_exists(getnameinfo "sys/socket.h;netdb.h" HAVE_GETNAMEINFO) # nic.cpp -check_struct_has_member("struct sockaddr" "sa_len" "sys/socket.h" HAVE_STRUCT_SOCKADDR_SA_LEN) # nic.cpp -check_function_exists(getifaddrs HAVE_GETIFADDRS) # nic.cpp -check_struct_has_member("struct ifreq" "ifr_ifru.ifru_hwaddr" "net/if.h" HAVE_STRUCT_IFREQ_IFR_IFRU_IFRU_HWADDR) # nic.cpp - -configure_file (config-nic.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-nic.h ) - - - ########### next target ############### set(kcm_nic_PART_SRCS nic.cpp ) - kde4_add_plugin(kcm_nic ${kcm_nic_PART_SRCS}) +target_link_libraries(kcm_nic + ${KDE4_KIO_LIBS} + ${QT_QTNETWORK_LIBRARY} +) -target_link_libraries(kcm_nic ${KDE4_KIO_LIBS} ) - -install(TARGETS kcm_nic DESTINATION ${KDE4_PLUGIN_INSTALL_DIR} ) - +install( + TARGETS kcm_nic + DESTINATION ${KDE4_PLUGIN_INSTALL_DIR} +) ########### install files ############### -install( FILES nic.desktop DESTINATION ${KDE4_SERVICES_INSTALL_DIR} ) - +install( + FILES nic.desktop + DESTINATION ${KDE4_SERVICES_INSTALL_DIR} +) diff --git a/kinfocenter/Modules/nics/config-nic.h.cmake b/kinfocenter/Modules/nics/config-nic.h.cmake deleted file mode 100644 index 538d5b41..00000000 --- a/kinfocenter/Modules/nics/config-nic.h.cmake +++ /dev/null @@ -1,15 +0,0 @@ - -/* Define to 1 if you have the header file. */ -#cmakedefine HAVE_SYS_SOCKIO_H 1 - -/* Define to 1 if you have the `getnameinfo' function. */ -#cmakedefine HAVE_GETNAMEINFO 1 - -/* Define to 1 if `sa_len' is member of `struct sockaddr'. */ -#cmakedefine HAVE_STRUCT_SOCKADDR_SA_LEN - -/* Define to 1 if you have the `getifaddrs' function. */ -#cmakedefine HAVE_GETIFADDRS 1 - -/* Define to 1 if `ifru_hwaddr' is member of `struct ifreq.ifr_ifru'. */ -#cmakedefine HAVE_STRUCT_IFREQ_IFR_IFRU_IFRU_HWADDR diff --git a/kinfocenter/Modules/nics/nic.cpp b/kinfocenter/Modules/nics/nic.cpp index d8391d4a..bf3ffe1c 100644 --- a/kinfocenter/Modules/nics/nic.cpp +++ b/kinfocenter/Modules/nics/nic.cpp @@ -20,19 +20,6 @@ #include "nic.h" -#include -#include -#include -#include -#include -#include -#include - -#include "config-nic.h" -#ifdef HAVE_SYS_SOCKIO_H -#include -#endif - #include #include #include @@ -44,274 +31,112 @@ #include #include #include +#include -#ifdef USE_SOLARIS -/* net/if.h is incompatible with STL on Solaris 2.6 - 2.8, redefine - map in the header file because we don't need it. -- Simon Josefsson */ -#define map junkmap -#endif -# include -#ifdef USE_SOLARIS -#undef map -#endif - -#include #include #include -#ifndef HAVE_STRUCT_SOCKADDR_SA_LEN -#undef HAVE_GETNAMEINFO -#undef HAVE_GETIFADDRS -#endif - -#if defined(HAVE_GETNAMEINFO) && defined(HAVE_GETIFADDRS) -#include -#include - -QString flags_tos (unsigned int flags); -#endif -K_PLUGIN_FACTORY(KCMNicFactory, - registerPlugin(); -) +K_PLUGIN_FACTORY(KCMNicFactory, registerPlugin();) K_EXPORT_PLUGIN(KCMNicFactory("kcmnic")) struct MyNIC { - QString name; - QString addr; - QString netmask; - QString state; - QString type; - QString HWaddr; + QString name; + QString addr; + QString netmask; + QString state; + QString type; + QString HWaddr; }; QList findNICs(); -KCMNic::KCMNic(QWidget *parent, const QVariantList &) : - KCModule(KCMNicFactory::componentData(), parent) { - QVBoxLayout *box=new QVBoxLayout(this); - box->setMargin(0); - box->setSpacing(KDialog::spacingHint()); - m_list=new QTreeWidget(this); - m_list->setRootIsDecorated(false); - box->addWidget(m_list); - QStringList columns; - columns<setHeaderLabels(columns); - QHBoxLayout *hbox=new QHBoxLayout(); - box->addItem(hbox); - m_updateButton=new QPushButton(i18n("&Update"),this); - hbox->addWidget(m_updateButton); - hbox->addStretch(1); - QTimer* timer=new QTimer(this); - timer->start(60000); - connect(m_updateButton, SIGNAL(clicked()), this, SLOT(update())); - connect(timer, SIGNAL(timeout()), this, SLOT(update())); - update(); - KAboutData *about = new KAboutData(I18N_NOOP("kcminfo"), 0, - ki18n("System Information Control Module"), - 0, KLocalizedString(), KAboutData::License_GPL, - ki18n("(c) 2001 - 2002 Alexander Neundorf")); - - about->addAuthor(ki18n("Alexander Neundorf"), KLocalizedString(), "neundorf@kde.org"); - setAboutData(about); - +KCMNic::KCMNic(QWidget *parent, const QVariantList &) + : KCModule(KCMNicFactory::componentData(), parent) { + QVBoxLayout *box=new QVBoxLayout(this); + box->setMargin(0); + box->setSpacing(KDialog::spacingHint()); + m_list=new QTreeWidget(this); + m_list->setRootIsDecorated(false); + box->addWidget(m_list); + QStringList columns; + columns << i18n("Name") << i18n("IP Address") << i18n("Network Mask") << i18n("Type") << i18n("State") << i18n("HWAddr"); + m_list->setHeaderLabels(columns); + QHBoxLayout *hbox = new QHBoxLayout(); + box->addItem(hbox); + m_updateButton = new QPushButton(i18n("&Update"),this); + hbox->addWidget(m_updateButton); + hbox->addStretch(1); + QTimer* timer = new QTimer(this); + timer->start(60000); + connect(m_updateButton, SIGNAL(clicked()), this, SLOT(update())); + connect(timer, SIGNAL(timeout()), this, SLOT(update())); + update(); + KAboutData *about = new KAboutData(I18N_NOOP("kcminfo"), 0, + ki18n("System Information Control Module"), + 0, KLocalizedString(), KAboutData::License_GPL, + ki18n("(c) 2001 - 2002 Alexander Neundorf")); + + about->addAuthor(ki18n("Alexander Neundorf"), KLocalizedString(), "neundorf@kde.org"); + setAboutData(about); } -void KCMNic::update() { - m_list->clear(); - QList nics=findNICs(); - - foreach(MyNIC* tmp, nics) { - QStringList lst; - lst << tmp->name<addr<netmask<type<state<HWaddr; - new QTreeWidgetItem(m_list,lst); - - delete tmp; - } - - nics.clear(); - -} - -static QString HWaddr2String(const char *hwaddr) { - QString ret; - for (int i=0; i<6; i++, hwaddr++) { - int v = (*hwaddr & 0xff); - QString num = QString("%1").arg(v, 0, 16); - if (num.length() < 2) - num.prepend("0"); - if (i>0) - ret.append(":"); - ret.append(num); - } - return ret; -} - -QList findNICs() { - QString upMessage(i18nc("State of network card is connected", "Up") ); - QString downMessage(i18nc("State of network card is disconnected", "Down") ); - - QList nl; - -#if !defined(HAVE_GETIFADDRS) || !defined(HAVE_GETNAMEINFO) - - int sockfd = socket(AF_INET, SOCK_DGRAM, 0); - - char buf[8*1024]; - struct ifconf ifc; - ifc.ifc_len = sizeof(buf); - ifc.ifc_req = (struct ifreq *) buf; - int result=ioctl(sockfd, SIOCGIFCONF, &ifc); - - for (char* ptr = buf; ptr < buf + ifc.ifc_len;) { - struct ifreq *ifr =(struct ifreq *) ptr; -#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN - int len = sizeof(struct sockaddr); - if (ifr->ifr_addr.sa_len > len) - len = ifr->ifr_addr.sa_len; /* length > 16 */ - ptr += sizeof(ifr->ifr_name) + len; /* for next one in buffer */ -#else - ptr += sizeof(*ifr); /* for next one in buffer */ -#endif - - int flags; - struct sockaddr_in *sinptr; - MyNIC *tmp=0; - switch (ifr->ifr_addr.sa_family) { - case AF_INET: - sinptr = (struct sockaddr_in *) &ifr->ifr_addr; - flags=0; - - struct ifreq ifcopy; - ifcopy=*ifr; - result=ioctl(sockfd, SIOCGIFFLAGS, &ifcopy); - flags=ifcopy.ifr_flags; - - tmp=new MyNIC; - tmp->name=ifr->ifr_name; - tmp->state= ((flags & IFF_UP) == IFF_UP) ? upMessage : downMessage; - - if ((flags & IFF_BROADCAST) == IFF_BROADCAST) - tmp->type=i18nc("@item:intext Mode of network card", "Broadcast"); - else if ((flags & IFF_POINTOPOINT) == IFF_POINTOPOINT) - tmp->type=i18nc("@item:intext Mode of network card", "Point to Point"); - else if ((flags & IFF_MULTICAST) == IFF_MULTICAST) - tmp->type=i18nc("@item:intext Mode of network card", "Multicast"); - else if ((flags & IFF_LOOPBACK) == IFF_LOOPBACK) - tmp->type=i18nc("@item:intext Mode of network card", "Loopback"); - else - tmp->type=i18nc("@item:intext Mode of network card", "Unknown"); - - tmp->addr=inet_ntoa(sinptr->sin_addr); - - ifcopy=*ifr; - result=ioctl(sockfd, SIOCGIFNETMASK, &ifcopy); - if (result==0) { - sinptr = (struct sockaddr_in *) &ifcopy.ifr_addr; - tmp->netmask=inet_ntoa(sinptr->sin_addr); - } else - tmp->netmask=i18nc("Unknown network mask", "Unknown"); - - ifcopy=*ifr; - result=-1; // if none of the two #ifs below matches, ensure that result!=0 so that "Unknown" is returned as result -#if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_IFRU_IFRU_HWADDR) - result=ioctl(sockfd, SIOCGIFHWADDR, &ifcopy); - if (result==0) { - char *n = &ifcopy.ifr_ifru.ifru_hwaddr.sa_data[0]; - tmp->HWaddr = HWaddr2String(n); - } -#elif defined SIOCGENADDR - result=ioctl(sockfd,SIOCGENADDR,&ifcopy); - if (result==0) - { - char *n = &ifcopy.ifr_ifru.ifru_enaddr[0]; - tmp->HWaddr = HWaddr2String(n); - } -#endif - if (result!=0) { - tmp->HWaddr = i18nc("Unknown HWaddr", "Unknown"); - } - - nl.append(tmp); - break; - - default: - break; - } - } -#else - struct ifaddrs *ifap, *ifa; - if (getifaddrs(&ifap) != 0) { - return nl; - } - - MyNIC *tmp=0; - for (ifa = ifap; ifa; ifa = ifa->ifa_next) { - switch (ifa->ifa_addr->sa_family) { - case AF_INET6: - case AF_INET: { - tmp = new MyNIC; - tmp->name = ifa->ifa_name; - - char buf[128]; - - bzero(buf, 128); - getnameinfo(ifa->ifa_addr, ifa->ifa_addr->sa_len, buf, 127, 0, 0, NI_NUMERICHOST); - tmp->addr = buf; - - if (ifa->ifa_netmask != NULL) { - bzero(buf, 128); - getnameinfo(ifa->ifa_netmask, ifa->ifa_netmask->sa_len, buf, 127, 0, 0, NI_NUMERICHOST); - tmp->netmask = buf; - } +void KCMNic::update() +{ + m_list->clear(); + QList nics = findNICs(); - tmp->state= (ifa->ifa_flags & IFF_UP) ? upMessage : downMessage; - tmp->type = flags_tos(ifa->ifa_flags); + foreach(MyNIC* tmp, nics) { + QStringList lst; + lst << tmp->name << tmp->addr << tmp->netmask << tmp->type << tmp->state << tmp->HWaddr; + new QTreeWidgetItem(m_list,lst); - nl.append(tmp); - break; - } - default: - break; - } - } + delete tmp; + } - freeifaddrs(ifap); -#endif - return nl; + nics.clear(); } -#if defined(HAVE_GETNAMEINFO) && defined(HAVE_GETIFADDRS) -QString flags_tos (unsigned int flags) +QList findNICs() { - QString tmp; - if (flags & IFF_POINTOPOINT) { - tmp += i18n("Point to Point"); - } - - if (flags & IFF_BROADCAST) { - if (tmp.length()) { - tmp += QLatin1String(", "); - } - tmp += i18n("Broadcast"); - } - - if (flags & IFF_MULTICAST) { - if (tmp.length()) { - tmp += QLatin1String(", "); - } - tmp += i18n("Multicast"); - } - - if (flags & IFF_LOOPBACK) { - if (tmp.length()) { - tmp += QLatin1String(", "); - } - tmp += i18n("Loopback"); - } - return tmp; + QString upMessage(i18nc("State of network card is connected", "Up") ); + QString downMessage(i18nc("State of network card is disconnected", "Down") ); + + QList nl; + + foreach (const QNetworkInterface &iface, QNetworkInterface::allInterfaces()) { + const QNetworkInterface::InterfaceFlags flags = iface.flags(); + const QList adresses = iface.addressEntries(); + + MyNIC *tmp = new MyNIC; + tmp->name = iface.name(); + tmp->addr = (!adresses.isEmpty() ? adresses.first().ip().toString() : QString()); + tmp->netmask = (!adresses.isEmpty() ? adresses.first().netmask().toString() : QString()); + if (tmp->netmask.isEmpty()) { + tmp->netmask = i18nc("Unknown network mask", "Unknown"); + } + tmp->state = (flags & QNetworkInterface::IsUp) ? upMessage : downMessage; + + if (flags & QNetworkInterface::CanBroadcast) + tmp->type=i18nc("@item:intext Mode of network card", "Broadcast"); + else if (flags & QNetworkInterface::IsPointToPoint) + tmp->type=i18nc("@item:intext Mode of network card", "Point to Point"); + else if (flags & QNetworkInterface::CanMulticast) + tmp->type=i18nc("@item:intext Mode of network card", "Multicast"); + else if (flags & QNetworkInterface::IsLoopBack) + tmp->type=i18nc("@item:intext Mode of network card", "Loopback"); + else + tmp->type=i18nc("@item:intext Mode of network card", "Unknown"); + + tmp->HWaddr = iface.hardwareAddress(); + if (tmp->HWaddr.isEmpty()) { + tmp->HWaddr = i18nc("Unknown HWaddr", "Unknown"); + } + + nl.append(tmp); + } + + return nl; } -#endif #include "moc_nic.cpp" diff --git a/kinfocenter/Modules/nics/nic.h b/kinfocenter/Modules/nics/nic.h index 92febc2b..2d5d9533 100644 --- a/kinfocenter/Modules/nics/nic.h +++ b/kinfocenter/Modules/nics/nic.h @@ -29,16 +29,15 @@ class KCMNic:public KCModule { Q_OBJECT - public: - explicit KCMNic(QWidget *parent=0, const QVariantList &list = QVariantList( )); +public: + explicit KCMNic(QWidget *parent=0, const QVariantList &list = QVariantList( )); - protected Q_SLOTS: - void update(); +protected Q_SLOTS: + void update(); - protected: - QTreeWidget *m_list; - QPushButton *m_updateButton; +protected: + QTreeWidget *m_list; + QPushButton *m_updateButton; }; -#endif - +#endif // KCONTROL_NIC_H -- 2.11.0