#!/usr/bin/perl
=for commnet
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
Copyright (C) 2015 Conrad Lara
See Contributors file for additional contributors
Copyright (c) 2013 David Rivenburg et al. BroadBand-HamNet
This program 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 version 3 of the License.
This program 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 this program. If not, see .
Additional Terms:
Additional use restrictions exist on the AREDN(TM) trademark and logo.
See AREDNLicense.txt for more info.
Attributions to the AREDN Project must be retained in the source code.
If importing this code into a new or existing project attribution
to the AREDN project must be added to the source code.
You must not misrepresent the origin of the material conained within.
Modified versions must be modified to attribute to the original source
and be marked in reasonable ways as differentiate it from the original
version.
=cut
$debug = 0;
BEGIN {push @INC, '/www/cgi-bin'};
use perlfunc;
%rateL = (
'MCS0' => '6.5',
'MCS1' => '13',
'MCS2' => '19.5',
'MCS3' => '26',
'MCS4' => '39',
'MCS5' => '52',
'MCS6' => '58.5',
'MCS7' => '65',
'MCS8' => '13',
'MCS9' => '26',
'MCS10' => '39',
'MCS11' => '52',
'MCS12' => '78',
'MCS13' => '104',
'MCS14' => '117',
'MCS15' => '130',
);
%rateS = (
'MCS0' => '7.2',
'MCS1' => '14.4',
'MCS2' => '21.7',
'MCS3' => '28.9',
'MCS4' => '43.3',
'MCS5' => '57.8',
'MCS6' => '65',
'MCS7' => '72.2',
'MCS8' => '14.4',
'MCS9' => '28.9',
'MCS10' => '43.3',
'MCS11' => '57.8',
'MCS12' => '86.7',
'MCS13' => '115.6',
'MCS14' => '130',
'MCS15' => '144.4',
);
# collect some variables
$node = nvram_get("node");
$node = "NOCALL" if $node eq "";
$tactical = nvram_get("tactical");
$config = nvram_get("config");
$config = "not set" if $config eq "" or not -d "/etc/config.mesh";
($my_ip) = get_ip4_network(get_interface("wifi"));
chomp ($chanbw = `cat /sys/kernel/debug/ieee80211/phy0/ath9k/chanbw`);
if ($chanbw eq "0x00000005") {$chanbw = 4;}
elsif ($chanbw eq "0x0000000a") {$chanbw = 2;}
else {$chanbw = 1;}
read_postdata();
system "mkdir -p /tmp/web";
system "touch /tmp/web/automesh" if $parms{auto};
system "rm -f /tmp/web/automesh" if $parms{stop};
# parse the txtinfo output
$table = "none";
chomp($tmperr = `mktemp /tmp/web/nc.XXXXXX`);
foreach(`echo /all | nc 127.0.0.1 2006 2>$tmperr`)
{
if(/^Table: (\w+)/)
{
$table = $1;
next;
}
next if /^\s*$/ or /^\D/;
if($table eq "Links")
{
($junk, $ip, $junk, $lq, $nlq) = split /\s+/, $_;
$links{$ip}{lq} = $lq;
$links{$ip}{nlq} = $nlq;
$mac = `grep $ip /proc/net/arp | grep wlan0 | grep -v "00:00:00:00:00:00" | head -1`;
$mac =~ s/^.*(\w\w:\w\w:\w\w:\w\w:\w\w:\w\w).*$/$1/;
chomp $mac;
if (! $mac or ! -e "/sys/kernel/debug/ieee80211/phy0/netdev:wlan0/stations/$mac" )
{
$mbps = "";
}
else
{
#802.11n
$mbps = `egrep 'HT(4|2)0\\\/\(L|S\)GI[ \t]+(A|T)' /sys/kernel/debug/ieee80211/phy0/netdev\:wlan0/stations/$mac/rc_stats`;
if ($mbps)
{
$mbps =~ /HT[42]0\/([LS]GI)[ \tTtPABCD]+(MCS[0-9]+)[ \t]*[0-9\.]+[ \t]*([0-9\.]+).*$/ ;
$mbps = $1 eq "LGI" ? $rateL{$2}*$3/100 : $rateS{$2}*$3/100 ;
}
else
{
#802.11a/g
$mbps = `egrep \"^A[BCDP \t]+[0-9\.]+\" /sys/kernel/debug/ieee80211/phy0/netdev\:wlan0/stations/$mac/rc_stats`;
if ($mbps)
{
$mbps =~ /^A[ \tBCDP]+([0-9\.]+)[ \t]+[0-9\.]+[ \t]+([0-9\.]+).*$/;
$mbps = $1*$2/100;
}
else
{
#802.11b
$mbps = `egrep \"CCK\/LP[ \t]*T\" /sys/kernel/debug/ieee80211/phy0/netdev\:wlan0/stations/$mac/rc_stats`;
if ($mbps)
{
$mbps =~ /CCK\/LP[ \tTtP]+([0-9\.]+)M[ \t]*[0-9\.]+[ \t]*([0-9\.]+).*$/ ;
$mbps = $1*$2/100;
}
else { $mbps = "0"; }
}
}
if ( ! $mbps eq "" )
{
$mbps /= $chanbw;
$links{$ip}{mbps} = sprintf "%.1f",$mbps;
}
else { $links{$ip}{mbps} = "0.0"; }
}
}
elsif($table eq "Neighbors")
{
}
elsif($table eq "Topology")
{
}
elsif($table eq "HNA")
{
($iproute, $ip) = split /\s+/, $_;
($net, $cidr) = split /\//, $iproute;
if ( $net eq "0.0.0.0" ) { $wangateway{$ip} = 1; }
}
elsif($table eq "MID")
{
($ip, $junk) = split /\s+/, $_;
foreach $aip ( split /;/, $junk ) { $ipalias{$aip} = $ip }
}
elsif($table eq "Routes")
{
($ip, $junk, $junk, $etx) = split /\s+/, $_;
($net, $cidr) = split /\//, $ip;
$routes{$net}{cidr} = $cidr;
$routes{$net}{etx} = $etx;
$routes{$net}{value} = ip2decimal($net);
$routes{$net}{mask} = 0xffffffff - ((1 << (32 - $cidr)) - 1);
}
}
# stat and -s do not work in microperl
@parts = split /\s+/, `ls -l $tmperr`;
$txtinfo_err = $parts[4];
unlink $tmperr;
# load the local hosts file
foreach(`cat /etc/hosts`)
{
next unless /^10[.]/;
chomp;
($ip, $name, $tactical) = split /\s+/, $_;
next if $name =~ /^(localhost|localnode|localap|dtdlink\..*)$/;
if ( $name !~ /\./ ) { $name="${name}.local.mesh"; }
if($ip eq $my_ip)
{
$tactical = "" unless $tactical;
$localhosts{$ip}{tactical} = $tactical;
$localhosts{$ip}{name} = $name;
}
else { push @{$localhosts{$my_ip}{hosts}}, $name; }
}
# load the olsr hosts file
foreach(`cat /var/run/hosts_olsr 2>/dev/null`)
{
next unless /^\d/;
chomp;
($ip, $name, $junk, $originator, $mid, $midnum) = split /\s+/, $_;
next unless $originator;
next if $originator eq "myself";
if (( $name !~ /\./ ) || ( $name =~ /^mid\.[^\.]*$/ )) { $name="${name}.local.mesh"; }
if ( $ip eq $originator )
{
if($hosts{$ip}{name}) { $hosts{$ip}{tactical} = $name }
else
{
$hosts{$ip}{name} = $name;
if ( $routes{$ip} ) { $hosts{$ip}{etx} = $routes{$ip}{etx} ; }
else { $hosts{$ip}{etx} = "99.000"; }
}
}
elsif ( $name =~ /^dtdlink\..*$/ )
{
$hosts{$ip}{name} = $name;
if ( $routes{$ip} ) { $hosts{$ip}{etx} = $routes{$ip}{etx} ; }
else { $hosts{$ip}{etx} = "99.000"; }
$dtd{$originator} = 1 ;
}
elsif ( $name =~ /^mid\d+\..*$/ )
{
$midcount{$originator} = $midcount{$originator} ? $midcount{$originator}+1: 1 ;
if (! $hosts{$ip}{name} )
{
if ( $routes{$ip} ) { $hosts{$ip}{etx} = $routes{$ip}{etx} ; }
else { $hosts{$ip}{etx} = "99.000"; }
$hosts{$ip}{name} = $name;
}
}
else { push @{$hosts{$originator}{hosts}}, $name; }
}
# load the olsr services file
foreach(`cat /var/run/services_olsr 2>/dev/null`)
{
next unless /^\w/;
chomp;
($url, $junk, $name) = split /\|/, $_;
next unless defined $name;
($protocol, $host, $port, $path) = $url =~ /^(\w+):\/\/([\w\-\.]+):(\d+)\/(.*)/;
next unless defined $path;
($name, $originator) = split /\#/, $name;
$name =~ s/\s+$//;
if ( $host !~ /\./ ) { $host="${host}.local.mesh"; }
# attempt to work around olsr never forgetting defunct services
# assume that the first entry in the file by this name is the most recent, ignore the rest
next if $services{$host}{$name};
$services{$host}{$name} = $port ? "$name" : $name;
}
# load the node history
foreach(`cat /tmp/node.history 2>/dev/null`)
{
chomp;
($ip, $age, $host) = split / /, $_;
next unless $age;
$host = "" unless $host;
$host =~ s|/| / |;
$history{$ip}{age} = $age;
$history{$ip}{host} = $host;
}
#delete $hosts{"127.0.0.1"};
# generate the page
http_header();
html_header("$node mesh status", 0);
print "\n" if -f "/tmp/web/automesh";
print "\n";
print "
";
page_footer();
print "