From 18e6b179111abf2b48e7720b44c083c2c3580031 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Wed, 4 Sep 2019 16:39:50 +0200 Subject: [PATCH] luci-base: network.js: add getHostHints() api Signed-off-by: Jo-Philipp Wich --- .../htdocs/luci-static/resources/network.js | 75 ++++++++++++++++++- 1 file changed, 72 insertions(+), 3 deletions(-) diff --git a/modules/luci-base/htdocs/luci-static/resources/network.js b/modules/luci-base/htdocs/luci-static/resources/network.js index 70e360b68..235ea1e41 100644 --- a/modules/luci-base/htdocs/luci-static/resources/network.js +++ b/modules/luci-base/htdocs/luci-static/resources/network.js @@ -99,6 +99,12 @@ var callGetProtoHandlers = rpc.declare({ expect: { '': {} } }); +var callGetHostHints = rpc.declare({ + object: 'luci', + method: 'getHostHints', + expect: { '': {} } +}); + var _init = null, _state = null, _protocols = {}, @@ -188,6 +194,16 @@ function getProtocolHandlers(cache) { }); } +function getHostHints(cache) { + return callGetHostHints().then(function(hosts) { + if (!L.isObject(hosts)) + throw !1; + return hosts; + }).catch(function() { + return {}; + }); +} + function getWifiStateBySid(sid) { var s = uci.get('wireless', sid); @@ -413,14 +429,15 @@ function initNetworkState(refresh) { if (_state == null || refresh) { _init = _init || Promise.all([ getInterfaceState(), getDeviceState(), getBoardState(), - getIfaddrState(), getNetdevState(), getWifidevState(), getProtocolHandlers(), + getIfaddrState(), getNetdevState(), getWifidevState(), + getHostHints(), getProtocolHandlers(), uci.load('network'), uci.load('wireless'), uci.load('luci') ]).then(function(data) { var board = data[2], ifaddrs = data[3], devices = data[4]; var s = { isTunnel: {}, isBridge: {}, isSwitch: {}, isWifi: {}, ifaces: data[0], devices: data[1], radios: data[5], - netdevs: {}, bridges: {}, switches: {} + hosts: data[6], netdevs: {}, bridges: {}, switches: {} }; for (var i = 0, a; (a = ifaddrs[i]) != null; i++) { @@ -674,7 +691,7 @@ function enumerateNetworks() { } -var Network, Protocol, Device, WifiDevice, WifiNetwork; +var Hosts, Network, Protocol, Device, WifiDevice, WifiNetwork; Network = L.Class.extend({ prefixToMask: prefixToMask, @@ -1238,6 +1255,58 @@ Network = L.Class.extend({ return initNetworkState().then(function() { return _state.hasDSLModem ? _state.hasDSLModem.type : null; }); + }, + + getHostHints: function() { + return initNetworkState().then(function() { + return new Hosts(_state.hosts); + }); + } +}); + +Hosts = L.Class.extend({ + __init__: function(hosts) { + this.hosts = hosts; + }, + + getHostnameByMACAddr: function(mac) { + return this.hosts[mac] ? this.hosts[mac].name : null; + }, + + getIPAddrByMACAddr: function(mac) { + return this.hosts[mac] ? this.hosts[mac].ipv4 : null; + }, + + getIP6AddrByMACAddr: function(mac) { + return this.hosts[mac] ? this.hosts[mac].ipv6 : null; + }, + + getHostnameByIPAddr: function(ipaddr) { + for (var mac in this.hosts) + if (this.hosts[mac].ipv4 == ipaddr && this.hosts[mac].name != null) + return this.hosts[mac].name; + return null; + }, + + getMACAddrByIPAddr: function(ipaddr) { + for (var mac in this.hosts) + if (this.hosts[mac].ipv4 == ipaddr) + return mac; + return null; + }, + + getHostnameByIP6Addr: function(ip6addr) { + for (var mac in this.hosts) + if (this.hosts[mac].ipv6 == ip6addr && this.hosts[mac].name != null) + return this.hosts[mac].name; + return null; + }, + + getMACAddrByIP6Addr: function(ip6addr) { + for (var mac in this.hosts) + if (this.hosts[mac].ipv6 == ip6addr) + return mac; + return null; } }); -- 2.25.1