From e080fcebd2396c138d8c916d1cf385a88e0db246 Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Fri, 8 Aug 2008 12:26:45 +0000 Subject: [PATCH] libs/core: Cleaned up luci.sys in favor of the new luci.ip module --- applications/luci-splash/root/usr/bin/luci-splashd | 8 +-- libs/core/luasrc/ip.lua | 4 +- libs/sys/luasrc/sys.lua | 71 +--------------------- .../admin-full/luasrc/view/admin_status/routes.htm | 6 +- .../luasrc/controller/freifunk/luciinfo.lua | 4 +- .../freifunk/luasrc/view/public_status/index.htm | 2 +- 6 files changed, 13 insertions(+), 82 deletions(-) diff --git a/applications/luci-splash/root/usr/bin/luci-splashd b/applications/luci-splash/root/usr/bin/luci-splashd index 5551ba684..e8c275209 100755 --- a/applications/luci-splash/root/usr/bin/luci-splashd +++ b/applications/luci-splash/root/usr/bin/luci-splashd @@ -1,7 +1,7 @@ #!/usr/bin/lua require("socket") -require("luci.sys") +require("luci.ip") require("luci.model.uci") luci.model.uci.set_savedir(luci.model.uci.savedir_state) @@ -14,13 +14,13 @@ while true do if client then client:settimeout(1) - local ip = client:getpeername() local srv + local ip = luci.ip.IPv4(client:getpeername()) luci.model.uci.foreach("network", "interface", function (section) if section.ipaddr then - local p = luci.sys.net.mask4prefix(section.netmask) - if luci.sys.net.belongs(ip, section.ipaddr, p) then + local net = luci.ip.IPv4(section.ipaddr, section.netmask) + if ip and net and net:contains(ip) then srv = section.ipaddr return end diff --git a/libs/core/luasrc/ip.lua b/libs/core/luasrc/ip.lua index 855bc4a81..68ac384dc 100644 --- a/libs/core/luasrc/ip.lua +++ b/libs/core/luasrc/ip.lua @@ -361,8 +361,8 @@ end function cidr.contains( self, addr ) assert( self[1] == addr[1], "Can't compare IPv4 and IPv6 addresses" ) - local mask1 = self:mask() - local mask2 = addr:mask() + local mask1 = self:prefix() + local mask2 = addr:prefix() if mask1 <= mask2 then return self:mask(addr:prefix()) == mask2 end diff --git a/libs/sys/luasrc/sys.lua b/libs/sys/luasrc/sys.lua index 14a2f294f..b8ec10e0f 100644 --- a/libs/sys/luasrc/sys.lua +++ b/libs/sys/luasrc/sys.lua @@ -30,6 +30,7 @@ require("posix") require("luci.bits") require("luci.util") require("luci.fs") +require("luci.ip") --- Invoke the luci-flash executable to write an image to the flash memory. -- @param kpattern Pattern of files to keep over flash process @@ -183,15 +184,6 @@ function net.arptable() return _parse_delimited_table(io.lines("/proc/net/arp"), "%s%s+") end ---- Test whether an IP-Adress belongs to a certain net. --- @param ip IPv4 address to test --- @param ipnet IPv4 network address of the net range to compare against --- @param prefix Network prefix of the net range to compare against --- @return Boolean indicating wheather the ip is within the range -function net.belongs(ip, ipnet, prefix) - return (net.ip4bin(ip):sub(1, prefix) == net.ip4bin(ipnet):sub(1, prefix)) -end - --- Determine the current default route. -- @return Table with the properties of the current default route. -- The following fields are defined: @@ -235,19 +227,6 @@ function net.ip4mac(ip) return mac end ---- Calculate the prefix from a given netmask. --- @param mask IPv4 net mask --- @return Number containing the corresponding numerical prefix -function net.mask4prefix(mask) - local bin = net.ip4bin(mask) - - if not bin then - return nil - end - - return #luci.util.split(bin, "1")-1 -end - --- Returns the current kernel routing table entries. -- @return Table of tables with properties of the corresponding routes. -- The following fields are defined for route entry tables: @@ -257,54 +236,6 @@ function net.routes() return _parse_delimited_table(io.lines("/proc/net/route")) end ---- Convert hexadecimal 32 bit value to IPv4 address. --- @param hex String containing the hexadecimal value --- @param be Boolean indicating wheather the given value is big endian --- @return String containing the corresponding IP4 address -function net.hexip4(hex, be) - if #hex ~= 8 then - return nil - end - - be = be or luci.util.bigendian() - - local hexdec = luci.bits.Hex2Dec - - local ip = "" - if be then - ip = ip .. tostring(hexdec(hex:sub(1,2))) .. "." - ip = ip .. tostring(hexdec(hex:sub(3,4))) .. "." - ip = ip .. tostring(hexdec(hex:sub(5,6))) .. "." - ip = ip .. tostring(hexdec(hex:sub(7,8))) - else - ip = ip .. tostring(hexdec(hex:sub(7,8))) .. "." - ip = ip .. tostring(hexdec(hex:sub(5,6))) .. "." - ip = ip .. tostring(hexdec(hex:sub(3,4))) .. "." - ip = ip .. tostring(hexdec(hex:sub(1,2))) - end - - return ip -end - ---- Convert given IPv4 address to binary value. --- @param ip String containing a IPv4 address --- @return String containing corresponding binary value -function net.ip4bin(ip) - local parts = luci.util.split(ip, '.') - if #parts ~= 4 then - return nil - end - - local decbin = luci.bits.Dec2Bin - - local bin = "" - bin = bin .. decbin(parts[1], 8) - bin = bin .. decbin(parts[2], 8) - bin = bin .. decbin(parts[3], 8) - bin = bin .. decbin(parts[4], 8) - - return bin -end --- Tests whether the given host responds to ping probes. -- @param host String containing a hostname or IPv4 address diff --git a/modules/admin-full/luasrc/view/admin_status/routes.htm b/modules/admin-full/luasrc/view/admin_status/routes.htm index c4ebf347a..bfee6227e 100644 --- a/modules/admin-full/luasrc/view/admin_status/routes.htm +++ b/modules/admin-full/luasrc/view/admin_status/routes.htm @@ -30,9 +30,9 @@ local routes = luci.sys.net.routes() for i, r in pairs(routes) do %> -<%=luci.sys.net.hexip4(r.Destination)%> -<%=luci.sys.net.hexip4(r.Mask)%> -<%=luci.sys.net.hexip4(r.Gateway)%> +<%=luci.ip.Hex(r.Destination, 32):string()%> +<%=luci.ip.Hex(r.Mask, 32):string()%> +<%=luci.ip.Hex(r.Gateway, 32):string()%> <%=r.Metric%> <%=r.Iface%> diff --git a/modules/freifunk/luasrc/controller/freifunk/luciinfo.lua b/modules/freifunk/luasrc/controller/freifunk/luciinfo.lua index 4ab63a068..5ddb791fe 100644 --- a/modules/freifunk/luasrc/controller/freifunk/luciinfo.lua +++ b/modules/freifunk/luasrc/controller/freifunk/luciinfo.lua @@ -28,7 +28,7 @@ function action_index() -- Sysinfo local s, m, r = luci.sys.sysinfo() local dr = luci.sys.net.defaultroute() - dr = dr and luci.sys.net.hexip4(dr.Gateway) or "" + dr = dr and luci.ip.Hex(dr.Gateway, 32, luci.ip.FAMILY_INET4):string() local l1, l5, l15 = luci.sys.loadavg() luci.http.write("sysinfo.system=" .. sanitize(s) .. "\n") @@ -38,7 +38,7 @@ function action_index() luci.http.write("sysinfo.load1=" .. tostring(l1) .. "\n") luci.http.write("sysinfo.load5=" .. tostring(l5) .. "\n") luci.http.write("sysinfo.load15=" .. tostring(l15) .. "\n") - luci.http.write("sysinfo.defaultgw=" .. dr .. "\n") + luci.http.write("sysinfo.defaultgw=" .. dr or "" .. "\n") -- Freifunk diff --git a/modules/freifunk/luasrc/view/public_status/index.htm b/modules/freifunk/luasrc/view/public_status/index.htm index a10f22f43..aa56834ba 100644 --- a/modules/freifunk/luasrc/view/public_status/index.htm +++ b/modules/freifunk/luasrc/view/public_status/index.htm @@ -88,7 +88,7 @@ $Id$ for i, rt in pairs(routes) do %> -<%=luci.sys.net.hexip4(rt.Gateway)%> +<%=luci.ip.Hex(rt.Gateway, 32):string()%> <%=rt.Metric%> <%=rt.Iface%> -- 2.11.0