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

Damien PIQUET piqudam at gmail.com
Fri Feb 22 21:49:38 CET 2013


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