10

I thought QHostAddress was it, but it strangely does not provide methods for validating whether or not the IP address is valid (anymore, got deprecated to Qt3).

Does anyone know?

sivabudh
  • 31,807
  • 63
  • 162
  • 228
  • When you say "methods for validating whether or not the IP address is valid", are you referring to the `isIPv4Address()` and `isIPv6Address()` methods or something else? – RA. Feb 10 '10 at 21:44
  • @RA, yes you are correct. Basically, I just wanted a class that would take QString, and tells me whether or not the QString is a valid IP address. I thought QHostAddress was that one, but probably not. – sivabudh Feb 10 '10 at 21:51
  • 1
    I believe that isIPv4Address() and isIPv6Address() are now deprecated because it's grouped under QT_3 (?) – sivabudh Feb 10 '10 at 21:52
  • I have same issue but those answers didn't resolve mine. Ex: QHostAddress ip("1.2"); ip.isNull(); //always true (I want false here). – aviit Jun 02 '15 at 11:30

6 Answers6

8

There is an alternative to using isIpv4Address() and isIPv6Address(). For example:

QHostAddress address(myString);
if (QAbstractSocket::IPv4Protocol == address.protocol())
{
   qDebug("Valid IPv4 address.");
}
else if (QAbstractSocket::IPv6Protocol == address.protocol())
{
   qDebug("Valid IPv6 address.");
}
else
{
   qDebug("Unknown or invalid address.");
}

See also:

http://doc.qt.digia.com/4.6/qhostaddress.html#protocol

Hope this helps.

mavroprovato
  • 8,023
  • 5
  • 37
  • 52
RA.
  • 7,542
  • 1
  • 34
  • 35
8

Here is the official answer from Nokia support engineer, name removed for privacy protection:

I posted a question on stackoverflow.com as follow:

Does Qt provide a class that represents an IP address?

You can see that someone posted a solution to my question already.

However, I want to ask how come Nokia doesn't just provide a method to

QHostAddress ( like isValid() ) that will check the host address's validity?

Thank you for your inquiry. You can use the isNull() method to check the validity. It will return true for invalid addresses: http://doc.qt.digia.com/4.6/qhostaddress.html#isNull

Hope this helps.

Regards,

Support Engineer, Qt Development Frameworks, Nokia

Community
  • 1
  • 1
sivabudh
  • 31,807
  • 63
  • 162
  • 228
4

The bool return value of QHostAddress::setAddress(const QString &address) tells if the string is successfully parsed as an IPv4 or IPv6 address.

QHostAddress addr;
if (addr.setAddress(myString)) {
    // valid
} else {
    // invalid
}

http://doc.qt.io/qt-5/qhostaddress.html#setAddress-5

rolevax
  • 1,670
  • 1
  • 14
  • 21
2

I found all of the above solutions to be unreliable for IPv4 addresses at least. For example when '192' and '192.' were used to create a QHostAddress. Both .setAddress() and if(QAbstractSocket::IPv4Protocol == address.protocol() returned true!

I found a more robust solution is to check the IP address string format using QRegularExpression and QRegularExpressionValidator.

An example of a good IPv4 regular expression can be found here

How to set Input Mask and QValidator to a QLineEdit at a time in Qt?

Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
PeterCAN76
  • 61
  • 4
1

For those seeking a solution, this approach takes an input string and verifies its proper format, ensuring there are no reported issues, as previously mentioned in the responses.

Example: IP addresses like 192.168.1.1 or 0.0.0.0 are considered valid, while formats such as 192.168.01.1, 192.168.1.010, or 192.168.001.1 are deemed invalid.

bool ipValidator(QString ip)
{
    static QRegularExpression ipPattern("^((25[0-5]|2[0-4][0-9]|[01]?[0-9]{1,2})\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9]{1,2})$");
    QRegularExpressionMatch match = ipPattern.match(ip);
    if(!match.hasMatch())
        return false;

    QStringList parts = ip.split('.');

    for (int i = 0; i < parts.size(); ++i)
    {
        const QString &part = parts.at(i);
        bool ok;
        int num = part.toInt(&ok);
        if (!ok || num < 0 || num > 255 || (part.length() > 1 && part.startsWith("0") && !part.startsWith("0.")))
            return false;
    }

    //if you wish, you could end immediately simply here
    //return true;


    //If you want to add one more control at the end you can add this
    QHostAddress ipAddress(ip);
    if(ipAddress.protocol() == QAbstractSocket::IPv4Protocol)
        return true;
    else
        return false;
}

You need to import the library if you want to use the last control.

#include <QHostAddress>
porrokynoa
  • 42
  • 6
0

As Harsha Biyani mentioned, there is a bug in function parseIp4Internal, (qtbase\src\corelib\io\qipaddress.cpp). IPv4 address parsing is not correct. QHostAddress uses that function to resolve IPv4 addresses.

UPD. Addresses like "1","1.1" are considered as correct addresses and are equivalent to "0.0.0.1" and "1.0.0.1" respectively.