applications/olsr: Fix invalid json output
[project/luci.git] / applications / luci-olsr / luasrc / controller / olsr.lua
index cf3743f..44849df 100644 (file)
@@ -106,8 +106,8 @@ function action_json()
        local http = require "luci.http"
        local utl = require "luci.util"
        local uci = require "luci.model.uci".cursor_state()
-       local jsonreq4 = ""
-       local jsonreq6 = ""
+       local jsonreq4
+       local jsonreq6
 
        local IpVersion = uci:get_first("olsrd", "olsrd","IpVersion")
        if IpVersion == "4" or IpVersion == "6and4" then
@@ -117,8 +117,13 @@ function action_json()
                jsonreq6 = utl.exec("echo /status | nc ::1 9090")
        end
        http.prepare_content("application/json")
-
-       http.write("{v4:" .. jsonreq4 .. ", v6:" .. jsonreq6 .. "}")
+       if not jsonreq4 or jsonreq4 == "" then
+               jsonreq4 = "{}"
+       end
+       if not jsonreq6 or jsonreq6 == "" then
+               jsonreq6 = "{}"
+       end
+       http.write('{"v4":' .. jsonreq4 .. ', "v6":' .. jsonreq6 .. '}')
 end
 
 function action_neigh(json)
@@ -199,11 +204,13 @@ function action_neigh(json)
                                local net = ntm:get_network(name)
                                local device = net and net:get_interface()
                                local locip = ipc.IPv6(v.localIP)
-                               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 {}
+                               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