add constants to routes

This commit is contained in:
Michal Humpula
2026-03-01 08:22:18 +01:00
parent e2e68c8e81
commit c913fc0fb1

View File

@@ -7,6 +7,11 @@ use std::net::Ipv4Addr;
const MAIN_TABLE_ID: u8 = 254; const MAIN_TABLE_ID: u8 = 254;
// Route metrics - higher priority = lower number
const FAILOVER_METRIC: u32 = 5;
const PRIMARY_METRIC: u32 = 10;
const SECONDARY_METRIC: u32 = 20;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct RouteInfo { pub struct RouteInfo {
pub gateway: Ipv4Addr, pub gateway: Ipv4Addr,
@@ -60,8 +65,6 @@ impl RouteManager {
} }
pub fn set_primary_route(&mut self, gateway: Ipv4Addr, interface: String) -> Result<()> { pub fn set_primary_route(&mut self, gateway: Ipv4Addr, interface: String) -> Result<()> {
let primary_metric = 10;
// Remove existing routes for this interface if any // Remove existing routes for this interface if any
if let Some(pos) = self.routes.iter().position(|r| r.interface == interface) { if let Some(pos) = self.routes.iter().position(|r| r.interface == interface) {
let existing_route = self.routes[pos].clone(); let existing_route = self.routes[pos].clone();
@@ -73,7 +76,7 @@ impl RouteManager {
} }
// Add as primary route // Add as primary route
self.add_route(gateway, interface, primary_metric)?; self.add_route(gateway, interface, PRIMARY_METRIC)?;
Ok(()) Ok(())
} }
@@ -88,20 +91,17 @@ impl RouteManager {
self.set_primary_route(primary_gateway, primary_interface)?; self.set_primary_route(primary_gateway, primary_interface)?;
// Set secondary route with metric 20 (lower priority) // Set secondary route with metric 20 (lower priority)
let secondary_metric = 20; self.add_route(secondary_gateway, secondary_interface, SECONDARY_METRIC)?;
self.add_route(secondary_gateway, secondary_interface, secondary_metric)?;
Ok(()) Ok(())
} }
pub fn add_failover_route(&mut self, gateway: Ipv4Addr, interface: String) -> Result<()> { pub fn add_failover_route(&mut self, gateway: Ipv4Addr, interface: String) -> Result<()> {
let failover_metric = 5; // Higher priority than both primary (10) and secondary (20) self.add_route(gateway, interface, FAILOVER_METRIC)?;
self.add_route(gateway, interface, failover_metric)?;
Ok(()) Ok(())
} }
pub fn remove_failover_route(&mut self, gateway: Ipv4Addr, interface: String) -> Result<()> { pub fn remove_failover_route(&mut self, gateway: Ipv4Addr, interface: String) -> Result<()> {
let failover_metric = 5; self.remove_route(gateway, &interface, FAILOVER_METRIC)?;
self.remove_route(gateway, &interface, failover_metric)?;
Ok(()) Ok(())
} }