Completed rewrite of network interface configuration page
[project/luci.git] / modules / admin-core / luasrc / tools / webadmin.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 module("luci.tools.webadmin", package.seeall)
17 require("luci.model.uci")
18 require("luci.sys")
19 require("luci.ip")
20
21 function byte_format(byte)
22         local suff = {"B", "KB", "MB", "GB", "TB"}
23         for i=1, 5 do
24                 if byte > 1024 and i < 5 then
25                         byte = byte / 1024
26                 else
27                         return string.format("%.2f %s", byte, suff[i]) 
28                 end 
29         end
30 end
31
32 function network_get_addresses(net)
33         local addr = {}
34         local ipv4 = luci.model.uci.get_statevalue("network", net, "ipaddr")
35         local mav4 = luci.model.uci.get_statevalue("network", net, "netmask")
36         local ipv6 = luci.model.uci.get_statevalue("network", net, "ip6addr")
37         
38         if ipv4 and mav4 then
39                 ipv4 = luci.ip.IPv4(ipv4, mav4)
40                 
41                 if ipv4 then 
42                         table.insert(addr, ipv4:string())
43                 end
44         end
45
46         if ipv6 then
47                 table.insert(addr, ipv6)
48         end
49         
50         luci.model.uci.foreach("network", "alias",
51                 function (section)
52                         if section.interface == net then
53                                 if section.ipaddr and section.netmask then
54                                         local ipv4 = luci.ip.IPv4(section.ipaddr, section.netmask)
55                                         
56                                         if ipv4 then
57                                                 table.insert(addr, ipv4:string())
58                                         end
59                                 end
60                                 
61                                 if section.ip6addr then
62                                         table.insert(addr, section.ip6addr)
63                                 end
64                         end
65                 end
66         )
67         
68         return addr
69 end
70
71 function cbi_add_networks(field)
72         luci.model.uci.foreach("network", "interface",
73                 function (section)
74                         if section[".name"] ~= "loopback" then
75                                 field:value(section[".name"])
76                         end
77                 end
78         )
79 end
80
81 function cbi_add_knownips(field)
82         for i, dataset in ipairs(luci.sys.net.arptable()) do
83                 field:value(dataset["IP address"])
84         end
85 end
86
87 function network_get_zones(net)
88         if not luci.model.uci.load("firewall") then
89                 return nil
90         end
91         
92         local zones = {}
93         
94         luci.model.uci.foreach("firewall", "zone", 
95                 function (section)
96                         local znet = section.network or section.name
97                         if luci.util.contains(luci.util.split(znet, " "), net) then
98                                 table.insert(zones, section.name)
99                         end
100                 end
101         )
102         
103         return zones
104 end
105
106 function firewall_find_zone(name)
107         local find
108         
109         luci.model.uci.foreach("firewall", "zone", 
110                 function (section)
111                         if section.name == name then
112                                 find = section[".name"]
113                         end
114                 end
115         )
116         
117         return find
118 end