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

View File

@ -90,12 +90,19 @@ void FindReplaceDialog::on_field_currentIndexChanged(int index)
if (index < 0) if (index < 0)
return; return;
QString fieldName = field->currentText();
FieldAttrib fieldAttrib = fieldAttrib_.at(index); FieldAttrib fieldAttrib = fieldAttrib_.at(index);
// Use heuristics to determine field type // Use heuristics to determine field type
if (fieldAttrib.bitSize == 48) { if (fieldAttrib.bitSize == 48) {
findValue->setType(FieldEdit::kMacAddress); findValue->setType(FieldEdit::kMacAddress);
replaceValue->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 { } else {
qDebug("XXXXXX %s bitSize %d max %llx", qDebug("XXXXXX %s bitSize %d max %llx",
qPrintable(field->currentText()), 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 #define _IPV4_ADDRESS_VALIDATOR_H
#include <QHostAddress> #include <QHostAddress>
#include <QValidator> #include <QRegularExpressionValidator>
class IPv4AddressValidator : public QValidator class IPv4AddressValidator : public QRegularExpressionValidator
{ {
public: public:
IPv4AddressValidator(QObject *parent = 0) 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 virtual void fixup(QString &input) const
{ {
input.append(".0.0.0.0"); QStringList bytes = input.split('.', QString::SkipEmptyParts);
QHostAddress addr(input);
int len = input.size();
qDebug("%s: %s", __FUNCTION__, qPrintable(input)); while (bytes.count() < 4)
bytes.append("0");
while (addr.protocol() != QAbstractSocket::IPv4Protocol) input = bytes.join('.');
{
len--;
Q_ASSERT(len >= 0);
addr.setAddress(input.left(len));
}
input = addr.toString();
} }
private:
QRegExp _ip4ValidChars;
}; };
#endif #endif