1
0
mirror of https://github.com/AR2000AR/openComputers_codes.git synced 2025-09-08 14:41:14 +02:00

[network] handle dns_common not being installed

This commit is contained in:
2023-03-31 17:01:51 +02:00
parent 0dfd8a7726
commit 43dbe7a9df
3 changed files with 41 additions and 24 deletions

View File

@@ -17,11 +17,17 @@ if (opts["help"] or opts["h"] or #args == 0) then
end
---=============================================================================
for k, v in pairs(opts) do print(k, v) end
opts.W = tonumber(opts.W) or 5
opts.s = tonumber(opts.s) or 56
opts.p = opts.p or "A"
opts.W = tonumber(opts.W) or 5
opts.s = tonumber(opts.s) or 56
opts.p = opts.p or "A"
local targetIP = ipv4Address.fromString(assert(dns.toip(args[1])))
--in case dns_common is not installed
local rawIP = args[1]
if (dns) then
rawIP = assert(dns.toip(args[1]))
end
local targetIP = ipv4Address.fromString(assert(rawIP))
local route = network.router:getRoute(targetIP)
if (not route) then
print("No route to destination")
@@ -47,7 +53,7 @@ local function ping()
if (sent) then
sentICMP[i] = t
else
print(reason)
io.stderr:write(reason .. "\n")
os.sleep(1)
ping()
end

View File

@@ -1,7 +1,8 @@
local os = require("os")
local resolv = require("resolv")
local ipv4Address = require("network.ipv4.address")
local dnsTypes = require("dns")
local resolvLoaded, resolv = pcall(require, "resolv")
local dnsTypes
if (resolvLoaded) then dnsTypes = require("dns") end
---@class socketdns
local dns = {}
@@ -21,28 +22,37 @@ function dns.tohostname(address)
return nil, "not implemented"
end
---Converts from host name to IP address.\
---address can be an IP address or host name.\
---Returns a string with the first IP address found for address, followed by a table with all information returned by the resolver. In case of error, the function returns nil followed by an error message.
---@param address string
---@return string? ip
---@return table|string resolvedOrReason
function dns.toip(address)
if (pcall(ipv4Address.fromString, address)) then
return address, {name = address, alias = {address}, ip = {address}}
else
local msg = resolv.resolve(address, dnsTypes.CLASS.IN, dnsTypes.TYPE.A)
if (not msg) then return nil, "address not found" end
if (#msg.answer == 0) then
return nil, "address not found"
end
local resolved = {
name = (msg.answer[1] --[[@as DNSRessourceRecord]]):getName(),
alias = {},
ip = {}
}
if (resolvLoaded) then --resolv may not be installed
local msg = resolv.resolve(address, dnsTypes.CLASS.IN, dnsTypes.TYPE.A)
if (not msg) then return nil, "address not found" end
if (#msg.answer == 0) then
return nil, "address not found"
end
local resolved = {
name = (msg.answer[1] --[[@as DNSRessourceRecord]]):getName(),
alias = {},
ip = {}
}
local ip = ipv4Address.tostring((msg.answer[1] --[[@as DNSRessourceRecord]]):getRdata())
for _, answer in pairs(msg.answer) do
---@cast answer DNSRessourceRecord
table.insert(resolved.ip, ipv4Address.tostring(answer:getRdata()))
table.insert(resolved.alias, answer:getName())
local ip = ipv4Address.tostring((msg.answer[1] --[[@as DNSRessourceRecord]]):getRdata())
for _, answer in pairs(msg.answer) do
---@cast answer DNSRessourceRecord
table.insert(resolved.ip, ipv4Address.tostring(answer:getRdata()))
table.insert(resolved.alias, answer:getName())
end
return ip, resolved
end
return ip, resolved
return nil, "No dns resolver installed"
end
end

View File

@@ -1,4 +1,5 @@
return {
udp = require("socket.udp"),
dns = require("socket.dns")
---@type socketdns
dns = select(2, pcall(require, "socket.dns"))
}