[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [OSSTEST PATCH 1/7] DhcpWatch: extract dhcp3 parsing into separate function



Preparation for dnsmasq support. Keep generic code in check_ip, but
extract format-specific handling into separat function. No intentional
behaviour change (besides slightly different warning reporting).

Signed-off-by: Marek Marczykowski-Górecki <marmarek@xxxxxxxxxxxxxxxxxxxxxx>
---
 Osstest/DhcpWatch/leases.pm | 131 ++++++++++++++++++++-----------------
 1 file changed, 71 insertions(+), 60 deletions(-)

diff --git a/Osstest/DhcpWatch/leases.pm b/Osstest/DhcpWatch/leases.pm
index 98c22b9..5ec305f 100644
--- a/Osstest/DhcpWatch/leases.pm
+++ b/Osstest/DhcpWatch/leases.pm
@@ -48,68 +48,20 @@ sub new {
     }, $class;
 }
 
-sub check_ip ($$) {
-    my ($mo, $gho) = @_;
-
-    my $leases;
-    my $leasesfn = $mo->{Source};
-
-    if ($leasesfn =~ m,/,) {
-       $leases= new IO::File $leasesfn, 'r';
-       if (!defined $leases) { return "open $leasesfn: $!"; }
-    } else {
-       $leases= new IO::Socket::INET(PeerAddr => $leasesfn);
-       if (!defined $leases) { return "connect to $leasesfn: $!"; }
-    }
-
-    my $lstash= "dhcpleases-$gho->{Guest}";
+sub parse_dhcp_dhcp3($$$$) {
+    my ($gho, $leases, $copy, $leasesfn) = @_;
     my $inlease;
     my $props;
     my $best;
-    my @warns;
-
-    my $copy= new IO::File "$stash/$lstash.new", 'w';
-    $copy or die "$lstash.new $!";
-
-    my $saveas= sub {
-        my ($fn,$keep) = @_;
-
-        while (<$leases>) { print $copy $_ or die $!; }
-        die $! unless $leases->eof;
-
-        my $rename= sub {
-            my ($src,$dst) = @_;
-            rename "$stash/$src", "$stash/$dst"
-                or $!==&ENOENT
-                or die "rename $fn.$keep $!";
-        };
-        while (--$keep>0) {
-            $rename->("$fn.$keep", "$fn.".($keep+1));
-        }
-        if ($keep>=0) {
-            die if $keep;
-            $rename->("$fn", "$fn.$keep");
-        }
-        $copy->close();
-        rename "$stash/$lstash.new", "$stash/$fn" or die "$lstash.new $fn $!";
-        logm("warning: $_") foreach grep { defined } @warns[0..5];
-        # logm("$fn: rotated and stashed current leases");
-    };
-
-    my $badleases= sub {
-        my ($m) = @_;
-        $m= "$leasesfn:$.: unknown syntax";
-        $saveas->("$lstash.bad", 7);
-        return $m;
-    };
 
     while (<$leases>) {
         print $copy $_ or die $!;
 
         chomp; s/^\s+//; s/\s+$//;
         next if m/^\#/;  next unless m/\S/;
+
         if (m/^lease\s+([0-9.]+)\s+\{$/) {
-            return $badleases->("lease inside lease") if defined $inlease;
+            die "lease inside lease" if defined $inlease;
             $inlease= $1;
             $props= { };
             next;
@@ -122,13 +74,13 @@ sub check_ip ($$) {
             s/^( [-a-z0-9]+
                ) \s+//x
                or
-              return $badleases->("unknown syntax");
+              die "unknown syntax";
             my $prop= $1;
-            s/\s*\;$// or return $badleases->("missing semicolon");
+            s/\s*\;$// or die "missing semicolon";
             $props->{$prop}= $_;
             next;
         }
-        return $badleases->("end lease not inside lease")
+        die "end lease not inside lease"
             unless defined $inlease;
 
         $props->{' addr'}= $inlease;
@@ -144,25 +96,84 @@ sub check_ip ($$) {
         my @missing= grep { !defined $props->{$_} }
             ('binding state', 'hardware ethernet', 'ends');
         if (@missing) {
-            push @warns, "$leasesfn:$.: lease without \`$_'"
+            warn "$leasesfn:$.: lease without \`$_'"
                 foreach @missing;
             next;
         }
 
-        # ignore leases for other hosts
-        next unless lc $props->{'hardware ethernet'} eq lc $gho->{Ether};
-
         $props->{' ends'}= $props->{'ends'};
         $props->{' ends'} =~
             s/^[0-6]\s+(\S+)\s+(\d+)\:(\d+\:\d+)$/
                 sprintf "%s %02d:%s", $1,$2,$3 /e
-                or return $badleases->("unexpected syntax for ends");
+                or die "unexpected syntax for ends";
+
+        # ignore leases for other hosts
+        next unless lc $props->{'hardware ethernet'} eq lc $gho->{Ether};
 
         next if $best &&
             $best->{' ends'} gt $props->{' ends'};
         $best= $props;
     }
 
+    return $best;
+}
+
+sub check_ip ($$) {
+    my ($mo, $gho) = @_;
+
+    my $leases;
+    my $leasesfn = $mo->{Source};
+
+    if ($leasesfn =~ m,/,) {
+       $leases= new IO::File $leasesfn, 'r';
+       if (!defined $leases) { return "open $leasesfn: $!"; }
+    } else {
+       $leases= new IO::Socket::INET(PeerAddr => $leasesfn);
+       if (!defined $leases) { return "connect to $leasesfn: $!"; }
+    }
+
+    my $lstash= "dhcpleases-$gho->{Guest}";
+    my $best;
+
+    my $copy= new IO::File "$stash/$lstash.new", 'w';
+    $copy or die "$lstash.new $!";
+
+    my $saveas= sub {
+        my ($fn,$keep) = @_;
+
+        while (<$leases>) { print $copy $_ or die $!; }
+        die $! unless $leases->eof;
+
+        my $rename= sub {
+            my ($src,$dst) = @_;
+            rename "$stash/$src", "$stash/$dst"
+                or $!==&ENOENT
+                or die "rename $fn.$keep $!";
+        };
+        while (--$keep>0) {
+            $rename->("$fn.$keep", "$fn.".($keep+1));
+        }
+        if ($keep>=0) {
+            die if $keep;
+            $rename->("$fn", "$fn.$keep");
+        }
+        $copy->close();
+        rename "$stash/$lstash.new", "$stash/$fn" or die "$lstash.new $fn $!";
+        # logm("$fn: rotated and stashed current leases");
+    };
+
+    my $badleases= sub {
+        my ($m) = @_;
+        $m= "$leasesfn:$.: unknown syntax " . $m;
+        $saveas->("$lstash.bad", 7);
+        return $m;
+    };
+
+    eval { $best = parse_dhcp_dhcp3($gho, $leases, $copy, $leasesfn) };
+    if ($@) {
+        return $badleases->($@);
+    }
+
     if (!$best) {
         $saveas->("$lstash.nolease", 3);
        if ($leases->error) {
-- 
git-series 0.9.1

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.