Was parsing incorrect column in route table
This commit is contained in:
+10
-17
@@ -205,8 +205,7 @@ end
|
||||
|
||||
---
|
||||
-- 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.
|
||||
-- The Omnissiah demands we follow the most efficient path.
|
||||
-- This version correctly parses the raw /proc/net/route file format.
|
||||
--
|
||||
-- @return (string) The IP address 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()
|
||||
local file = io.open("/proc/net/route", "r")
|
||||
if not file then
|
||||
-- Log the heresy of a missing route table
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
-- Skip the header line to begin parsing the sacred texts.
|
||||
file:read("*l")
|
||||
file:read("*l") -- Skip header line
|
||||
|
||||
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
|
||||
-- Capture all relevant fields, including the all-important Metric (field 6).
|
||||
local iface, dest, gw_hex, flags_hex, _, metric_str = line:match("^(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+%S+%s+(%d+)")
|
||||
-- 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+%S+%s+(%d+)")
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
-- Correctly parse the little-endian hex representation of the IP.
|
||||
if #gw_hex == 8 then
|
||||
local o4 = tonumber(string.sub(gw_hex, 1, 2), 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)
|
||||
|
||||
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, {
|
||||
iface = iface,
|
||||
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
|
||||
@@ -258,16 +252,15 @@ function utils.get_active_default_gateway()
|
||||
file:close()
|
||||
|
||||
if #routes == 0 then
|
||||
-- No valid paths to the datasphere were found.
|
||||
return nil, nil
|
||||
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)
|
||||
return a.metric < b.metric
|
||||
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]
|
||||
return active_route.gateway, active_route.iface
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user