19
19
#include <util/network.h>
20
20
#include <util/settings.h>
22
bool Network::FindIPv4Address(QNetworkInterface interface,
23
AddressType desired_address,
24
QHostAddress * address)
22
bool Network::FindIPv4Address(QNetworkInterface interface, QHostAddress * ip,
23
QHostAddress * broadcast, QHostAddress * netmask)
25
/* Check each address to determine if it is a valid IPv4 address. */
26
26
foreach(QNetworkAddressEntry entry, interface.addressEntries())
28
// If the user wants a valid IP address, check for one
29
if(desired_address == IPAddress && entry.ip().toIPv4Address())
31
if(address) *address = entry.ip();
34
else if(desired_address == BroadcastAddress && entry.broadcast().toIPv4Address())
36
if(address) *address = entry.broadcast();
41
// If we've come this far, there is no IPv4 addresses on the
42
// specified interface of the specified type
27
if(entry.ip().toIPv4Address())
29
if(ip) *ip = entry.ip();
30
if(broadcast) *broadcast = entry.broadcast();
31
if(netmask) *netmask = entry.netmask();
36
/* If we've come this far, there is no IPv4 addresses on the specified interface. */
46
40
bool Network::IsInterfaceUsable(QNetworkInterface interface)
48
// We consider an interface usable if it meets the following criteria
49
// * we can broadcast on it
50
// * it's up & running
51
// * it isn't the loopback device (we may make this an option for testing sometime)
52
// * it contains at least one IPv4 address
42
/* We consider an interface usable if it meets the following criteria
43
- we can broadcast on it
45
- it isn't the loopback device (we may make this an option for testing sometime)
46
- it contains at least one IPv4 address */
53
47
return interface.flags() & (QNetworkInterface::CanBroadcast |
54
48
QNetworkInterface::IsUp |
55
49
QNetworkInterface::IsRunning) &&
56
50
~interface.flags() & QNetworkInterface::IsLoopBack &&
57
FindIPv4Address(interface, IPAddress);
51
FindIPv4Address(interface);
60
54
QList<QNetworkInterface> Network::FindUsableInterfaces()