Verify and fix Ip4Address type field edit

This commit is contained in:
Srivats P 2021-12-06 20:56:40 +05:30
parent 9385b31bc5
commit 7cf9c91014
3 changed files with 22 additions and 36 deletions

View File

@ -29,6 +29,7 @@ void FieldEdit::setType(FieldType type)
{
// clear existing contents before changing the validator
clear();
setPlaceholderText("");
type_ = type;
switch (type_) {
@ -40,6 +41,7 @@ void FieldEdit::setType(FieldType type)
break;
case kIp4Address:
setValidator(&ip4Validator_);
setPlaceholderText("0.0.0.0");
break;
case kIp6Address:
setValidator(&ip6Validator_);
@ -65,6 +67,9 @@ QString FieldEdit::text() const
str.remove(QRegularExpression("[:-]"));
str.prepend("0x");
break;
case kIp4Address:
str = QString::number(QHostAddress(str).toIPv4Address());
break;
default:
break;
}

View File

@ -90,12 +90,19 @@ void FindReplaceDialog::on_field_currentIndexChanged(int index)
if (index < 0)
return;
QString fieldName = field->currentText();
FieldAttrib fieldAttrib = fieldAttrib_.at(index);
// Use heuristics to determine field type
if (fieldAttrib.bitSize == 48) {
findValue->setType(FieldEdit::kMacAddress);
replaceValue->setType(FieldEdit::kMacAddress);
} else if ((fieldAttrib.bitSize == 32)
&& (fieldName.contains(QRegularExpression(
"address|source|destination",
QRegularExpression::CaseInsensitiveOption)))) {
findValue->setType(FieldEdit::kIp4Address);
replaceValue->setType(FieldEdit::kIp4Address);
} else {
qDebug("XXXXXX %s bitSize %d max %llx",
qPrintable(field->currentText()),

View File

@ -21,54 +21,28 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#define _IPV4_ADDRESS_VALIDATOR_H
#include <QHostAddress>
#include <QValidator>
#include <QRegularExpressionValidator>
class IPv4AddressValidator : public QValidator
class IPv4AddressValidator : public QRegularExpressionValidator
{
public:
IPv4AddressValidator(QObject *parent = 0)
: QValidator(parent)
: QRegularExpressionValidator(
QRegularExpression(
"((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)(\\.(?!$)|$)){4}"),
parent)
{
_ip4ValidChars.setPattern("[0-9]{1,3}(.[0-9]{1,3}){0,4}");
}
~IPv4AddressValidator() {}
virtual QValidator::State validate(QString &input, int& /*pos*/) const
{
QValidator::State state;
QHostAddress addr(input);
qDebug("%s: %s", __FUNCTION__, qPrintable(input));
if (addr.protocol() == QAbstractSocket::IPv4Protocol)
state = Acceptable;
else
if (_ip4ValidChars.exactMatch(input))
state = Intermediate;
else
state = Invalid;
qDebug("%s(%d): %s, ", __FUNCTION__, state, qPrintable(input));
return state;
}
virtual void fixup(QString &input) const
{
input.append(".0.0.0.0");
QHostAddress addr(input);
int len = input.size();
QStringList bytes = input.split('.', QString::SkipEmptyParts);
qDebug("%s: %s", __FUNCTION__, qPrintable(input));
while (bytes.count() < 4)
bytes.append("0");
while (addr.protocol() != QAbstractSocket::IPv4Protocol)
{
len--;
Q_ASSERT(len >= 0);
addr.setAddress(input.left(len));
}
input = addr.toString();
input = bytes.join('.');
}
private:
QRegExp _ip4ValidChars;
};
#endif