#!/usr/bin/lua
--[[
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
Copyright (C) 2021 Tim Wilkinson
Original Perl Copyright (C) 2015 Conrad Lara
See Contributors file for additional contributors
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 contained 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
--]]
require("nixio")
require("aredn.hardware")
require("aredn.http")
require("aredn.utils")
require("aredn.html")
require("uci")
aredn.info = require("aredn.info")
aredn.olsr = require("aredn.olsr")
require("iwinfo")
local html = aredn.html
local rateL = {
["1.0M"] = 1, -- CCP LP
["2.0M"] = 2, -- CCP LP
["5.5M"] = 5.5, -- CCP LP
MCS0 = 6.5, -- HT20 LGI
["11.0M"] = 11, -- CCP LP
MCS1 = 13, -- HT20 LGI...
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
}
local 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
}
local node = aredn.info.get_nvram("node")
if node == "" then
node = "NOCALL"
end
local tactical = aredn.info.get_nvram("tactical")
local config = aredn.info.get_nvram("config")
if config == "" or nixio.fs.stat("/etc/config.mesh", "type") ~= "dir" then
config = "not set"
end
local wifiif = aredn.hardware.get_iface_name("wifi")
local my_ip = aredn.hardware.get_interface_ip4(wifiif)
if not my_ip then
my_ip = "none"
end
local phy = iwinfo.nl80211.phyname(wifiif)
if not phy then
phy = 0
end
local chanbw = 1
local cb = "/sys/kernel/debug/ieee80211/" .. phy .. "/ath9k/chanbw"
if nixio.fs.stat(cb) then
for line in io.lines(cb)
do
if line == "0x00000005" then
chanbw = 4
elseif line == "0x0000000a" then
chanbw = 2
end
break
end
end
if not nixio.fs.stat("/tmp/web") then
nixio.fs.mkdir("/tmp/web")
end
-- post data
if os.getenv("REQUEST_METHOD") == "POST" then
require('luci.http')
require('luci.sys')
local request = luci.http.Request(luci.sys.getenv(),
function()
local v = io.read(1024)
if not v then
io.close()
end
return v
end
)
if request:formvalue("auto") then
io.open("/tmp/web/automesh", "w"):close()
end
if request:formvalue("stop") then
os.remove("/tmp/web/automesh")
end
end
local cursor = uci.cursor()
local node_desc = cursor:get("system", "@system[0]", "description")
local lat_lon = "Location Not Available"
local lat = cursor:get("aredn", "@location[0]", "lat")
local lon = cursor:get("aredn", "@location[0]", "lon")
if lat and lon then
lat_lon = string.format("
Location: %s %s
", lat, lon)
end
-- low memory mitigation
local lowmemory = cursor:get("aredn", "@meshstatus[0]", "lowmem")
if not lowmemory then
lowmemory = 10000
else
lowmemory = tonumber(lowmemory)
end
local lowroutes = cursor:get("aredn", "@meshstatus[0]", "lowroutes")
if not lowroutes then
lowroutes = 1000
else
lowroutes = tonumber(lowroutes)
end
local routes = {}
local links = {}
local neighbor = {}
local wangateway = {}
local ipalias = {}
local localhosts = {}
local dtd = {}
local midcount = {}
local hosts = {}
local services = {}
local history = {}
local olsr_total = 0
local olsr_nodes = 0
local olsr_routes = 0
for i, node in ipairs(aredn.olsr.getOLSRRoutes())
do
if node.genmask ~= 0 then -- don't count default route
olsr_total = olsr_total + 1
if node.genmask ~= 32 then
olsr_nodes = olsr_nodes + 1
end
if node.etx <= 50 then
routes[node.destination] = { etx = node.etx }
olsr_routes = olsr_routes + 1
end
end
end
-- low memory route reduction
if olsr_routes > lowroutes and nixio.sysinfo().freeram < lowmemory then
local list = {}
for k,v in pairs(routes)
do
list[#list + 1] = { key = k, etx = v.etx }
end
table.sort(list, function (a, b) return a.etx < b.etx end)
for i = lowroutes,olsr_routes
do
table.remove(routes, list[i].key)
end
end
-- load up arpcache
local arpcache = {}
arptable(function(a)
arpcache[a["IP address"]] = a
end)
local prefix = "/sys/kernel/debug/ieee80211/" .. phy .. "/netdev:" .. wifiif .. "/stations/"
for i, node in ipairs(aredn.olsr.getOLSRLinks())
do
links[node.remoteIP] = { lq = node.linkQuality, nlq = node.neighborLinkQuality, mbps = "" }
neighbor[node.remoteIP] = true
local mac = arpcache[node.remoteIP]
if mac then
local f = io.open(prefix .. mac["HW address"] .. "/rc_stats_csv", "r")
if f then
for line in f:lines()
do
local gi, rate, ewma = line:match("^[^,]*,([^,]*),[^,]*,A[^,]*,([^,%s]*)%s*,[^,]*,[^,]*,[^,]*,[^,]*,([^,]*),")
ewma = tonumber(ewma)
if gi and ewma then
-- 802.11b/n
if gi == "SGI" then
links[node.remoteIP].mbps = string.format("%.1f", rateS[rate] * ewma / 100 / chanbw)
else
links[node.remoteIP].mbps = string.format("%.1f", rateL[rate] * ewma / 100 / chanbw)
end
else
rate, ewma = line:match("^A[^,]*,([^,]*),[^,]*,[^,]*,([^,]*,)")
rate = tonumber(rate)
ewma = tonumber(ewma)
if rate and ewma then
-- 802.11a/b/g
links[node.remoteIP].mbps = string.format("%.1f", rate * ewma / 100 / chanbw)
end
end
end
f:close()
end
end
end
-- discard
arpcache = nil
for i, node in ipairs(aredn.olsr.getOLSRHNA())
do
if node.destination == "0.0.0.0" then
wangateway[node.gateway] = true
end
end
for i, node in ipairs(aredn.olsr.getOLSRMid())
do
local ip = node.main.ipAddress
for _, alias in ipairs(node.aliases)
do
local aip = alias.ipAddress
ipalias[aip] = ip
neighbor[aip] = true
if links[aip] then
neighbor[ip] = true
end
end
end
-- load the local hosts file
for line in io.lines("/etc/hosts")
do
if line:match("^10%.") then
local ip, name = line:match("([%d%.]+)%s+(%S+)")
if name then
local name9 = name:sub(1, 9)
if name9 ~= "localhost" and name9 ~= "localnode" then
local name7 = name:sub(1, 7)
if name7 ~= "localap" and name7 ~= "dtdlink" then
if not name:match("%.") then
name = name .. ".local.mesh"
end
local tac = line:match("[%d%.]+%s+%S+%s+(%S+)")
if not tac then
tac = ""
end
if not localhosts[my_ip] then
localhosts[my_ip] = { hosts = {}, noprops = {}, aliases = {}, name = name, tactical = tac }
end
local host = localhosts[my_ip]
if ip == my_ip then
host.tactical = tac
host.name = name
else
host.hosts[#host.hosts + 1] = name
end
if tac == "#NOPROP" then
host.noprops[#host.noprops + 1] = name
end
if tac == "#ALIAS" then
host.aliases[#host.aliases + 1] = name
end
end
end
end
end
end
-- load the olsr hosts file
for line in io.lines("/var/run/hosts_olsr")
do
local ip, name, originator = line:match("^([%d%.]+)%s+(%S+)%s+%S+%s+(%S+)")
if ip and originator and originator ~= "myself" and (routes[ip] or routes[originator]) then
local etx = routes[ip]
if not etx then
etx = routes[originator]
end
etx = etx.etx
if not name:match("%.") or name:match("^mid%.[^%.]*$") then
name = name .. ".local.mesh"
end
if ip == originator then
if not hosts[originator] then
hosts[originator] = { hosts = {} }
end
local host = hosts[originator]
if host.name then
host.tactical = name
else
host.name = name
host.etx = etx
end
elseif name:match("^dtdlink%.") then
dtd[originator] = true
if links[ip] then
links[ip].dtd = true
end
elseif name:match("^mid%d+%.") then
if not midcount[originator] then
midcount[originator] = 1
else
midcount[originator] = midcount[originator] + 1
end
if links[ip] then
links[ip].tun = true
end
else
if not hosts[originator] then
hosts[originator] = { hosts = {} }
end
local host = hosts[originator]
host.hosts[#host.hosts + 1] = name
end
end
end
-- discard
routes = nil
for line in io.lines("/var/run/services_olsr")
do
if line:match("^%w") then
local url, name = line:match("^(.*)|.*|(.*)$")
if name then
local protocol, host, port, path = url:match("^([%w][%w%+%-%.]+)%://(.+):(%d+)/(.*)")
if path then
local name, originator = name:match("(.*%S)%s*#(.*)")
if originator == " my own service" or (hosts[originator] and hosts[originator].name) then
if not host:match("%.") then
host = host .. ".local.mesh"
end
if not services[host] then
services[host] = {}
end
if not services[host][name] then
if port ~= "0" then
services[host][name] = "" .. name .. ""
else
services[host][name] = name
end
end
end
end
end
end
end
-- load the node history
if nixio.fs.stat("/tmp/node.history") then
for line in io.lines("/tmp/node.history")
do
local ip, age, host = line:match("^(.*) (.*) (.*)")
if age then
history[ip] = { age = age, host = host:gsub("/", " / ") }
end
end
end
http_header()
html.header(node .. " mesh status", false)
local automesh = nixio.fs.stat("/tmp/web/automesh");
if automesh then
html.print("")
end
html.print("")
html.print("")
html.footer()
html.print("