libs/core: Cleaned up luci.sys in favor of the new luci.ip module
authorSteven Barth <steven@midlink.org>
Fri, 8 Aug 2008 12:26:45 +0000 (12:26 +0000)
committerSteven Barth <steven@midlink.org>
Fri, 8 Aug 2008 12:26:45 +0000 (12:26 +0000)
applications/luci-splash/root/usr/bin/luci-splashd
libs/core/luasrc/ip.lua
libs/sys/luasrc/sys.lua
modules/admin-full/luasrc/view/admin_status/routes.htm
modules/freifunk/luasrc/controller/freifunk/luciinfo.lua
modules/freifunk/luasrc/view/public_status/index.htm

index 5551ba6..e8c2752 100755 (executable)
@@ -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
index 855bc4a..68ac384 100644 (file)
@@ -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
index 14a2f29..b8ec10e 100644 (file)
@@ -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
index c4ebf34..bfee622 100644 (file)
@@ -30,9 +30,9 @@ local routes = luci.sys.net.routes()
 for i, r in pairs(routes) do
 %>
 <tr>
-<td><%=luci.sys.net.hexip4(r.Destination)%></td>
-<td><%=luci.sys.net.hexip4(r.Mask)%></td>
-<td><%=luci.sys.net.hexip4(r.Gateway)%></td>
+<td><%=luci.ip.Hex(r.Destination, 32):string()%></td>
+<td><%=luci.ip.Hex(r.Mask, 32):string()%></td>
+<td><%=luci.ip.Hex(r.Gateway, 32):string()%></td>
 <td><%=r.Metric%></td>
 <td><%=r.Iface%></td>
 </tr>
index 4ab63a0..5ddb791 100644 (file)
@@ -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
index a10f22f..aa56834 100644 (file)
@@ -88,7 +88,7 @@ $Id$
 for i, rt in pairs(routes) do
 %>
 <tr>
-<td><%=luci.sys.net.hexip4(rt.Gateway)%></th>
+<td><%=luci.ip.Hex(rt.Gateway, 32):string()%></th>
 <td><%=rt.Metric%></th>
 <td><%=rt.Iface%></th>
 </tr>