proto_relay.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.is_up(self)
  26. local iface = self:get_interface()
  27. return iface and iface:is_up() or false
  28. end
  29. function proto.get_interface(self)
  30. return device(self.sid, self)
  31. end
  32. function proto.get_interfaces(self)
  33. if not self.ifaces then
  34. local ifs = { }
  35. local _, net, dev
  36. for net in luci.util.imatch(self:_get("network")) do
  37. net = netmod:get_network(net)
  38. if net then
  39. dev = net:get_interface()
  40. if dev then
  41. ifs[dev:name()] = dev
  42. end
  43. end
  44. end
  45. for dev in luci.util.imatch(self:_get("ifname")) do
  46. dev = netmod:get_interface(dev)
  47. if dev then
  48. ifs[dev:name()] = dev
  49. end
  50. end
  51. self.ifaces = { }
  52. for _, dev in luci.util.kspairs(ifs) do
  53. self.ifaces[#self.ifaces+1] = dev
  54. end
  55. end
  56. return self.ifaces
  57. end
  58. function proto.uptime(self)
  59. local net
  60. local upt = 0
  61. for net in luci.util.imatch(self:_get("network")) do
  62. net = netmod:get_network(net)
  63. if net then
  64. upt = math.max(upt, net:uptime())
  65. end
  66. end
  67. return upt
  68. end
  69. function proto.errors(self)
  70. return nil
  71. end
  72. function device.__init__(self, ifname, network)
  73. self.ifname = ifname
  74. self.network = network
  75. end
  76. function device.type(self)
  77. return "tunnel"
  78. end
  79. function device.is_up(self)
  80. if self.network then
  81. local _, dev
  82. for _, dev in ipairs(self.network:get_interfaces()) do
  83. if not dev:is_up() then
  84. return false
  85. end
  86. end
  87. return true
  88. end
  89. return false
  90. end
  91. function device._stat(self, what)
  92. local v = 0
  93. if self.network then
  94. local _, dev
  95. for _, dev in ipairs(self.network:get_interfaces()) do
  96. v = v + dev[what](dev)
  97. end
  98. end
  99. return v
  100. end
  101. function device.rx_bytes(self) return self:_stat("rx_bytes") end
  102. function device.tx_bytes(self) return self:_stat("tx_bytes") end
  103. function device.rx_packets(self) return self:_stat("rx_packets") end
  104. function device.tx_packets(self) return self:_stat("tx_packets") end
  105. function device.mac(self)
  106. if self.network then
  107. local _, dev
  108. for _, dev in ipairs(self.network:get_interfaces()) do
  109. return dev:mac()
  110. end
  111. end
  112. end
  113. function device.ipaddrs(self)
  114. local addrs = { }
  115. if self.network then
  116. addrs[1] = luci.ip.IPv4(self.network:_get("ipaddr"))
  117. end
  118. return addrs
  119. end
  120. function device.ip6addrs(self)
  121. return { }
  122. end
  123. function device.shortname(self)
  124. return "%s %q" % { luci.i18n.translate("Relay"), self.ifname }
  125. end
  126. function device.get_type_i18n(self)
  127. return luci.i18n.translate("Relay Bridge")
  128. end