Revision: 2692
          http://ipcop.svn.sourceforge.net/ipcop/?rev=2692&view=rev
Author:   owes
Date:     2009-04-11 11:54:15 +0000 (Sat, 11 Apr 2009)

Log Message:
-----------
Convert rc.connectioncheck into Perl.
I've probably got this right, but the rc.d red scripts are a complicated bunch 
...

Modified Paths:
--------------
    ipcop/trunk/src/rc.d/rc.connectioncheck

Modified: ipcop/trunk/src/rc.d/rc.connectioncheck
===================================================================
--- ipcop/trunk/src/rc.d/rc.connectioncheck     2009-04-11 09:36:41 UTC (rev 
2691)
+++ ipcop/trunk/src/rc.d/rc.connectioncheck     2009-04-11 11:54:15 UTC (rev 
2692)
@@ -1,143 +1,178 @@
-#!/bin/bash
+#!/usr/bin/perl
 #
+# This file is part of the IPCop Firewall.
+#
+# IPCop is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# IPCop is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with IPCop; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+#
+# (c) 2009, the IPCop team
+#
 # $Id$
 #
 
-eval $(/usr/local/bin/readhash /var/ipcop/ppp/settings)
 
-# MAXRETRIES is now a count before rc.red clear and if reconnection fail, 
eventually switching to a backup profil
-MAXCONFIGURE=160
-if [ ! $HOLDOFF ]; then
-    HOLDOFF=30
-fi
+use strict;
+require '/usr/lib/ipcop/general-functions.pl';
 
-# Debugging. Comment it out to stop logging
-DEBUG="yes"
-msg() {
-    if [ "z$DEBUG" != "z" ] ; then
-        /usr/bin/logger -t red "Connectioncheck: $*"
-    fi
-    /bin/echo "$*"
+
+my $attempts = 0;
+my %pppsettings = ();
+$pppsettings{'HOLDOFF'} = 30;
+$pppsettings{'DEBUG'} = 'off';
+$pppsettings{'MAXCONFIGURE'} = 160;
+
+&General::readhash('/var/ipcop/ppp/settings', \%pppsettings);
+
+if (-s "/var/ipcop/red/keepconnected") {
+    $attempts = `/bin/cat /var/ipcop/red/keepconnected`; 
 }
+else {
+    system('echo "0" > /var/ipcop/red/keepconnected');    
+}
 
-if [ -s "/var/ipcop/red/keepconnected" ]; then
-    ATTEMPTS=$(/bin/cat /var/ipcop/red/keepconnected)
-else
-    echo "0" > /var/ipcop/red/keepconnected
-fi
-
-case "$1" in
-start)
+if ($ARGV[0] eq 'start') {
     # waiting pppd start or sync timout set at 90 s when available in driver
     #rc.eciadsl/eci-load2 timout after 60s and 5 eci-load2 maximum attempts 
are done in rc.eciadsl.
     #So just wait 5 s after 'rc.red start' end where pppd should have been 
started
-    TIMETOSTART=0
-    while ( ! /bin/ps ax | /bin/grep -q [p]ppd ); do
-        if [ ! -e "/var/ipcop/red/keepconnected" ]; then
+    
+    my $timetostart = 0;
+
+    while (system("/bin/ps ax | /bin/grep -q [p]ppd")) {
+
+        if (! -e "/var/ipcop/red/keepconnected") {
             # user pressed disconnect in gui
             exit 0
-        fi
-        RCREDSTART=`/bin/ps ax | /bin/grep '[r]c.red start'`
-        if [ 0"$TIMETOSTART" -eq 1 ]; then
-            msg "fail before pppd start : no sync, trying again"
-            /etc/rc.d/rc.red stop
-            /etc/rc.d/rc.red clear
-            /etc/rc.d/rc.red start
-            exit
-        fi
+        }
+
+        my $rcredstart = system("/bin/ps ax | /bin/grep '[r]c.red start'");
+        if ($timetostart == 1) {
+            &msg('fail before pppd start : no sync, trying again');
+            system('/etc/rc.d/rc.red stop');
+            system('/etc/rc.d/rc.red clear');
+            system('/etc/rc.d/rc.red start');
+            exit 0
+        }
         # give 5 s more at pppd to be visible  with ps ax after 'rc.red start' 
end
-        if [ "$RCREDSTART" = '' ]; then
-            TIMETOSTART=1
-        fi
-        /bin/sleep 5
-    done
+        if ($rcredstart != 0) {
+            $timetostart = 1;
+        }
+        sleep 5;
+    }
 
-    #watching if an IP is received in $MAXCONFIGURE time to avoid some pppoa 
waiting forever even after LCP timeout
-    COUNT=0
-    while [ "$COUNT" -le "$MAXCONFIGURE" ]; do
-        if [ ! -e "/var/ipcop/red/keepconnected" ]; then
+    #watching if an IP is received in MAXCONFIGURE time to avoid some pppoa 
waiting forever even after LCP timeout
+    my $count = 0;
+    while ($count < $pppsettings{'MAXCONFIGURE'}) {
+        if (! -e "/var/ipcop/red/keepconnected") {
             # user pressed disconnect in gui
-            exit 0
-        fi
-        if [ -e "/var/ipcop/red/active" ]; then
+            exit 0;
+        }
+
+        if (-e "/var/ipcop/red/active") {
             # connected in time so exit
-            echo "0" > /var/ipcop/red/keepconnected
-            exit 0
-        fi
-        if [ -e "/var/ipcop/red/dial-on-demand" ]; then
-            # Don't count time when dial-on-demand is not really connected
-            if ( ! /sbin/ifconfig | /bin/grep -q addr:10.64.64.64 ); then
-                (( COUNT += 5 ))
-            fi
-        else
-            (( COUNT += 5 ))
-        fi
-        if ( ! /bin/ps ax | /bin/grep -q [p]ppd ); then
+            system('echo "0" > /var/ipcop/red/keepconnected');    
+            exit 0;
+        }
+
+        $count += 5;
+        # TODO: why this IP address? Some test from someone?
+        #if [ -e "/var/ipcop/red/dial-on-demand" ]; then
+        #    # Don't count time when dial-on-demand is not really connected
+        #    if ( ! /sbin/ifconfig | /bin/grep -q addr:10.64.64.64 ); then
+        #        (( COUNT += 5 ))
+        #    fi
+        #else
+        #    (( COUNT += 5 ))
+        #fi
+
+        if (system("/bin/ps ax | /bin/grep -q [p]ppd")) {
             #pppd exit, so don't need to wait MAXCONFIGURE
-            break
-        fi
-        /bin/sleep 5
-    done
-    msg "RED fail to connect"
-    RCREDSTART=`/bin/ps ax | /bin/grep '[r]c.red start' | /usr/bin/cut -f1 -d 
' '`
-    if [ "$RCREDSTART" != '' ]; then
-        /bin/kill "$RCREDSTART"
-    fi
+            $count = $pppsettings{'MAXCONFIGURE'};
+
+            # TODO: but we still want to sleep after?
+        }
+        else {
+            sleep 5;
+        }
+    }
+
+    &msg('RED fail to connect');
+    my $rcredstart = `/bin/ps ax | /bin/grep '[r]c.red start' | /usr/bin/cut 
-f1 -d ' '`;
+    if ($rcredstart ne '') {
+        system("/bin/kill $rcredstart");
+    }
     # this attempt to connect fail, so retry
 
     #Don't erase keepconnected because it is used to watch user actions
-    /usr/bin/touch /var/ipcop/red/redial
-    /etc/rc.d/rc.red stop
-    while ( /bin/ps ax | /bin/grep -q [p]ppd ); do
-        msg "waiting pppd exit"
-        /bin/sleep 2
-    done
-    /bin/sleep $HOLDOFF
-    if [ ! -e "/var/ipcop/red/keepconnected" ]; then
+    system("/usr/bin/touch /var/ipcop/red/redial");
+    system("/etc/rc.d/rc.red stop");
+    while (! system("/bin/ps ax | /bin/grep -q [p]ppd") ) {
+        &msg('waiting pppd exit');
+        sleep 2;
+    }
+    sleep $pppsettings{'HOLDOFF'};
+
+    if (! -e "/var/ipcop/red/keepconnected") {
         # user pressed disconnect in gui
-        exit 0
-    fi
-    (( ATTEMPTS += 1 ))
-    msg "Restarting $ATTEMPTS/$MAXRETRIES"
-    if [ "$ATTEMPTS" -ge "$MAXRETRIES" ]; then
-        echo "0" > /var/ipcop/red/keepconnected
-        msg "Clearing RED interface"
-        /etc/rc.d/rc.red clear
-        if [ "$BACKUPPROFILE" != '' -a "$BACKUPPROFILE" != "0" ]; then
-            /bin/rm -f /var/ipcop/ppp/settings
-            /bin/cp "/var/ipcop/ppp/settings-$BACKUPPROFILE" 
/var/ipcop/ppp/settings
-            # read the new profile 
-            eval $(/usr/local/bin/readhash /var/ipcop/ppp/settings)
-            # so we can re-write the secrets file
-            echo "'$USERNAME' * '$PASSWORD'" > /var/ipcop/ppp/secrets
-            msg "Changed to backup profile $BACKUPPROFILE $PROFILENAME"
-        else
-            msg "Max. retries and no backup profile, giving up."
-            exit 0
-        fi
-    else
-        echo $ATTEMPTS > /var/ipcop/red/keepconnected
-    fi
-    /etc/rc.d/rc.red start
-    ;;
-reconnect)
-    while ( /bin/ps ax | /bin/grep -q [p]ppd ); do
-        /bin/sleep 2
-    done
-    if [ "$PROTOCOL" = RFC1483 ]; then
+        exit 0;
+    }
+    $attempts++;
+    &msg("Restarting $attempts/$pppsettings{'MAXRETRIES'}");
+    if ($attempts >= $pppsettings{'MAXRETRIES'}) {
+        system('echo "0" > /var/ipcop/red/keepconnected');
+        &msg('Clearing RED interface');
+        system("/etc/rc.d/rc.red clear");
+        if (defined($pppsettings{'BACKUPPROFILE'}) && 
($pppsettings{'BACKUPPROFILE'} != 0)) {
+           my $backup = $pppsettings{'BACKUPPROFILE'};
+            &General::SelectProfile($backup);
+
+           # read the new profile
+            &General::readhash('/var/ipcop/ppp/settings', \%pppsettings);
+            &msg("Changed to backup profile $backup 
$pppsettings{'PROFILENAME'}");
+        }
+        else {
+            &msg("Max. retries and no backup profile, giving up.");
+            exit 0;
+        }
+    } else {
+        system("echo $attempts > /var/ipcop/red/keepconnected");
+    }
+
+    system("/etc/rc.d/rc.red start");
+}
+elsif ($ARGV[0] eq 'reconnect') {
+    while (! system("/bin/ps ax | /bin/grep -q [p]ppd") ) {
+        sleep 2;
+    }
+
+    if ($pppsettings{'PROTOCOL'} eq 'RFC1483') {
         # remove bridged/routed user-space programs
-        if [ "$TYPE" = eciadsl ]; then
-            /etc/rc.d/rc.eciadsl stop
-        fi
-        if [ "$TYPE" = alcatelusb ]; then
-            /etc/rc.d/rc.alcatelusb stop
-        fi
-    fi
-    msg "will connect again"
-    /etc/rc.d/rc.red start
-    ;;
-*)
-    /bin/echo "Usage: $0 {start|reconnect}"
-    exit 1
-    ;;
-esac
+        if ($pppsettings{'TYPE'} eq 'eciadsl') {
+            system("/etc/rc.d/rc.eciadsl stop");
+        }
+        if ($pppsettings{'TYPE'} eq 'alcatelusb') {
+            system("/etc/rc.d/rc.alcatelusb stop");
+        }
+    }
+    &msg("Will connect again");
+    system("/etc/rc.d/rc.red start");
+}
+else {
+    print "Usage: $0 {start|reconnect}\n";
+    exit 1;
+}
+
+
+sub msg() {
+    &General::log("red connectioncheck", "$_[0]") if ($pppsettings{'DEBUG'} eq 
'on');
+}


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Ipcop-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ipcop-svn

Reply via email to