QNetworkInterface: replace CMake check for ifr_index with SFINAE

Detecting if a member exists is very easy and saves us from running the
test at CMake time. Saves about 350 ms.

Change-Id: I2b24e1d3cad44897906efffd178007fdcdd18e37
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Thiago Macieira 2023-08-29 19:24:06 -07:00
parent 88e9ce201b
commit 62b53011d3
2 changed files with 14 additions and 36 deletions

View File

@ -37,22 +37,6 @@ freeifaddrs(list);
"# FIXME: use: unmapped library: network
)
# ifr_index
qt_config_compile_test(ifr_index
LABEL "ifr_index"
CODE
"#include <net/if.h>
int main(void)
{
/* BEGIN TEST: */
struct ifreq req;
req.ifr_index = 0;
/* END TEST: */
return 0;
}
")
# ipv6ifname
qt_config_compile_test(ipv6ifname
LABEL "IPv6 ifname"
@ -212,10 +196,6 @@ qt_feature("getifaddrs" PUBLIC
CONDITION UNIX AND NOT QT_FEATURE_linux_netlink AND TEST_getifaddrs
)
qt_feature_definition("getifaddrs" "QT_NO_GETIFADDRS" NEGATE VALUE "1")
qt_feature("ifr_index" PRIVATE
LABEL "ifr_index"
CONDITION UNIX AND NOT QT_FEATURE_linux_netlink AND TEST_ifr_index
)
qt_feature("ipv6ifname" PUBLIC
LABEL "IPv6 ifname"
CONDITION UNIX AND NOT QT_FEATURE_linux_netlink AND TEST_ipv6ifname

View File

@ -52,7 +52,18 @@ static QHostAddress addressFromSockaddr(sockaddr *sa, int ifindex = 0, const QSt
}
}
return address;
}
template <typename Req> [[maybe_unused]]
static auto &ifreq_index(Req &req, std::enable_if_t<sizeof(std::declval<Req>().ifr_index) != 0, int> = 0)
{
return req.ifr_index;
}
template <typename Req> [[maybe_unused]]
static auto &ifreq_index(Req &req, std::enable_if_t<sizeof(std::declval<Req>().ifr_ifindex) != 0, int> = 0)
{
return req.ifr_ifindex;
}
uint QNetworkInterfaceManager::interfaceIndexFromName(const QString &name)
@ -71,11 +82,7 @@ uint QNetworkInterfaceManager::interfaceIndexFromName(const QString &name)
uint id = 0;
if (qt_safe_ioctl(socket, SIOCGIFINDEX, &req) >= 0)
# if QT_CONFIG(ifr_index)
id = req.ifr_index;
# else
id = req.ifr_ifindex;
# endif
id = ifreq_index(req);
qt_safe_close(socket);
return id;
#else
@ -95,12 +102,7 @@ QString QNetworkInterfaceManager::interfaceNameFromIndex(uint index)
int socket = qt_safe_socket(AF_INET, SOCK_STREAM, 0);
if (socket >= 0) {
memset(&req, 0, sizeof(ifreq));
# if QT_CONFIG(ifr_index)
req.ifr_index = index;
# else
req.ifr_ifindex = index;
# endif
ifreq_index(req) = index;
if (qt_safe_ioctl(socket, SIOCGIFNAME, &req) >= 0) {
qt_safe_close(socket);
return QString::fromLatin1(req.ifr_name);
@ -185,11 +187,7 @@ static QNetworkInterfacePrivate *findInterface(int socket, QList<QNetworkInterfa
// Get the interface index
# ifdef SIOCGIFINDEX
if (qt_safe_ioctl(socket, SIOCGIFINDEX, &req) >= 0)
# if QT_CONFIG(ifr_index)
ifindex = req.ifr_index;
# else
ifindex = req.ifr_ifindex;
# endif
ifindex = ifreq_index(req);
# else
ifindex = if_nametoindex(req.ifr_name);
# endif