Update my email addresses in the license headers
[project/luci.git] / modules / luci-mod-admin-full / luasrc / model / cbi / admin_network / iface_add.lua
1 -- Copyright 2009-2010 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local nw  = require "luci.model.network".init()
5 local fw  = require "luci.model.firewall".init()
6 local utl = require "luci.util"
7 local uci = require "luci.model.uci".cursor()
8
9 m = SimpleForm("network", translate("Create Interface"))
10 m.redirect = luci.dispatcher.build_url("admin/network/network")
11 m.reset = false
12
13 newnet = m:field(Value, "_netname", translate("Name of the new interface"),
14         translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
15                 "<code>0-9</code> and <code>_</code>"
16         ))
17
18 newnet:depends("_attach", "")
19 newnet.default = arg[1] and "net_" .. arg[1]:gsub("[^%w_]+", "_")
20 newnet.datatype = "uciname"
21
22 newproto = m:field(ListValue, "_netproto", translate("Protocol of the new interface"))
23
24 netbridge = m:field(Flag, "_bridge", translate("Create a bridge over multiple interfaces"))
25
26
27 sifname = m:field(Value, "_ifname", translate("Cover the following interface"))
28
29 sifname.widget = "radio"
30 sifname.template  = "cbi/network_ifacelist"
31 sifname.nobridges = true
32
33
34 mifname = m:field(Value, "_ifnames", translate("Cover the following interfaces"))
35
36 mifname.widget = "checkbox"
37 mifname.template  = "cbi/network_ifacelist"
38 mifname.nobridges = true
39
40
41 local _, p
42 for _, p in ipairs(nw:get_protocols()) do
43         if p:is_installed() then
44                 newproto:value(p:proto(), p:get_i18n())
45                 if not p:is_virtual()  then netbridge:depends("_netproto", p:proto()) end
46                 if not p:is_floating() then
47                         sifname:depends({ _bridge = "",  _netproto = p:proto()})
48                         mifname:depends({ _bridge = "1", _netproto = p:proto()})
49                 end
50         end
51 end
52
53 function newproto.validate(self, value, section)
54         local name = newnet:formvalue(section)
55         if not name or #name == 0 then
56                 newnet:add_error(section, translate("No network name specified"))
57         elseif m:get(name) then
58                 newnet:add_error(section, translate("The given network name is not unique"))
59         end
60
61         local proto = nw:get_protocol(value)
62         if proto and not proto:is_floating() then
63                 local br = (netbridge:formvalue(section) == "1")
64                 local ifn = br and mifname:formvalue(section) or sifname:formvalue(section)
65                 for ifn in utl.imatch(ifn) do
66                         return value
67                 end
68                 return nil, translate("The selected protocol needs a device assigned")
69         end
70         return value
71 end
72
73 function newproto.write(self, section, value)
74         local name = newnet:formvalue(section)
75         if name and #name > 0 then
76                 local br = (netbridge:formvalue(section) == "1") and "bridge" or nil
77                 local net = nw:add_network(name, { proto = value, type = br })
78                 if net then
79                         local ifn
80                         for ifn in utl.imatch(
81                                 br and mifname:formvalue(section) or sifname:formvalue(section)
82                         ) do
83                                 net:add_interface(ifn)
84                         end
85                         nw:save("network")
86                         nw:save("wireless")
87                 end
88                 luci.http.redirect(luci.dispatcher.build_url("admin/network/network", name))
89         end
90 end
91
92 return m