6#include "validatorip_p.h"
10#include <QHostAddress>
13using namespace Qt::Literals::StringLiterals;
15const QRegularExpression ValidatorIpPrivate::regex{
16 u
"^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"_s};
19 Constraints constraints,
21 const QString &defValKey)
22 :
ValidatorRule(*new ValidatorIpPrivate(field, constraints, messages, defValKey))
34 const QString v =
value(params);
39 result.
value.setValue(v);
42 qCDebug(C_VALIDATOR).noquote() <<
"Not a valid IP address";
58 if (!
value.contains(QLatin1Char(
':')) && !
value.contains(ValidatorIpPrivate::regex)) {
65 static const std::vector<std::pair<QHostAddress, int>> ipv4Private(
68 {QHostAddress(QStringLiteral(
"10.0.0.0")), 8},
73 {QHostAddress(QStringLiteral(
"169.254.0.0")), 16},
77 {QHostAddress(QStringLiteral(
"172.16.0.0")), 12},
81 {QHostAddress(QStringLiteral(
"192.168.0.0")), 12}});
84 static const std::vector<std::pair<QHostAddress, int>> ipv4Reserved(
87 {QHostAddress(QStringLiteral(
"0.0.0.0")), 8},
91 {QHostAddress(QStringLiteral(
"100.64.0.0")), 10},
95 {QHostAddress(QStringLiteral(
"127.0.0.1")), 8},
99 {QHostAddress(QStringLiteral(
"192.0.0.0")), 24},
103 {QHostAddress(QStringLiteral(
"192.0.2.0")), 24},
107 {QHostAddress(QStringLiteral(
"192.88.99.0")), 24},
111 {QHostAddress(QStringLiteral(
"198.18.0.0")), 15},
115 {QHostAddress(QStringLiteral(
"198.51.100.0")), 24},
119 {QHostAddress(QStringLiteral(
"203.0.113.0")), 24},
123 {QHostAddress(QStringLiteral(
"240.0.0.0")), 4},
127 {QHostAddress(QStringLiteral(
"255.255.255.255")), 32}});
130 static const std::vector<std::pair<QHostAddress, int>> ipv6Private(
132 {QHostAddress(QStringLiteral(
"fc00::")), 7},
135 {QHostAddress(QStringLiteral(
"fe80::")), 10}});
138 static const std::vector<std::pair<QHostAddress, int>> ipv6Reserved(
140 {QHostAddress(QStringLiteral(
"::")), 128},
143 {QHostAddress(QStringLiteral(
"::1")), 128},
146 {QHostAddress(QStringLiteral(
"::ffff:0:0")), 96},
150 {QHostAddress(QStringLiteral(
"100::")), 64},
154 {QHostAddress(QStringLiteral(
"64:ff9b::")), 96},
157 {QHostAddress(QStringLiteral(
"2001::")), 32},
160 {QHostAddress(QStringLiteral(
"2001:10::")), 28},
163 {QHostAddress(QStringLiteral(
"2001:20::")), 28},
166 {QHostAddress(QStringLiteral(
"2001:db8::")), 32},
169 {QHostAddress(QStringLiteral(
"2002::")), 16}});
173 if (a.setAddress(
value)) {
177 if (a.protocol() == QAbstractSocket::IPv4Protocol) {
179 if (constraints.testFlag(
IPv6Only)) {
186 for (
const std::pair<QHostAddress, int> &subnet : ipv4Private) {
187 if (a.isInSubnet(subnet.first, subnet.second)) {
197 for (
const std::pair<QHostAddress, int> &subnet : ipv4Reserved) {
198 if (a.isInSubnet(subnet.first, subnet.second)) {
207 if (a.isInSubnet(QHostAddress(QStringLiteral(
"224.0.0.0")), 4)) {
214 if (constraints.testFlag(
IPv4Only)) {
221 for (
const std::pair<QHostAddress, int> &subnet : ipv6Private) {
222 if (a.isInSubnet(subnet.first, subnet.second)) {
232 for (
const std::pair<QHostAddress, int> &subnet : ipv6Reserved) {
233 if (a.isInSubnet(subnet.first, subnet.second)) {
242 if (a.isInSubnet(QHostAddress(QStringLiteral(
"ff00::")), 8)) {
260 const QString _label =
label(c);
261 if (_label.isEmpty()) {
263 return c->
qtTrId(
"cutelyst-valip-genvalerr");
267 return c->
qtTrId(
"cutelyst-valip-genvalerr-label").arg(_label);
QString qtTrId(const char *id, int n=-1) const
Checks if the field value is a valid IP address.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorIp(const QString &field, Constraints constraints=NoConstraint, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap ¶ms) const
QMultiMap< QString, QString > ParamsMultiMap
static bool validate(const QString &value, Constraints constraints=NoConstraint)
Returns true if value is a valid IP address within the constraints.
The Cutelyst namespace holds all public Cutelyst API.
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.