[pve-devel] [PATCH] Improved ipv4 addresses validation (sub pve_verify_ipv4 line 139)

damien piquet piqudam at gmail.com
Fri Feb 22 21:59:52 CET 2013


Hi all !

Sent a patch to improve ipv4 validation procedure. Now you can enter
ip addresses like 10.1.0.0 (final 0 was denied in previous version).

However, this is a classful validation, a better way to do this would
be comparing the address with the associated mask.

Is it possible to send the mask to this function, or should we merge
the two functions in a new one ?

Damien

2013/2/22 Damien PIQUET <piqudam at gmail.com>:
>
> Signed-off-by: Damien PIQUET <piqudam at gmail.com>
> ---
>  data/PVE/JSONSchema.pm |   45 +++++++++++++++++++++++++++++++++++++++------
>  1 file changed, 39 insertions(+), 6 deletions(-)
>
> diff --git a/data/PVE/JSONSchema.pm b/data/PVE/JSONSchema.pm
> index 4b33646..d0d11f8 100644
> --- a/data/PVE/JSONSchema.pm
> +++ b/data/PVE/JSONSchema.pm
> @@ -140,14 +140,47 @@ register_format('ipv4', \&pve_verify_ipv4);
>  sub pve_verify_ipv4 {
>      my ($ipv4, $noerr) = @_;
>
> -   if ($ipv4 !~ m/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ ||
> -       !(($1 > 0) && ($1 < 255) &&
> -        ($2 <= 255) && ($3 <= 255) &&
> -        ($4 > 0) && ($4 < 255)))  {
> -          return undef if $noerr;
> +    my $valid = 0;
> +
> +    if ($ipv4 =~ m/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) {
> +       # got a class A address
> +       if ($1 < 128) {
> +          if (($1 > 0)&&
> +             ($2 <= 255)&&($2 >= 0) &&
> +             ($3 <= 255)&&($3 >= 0) &&
> +             ($4 <= 255)&&($4 >= 0) &&
> +             ($2+$3+$4 > 0)&&($2+$3+$4 < 765)) {
> +                 $valid = 1;
> +          }
> +       }
> +       # got a class B address
> +       elsif ($1 < 192) {
> +          if (($1 > 0)&&
> +             ($2 <= 255)&&($2 >= 0) &&
> +             ($3 <= 255)&&($3 >= 0) &&
> +             ($4 < 255)&&($4 > 0) &&
> +             ($3+$4 > 0)&&($3+$4 < 510)) {
> +                 $valid = 1;
> +          }
> +       }
> +       # got a class C address
> +       else {
> +          if (($1 > 0)&&
> +             ($2 <= 255)&&($2 >= 0) &&
> +             ($3 <= 255)&&($3 >= 0) &&
> +             ($4 < 255)&&($4 > 0)) {
> +                 $valid = 1;
> +          }
> +       }
> +    }
> +
> +    if($valid) {
> +       return $ipv4;
> +    }
> +    else {
> +       return undef if $noerr;
>         die "value does not look like a valid IP address\n";
>      }
> -    return $ipv4;
>  }
>
>  my $ipv4_mask_hash = {
> --
> 1.7.10.4
>



More information about the pve-devel mailing list