Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Wireless-Modem-API
- This is a simple interface for wireless modems and makes use
- of the DNS servers that you have to set up seperately.
- With this you can register an URL at the specific DNS server so
- other clients can look up your long modem address just by entering
- your URL. (e.g. "google.mc" > "abcd-2q3as-2qejh-ßjadf-blah")
- By Piorjade
- ]]
- --CONFIGURATION
- local conf = {
- _DNS = "", -----ENTER THE ADDRESS OF THE DNS SERVER'S MODEM!!!
- _PORT = 8080, -----PORT MUST BE SAME AS THE PORT OF THE DNS SERVER!
- }
- --CONFIGURATION END
- local component = require("component")
- local computer = require("computer")
- local ser = require("serialization")
- local event = require("event")
- local os = require("os")
- local COMMMANDS = {
- registerURL = "rURL",
- deleteURL = "dURL",
- getAddress = "gADDR",
- getURL = "gURL",
- getAll = "gAll",
- }
- local errorMessages = {
- DNS_URL_NOT_FOUND = true,
- DNS_DELETE_FAILED = true,
- DNS_ADDR_NOT_FOUND = true,
- }
- local card
- for addr, a in component.list("modem") do
- card = component.proxy(addr)
- break
- end
- if not card then return error("no card detected") end
- local net = {}
- card.open(conf._PORT) --open up the port
- if not card.isOpen(conf._PORT) then print("FAILED TO OPEN PORT!") return end
- net.foundServer = nil
- function net.listenForDNS()
- net.foundServer = nil
- local _, myAddr, sAddr, port, distance, msg, meta = event.pull("modem_message", nil, nil, nil, nil, "DNS_SERVER")
- if port == conf._PORT and msg == "DNS_SERVER" and meta then
- meta = ser.unserialize(meta)
- if meta.type == "dns_server" then
- net.foundServer = {type = "dns_server", host = meta.host, address = sAddr}
- end
- end
- end
- function net.registerURL(URL)
- if conf._DNS ~= nil then
- card.send(conf._DNS, conf._PORT, "rURL", URL)
- local _, myAddr, sAddr, port, distance, msg, meta = event.pull(2, "modem_message", nil, conf._DNS)
- if _ and port == conf._PORT and sAddr == conf._DNS then
- if not errorMessages[msg] then
- return true, msg
- else
- return false, msg
- end
- elseif not _ then
- return false, "NO_RESPONSE"
- end
- else
- return false, "NO_DNS"
- end
- end
- function net.removeURL(URL)
- if conf._DNS ~= nil then
- card.send(conf._DNS, conf._PORT, "dURL", URL)
- local _, myAddr, sAddr, port, distance, msg, meta = event.pull(2, "modem_message", nil, conf._DNS)
- if _ and port == conf._PORT and sAddr == conf._DNS then
- if not errorMessages[msg] then
- return true, msg
- else
- return false, msg
- end
- elseif not _ then
- return false, "NO_RESPONSE"
- end
- else
- return false, "NO_DNS"
- end
- end
- function net.setDNS(addr)
- conf._DNS = addr
- end
- function net.getDNS()
- return conf._DNS
- end
- function net.getPort()
- return conf._PORT
- end
- function net.setPort(port)
- if type(port) == "number" then
- card.close(conf._PORT)
- conf._PORT = port
- card.open(conf._PORT)
- end
- end
- function net.getCard()
- return card
- end
- function net.resolve(address)
- if conf._DNS ~= nil then
- card.send(conf._DNS, conf._PORT, "gADDR", address)
- local _, myAddr, sAddr, port, distance, msg, meta = event.pull(2, "modem_message", nil, conf._DNS)
- if _ and port == conf._PORT and sAddr == conf._DNS then
- if not errorMessages[msg] then
- return true, msg
- else
- return false, msg
- end
- elseif not _ then
- return false, "NO_RESPONSE"
- end
- else
- return false, "NO_DNS"
- end
- end
- function net.getURL(address)
- if conf._DNS ~= nil then
- card.send(conf._DNS, conf._PORT, "gURL", address)
- local _, myAddr, sAddr, port, distance, msg, meta = event.pull(2, "modem_message", nil, conf._DNS)
- if _ and port == conf._PORT and sAddr == conf._DNS then
- if not errorMessages[msg] then
- return true, msg
- else
- return false, msg
- end
- elseif not _ then
- return false, "NO_RESPONSE"
- end
- else
- return false, "NO_DNS"
- end
- end
- function net.getALL()
- if conf._DNS ~= nil then
- card.send(conf._DNS, conf._PORT, "gAll")
- local _, myAddr, sAddr, port, distance, msg, meta = event.pull(2, "modem_message", nil, conf._DNS)
- if _ and port == conf._PORT and sAddr == conf._DNS then
- if not errorMessages[msg] then
- return true, ser.unserialize(msg)
- else
- return false, msg
- end
- elseif not _ then
- return false, "NO_RESPONSE"
- end
- else
- return false, "NO_DNS"
- end
- end
- return net
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement