Changeset 7607

Show
Ignore:
Timestamp:
10/04/11 15:32:18 (20 months ago)
Author:
jow
Message:

libs/core: rework class structure of network model, add per protocol is_installed() and opkg_package() utility functions to query availability

Location:
luci/trunk/libs/core/luasrc/model
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • luci/trunk/libs/core/luasrc/model/network.lua

    r7605 r7607  
    3939 
    4040 
    41 proto = { } 
    42 proto.generic = utl.class() 
     41protocol = utl.class() 
     42 
     43local _protocols = { } 
    4344 
    4445local _interfaces, _bridge, _switch, _tunnel 
     
    272273    if utl.instanceof(x, interface) then 
    273274        return x:name() 
    274     elseif utl.instanceof(x, proto.generic) then 
     275    elseif utl.instanceof(x, protocol) then 
    275276        return x:ifname() 
    276277    elseif type(x) == "string" then 
     
    278279    end 
    279280end 
     281 
     282function get_protocols(self) 
     283    local p = { } 
     284    local _, v 
     285    for _, v in ipairs(_protocols) do 
     286        p[#p+1] = v("__dummy__") 
     287    end 
     288    return p 
     289end 
     290 
     291function register_protocol(self, protoname) 
     292    local proto = utl.class(protocol) 
     293 
     294    function proto.__init__(self, name) 
     295        self.sid = name 
     296    end 
     297 
     298    function proto.proto(self) 
     299        return protoname 
     300    end 
     301 
     302    _protocols[#_protocols+1] = proto 
     303    _protocols[protoname]     = proto 
     304 
     305    return proto 
     306end 
     307 
     308function register_pattern_virtual(self, pat) 
     309    IFACE_PATTERNS_VIRTUAL[#IFACE_PATTERNS_VIRTUAL+1] = pat 
     310end 
     311 
    280312 
    281313function has_ipv6(self) 
     
    521553 
    522554 
    523 function network(name) 
     555function network(name, proto) 
    524556    if name then 
    525         local p = _uci_real:get("network", name, "proto") 
    526         local c = p and proto[p] or proto.generic 
     557        local p = proto or _uci_real:get("network", name, "proto") 
     558        local c = p and _protocols[p] or protocol 
    527559        return c(name) 
    528560    end 
    529561end 
    530562 
    531 function proto.generic.__init__(self, name) 
     563function protocol.__init__(self, name) 
    532564    self.sid = name 
    533565end 
    534566 
    535 function proto.generic._get(self, opt) 
     567function protocol._get(self, opt) 
    536568    local v = _uci_real:get("network", self.sid, opt) 
    537569    if type(v) == "table" then 
     
    541573end 
    542574 
    543 function proto.generic._ip(self, opt, family, list) 
     575function protocol._ip(self, opt, family, list) 
    544576    local ip = _uci_state:get("network", self.sid, opt) 
    545577    local fc = (family == 6) and ipc.IPv6 or ipc.IPv4 
     
    559591end 
    560592 
    561 function proto.generic.get(self, opt) 
     593function protocol.get(self, opt) 
    562594    return _get("network", self.sid, opt) 
    563595end 
    564596 
    565 function proto.generic.set(self, opt, val) 
     597function protocol.set(self, opt, val) 
    566598    return _set("network", self.sid, opt, val) 
    567599end 
    568600 
    569 function proto.generic.ifname(self) 
     601function protocol.ifname(self) 
    570602    local p = self:proto() 
    571603    if self:is_bridge() then 
     
    600632end 
    601633 
    602 function proto.generic.proto(self) 
    603     return self:_get("proto") or "none" 
    604 end 
    605  
    606 function proto.generic.type(self) 
     634function protocol.proto(self) 
     635    return "none" 
     636end 
     637 
     638function protocol.get_i18n(self) 
     639    local p = self:proto() 
     640    if p == "none" then 
     641        return i18n.translate("Unmanaged") 
     642    elseif p == "static" then 
     643        return i18n.translate("Static address") 
     644    elseif p == "dhcp" then 
     645        return i18n.translate("DHCP client") 
     646    else 
     647        return i18n.translate("Unknown") 
     648    end 
     649end 
     650 
     651function protocol.type(self) 
    607652    return self:_get("type") 
    608653end 
    609654 
    610 function proto.generic.name(self) 
     655function protocol.name(self) 
    611656    return self.sid 
    612657end 
    613658 
    614 function proto.generic.uptime(self) 
     659function protocol.uptime(self) 
    615660    local cnt = tonumber(_uci_state:get("network", self.sid, "connect_time")) 
    616661    if cnt ~= nil then 
     
    621666end 
    622667 
    623 function proto.generic.expires(self) 
     668function protocol.expires(self) 
    624669    local a = tonumber(_uci_state:get("network", self.sid, "lease_acquired")) 
    625670    local l = tonumber(_uci_state:get("network", self.sid, "lease_lifetime")) 
     
    631676end 
    632677 
    633 function proto.generic.metric(self) 
     678function protocol.metric(self) 
    634679    return tonumber(_uci_state:get("network", self.sid, "metric")) or 0 
    635680end 
    636681 
    637 function proto.generic.ipaddr(self) 
     682function protocol.ipaddr(self) 
    638683    return self:_ip("ipaddr", 4) 
    639684end 
    640685 
    641 function proto.generic.netmask(self) 
     686function protocol.netmask(self) 
    642687    return self:_ip("netmask", 4) 
    643688end 
    644689 
    645 function proto.generic.gwaddr(self) 
     690function protocol.gwaddr(self) 
    646691    return self:_ip("gateway", 4) 
    647692end 
    648693 
    649 function proto.generic.dnsaddrs(self) 
     694function protocol.dnsaddrs(self) 
    650695    return self:_ip("dns", 4, true) 
    651696end 
    652697 
    653 function proto.generic.ip6addr(self) 
     698function protocol.ip6addr(self) 
    654699    local ip6 = self:_ip("ip6addr", 6) 
    655700    if not ip6 then 
     
    668713end 
    669714 
    670 function proto.generic.gw6addr(self) 
     715function protocol.gw6addr(self) 
    671716    local ip6 = self:_ip("ip6gw", 6) 
    672717    if not ip6 then 
     
    679724end 
    680725 
    681 function proto.generic.dns6addrs(self) 
     726function protocol.dns6addrs(self) 
    682727    return self:_ip("dns", 6, true) 
    683728end 
    684729 
    685 function proto.generic.is_bridge(self) 
    686     return (self:type() == "bridge") 
    687 end 
    688  
    689 function proto.generic.is_virtual(self) 
     730function protocol.is_bridge(self) 
     731    return (not self:is_virtual() and self:type() == "bridge") 
     732end 
     733 
     734function protocol.opkg_package(self) 
     735    return nil 
     736end 
     737 
     738function protocol.is_installed(self) 
     739    return true 
     740end 
     741 
     742function protocol.is_virtual(self) 
    690743    return false 
    691744end 
    692745 
    693 function proto.generic.is_floating(self) 
     746function protocol.is_floating(self) 
    694747    return false 
    695748end 
    696749 
    697 function proto.generic.is_empty(self) 
     750function protocol.is_empty(self) 
    698751    if self:is_virtual() then 
    699752        return false 
     
    717770end 
    718771 
    719 function proto.generic.add_interface(self, ifname) 
     772function protocol.add_interface(self, ifname) 
    720773    ifname = _M:ifnameof(ifname) 
    721774    if ifname and not self:is_floating() then 
     
    738791end 
    739792 
    740 function proto.generic.del_interface(self, ifname) 
     793function protocol.del_interface(self, ifname) 
    741794    ifname = _M:ifnameof(ifname) 
    742795    if ifname and not self:is_floating() then 
     
    750803end 
    751804 
    752 function proto.generic.get_interface(self) 
     805function protocol.get_interface(self) 
    753806    if self:is_virtual() then 
    754807        _tunnel[self:proto() .. "-" .. self.sid] = true 
     
    779832end 
    780833 
    781 function proto.generic.get_interfaces(self) 
     834function protocol.get_interfaces(self) 
    782835    if self:is_bridge() or (self:is_virtual() and not self:is_floating()) then 
    783836        local ifaces = { } 
     
    815868end 
    816869 
    817 function proto.generic.contains_interface(self, ifname) 
     870function protocol.contains_interface(self, ifname) 
    818871    ifname = _M:ifnameof(ifname) 
    819872    if not ifname then 
     
    841894end 
    842895 
    843 function proto.generic.adminlink(self) 
     896function protocol.adminlink(self) 
    844897    return dsp.build_url("admin", "network", "network", self.sid) 
    845898end 
     
    13411394 
    13421395 
     1396-- setup base protocols 
     1397_M:register_protocol("static") 
     1398_M:register_protocol("dhcp") 
     1399_M:register_protocol("none") 
     1400 
    13431401-- load protocol extensions 
    13441402local exts = nfs.dir(utl.libpath() .. "/model/network") 
  • luci/trunk/libs/core/luasrc/model/network/proto_6x4.lua

    r7605 r7607  
    1919 
    2020local netmod = luci.model.network 
    21 local proto  = luci.util.class(netmod.proto.generic) 
    2221 
    23 netmod.IFACE_PATTERNS_VIRTUAL[#netmod.IFACE_PATTERNS_VIRTUAL+1] = "^6to4-%w" 
    24 netmod.IFACE_PATTERNS_VIRTUAL[#netmod.IFACE_PATTERNS_VIRTUAL+1] = "^6in4-%w" 
     22local _, p 
     23for _, p in ipairs({"6in4", "6to4"}) do 
    2524 
    26 function proto.__init__(self, name) 
    27     self.sid = name 
     25    local proto = netmod:register_protocol(p) 
     26 
     27    function proto.get_i18n(self) 
     28        if p == "6in4" then 
     29            return luci.i18n.translate("IPv6-in-IPv4 (RFC4213)") 
     30        elseif p == "6to4" then 
     31            return luci.i18n.translate("IPv6-over-IPv4") 
     32        end 
     33    end 
     34 
     35    function proto.ifname(self) 
     36        return p .. "-" .. self.sid 
     37    end 
     38 
     39    function proto.opkg_package(self) 
     40        return p 
     41    end 
     42 
     43    function proto.is_installed(self) 
     44        return nixio.fs.access("/lib/network/" .. p .. ".sh") 
     45    end 
     46 
     47    function proto.is_floating(self) 
     48        return true 
     49    end 
     50 
     51    function proto.is_virtual(self) 
     52        return true 
     53    end 
     54 
     55    function proto.get_interfaces(self) 
     56        return nil 
     57    end 
     58 
     59    function proto.contains_interface(self, ifname) 
     60        return (netmod:ifnameof(ifc) == self:ifname()) 
     61    end 
     62 
     63    netmod:register_pattern_virtual("^%s-%%w" % p) 
    2864end 
    29  
    30 function proto.ifname(self) 
    31     return self:proto() .. "-" .. self.sid 
    32 end 
    33  
    34 function proto.is_floating(self) 
    35     return true 
    36 end 
    37  
    38 function proto.is_virtual(self) 
    39     return true 
    40 end 
    41  
    42 function proto.get_interfaces(self) 
    43     return nil 
    44 end 
    45  
    46 function proto.contains_interface(self, ifname) 
    47     return (netmod:ifnameof(ifc) == self:ifname()) 
    48 end 
    49  
    50  
    51 netmod.proto["6to4"] = proto 
    52 netmod.proto["6in4"] = proto 
  • luci/trunk/libs/core/luasrc/model/network/proto_ppp.lua

    r7605 r7607  
    1919 
    2020local netmod = luci.model.network 
    21 local proto  = luci.util.class(netmod.proto.generic) 
    2221 
    23 netmod.IFACE_PATTERNS_VIRTUAL[#netmod.IFACE_PATTERNS_VIRTUAL+1] = "^3g-%w" 
    24 netmod.IFACE_PATTERNS_VIRTUAL[#netmod.IFACE_PATTERNS_VIRTUAL+1] = "^ppp-%w" 
    25 netmod.IFACE_PATTERNS_VIRTUAL[#netmod.IFACE_PATTERNS_VIRTUAL+1] = "^pptp-%w" 
    26 netmod.IFACE_PATTERNS_VIRTUAL[#netmod.IFACE_PATTERNS_VIRTUAL+1] = "^pppoe-%w" 
    27 netmod.IFACE_PATTERNS_VIRTUAL[#netmod.IFACE_PATTERNS_VIRTUAL+1] = "^pppoa-%w" 
     22local _, p 
     23for _, p in ipairs({"ppp", "pptp", "pppoe", "pppoa", "3g"}) do 
    2824 
    29 function proto.__init__(self, name) 
    30     self.sid = name 
     25    local proto = netmod:register_protocol(p) 
     26 
     27    function proto.get_i18n(self) 
     28        if p == "ppp" then 
     29            return luci.i18n.translate("PPP") 
     30        elseif p == "pptp" then 
     31            return luci.i18n.translate("PPtP") 
     32        elseif p == "3g" then 
     33            return luci.i18n.translate("UMTS/GPRS/EV-DO") 
     34        elseif p == "pppoe" then 
     35            return luci.i18n.translate("PPPoE") 
     36        elseif p == "pppoa" then 
     37            return luci.i18n.translate("PPPoATM") 
     38        end 
     39    end 
     40 
     41    function proto.ifname(self) 
     42        return p .. "-" .. self.sid 
     43    end 
     44 
     45    function proto.opkg_package(self) 
     46        if p == "ppp" or p == "pptp" then 
     47            return p 
     48        elseif p == "3g" then 
     49            return "comgt" 
     50        elseif p == "pppoe" then 
     51            return "ppp-mod-pppoe" 
     52        elseif p == "pppoa" then 
     53            return "ppp-mod-pppoa" 
     54        end 
     55    end 
     56 
     57    function proto.is_installed(self) 
     58        return nixio.fs.access("/lib/network/" .. p .. ".sh") 
     59    end 
     60 
     61    function proto.is_floating(self) 
     62        return (p ~= "pppoe") 
     63    end 
     64 
     65    function proto.is_virtual(self) 
     66        return true 
     67    end 
     68 
     69    function proto.get_interfaces(self) 
     70        if self:is_floating() then 
     71            return nil 
     72        else 
     73            return netmod.protocol.get_interfaces(self) 
     74        end 
     75    end 
     76 
     77    function proto.contains_interface(self, ifc) 
     78        if self:is_floating() then 
     79            return (netmod:ifnameof(ifc) == self:ifname()) 
     80        else 
     81            return netmod.protocol.contains_interface(self, ifname) 
     82        end 
     83    end 
     84 
     85    netmod:register_pattern_virtual("^%s-%%w" % p) 
    3186end 
    32  
    33 function proto.ifname(self) 
    34     return self:proto() .. "-" .. self.sid 
    35 end 
    36  
    37 function proto.is_floating(self) 
    38     return (self:proto() ~= "pppoe") 
    39 end 
    40  
    41 function proto.is_virtual(self) 
    42     return true 
    43 end 
    44  
    45 function proto.get_interfaces(self) 
    46     if self:is_floating() then 
    47         return nil 
    48     else 
    49         return netmod.proto.generic.get_interfaces(self) 
    50     end 
    51 end 
    52  
    53 function proto.contains_interface(self, ifc) 
    54     if self:is_floating() then 
    55         return (netmod:ifnameof(ifc) == self:ifname()) 
    56     else 
    57         return netmod.proto.generic.contains_interface(self, ifname) 
    58     end 
    59 end 
    60  
    61  
    62 netmod.proto["3g"]    = proto 
    63 netmod.proto["ppp"]   = proto 
    64 netmod.proto["pptp"]  = proto 
    65 netmod.proto["pppoe"] = proto 
    66 netmod.proto["pppoa"] = proto 
  • luci/trunk/libs/core/luasrc/model/network/proto_relay.lua

    r7605 r7607  
    1919 
    2020local netmod = luci.model.network 
    21 local proto  = luci.util.class(netmod.proto.generic) 
    2221local device = luci.util.class(netmod.interface) 
    2322 
    24 netmod.IFACE_PATTERNS_VIRTUAL[#netmod.IFACE_PATTERNS_VIRTUAL+1] = "^relay-%w" 
     23netmod:register_pattern_virtual("^relay-%w") 
    2524 
    26 function proto.__init__(self, name) 
    27     self.sid = name 
     25local proto = netmod:register_protocol("relay") 
     26 
     27function proto.get_i18n(self) 
     28    return luci.i18n.translate("Relay bridge") 
    2829end 
    2930 
    3031function proto.ifname(self) 
    3132    return "relay-" .. self.sid 
     33end 
     34 
     35function proto.opkg_package(self) 
     36    return "relayd" 
     37end 
     38 
     39function proto.is_installed(self) 
     40    return nixio.fs.access("/lib/network/relay.sh") 
    3241end 
    3342 
     
    155164    return luci.i18n.translate("Relay Bridge") 
    156165end 
    157  
    158  
    159 netmod.proto.relay = proto