diff options
author | Jo-Philipp Wich <jo@mein.io> | 2019-09-04 16:39:50 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2019-09-10 15:28:16 +0200 |
commit | 18e6b179111abf2b48e7720b44c083c2c3580031 (patch) | |
tree | ddfabb3e2931988ec2d7826d8a9c31f23848069d /modules/luci-base/htdocs/luci-static | |
parent | 6f6a29bcb00e8d86508f89b4b4837e5e390cac84 (diff) |
luci-base: network.js: add getHostHints() api
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-base/htdocs/luci-static')
-rw-r--r-- | modules/luci-base/htdocs/luci-static/resources/network.js | 75 |
1 files 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 70e360b68f..235ea1e418 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; } }); |