OpenComputers Von Neumann Machine Programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
3.7 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. local net = require("net")
  2. local fs = require("filesystem")
  3. local io = require("io")
  4. local serial = require("serialization")
  5. local dhcp = {
  6. SERV_PORT = 67,
  7. CLIENT_PORT = 68,
  8. CONFIG_FILE = "/etc/dhcp/config"
  9. }
  10. -- Local function for creating directory
  11. function createConfigDir(fileName)
  12. local configsExist = true
  13. local dir = fs.path(fileName)
  14. if fs.exists(dir) and not fs.isDirectory(dir) then
  15. fs.remove(dir)
  16. end
  17. if not fs.exists(dir) then
  18. fs.makeDirectory(dir)
  19. return true
  20. end
  21. return false
  22. end
  23. -- Gets an existing server config or creates one if it doesn't exist
  24. function dhcp.getServerConfig(fileName)
  25. if fileName == nil then fileName = dhcp.CONFIG_FILE end
  26. local configsExist = true
  27. if not createConfigDir(fileName) then
  28. if fs.isDirectory(fileName) then
  29. fs.remove(fileName)
  30. end
  31. if not fs.exists(fileName) then
  32. configsExist = false
  33. end
  34. else
  35. configsExist = false
  36. end
  37. if not configsExist then
  38. print("Creating config since one doesn't exist")
  39. local f = io.open(fileName, "w")
  40. config = {dns = "", pxe = ""}
  41. f:write(serial.serialize(config, 5))
  42. f:close()
  43. return config
  44. else
  45. f = io.open(fileName, "r")
  46. local data = ""
  47. while true do
  48. local line = f:read()
  49. if line ~= nil then
  50. data = data..line
  51. else
  52. break
  53. end
  54. end
  55. f:close()
  56. return serial.unserialize(data)
  57. end
  58. end
  59. function dhcp.startServer(servPort, clientPort, nonblocking, cacheConfig)
  60. -- Make sure ports aren't invalid
  61. if type(servPort) ~= "number" then
  62. servPort = dhcp.SERV_PORT
  63. end
  64. if type(clientPort) ~= "number" then
  65. clientPort = dhcp.CLIENT_PORT
  66. end
  67. -- Filter function for getting dhcp server messages
  68. function dhcp.serverMsg(name, ...)
  69. local t = {...}
  70. if t[3] == servPort and
  71. t[5] == "dhcp" and
  72. t[6] == clientPort and
  73. t[7] == "get" then
  74. return true
  75. end
  76. return false
  77. end
  78. print("Starting up DHCP server")
  79. if cacheConfig == nil then
  80. cacheConfig = true
  81. else
  82. cacheConfig = false
  83. end
  84. -- Read from config file each time, or just read once and use that for life of execution
  85. local config = dhcp.getServerConfig()
  86. while true do
  87. local pkt, err = net.recv{servPort, filter = dhcp.serverMsg, nonblocking = nonblocking}
  88. if pkt == nil and err == "interrupted" then
  89. print("Interrupted")
  90. return pkt, err
  91. end
  92. print("Received request from "..pkt.raddr)
  93. if cacheConfig then
  94. net.send(pkt.raddr, clientPort, "dhcp", serial.serialize(config))
  95. else
  96. print("Here")
  97. net.send(pkt.raddr, clientPort, "dhcp", serial.serialize(dhcp.getServerConfig()))
  98. end
  99. end
  100. end
  101. function dhcp.query(servPort, clientPort, nonblocking)
  102. -- Make sure ports aren't invalid
  103. if type(servPort) ~= "number" then
  104. servPort = dhcp.SERV_PORT
  105. end
  106. if type(clientPort) ~= "number" then
  107. clientPort = dhcp.CLIENT_PORT
  108. end
  109. -- Filter function for getting dhcp client messages
  110. function dhcp.clientMsg(name, ...)
  111. local t = {...}
  112. if t[3] ~= clientPort or t[5] ~= "dhcp" then
  113. return false
  114. elseif type(t[6]) ~= "string" then
  115. return false
  116. elseif serial.unserialize(t[6]) == nil then
  117. return false
  118. else
  119. return true
  120. end
  121. end
  122. net.broadcast(servPort, "dhcp", clientPort, "get")
  123. pkt, err = net.recv{
  124. clientPort,
  125. timeout = 3,
  126. filter = dhcp.clientMsg,
  127. nonblocking = not not nonblocking
  128. }
  129. return pkt, err
  130. end
  131. -- wget -f http://mc.bashed.rocks:13699/lib/dhcp.lua lib/dhcp.lua
  132. return dhcp