proto_relay.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. -- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local netmod = luci.model.network
  4. local device = luci.util.class(netmod.interface)
  5. netmod:register_pattern_virtual("^relay-%w")
  6. local proto = netmod:register_protocol("relay")
  7. function proto.get_i18n(self)
  8. return luci.i18n.translate("Relay bridge")
  9. end
  10. function proto.ifname(self)
  11. return "relay-" .. self.sid
  12. end
  13. function proto.opkg_package(self)
  14. return "relayd"
  15. end
  16. function proto.is_installed(self)
  17. return nixio.fs.access("/etc/init.d/relayd")
  18. end
  19. function proto.is_floating(self)
  20. return true
  21. end
  22. function proto.is_virtual(self)
  23. return true
  24. end
  25. function proto.get_interface(self)
  26. return device(self.sid, self)
  27. end
  28. function proto.get_interfaces(self)
  29. if not self.ifaces then
  30. local ifs = { }
  31. local _, net, dev
  32. for net in luci.util.imatch(self:_get("network")) do
  33. net = netmod:get_network(net)
  34. if net then
  35. dev = net:get_interface()
  36. if dev then
  37. ifs[dev:name()] = dev
  38. end
  39. end
  40. end
  41. for dev in luci.util.imatch(self:_get("ifname")) do
  42. dev = netmod:get_interface(dev)
  43. if dev then
  44. ifs[dev:name()] = dev
  45. end
  46. end
  47. self.ifaces = { }
  48. for _, dev in luci.util.kspairs(ifs) do
  49. self.ifaces[#self.ifaces+1] = dev
  50. end
  51. end
  52. return self.ifaces
  53. end
  54. function proto.uptime(self)
  55. local net
  56. local upt = 0
  57. for net in luci.util.imatch(self:_get("network")) do
  58. net = netmod:get_network(net)
  59. if net then
  60. upt = math.max(upt, net:uptime())
  61. end
  62. end
  63. return upt
  64. end
  65. function device.__init__(self, ifname, network)
  66. self.ifname = ifname
  67. self.network = network
  68. end
  69. function device.type(self)
  70. return "tunnel"
  71. end
  72. function device.is_up(self)
  73. if self.network then
  74. local _, dev
  75. for _, dev in ipairs(self.network:get_interfaces()) do
  76. if not dev:is_up() then
  77. return false
  78. end
  79. end
  80. return true
  81. end
  82. return false
  83. end
  84. function device._stat(self, what)
  85. local v = 0
  86. if self.network then
  87. local _, dev
  88. for _, dev in ipairs(self.network:get_interfaces()) do
  89. v = v + dev[what](dev)
  90. end
  91. end
  92. return v
  93. end
  94. function device.rx_bytes(self) return self:_stat("rx_bytes") end
  95. function device.tx_bytes(self) return self:_stat("tx_bytes") end
  96. function device.rx_packets(self) return self:_stat("rx_packets") end
  97. function device.tx_packets(self) return self:_stat("tx_packets") end
  98. function device.mac(self)
  99. if self.network then
  100. local _, dev
  101. for _, dev in ipairs(self.network:get_interfaces()) do
  102. return dev:mac()
  103. end
  104. end
  105. end
  106. function device.ipaddrs(self)
  107. local addrs = { }
  108. if self.network then
  109. addrs[1] = luci.ip.IPv4(self.network:_get("ipaddr"))
  110. end
  111. return addrs
  112. end
  113. function device.ip6addrs(self)
  114. return { }
  115. end
  116. function device.shortname(self)
  117. return "%s %q" % { luci.i18n.translate("Relay"), self.ifname }
  118. end
  119. function device.get_type_i18n(self)
  120. return luci.i18n.translate("Relay Bridge")
  121. end