udp.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. -- Cjdns admin module for Lua
  2. -- Written by Philip Horger
  3. common = require 'cjdns/common'
  4. UDPInterface = {}
  5. UDPInterface.__index = UDPInterface
  6. common.UDPInterface = UDPInterface
  7. function UDPInterface.new(ai, config, ptype)
  8. properties = {
  9. ai = ai,
  10. config = config or ai.config,
  11. ptype = ptype or "ai"
  12. }
  13. return setmetatable(properties, UDPInterface)
  14. end
  15. function UDPInterface:call(name, args)
  16. local func = self[name .. "_" .. self.ptype]
  17. return func(self, unpack(args))
  18. end
  19. function UDPInterface:newBind(...)
  20. return self:call("newBind", arg)
  21. end
  22. function UDPInterface:beginConnection(...)
  23. return self:call("beginConnection", arg)
  24. end
  25. function UDPInterface:newBind_ai(address)
  26. local response, err = self.ai:auth({
  27. q = "UDPInterface_new",
  28. bindAddress = address
  29. })
  30. if not response then
  31. return nil, err
  32. elseif response.error ~= "none" then
  33. return nil, response.error
  34. elseif response.interfaceNumber then
  35. return response.interfaceNumber
  36. else
  37. return nil, "bad response format"
  38. end
  39. end
  40. function UDPInterface:newBind_config(address)
  41. local udpif = self.config.contents.interfaces.UDPInterface
  42. local new_interface = {
  43. bind = address,
  44. connectTo = {}
  45. }
  46. table.insert(udpif, new_interface)
  47. return (#udpif - 1), new_interface
  48. end
  49. function UDPInterface:newBind_perm(...)
  50. return
  51. self:newBind_config(unpack(arg)),
  52. self:newBind_ai(unpack(arg))
  53. end
  54. function UDPInterface:beginConnection_ai(pubkey, addr, password, interface)
  55. local request = {
  56. q = "UDPInterface_beginConnection",
  57. publicKey = pubkey,
  58. address = addr,
  59. password = password
  60. }
  61. if interface then
  62. request.interfaceNumber = interface
  63. end
  64. local response, err = self.ai:auth(request)
  65. if not response then
  66. return nil, err
  67. elseif response.error == "none" then
  68. -- Unfortunately, no real success indicator either.
  69. return "No error"
  70. else
  71. return nil, response.error
  72. end
  73. end
  74. function UDPInterface:beginConnection_config(pubkey, addr, password, interface)
  75. local udpif = self.config.contents.interfaces.UDPInterface
  76. local connections = udpif[(interface or 0) + 1].connectTo
  77. local this_conn = {
  78. password = password,
  79. publicKey = pubkey
  80. }
  81. connections[addr] = this_conn
  82. return this_conn -- allows adding metadata fields afterwards
  83. end
  84. function UDPInterface:beginConnection_perm(...)
  85. return
  86. self:beginConnection_config(unpack(arg)),
  87. self:beginConnection_ai(unpack(arg))
  88. end