treewide: filter shell arguments through shellquote() where applicable
[project/luci.git] / applications / luci-app-olsr / luasrc / controller / olsr.lua
index 4573f69..c5fb2b2 100644 (file)
@@ -1,5 +1,8 @@
 module("luci.controller.olsr", package.seeall)
 
+local neigh_table = nil
+local ifaddr_table = nil
+
 function index()
        local ipv4,ipv6
        if nixio.fs.access("/etc/config/olsrd") then
@@ -77,12 +80,15 @@ end
 function action_json()
        local http = require "luci.http"
        local utl = require "luci.util"
-       local uci = require "luci.model.uci".cursor_state()
+       local uci = require "luci.model.uci".cursor()
        local jsonreq4
        local jsonreq6
 
-       jsonreq4 = utl.exec("echo /status | nc 127.0.0.1 9090")
-       jsonreq6 = utl.exec("echo /status | nc ::1 9090")
+       local v4_port = tonumber(uci:get("olsrd", "olsrd_jsoninfo", "port") or "") or 9090
+       local v6_port = tonumber(uci:get("olsrd6", "olsrd_jsoninfo", "port") or "") or 9090
+
+       jsonreq4 = utl.exec("(echo /status | nc 127.0.0.1 %d | sed -n '/^[}{ ]/p') 2>/dev/null" % v4_port)
+       jsonreq6 = utl.exec("(echo /status | nc ::1 %d | sed -n '/^[}{ ]/p') 2>/dev/null" % v6_port)
        http.prepare_content("application/json")
        if not jsonreq4 or jsonreq4 == "" then
                jsonreq4 = "{}"
@@ -93,6 +99,24 @@ function action_json()
        http.write('{"v4":' .. jsonreq4 .. ', "v6":' .. jsonreq6 .. '}')
 end
 
+
+local function local_mac_lookup(ipaddr)
+       local _, rt
+       for _, rt in ipairs(luci.ip.routes({ type = 1, src = ipaddr })) do
+               local link = rt.dev and luci.ip.link(rt.dev)
+               local mac = link and luci.ip.checkmac(link.mac)
+               if mac then return mac end
+       end
+end
+
+local function remote_mac_lookup(ipaddr)
+       local _, n
+       for _, n in ipairs(luci.ip.neighbors({ dest = ipaddr })) do
+               local mac = luci.ip.checkmac(n.mac)
+               if mac then return mac end
+       end
+end
+
 function action_neigh(json)
        local data, has_v4, has_v6, error = fetch_jsoninfo('links')
 
@@ -107,13 +131,13 @@ function action_neigh(json)
        local sys = require "luci.sys"
        local assoclist = {}
        --local neightbl = require "neightbl"
+       local ntm = require "luci.model.network"
        local ipc = require "luci.ip"
+       local nxo = require "nixio"
+       local defaultgw
 
-       luci.sys.net.routes(function(r) 
-               if r.dest:prefix() == 0 then 
-                       defaultgw = r.gateway:string() 
-               end
-       end)
+       ipc.routes({ family = 4, type = 1, dest_exact = "0.0.0.0/0" },
+               function(rt) defaultgw = rt.gw end)
 
        local function compare(a,b)
                if a.proto == b.proto then
@@ -125,76 +149,39 @@ function action_neigh(json)
 
        for _, dev in ipairs(devices) do
                for _, net in ipairs(dev:get_wifinets()) do
-                       assoclist[#assoclist+1] = {} 
-                       assoclist[#assoclist]['ifname'] = net.iwdata.ifname
-                       assoclist[#assoclist]['network'] = net.iwdata.network
-                       assoclist[#assoclist]['device'] = net.iwdata.device
-                       assoclist[#assoclist]['list'] = net.iwinfo.assoclist
+                       local radio = net:get_device()
+                       assoclist[#assoclist+1] = {}
+                       assoclist[#assoclist]['ifname'] = net:ifname()
+                       assoclist[#assoclist]['network'] = net:network()[1]
+                       assoclist[#assoclist]['device'] = radio and radio:name() or nil
+                       assoclist[#assoclist]['list'] = net:assoclist()
                end
        end
 
        for k, v in ipairs(data) do
-               local interface
                local snr = 0
                local signal = 0
                local noise = 0
-               local arptable = sys.net.arptable()
                local mac = ""
-               local rmac = ""
-               local lmac = ""
                local ip
                local neihgt = {}
-               
+
                if resolve == "1" then
                        hostname = nixio.getnameinfo(v.remoteIP, nil, 100)
                        if hostname then
                                v.hostname = hostname
                        end
                end
-               if v.proto == '4' then
-                       uci:foreach("network", "interface",function(vif)
-                               if vif.ipaddr and vif.ipaddr == v.localIP then
-                                       interface = vif['.name'] or vif.interface
-                                       lmac = string.lower(vif.macaddr or "") 
-                                       return
-                               end
-                       end)
-                       for _, arpt in ipairs(arptable) do
-                               ip = arpt['IP address']
-                               if ip == v.remoteIP then
-                                       rmac = string.lower(arpt['HW address'] or "")
-                               end
-                       end
-               elseif v.proto == '6' then
-                       uci:foreach("network", "interface",function(vif)
-                               local name = vif['.name']
-                               local net = ntm:get_network(name)
-                               local device = net and net:get_interface()
-                               local locip = ipc.IPv6(v.localIP)
-                               if device and device:ip6addrs() and locip then
-                                       for _, a in ipairs(device:ip6addrs()) do
-                                               if not a:is6linklocal() then
-                                                       if a:host() == locip:host() then
-                                                               interface = name
-                                                               --neihgt = neightbl.get(device.ifname) or {}
-                                                       end
-                                               end
-                                       end
-                               end
-                       end)
-                       --[[
-                       for ip,mac in pairs(neihgt) do
-                               if ip == v.remoteIP then
-                                       rmac = mac
-                               end
-                       end
-                       ]]--
-               end
+
+               local interface = ntm:get_status_by_address(v.localIP)
+               local lmac = local_mac_lookup(v.localIP)
+               local rmac = remote_mac_lookup(v.remoteIP)
+
                for _, val in ipairs(assoclist) do
                        if val.network == interface and val.list then
+                               local assocmac, assot
                                for assocmac, assot in pairs(val.list) do
-                                       assocmac = string.lower(assocmac or "")
-                                       if rmac == assocmac then
+                                       if rmac == luci.ip.checkmac(assocmac) then
                                                signal = tonumber(assot.signal)
                                                noise = tonumber(assot.noise)
                                                snr = (noise*-1) - (signal*-1)
@@ -363,8 +350,11 @@ function fetch_jsoninfo(otable)
        local IpVersion = uci:get_first("olsrd", "olsrd","IpVersion")
        local jsonreq4 = ""
        local jsonreq6 = ""
-       jsonreq4 = utl.exec("echo /" .. otable .. " | nc 127.0.0.1 9090")
-       jsonreq6 = utl.exec("echo /" .. otable .. " | nc ::1 9090")
+       local v4_port = tonumber(uci:get("olsrd", "olsrd_jsoninfo", "port") or "") or 9090
+       local v6_port = tonumber(uci:get("olsrd6", "olsrd_jsoninfo", "port") or "") or 9090
+
+       jsonreq4 = utl.exec("(echo /%s | nc 127.0.0.1 %d | sed -n '/^[}{ ]/p') 2>/dev/null" %{ otable, v4_port })
+       jsonreq6 = utl.exec("(echo /%s | nc ::1 %d | sed -n '/^[}{ ]/p') 2>/dev/null" %{ otable, v6_port })
        local jsondata4 = {}
        local jsondata6 = {}
        local data4 = {}