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:
parent
88e9ce201b
commit
62b53011d3
@ -37,22 +37,6 @@ freeifaddrs(list);
|
|||||||
"# FIXME: use: unmapped library: network
|
"# 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
|
# ipv6ifname
|
||||||
qt_config_compile_test(ipv6ifname
|
qt_config_compile_test(ipv6ifname
|
||||||
LABEL "IPv6 ifname"
|
LABEL "IPv6 ifname"
|
||||||
@ -212,10 +196,6 @@ qt_feature("getifaddrs" PUBLIC
|
|||||||
CONDITION UNIX AND NOT QT_FEATURE_linux_netlink AND TEST_getifaddrs
|
CONDITION UNIX AND NOT QT_FEATURE_linux_netlink AND TEST_getifaddrs
|
||||||
)
|
)
|
||||||
qt_feature_definition("getifaddrs" "QT_NO_GETIFADDRS" NEGATE VALUE "1")
|
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
|
qt_feature("ipv6ifname" PUBLIC
|
||||||
LABEL "IPv6 ifname"
|
LABEL "IPv6 ifname"
|
||||||
CONDITION UNIX AND NOT QT_FEATURE_linux_netlink AND TEST_ipv6ifname
|
CONDITION UNIX AND NOT QT_FEATURE_linux_netlink AND TEST_ipv6ifname
|
||||||
|
@ -52,7 +52,18 @@ static QHostAddress addressFromSockaddr(sockaddr *sa, int ifindex = 0, const QSt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return address;
|
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)
|
uint QNetworkInterfaceManager::interfaceIndexFromName(const QString &name)
|
||||||
@ -71,11 +82,7 @@ uint QNetworkInterfaceManager::interfaceIndexFromName(const QString &name)
|
|||||||
|
|
||||||
uint id = 0;
|
uint id = 0;
|
||||||
if (qt_safe_ioctl(socket, SIOCGIFINDEX, &req) >= 0)
|
if (qt_safe_ioctl(socket, SIOCGIFINDEX, &req) >= 0)
|
||||||
# if QT_CONFIG(ifr_index)
|
id = ifreq_index(req);
|
||||||
id = req.ifr_index;
|
|
||||||
# else
|
|
||||||
id = req.ifr_ifindex;
|
|
||||||
# endif
|
|
||||||
qt_safe_close(socket);
|
qt_safe_close(socket);
|
||||||
return id;
|
return id;
|
||||||
#else
|
#else
|
||||||
@ -95,12 +102,7 @@ QString QNetworkInterfaceManager::interfaceNameFromIndex(uint index)
|
|||||||
int socket = qt_safe_socket(AF_INET, SOCK_STREAM, 0);
|
int socket = qt_safe_socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (socket >= 0) {
|
if (socket >= 0) {
|
||||||
memset(&req, 0, sizeof(ifreq));
|
memset(&req, 0, sizeof(ifreq));
|
||||||
# if QT_CONFIG(ifr_index)
|
ifreq_index(req) = index;
|
||||||
req.ifr_index = index;
|
|
||||||
# else
|
|
||||||
req.ifr_ifindex = index;
|
|
||||||
# endif
|
|
||||||
|
|
||||||
if (qt_safe_ioctl(socket, SIOCGIFNAME, &req) >= 0) {
|
if (qt_safe_ioctl(socket, SIOCGIFNAME, &req) >= 0) {
|
||||||
qt_safe_close(socket);
|
qt_safe_close(socket);
|
||||||
return QString::fromLatin1(req.ifr_name);
|
return QString::fromLatin1(req.ifr_name);
|
||||||
@ -185,11 +187,7 @@ static QNetworkInterfacePrivate *findInterface(int socket, QList<QNetworkInterfa
|
|||||||
// Get the interface index
|
// Get the interface index
|
||||||
# ifdef SIOCGIFINDEX
|
# ifdef SIOCGIFINDEX
|
||||||
if (qt_safe_ioctl(socket, SIOCGIFINDEX, &req) >= 0)
|
if (qt_safe_ioctl(socket, SIOCGIFINDEX, &req) >= 0)
|
||||||
# if QT_CONFIG(ifr_index)
|
ifindex = ifreq_index(req);
|
||||||
ifindex = req.ifr_index;
|
|
||||||
# else
|
|
||||||
ifindex = req.ifr_ifindex;
|
|
||||||
# endif
|
|
||||||
# else
|
# else
|
||||||
ifindex = if_nametoindex(req.ifr_name);
|
ifindex = if_nametoindex(req.ifr_name);
|
||||||
# endif
|
# endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user