From 9e936dd974391ae8c694e9e04bf5393b8a68e477 Mon Sep 17 00:00:00 2001
From: Mohit Mehta <mohit.mehta@vyatta.com>
Date: Mon, 17 Nov 2008 14:06:50 -0800
Subject: move submodules to VyattaMisc.pm so that they can be used elsewhere
 as well

---
 scripts/VyattaMisc.pm        | 47 +++++++++++++++++++++++++++++++++++++
 scripts/vyatta-interfaces.pl | 55 ++++----------------------------------------
 2 files changed, 52 insertions(+), 50 deletions(-)

diff --git a/scripts/VyattaMisc.pm b/scripts/VyattaMisc.pm
index 7072908..b36e157 100755
--- a/scripts/VyattaMisc.pm
+++ b/scripts/VyattaMisc.pm
@@ -32,6 +32,53 @@ use strict;
 
 use VyattaConfig;
 
+# check if interace is configured to get an IP address using dhcp
+sub is_dhcp_enabled {
+    my ($intf, $outside_cli) = @_;
+
+    my $config = new VyattaConfig;
+    if (!($outside_cli eq '')) {
+     $config->{_active_dir_base} = "/opt/vyatta/config/active/";
+    }
+
+    if ($intf =~ m/^eth/) {
+        if ($intf =~ m/(\w+)\.(\d+)/) {
+            $config->setLevel("interfaces ethernet $1 vif $2");
+        } else {
+            $config->setLevel("interfaces ethernet $intf");
+        }
+    } elsif ($intf =~ m/^br/) {
+        $config->setLevel("interfaces bridge $intf");
+    } elsif ($intf =~ m/^bond/) {
+        $config->setLevel("interfaces bonding $intf");
+    } else {
+        #
+        # add other interfaces that can be configured to use dhcp above
+        #
+        return 0;
+    }
+    my @addrs = $config->returnOrigValues("address");
+    foreach my $addr (@addrs) {
+        if (defined $addr && $addr eq "dhcp") {
+            return 1;
+        }
+    }
+    return 0;
+}
+
+# return dhclient related files for interface
+sub generate_dhclient_intf_files {
+    my $intf = shift;
+    my $dhclient_dir = '/var/lib/dhcp3/';
+
+    $intf =~ s/\./_/g;
+    my $intf_config_file = $dhclient_dir . 'dhclient_' . $intf . '.conf';
+    my $intf_process_id_file = $dhclient_dir . 'dhclient_' . $intf . '.pid';
+    my $intf_leases_file = $dhclient_dir . 'dhclient_' . $intf . '.leases';
+    return ($intf_config_file, $intf_process_id_file, $intf_leases_file);
+
+}
+
 # getInterfacesIPadresses() returns IP addresses for the interface type passed to it
 # possible type of interfaces : 'broadcast', 'pointopoint', 'multicast', 'all'
 # the loopback IP address is never returned with any of the above parameters
diff --git a/scripts/vyatta-interfaces.pl b/scripts/vyatta-interfaces.pl
index b5d38af..6ece6c0 100644
--- a/scripts/vyatta-interfaces.pl
+++ b/scripts/vyatta-interfaces.pl
@@ -43,7 +43,6 @@ use strict;
 use warnings;
 
 my $dhcp_daemon = '/sbin/dhclient';
-my $dhclient_dir = '/var/lib/dhcp3/';
 
 my ($eth_update, $eth_delete, $addr, $dev, $mac, $mac_update, $op_dhclient);
 
@@ -123,39 +122,6 @@ sub dhcp_conf_header {
     return $output;
 }
 
-sub is_dhcp_enabled {
-    my $intf = shift;
-
-    my $config = new VyattaConfig;
-
-    # FIXME: this is wrong and depends on name of interface -> type
-    #   which is not dependable
-    if ($intf =~ m/^eth/) {
-	if ($intf =~ m/(\w+)\.(\d+)/) {
-	    $config->setLevel("interfaces ethernet $1 vif $2");
-	} else {
-	    $config->setLevel("interfaces ethernet $intf");
-	}
-    } elsif ($intf =~ m/^br/) {
-	$config->setLevel("interfaces bridge $intf");
-    } elsif ($intf =~ m/^bond/) {
-        $config->setLevel("interfaces bonding $intf");
-    } else {
-	#
-	# FIXME: currently we only support dhcp on ethernet 
-	# and bridge interfaces (what about wireles, ...???)
-	#
-	return 0;
-    }
-    my @addrs = $config->returnOrigValues("address");
-    foreach my $addr (@addrs) {
-	if (defined $addr && $addr eq "dhcp") {
-	    return 1;
-	}
-    }
-    return 0;
-}
-
 sub is_address_enabled {
     my $intf = shift;
 
@@ -240,21 +206,10 @@ sub dhcp_update_config {
     dhcp_write_file($conf_file, $output);
 }
 
-sub generate_dhclient_intf_files {
-    my $intf = shift;
-
-    $intf =~ s/\./_/g;
-    my $intf_config_file = $dhclient_dir . 'dhclient_' . $intf . '.conf';
-    my $intf_process_id_file = $dhclient_dir . 'dhclient_' . $intf . '.pid';
-    my $intf_leases_file = $dhclient_dir . 'dhclient_' . $intf . '.leases';
-    return ($intf_config_file, $intf_process_id_file, $intf_leases_file);
-
-}
-
 sub run_dhclient {
     my $intf = shift;
 
-    my ($intf_config_file, $intf_process_id_file, $intf_leases_file) = generate_dhclient_intf_files($intf);
+    my ($intf_config_file, $intf_process_id_file, $intf_leases_file) = VyattaMisc::generate_dhclient_intf_files($intf);
     dhcp_update_config($intf_config_file, $intf);
     my $cmd = "$dhcp_daemon -q -nw -cf $intf_config_file -pf $intf_process_id_file  -lf $intf_leases_file $intf 2> /dev/null &";
     # adding & at the end to make the process into a daemon immediately
@@ -265,7 +220,7 @@ sub run_dhclient {
 sub stop_dhclient {
     my $intf = shift;
 
-    my ($intf_config_file, $intf_process_id_file, $intf_leases_file) = generate_dhclient_intf_files($intf);
+    my ($intf_config_file, $intf_process_id_file, $intf_leases_file) = VyattaMisc::generate_dhclient_intf_files($intf);
     my $release_cmd = "$dhcp_daemon -q -cf $intf_config_file -pf $intf_process_id_file -lf $intf_leases_file -r $intf 2> /dev/null";
     system ($release_cmd) == 0
 	or warn "stop $dhcp_daemon failed: $?\n";
@@ -393,7 +348,7 @@ sub is_valid_addr {
 	    print "Error: can't use dhcp client on loopback interface\n";
 	    exit 1;
 	}
-	if (is_dhcp_enabled($intf)) {
+	if (VyattaMisc::is_dhcp_enabled($intf)) {
 	    print "Error: dhcp already configured for $intf\n";
 	    exit 1;
 	}
@@ -439,7 +394,7 @@ sub is_valid_addr {
        }
     }
 
-    if (is_dhcp_enabled($intf)) {
+    if (VyattaMisc::is_dhcp_enabled($intf)) {
 	print "Error: remove dhcp before adding static addresses for $intf\n";
 	exit 1;
     }
@@ -465,7 +420,7 @@ sub is_valid_addr {
 sub op_dhcp_command {
     my ($op_command, $intf) = @_;
 
-    if (!is_dhcp_enabled($intf)) {
+    if (!VyattaMisc::is_dhcp_enabled($intf)) {
         print "$intf is not using DHCP to get an IP address\n";
         exit 1;
     }
-- 
cgit v1.2.3