6#include "validatorip_p.h"
10#include <QHostAddress>
14const QRegularExpression ValidatorIpPrivate::regex{
15 u
"^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"_qs};
18 Constraints constraints,
20 const QString &defValKey)
21 :
ValidatorRule(*new ValidatorIpPrivate(field, constraints, messages, defValKey))
33 const QString v =
value(params);
38 result.
value.setValue(v);
41 qCDebug(C_VALIDATOR).noquote() <<
"Not a valid IP address";
57 if (!
value.contains(QLatin1Char(
':')) && !
value.contains(ValidatorIpPrivate::regex)) {
64 static const std::vector<std::pair<QHostAddress, int>> ipv4Private(
67 {QHostAddress(QStringLiteral(
"10.0.0.0")), 8},
72 {QHostAddress(QStringLiteral(
"169.254.0.0")), 16},
76 {QHostAddress(QStringLiteral(
"172.16.0.0")), 12},
80 {QHostAddress(QStringLiteral(
"192.168.0.0")), 12}});
83 static const std::vector<std::pair<QHostAddress, int>> ipv4Reserved(
86 {QHostAddress(QStringLiteral(
"0.0.0.0")), 8},
90 {QHostAddress(QStringLiteral(
"100.64.0.0")), 10},
94 {QHostAddress(QStringLiteral(
"127.0.0.1")), 8},
98 {QHostAddress(QStringLiteral(
"192.0.0.0")), 24},
102 {QHostAddress(QStringLiteral(
"192.0.2.0")), 24},
106 {QHostAddress(QStringLiteral(
"192.88.99.0")), 24},
110 {QHostAddress(QStringLiteral(
"198.18.0.0")), 15},
114 {QHostAddress(QStringLiteral(
"198.51.100.0")), 24},
118 {QHostAddress(QStringLiteral(
"203.0.113.0")), 24},
122 {QHostAddress(QStringLiteral(
"240.0.0.0")), 4},
126 {QHostAddress(QStringLiteral(
"255.255.255.255")), 32}});
129 static const std::vector<std::pair<QHostAddress, int>> ipv6Private(
131 {QHostAddress(QStringLiteral(
"fc00::")), 7},
134 {QHostAddress(QStringLiteral(
"fe80::")), 10}});
137 static const std::vector<std::pair<QHostAddress, int>> ipv6Reserved(
139 {QHostAddress(QStringLiteral(
"::")), 128},
142 {QHostAddress(QStringLiteral(
"::1")), 128},
145 {QHostAddress(QStringLiteral(
"::ffff:0:0")), 96},
149 {QHostAddress(QStringLiteral(
"100::")), 64},
153 {QHostAddress(QStringLiteral(
"64:ff9b::")), 96},
156 {QHostAddress(QStringLiteral(
"2001::")), 32},
159 {QHostAddress(QStringLiteral(
"2001:10::")), 28},
162 {QHostAddress(QStringLiteral(
"2001:20::")), 28},
165 {QHostAddress(QStringLiteral(
"2001:db8::")), 32},
168 {QHostAddress(QStringLiteral(
"2002::")), 16}});
172 if (a.setAddress(
value)) {
176 if (a.protocol() == QAbstractSocket::IPv4Protocol) {
178 if (constraints.testFlag(
IPv6Only)) {
185 for (
const std::pair<QHostAddress, int> &subnet : ipv4Private) {
186 if (a.isInSubnet(subnet.first, subnet.second)) {
196 for (
const std::pair<QHostAddress, int> &subnet : ipv4Reserved) {
197 if (a.isInSubnet(subnet.first, subnet.second)) {
206 if (a.isInSubnet(QHostAddress(QStringLiteral(
"224.0.0.0")), 4)) {
213 if (constraints.testFlag(
IPv4Only)) {
220 for (
const std::pair<QHostAddress, int> &subnet : ipv6Private) {
221 if (a.isInSubnet(subnet.first, subnet.second)) {
231 for (
const std::pair<QHostAddress, int> &subnet : ipv6Reserved) {
232 if (a.isInSubnet(subnet.first, subnet.second)) {
241 if (a.isInSubnet(QHostAddress(QStringLiteral(
"ff00::")), 8)) {
260 const QString _label =
label(c);
261 if (_label.isEmpty()) {
262 error = c->
translate(
"Cutelyst::ValidatorIp",
"IP address is invalid or not acceptable.");
265 error = c->
translate(
"Cutelyst::ValidatorIp",
266 "The IP address in the “%1” field is invalid or not acceptable.")
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Checks if the field value is a valid IP address.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
ValidatorIp(const QString &field, Constraints constraints=NoConstraint, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ip validator.
~ValidatorIp() override
Deconstructs the ip validator.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
void defaultValue(Context *c, ValidatorReturnType *result) const
I a defValKey has been set in the constructor, this will try to get the default value from the stash ...
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
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.
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.