luci-mod-admin-full: map DHCPv6 DUIDs to MACs
authorJo-Philipp Wich <jow@openwrt.org>
Mon, 25 Jan 2016 16:54:38 +0000 (17:54 +0100)
committerJo-Philipp Wich <jow@openwrt.org>
Mon, 25 Jan 2016 16:54:44 +0000 (17:54 +0100)
Map DUIDs to their corresponding MAC addresses in order to correlate them with
IPv4 information. This is useful to e.g. identify IPv6 hosts which do not send
a name.

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
modules/luci-mod-admin-full/luasrc/view/admin_network/lease_status.htm
modules/luci-mod-admin-full/luasrc/view/admin_status/index.htm

index ab8e732..f7787dd 100644 (file)
@@ -1,4 +1,18 @@
 <script type="text/javascript">//<![CDATA[
+       function duid2mac(duid) {
+               // DUID-LLT / Ethernet
+               if (duid.length === 28 && duid.substr(0, 8) === '00010001')
+                       return duid.substr(16).replace(/(..)(?=..)/g, '$1:').toUpperCase();
+
+               // DUID-LL / Ethernet
+               if (duid.length === 24 && duid.substr(0, 8) === '00030001')
+                       return duid.substr(8).replace(/(..)(?=..)/g, '$1:').toUpperCase();
+
+               return null;
+       }
+
+       var hosts = <%=luci.http.write_json(luci.sys.net.host_hints())%>;
+
        XHR.poll(5, '<%=url('admin/network/dhcplease_status')%>', null,
                function(x, st)
                {
                                        var tr = tb6.insertRow(-1);
                                                tr.className = 'cbi-section-table-row cbi-rowstyle-' + ((i % 2) + 1);
 
-                                       tr.insertCell(-1).innerHTML = st[1][i].hostname ? st[1][i].hostname : '?';
+                                       var host = hosts[duid2mac(st[1][i].duid)];
+                                       if (host)
+                                               tr.insertCell(-1).innerHTML = String.format(
+                                                       '<div style="max-width:200px;overflow:hidden;text-overflow:ellipsis">%s</div>',
+                                                       ((host.name && (host.ipv4 || host.ipv6))
+                                                               ? '%h (%s)'.format(host.name, host.ipv4 || host.ipv6)
+                                                               : '%h'.format(host.name || host.ipv4 || host.ipv6)).nobr()
+                                               );
+                                       else
+                                               tr.insertCell(-1).innerHTML = st[1][i].hostname ? st[1][i].hostname : '?';
+
                                        tr.insertCell(-1).innerHTML = st[1][i].ip6addr;
                                        tr.insertCell(-1).innerHTML = st[1][i].duid;
                                        tr.insertCell(-1).innerHTML = timestr;
        <legend><%:Active DHCPv6 Leases%></legend>
        <table class="cbi-section-table" id="lease6_status_table">
                <tr class="cbi-section-table-titles">
-                       <th class="cbi-section-table-cell"><%:Hostname%></th>
+                       <th class="cbi-section-table-cell"><%:Host%></th>
                        <th class="cbi-section-table-cell"><%:IPv6-Address%></th>
                        <th class="cbi-section-table-cell"><%:DUID%></th>
                        <th class="cbi-section-table-cell"><%:Leasetime remaining%></th>
index a37982e..8bfc61b 100644 (file)
                return s;
        }
 
+       function duid2mac(duid) {
+               // DUID-LLT / Ethernet
+               if (duid.length === 28 && duid.substr(0, 8) === '00010001')
+                       return duid.substr(16).replace(/(..)(?=..)/g, '$1:').toUpperCase();
+
+               // DUID-LL / Ethernet
+               if (duid.length === 24 && duid.substr(0, 8) === '00030001')
+                       return duid.substr(8).replace(/(..)(?=..)/g, '$1:').toUpperCase();
+
+               return null;
+       }
+
        var npoll = 1;
        var hosts = <%=luci.http.write_json(luci.sys.net.host_hints())%>;
 
                                        var tr = ls6.rows[0].parentNode.insertRow(-1);
                                                tr.className = 'cbi-section-table-row cbi-rowstyle-' + ((i % 2) + 1);
 
-                                       tr.insertCell(-1).innerHTML = info.leases6[i].hostname ? info.leases6[i].hostname : '?';
+                                       var host = hosts[duid2mac(info.leases6[i].duid)];
+                                       if (host)
+                                               tr.insertCell(-1).innerHTML = String.format(
+                                                       '<div style="max-width:200px;overflow:hidden;text-overflow:ellipsis">%s</div>',
+                                                       ((host.name && (host.ipv4 || host.ipv6))
+                                                               ? '%h (%s)'.format(host.name, host.ipv4 || host.ipv6)
+                                                               : '%h'.format(host.name || host.ipv4 || host.ipv6)).nobr()
+                                               );
+                                       else
+                                               tr.insertCell(-1).innerHTML = info.leases6[i].hostname ? info.leases6[i].hostname : '?';
+
                                        tr.insertCell(-1).innerHTML = info.leases6[i].ip6addr;
                                        tr.insertCell(-1).innerHTML = info.leases6[i].duid;
                                        tr.insertCell(-1).innerHTML = timestr;
 
        <table class="cbi-section-table" id="lease6_status_table">
                <tr class="cbi-section-table-titles">
-                       <th class="cbi-section-table-cell"><%:Hostname%></th>
+                       <th class="cbi-section-table-cell"><%:Host%></th>
                        <th class="cbi-section-table-cell"><%:IPv6-Address%></th>
                        <th class="cbi-section-table-cell"><%:DUID%></th>
                        <th class="cbi-section-table-cell"><%:Leasetime remaining%></th>