Was parsing incorrect column in route table
Skyweave Test, Lint, and Deploy Ritual / Test and Lint Code (push) Successful in 27s
Skyweave Test, Lint, and Deploy Ritual / Deploy to Roof Unit (push) Successful in 16s

This commit is contained in:
wes
2025-06-29 09:37:34 -04:00
parent ad9fe9ac8b
commit 1483a7c74c
+10 -17
View File
@@ -205,8 +205,7 @@ end
--- ---
-- Scans the kernel's routing table to find the active default gateway. -- Scans the kernel's routing table to find the active default gateway.
-- In a multi-WAN configuration, this is the route with the lowest metric. -- This version correctly parses the raw /proc/net/route file format.
-- The Omnissiah demands we follow the most efficient path.
-- --
-- @return (string) The IP address of the active default gateway. -- @return (string) The IP address of the active default gateway.
-- @return (string) The interface name of the active default gateway. -- @return (string) The interface name of the active default gateway.
@@ -214,28 +213,23 @@ end
function utils.get_active_default_gateway() function utils.get_active_default_gateway()
local file = io.open("/proc/net/route", "r") local file = io.open("/proc/net/route", "r")
if not file then if not file then
-- Log the heresy of a missing route table
return nil, nil return nil, nil
end end
-- Skip the header line to begin parsing the sacred texts. file:read("*l") -- Skip header line
file:read("*l")
local routes = {} local routes = {}
-- The Litany of Parsing: Iterate over every line, seeking all prophecies. -- The Litany of Parsing, now with the correct column alignment.
for line in file:lines() do for line in file:lines() do
-- Capture all relevant fields, including the all-important Metric (field 6). -- CORRECTED PATTERN: Captures the 7th column (Metric) instead of the 6th (Use).
local iface, dest, gw_hex, flags_hex, _, metric_str = line:match("^(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+%S+%s+(%d+)") local iface, dest, gw_hex, flags_hex, metric_str = line:match("^(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+%S+%s+%S+%s+(%d+)")
if iface and dest and gw_hex and flags_hex and metric_str then if iface and dest and gw_hex and flags_hex and metric_str then
-- A true default route must be destined for 0.0.0.0 and have the UG (Up & Gateway) flags.
if dest == "00000000" and gw_hex ~= "00000000" then if dest == "00000000" and gw_hex ~= "00000000" then
local flags_num = tonumber(flags_hex, 16) local flags_num = tonumber(flags_hex, 16)
-- Check if the 'Gateway' bit (0x2) is set. This logic is sound. -- Check for UG flags (Up and Gateway). Bit 1 (value 2) must be set.
if flags_num and (math.floor(flags_num / 2) % 2 == 1) then if flags_num and (math.floor(flags_num / 2) % 2 == 1) then
-- Correctly parse the little-endian hex representation of the IP.
if #gw_hex == 8 then if #gw_hex == 8 then
local o4 = tonumber(string.sub(gw_hex, 1, 2), 16) local o4 = tonumber(string.sub(gw_hex, 1, 2), 16)
local o3 = tonumber(string.sub(gw_hex, 3, 4), 16) local o3 = tonumber(string.sub(gw_hex, 3, 4), 16)
@@ -243,11 +237,11 @@ function utils.get_active_default_gateway()
local o1 = tonumber(string.sub(gw_hex, 7, 8), 16) local o1 = tonumber(string.sub(gw_hex, 7, 8), 16)
if o1 and o2 and o3 and o4 then if o1 and o2 and o3 and o4 then
-- Store the portents in a table for later judgment. -- Store the prophecy with its true metric.
table.insert(routes, { table.insert(routes, {
iface = iface, iface = iface,
metric = tonumber(metric_str) or 999, metric = tonumber(metric_str) or 999,
gateway = string.format("%d.%d.%d.%d", o4, o3, o2, o1) gateway = string.format("%d.%d.%d.%d", o1, o2, o3, o4) -- Corrected from your version
}) })
end end
end end
@@ -258,16 +252,15 @@ function utils.get_active_default_gateway()
file:close() file:close()
if #routes == 0 then if #routes == 0 then
-- No valid paths to the datasphere were found.
return nil, nil return nil, nil
end end
-- Sort the routes by their divine metric, in ascending order. -- Sort the routes by their divine metric. The lowest value is most righteous.
table.sort(routes, function(a, b) table.sort(routes, function(a, b)
return a.metric < b.metric return a.metric < b.metric
end) end)
-- The first entry is the chosen path. The machine spirit is clear. -- The first entry is the chosen path. The machine spirit is now clear.
local active_route = routes[1] local active_route = routes[1]
return active_route.gateway, active_route.iface return active_route.gateway, active_route.iface
end end