diff -ru wlandetect-0.3/Makefile wlandetect/Makefile --- wlandetect-0.3/Makefile 2003-11-02 21:13:55.000000000 -0500 +++ wlandetect/Makefile 2005-03-15 13:54:23.000000000 -0500 @@ -4,6 +4,7 @@ mandir = $(prefix)/share/man docdir = $(prefix)/share/doc etcdir = /etc +initdir = $(etcdir)/init.d INSTALL = install install: @@ -13,3 +14,4 @@ $(INSTALL) wlandetect $(DESTDIR)$(sbindir) $(INSTALL) -d $(DESTDIR)$(docdir)/wlandetect $(INSTALL) AUTHORS wlandetect.conf.example COPYING $(DESTDIR)$(docdir)/wlandetect + $(INSTALL) wlandetect-init.d $(initdir)/wlandetect diff -ru wlandetect-0.3/wlandetect wlandetect/wlandetect --- wlandetect-0.3/wlandetect 2003-12-08 18:30:53.000000000 -0500 +++ wlandetect/wlandetect 2005-03-15 13:49:12.000000000 -0500 @@ -6,86 +6,164 @@ use Getopt::Std; my $version = "0.3"; -my $timeout = 60; +my $timeout = 15; our $iwlist_path = "/sbin/iwlist"; our $configfile = "/etc/wlandetect.conf"; -$current_device = ""; - our %interfaces = (); -our %current = (); -sub read_interfaces { - open IWLIST, "$iwlist_path scan 2>/dev/null |" or die("Can't run iwlist!"); +sub list_interfaces +{ + my $procwifi = "/proc/net/wireless"; + + open(PROCWIFI, "< $procwifi") or die "Can't open $procwifi!"; + + while () { + if (/^\s+([a-z]+\d+):/) { + $interfaces{$1} = 1; + print "discovered interface $1\n" if $options{v}; + } + } + + close(PROCWIFI); +} + +sub scan_aps +{ + my $dev = shift; + my %aps; + + open IWLIST, "$iwlist_path $dev scan 2>/dev/null |" + or die("Can't run iwlist!"); + while() { - if(/([^ ]+)([ \t]+)Scan completed :/) { - $current_device = $1; - } elsif(/.*Address: (..):(..):(..):(..):(..):(..)/) { - $interfaces{$current_device}{"$1:$2:$3:$4:$5:$6"} = 1; - } elsif(/.*ESSID:"(.*)"/) { - $interfaces{$current_device}{$1} = 1; + if(/.*Address: (..):(..):(..):(..):(..):(..)/) { + my $bssid = "$1:$2:$3:$4:$5:$6"; + $aps{$bssid} = 1; + print "found $dev bssid $bssid\n" if $options{v}; + } + elsif(/.*ESSID:"(.*)"/) { + $aps{$1} = 1; + print "found $dev essid $1\n" if $options{v}; } } close IWLIST; + + return %aps; } -sub set_interfaces { - foreach (keys %interfaces) { +sub configure_interfaces +{ + foreach (keys %interfaces) + { $dev = $_; my $l = 0; - # Check whether interface is connected + # Don't configure if interface already connected to the net + next if $options{c} && check_connectivity($dev); + + # Scan for access points on this device + my %aps = scan_aps($dev); open MAPPINGS, $configfile or die("Can't find configuration file"); + while() { $l++; + if(/^#.*$/) { # Ignore comments - } elsif(/([^ \t]+)([ \t]+)(.*)/) { - if($interfaces{$dev}{$1} == 1 && $cur{$dev} cmp $1) { + } + elsif(/([^ \t]+)([ \t]+)(.*)/) + { + if($aps{$1} == 1) + { + print "matched $1, configuring\n" if $options{v}; + $command = $3; $command =~ s/\@DEV\@/$dev/g; - if(!$options{'d'}) { print "Executing '$command'\n"; } - system($command); - $cur{$dev} = $1; + + # if($options{'v'}) { print "Executing '$command'\n"; } + + system($command); last; } - } else { + } + else { print "Unparseable line : '$_' in wlandetect.conf, line $l\n"; } } + close MAPPINGS; } } -our %options = (); -getopts("dqht:", \%options); +sub check_connectivity +{ + my $dev = shift; + my $ip = get_ip($dev); -print "Wlandetect $version\n(C) 2003 Jelmer Vernooij \n" unless defined $options{q}; + return 0 if !$ip; + return ping_check($ip); +} -if(defined $options{h}) { - print "-d Daemon mode (go to the background and try to keep connection open)\n"; - print "-q Be quiet (don't show version info)\n"; - print "-t int Seconds to wait before searching for peers/AP's again (daemon mode only)[60]\n"; - print "-h Print this help message\n"; - exit 0; +sub get_ip +{ + my $dev = shift; + my $ip; + + print "seeing if $dev has an ip address\n" if $options{v}; + + my $ifconfig = "ifconfig $dev"; + + open(IFCONFIG, "$ifconfig |"); + + while (my $line = ) { + if ($line =~ /inet addr:\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/i) { + $ip = $1; + } + } + + close(IFCONFIG); + + print $ip ? "$dev has ip $ip\n" : "$dev has no ip\n" if $options{v}; + + return $ip; } -read_mappings; +sub ping_check +{ + my $sourceip = shift; + my $ping = "ping -c 1 -q -I $sourceip google.com 2>/dev/null > /dev/null"; -# Execute this part once in a while -read_interfaces; + print "checking connectivity with $ping\n" if $options{v}; -if(!$options{'d'}) { - set_interfaces; + my $pingok = system($ping) ? 0 : 1; + print $pingok ? "ping succeeded\n" : "ping failed\n" if $options{v}; + + return $pingok; +} + +our %options = ( t => $timeout, c => 1 ); +getopts("cdqhvt:", \%options); + +print "Wlandetect $version\n(C) 2003 Jelmer Vernooij \n" unless defined $options{q}; + +if(defined $options{h}) { + print "-c Perform connectivity checks [yes]\n"; + print "-d Daemon mode (wake up at intervals, try to connect)\n"; + print "-q Be quiet (don't show version info)\n"; + print "-t int Seconds to wait before searching for peers/AP's again (daemon mode only) [15]\n"; + print "-v Enable debugging messages\n"; + print "-h Print this help message\n"; exit 0; } -# Fork -if(fork() > 0) { exit 0; } -while(1) { - sleep($timeout); - set_interfaces; +while (1) { + list_interfaces(); + configure_interfaces(); + exit 0 if !$options{d}; + sleep($options{t}); } + diff -ru wlandetect-0.3/wlandetect-init.d wlandetect/wlandetect-init.d --- wlandetect-0.3/wlandetect-init.d 2003-12-07 11:29:18.000000000 -0500 +++ wlandetect/wlandetect-init.d 2005-03-11 13:25:03.000000000 -0500 @@ -1,17 +1,38 @@ #!/bin/sh -test -x /usr/local/sbin/wlandetect || exit 0 +pidfile="/var/run/wlandetect.pid" +logfile="/var/log/wlandetect.log" +wlandetect="/usr/local/sbin/wlandetect" +opts="-d -q -v" + +test -x "$wlandetect" || exit 0 case "$1" in - start | restart | force-reload | reload) - killall wlandetect - /usr/local/sbin/wlandetect -d - ;; - stop) - killall wlandetect - ;; -*) - echo "Usage: $0 {start|stop|restart|reload|force-reload}" - exit 1 - ;; + start | restart | force-reload | reload) + + if [ -f "$pidfile" ]; then + echo "`date` Stopping wlandetect." >> "$logfile" + kill -9 `cat "$pidfile"` + rm -rf "$pidfile" + fi + + echo "`date` Starting wlandetect." >> "$logfile" + ("$wlandetect" $opts 2>>$logfile 1>>$logfile) & + echo -n $! > "$pidfile" + ;; + + stop) + + if [ -f "$pidfile" ]; then + echo "`date` Stopping wlandetect." >> "$logfile" + kill -9 `cat "$pidfile"` + rm -rf "$pidfile" + fi + ;; + + *) + + echo "Usage: $0 {start|stop|restart|reload|force-reload}" + exit 1 + ;; esac