~mateo-salta/nitroshare/nitroshare

« back to all changes in this revision

Viewing changes to src/util/network.cpp

  • Committer: Nathan Osman
  • Date: 2012-06-29 22:01:55 UTC
  • Revision ID: admin@quickmediasolutions.com-20120629220155-ogwrn9fxxve9wyw1
Moved a TON of the broadcast and discovery code around, creating separate classes for each and partially implementing their use in the first start wizard.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
#include <util/network.h>
20
20
#include <util/settings.h>
21
21
 
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
24
{
 
25
    /* Check each address to determine if it is a valid IPv4 address. */
26
26
    foreach(QNetworkAddressEntry entry, interface.addressEntries())
27
 
    {
28
 
        // If the user wants a valid IP address, check for one
29
 
        if(desired_address == IPAddress && entry.ip().toIPv4Address())
30
 
        {
31
 
            if(address) *address = entry.ip();
32
 
            return true;
33
 
        }
34
 
        else if(desired_address == BroadcastAddress && entry.broadcast().toIPv4Address())
35
 
        {
36
 
            if(address) *address = entry.broadcast();
37
 
            return true;
38
 
        }
39
 
    }
40
 
 
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())
 
28
        {
 
29
            if(ip)        *ip        = entry.ip();
 
30
            if(broadcast) *broadcast = entry.broadcast();
 
31
            if(netmask)   *netmask   = entry.netmask();
 
32
 
 
33
            return true;
 
34
        }
 
35
 
 
36
    /* If we've come this far, there is no IPv4 addresses on the specified interface. */
43
37
    return false;
44
38
}
45
39
 
46
40
bool Network::IsInterfaceUsable(QNetworkInterface interface)
47
41
{
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
 
44
       - it's up & running
 
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);
58
52
}
59
53
 
60
54
QList<QNetworkInterface> Network::FindUsableInterfaces()