modules/admin-full: fix "unsupported protocol" vs. "no interface assigned" errors...
[project/luci.git] / modules / admin-full / luasrc / view / admin_network / iface_overview.htm
index f77ee63..4be0629 100644 (file)
@@ -8,31 +8,44 @@ You may obtain a copy of the License at
 
        http://www.apache.org/licenses/LICENSE-2.0
 
-$Id$
-
 -%>
 
 <%-
        local ntm = require "luci.model.network".init()
+       local fwm = require "luci.model.firewall".init()
 
        local net
+       local ifaces = { }
        local netlist = { }
        for _, net in ipairs(ntm:get_networks()) do
                if net:name() ~= "loopback" then
-                       netlist[#netlist+1] = net:name()
+                       local z = fwm:get_zone_by_network(net:name())
+                       ifaces[#ifaces+1] = net:name()
+                       netlist[#netlist+1] = {
+                               net:name(), z and z:name() or "-", z
+                       }
                end
        end
+
+       table.sort(netlist,
+               function(a, b)
+                       if a[2] ~= b[2] then
+                               return a[2] < b[2]
+                       else
+                               return a[1] < b[1]
+                       end
+               end)
 -%>
 
 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
-<script type="text/javascript"><![CDATA[
+<script type="text/javascript">//<![CDATA[
        function iface_shutdown(id, reconnect) {
-               if (!reconnect && !confirm(String.format('<%_Really shutdown interface "%s" ?\nYou might loose access to this router if you are connected via this interface.%>', id)))
+               if (!reconnect && !confirm(String.format('<%_Really shutdown interface "%s" ?\nYou might lose access to this device if you are connected via this interface.%>', id)))
                        return;
 
-               var a = document.getElementById(id + '-ifc-addrs');
-               if (a)
-                       a.innerHTML = reconnect
+               var d = document.getElementById(id + '-ifc-description');
+               if (d)
+                       d.innerHTML = reconnect
                                ? '<em><%:Interface is reconnecting...%></em>'
                                : '<em><%:Interface is shutting down...%></em>';
 
@@ -40,11 +53,10 @@ $Id$
                if (s)
                {
                        s.parentNode.style.display = 'block';
-                       s.innerHTML = '<%:Waiting for router...%>';
+                       s.innerHTML = '<%:Waiting for changes to be applied...%>';
                }
 
-               var rcxhr = new XHR();
-               rcxhr.get('<%=luci.dispatcher.build_url("admin", "network")%>/iface_' + (reconnect ? 'reconnect' : 'shutdown') + '/' + id, null,
+               XHR.get('<%=luci.dispatcher.build_url("admin", "network")%>/iface_' + (reconnect ? 'reconnect' : 'shutdown') + '/' + id, null,
                        function(x)
                        {
                                if (s)
@@ -66,114 +78,138 @@ $Id$
        var wifidevs = <%=luci.http.write_json(netdevs)%>;
        var arptable = <%=luci.http.write_json(arpcache)%>;
 
-       var update_status = function() {
-               iwxhr.get('<%=luci.dispatcher.build_url("admin", "network", "iface_status", table.concat(netlist, ","))%>', null,
-                       function(x, ifcs)
+       XHR.poll(5, '<%=luci.dispatcher.build_url("admin", "network", "iface_status", table.concat(ifaces, ","))%>', null,
+               function(x, ifcs)
+               {
+                       if (ifcs)
                        {
-                               if (ifcs)
+                               for (var idx = 0; idx < ifcs.length; idx++)
                                {
-                                       for (var i = 0; i < ifcs.length; i++)
+                                       var ifc = ifcs[idx];
+                                       var html = '';
+
+                                       var s = document.getElementById(ifc.id + '-ifc-devices');
+                                       if (s)
                                        {
-                                               var ifc = ifcs[i];
-                                               var is_up = (ifc.flags && ifc.flags.up);
-                                               var rxb = ifc.stats ? (ifc.stats["rx_bytes"] / 1024) : 0;
-                                               var txb = ifc.stats ? (ifc.stats["tx_bytes"] / 1024) : 0;
-                                               var rxp = ifc.stats ? ifc.stats["rx_packets"] : 0;
-                                               var txp = ifc.stats ? ifc.stats["tx_packets"] : 0;
-                                               var mac = ifc.macaddr ? ifc.macaddr : '00:00:00:00:00:00';
-
-                                               var icon;
-                                               if (is_up)
-                                                       icon = "<%=resource%>/icons/ethernet.png";
-                                               else
-                                                       icon = "<%=resource%>/icons/ethernet_disabled.png";
-
-                                               var s = document.getElementById(ifc.id + '-ifc-signal');
-                                               if (s)
+                                               var stat = String.format(
+                                                       '<img src="<%=resource%>/icons/%s%s.png" style="width:16px; height:16px; vertical-align:middle" />',
+                                                               ifc.type,
+                                                               ifc.is_up ? '' : '_disabled'
+                                               );
+
+                                               if (ifc.subdevices && ifc.subdevices.length)
                                                {
-                                                       s.innerHTML = String.format(
-                                                               '<img src="%s" style="width:16px; height:16px" /><br />' +
-                                                               '<small>%s</small>',
-                                                                       icon, ifc.ifname ? ifc.ifname : '?'
-                                                       );
+                                                       stat += ' <strong>(';
+
+                                                       for (var j = 0; j < ifc.subdevices.length; j++)
+                                                       {
+                                                               var sif = ifc.subdevices[j];
+
+                                                               stat += String.format(
+                                                                       '<img src="<%=resource%>/icons/%s%s.png" style="width:16px; height:16px; vertical-align:middle" title="%h" />',
+                                                                               sif.type,
+                                                                               sif.is_up ? '' : '_disabled',
+                                                                               sif.name
+                                                               );
+                                                       }
+
+                                                       stat += ')</strong>';
                                                }
 
-                                               var m = document.getElementById(ifc.id + '-ifc-mac');
-                                               if (m)
+                                               stat += String.format(
+                                                       '<br /><small>%h</small>',
+                                                               ifc.name
+                                               );
+
+                                               s.innerHTML = stat;
+                                       }
+
+                                       var d = document.getElementById(ifc.id + '-ifc-description');
+                                       if (d && ifc.proto && ifc.ifname)
+                                       {
+                                               if (ifc.is_up)
                                                {
-                                                       m.innerHTML = mac.toUpperCase();
+                                                       html += String.format('<strong><%:Uptime%>:</strong> %t<br />', ifc.uptime);
                                                }
 
-                                               var a = document.getElementById(ifc.id + '-ifc-addrs');
-                                               if (a)
+                                               if (ifc.type != 'tunnel')
                                                {
-                                                       if (ifc.ifname)
-                                                       {
-                                                               a.innerHTML = '';
-
-                                                               if (ifc.ipaddrs && ifc.ipaddrs.length)
-                                                               {
-                                                                       a.innerHTML += '<strong><%:IPv4%>: </strong>';
-
-                                                                       for (var j = 0; j < ifc.ipaddrs.length; j++)
-                                                                               a.innerHTML += String.format(
-                                                                                       '%s%s/%d',
-                                                                                       j ? ', ' : '',
-                                                                                       ifc.ipaddrs[j].addr,
-                                                                                       ifc.ipaddrs[j].prefix
-                                                                               );
-
-                                                                       a.innerHTML += '<br />';
-                                                               }
-
-                                                               if (ifc.ip6addrs && ifc.ip6addrs.length)
-                                                               {
-                                                                       a.innerHTML += '<strong><%:IPv6%>: </strong>';
-
-                                                                       for (var j = 0; j < ifc.ip6addrs.length; j++)
-                                                                               a.innerHTML += String.format(
-                                                                                       '%s%s/%d',
-                                                                                       j ? ', ' : '',
-                                                                                       ifc.ip6addrs[j].addr.toUpperCase(),
-                                                                                       ifc.ip6addrs[j].prefix
-                                                                               );
-
-                                                                       a.innerHTML += '<br />';
-                                                               }
-
-                                                               if (!a.innerHTML)
-                                                                       a.innerHTML = '<em><%:No address configured on this interface.%></em>'
-                                                       }
-                                                       else
-                                                       {
-                                                               a.innerHTML = '<em><%:Interface not present or not connected yet.%></em>';
-                                                       }
+                                                       html += String.format('<strong><%:MAC-Address%>:</strong> %s<br />', ifc.macaddr);
+                                               }
+
+                                               html += String.format(
+                                                       '<strong><%:RX%></strong>: %.2mB (%d <%:Pkts.%>)<br />' +
+                                                       '<strong><%:TX%></strong>: %.2mB (%d <%:Pkts.%>)<br />',
+                                                               ifc.rx_bytes, ifc.rx_packets,
+                                                               ifc.tx_bytes, ifc.tx_packets
+                                               );
+
+                                               if (ifc.ipaddrs && ifc.ipaddrs.length)
+                                               {
+                                                       html += '<strong><%:IPv4%>: </strong>';
+
+                                                       for (var i = 0; i < ifc.ipaddrs.length; i++)
+                                                               html += String.format(
+                                                                       '%s%s/%d',
+                                                                       i ? ', ' : '',
+                                                                       ifc.ipaddrs[i].addr,
+                                                                       ifc.ipaddrs[i].prefix
+                                                               );
+
+                                                       html += '<br />';
                                                }
 
-                                               var t = document.getElementById(ifc.id + '-ifc-transfer');
-                                               if (t)
+                                               if (ifc.ip6addrs && ifc.ip6addrs.length)
                                                {
-                                                       t.innerHTML = String.format(
-                                                               '<strong><%:RX%></strong>: %.2f <%:KB%> (%d <%:Pkts.%>)<br />' +
-                                                               '<strong><%:TX%></strong>: %.2f <%:KB%> (%d <%:Pkts.%>)<br />',
-                                                                       txb, txp, rxb, rxp
-                                                       );
+                                                       html += '<strong><%:IPv6%>: </strong>';
+
+                                                       for (var i = 0; i < ifc.ip6addrs.length; i++)
+                                                               html += String.format(
+                                                                       '%s%s/%d',
+                                                                       i ? ', ' : '',
+                                                                       ifc.ip6addrs[i].addr.toUpperCase(),
+                                                                       ifc.ip6addrs[i].prefix
+                                                               );
+
+                                                       html += '<br />';
                                                }
+
+                                               d.innerHTML = html;
+                                       }
+                                       else if (d && !ifc.proto)
+                                       {
+                                               var e = document.getElementById(ifc.id + '-ifc-edit');
+                                               if (e)
+                                                       e.disabled = true;
+
+                                               d.innerHTML = String.format(
+                                                       '<em><%:Unsupported protocol type.%></em><br />' +
+                                                       '<a href="%h"><%:Install protocol extensions...%></a>',
+                                                               '<%=luci.dispatcher.build_url("admin/system/packages")%>?query=luci-proto&display=available'
+                                               );
+                                       }
+                                       else if (d && !ifc.ifname)
+                                       {
+                                               d.innerHTML = String.format(
+                                                       '<em><%:Network without interfaces.%></em><br />' +
+                                                       '<a href="<%=luci.dispatcher.build_url("admin/network/network/%s")%>?tab.network.%s=physical"><%:Assign interfaces...%></a>',
+                                                               ifc.name, ifc.name
+                                               );
+                                       }
+                                       else if (d)
+                                       {
+                                               d.innerHTML = '<em><%:Interface not present or not connected yet.%></em>';
                                        }
                                }
-
-                               window.setTimeout(update_status, 5000);
                        }
-               )
-       };
-
-       update_status();
-]]></script>
+               }
+       );
+//]]></script>
 
 <fieldset class="cbi-section" style="display:none">
        <legend><%:Reconnecting interface%></legend>
        <img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" />
-       <span id="ifc-rc-status"><%:Waiting for router...%></span>
+       <span id="ifc-rc-status"><%:Waiting for changes to be applied...%></span>
 </fieldset>
 
 <div class="cbi-map">
@@ -182,42 +218,41 @@ $Id$
 
                <table class="cbi-section-table" style="margin:10px; empty-cells:hide">
                        <tr class="cbi-section-table-titles">
-                               <th class="cbi-section-table-cell">&nbsp;</th>
-                               <th class="cbi-section-table-cell"><%:Interface%></th>
-                               <th class="cbi-section-table-cell"><%:MAC%></th>
-                               <th class="cbi-section-table-cell" style="text-align:left"><%:Addresses%></th>
-                               <th class="cbi-section-table-cell" style="text-align:left"><%:Transfer%></th>
-                               <th class="cbi-section-table-cell" colspan="2"><%:Actions%></th>
+                               <th class="cbi-section-table-cell"><%:Network%></th>
+                               <th class="cbi-section-table-cell" style="text-align:left"><%:Status%></th>
+                               <th class="cbi-section-table-cell"><%:Actions%></th>
                        </tr>
-                       <% for i, net in ipairs(netlist) do %>
+                       <%
+                               for i, net in ipairs(netlist) do
+                                       local z = net[3]
+                                       local c = z and z:get_color() or "#EEEEEE"
+                                       local t = z and translate("Part of zone %q" % z:name()) or translate("No zone assigned")
+                       %>
                                <tr class="cbi-section-table-row cbi-rowstyle-<%=i % 2 + 1%>">
-                                       <td>
-                                               <span style="background-color:#FFFFFF; border:1px solid #CCCCCC; padding:2px"><%=net%></span>
-                                       </td>
-                                       <td class="cbi-value-field" style="width:16px; padding:3px; text-align:center" id="<%=net%>-ifc-signal">
-                                               <img src="<%=resource%>/icons/ethernet_disabled.png" style="width:16px; height:16px" /><br />
-                                               <small>?</small>
-                                       </td>
-                                       <td class="cbi-value-field" id="<%=net%>-ifc-mac">?</td>
-                                       <td class="cbi-value-field" style="text-align:left; padding:3px" id="<%=net%>-ifc-addrs"><em><%:Collecting data...%></em></td>
-                                       <td class="cbi-value-field" style="text-align:left; padding:3px" id="<%=net%>-ifc-transfer">
-                                               <strong><%:RX%></strong>: 0 <%:KB%> (0 <%:Pkts.%>)<br />
-                                               <strong><%:TX%></strong>: 0 <%:KB%> (0 <%:Pkts.%>)<br />
+                                       <td class="cbi-value-field" style="padding:3px">
+                                               <div class="ifacebox">
+                                                       <div class="ifacebox-head" style="background-color:<%=c%>" title="<%=pcdata(t)%>">
+                                                               <strong><%=net[1]:upper()%></strong>
+                                                       </div>
+                                                       <div class="ifacebox-body" id="<%=net[1]%>-ifc-devices">
+                                                               <img src="<%=resource%>/icons/ethernet_disabled.png" style="width:16px; height:16px" /><br />
+                                                               <small>?</small>
+                                                       </div>
+                                               </div>
                                        </td>
-                                       <td>
-                                               <a href="#" onclick="iface_shutdown('<%=net%>', true)"><img style="border:none" src="<%=resource%>/cbi/reload.gif" alt="<%:Reconnect this interface%>" title="<%:Reconnect this interface%>" /></a>
-                                               <a href="#" onclick="iface_shutdown('<%=net%>', false)"><img style="border:none" src="<%=resource%>/cbi/reset.gif" alt="<%:Shutdown this interface%>" title="<%:Shutdown this interface%>" /></a>
+                                       <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px" id="<%=net[1]%>-ifc-description">
+                                               <em><%:Collecting data...%></em>
                                        </td>
-                                       <td>
-                                               <a href="<%=luci.dispatcher.build_url("admin/network/network", net)%>"><img style="border:none" src="<%=resource%>/cbi/edit.gif" alt="<%:Edit this interface%>" title="<%:Edit this interface%>" /></a>
-                                               <a href="<%=luci.dispatcher.build_url("admin/network/iface_delete", net)%>" onclick="return confirm('<%:Really delete this interface? The deletion cannot be undone!\nYou might loose access to this router if you are connected via this interface.%>')"><img style="border:none" src="<%=resource%>/cbi/remove.gif" alt="<%:Delete this interface%>" title="<%:Delete this interface%>" /></a>
+                                       <td style="width:420px">
+                                               <input type="button" class="cbi-button cbi-button-reload" style="width:100px" onclick="iface_shutdown('<%=net[1]%>', true)" title="<%:Reconnect this interface%>" value="<%:Connect%>" />
+                                               <input type="button" class="cbi-button cbi-button-reset" style="width:100px" onclick="iface_shutdown('<%=net[1]%>', false)" title="<%:Shutdown this interface%>" value="<%:Stop%>" />
+                                               <input type="button" class="cbi-button cbi-button-edit" style="width:100px" onclick="location.href='<%=luci.dispatcher.build_url("admin/network/network", net[1])%>'" title="<%:Edit this interface%>" value="<%:Edit%>" id="<%=net[1]%>-ifc-edit" />
+                                               <input type="button" class="cbi-button cbi-button-remove" style="width:100px" onclick="if (confirm('<%:Really delete this interface? The deletion cannot be undone!\nYou might lose access to this device if you are connected via this interface.%>')) location.href='<%=luci.dispatcher.build_url("admin/network/iface_delete", net[1])%>'" title="<%:Delete this interface%>" value="<%:Delete%>" />
                                        </td>
                                </tr>
                        <% end %>
                </table>
 
-               <form action="<%=luci.dispatcher.build_url("admin/network/iface_add")%>" method="post">
-                       <input type="submit" class="cbi-button cbi-button-add" value="<%:Add new interface...%>" />
-               </form>
+               <input type="button" class="cbi-button cbi-button-add" value="<%:Add new interface...%>" onclick="location.href='<%=luci.dispatcher.build_url("admin/network/iface_add")%>'" />
        </fieldset>
 </div>