netdev/route/
mod.rs

1use crate::interface::interface::Interface;
2use crate::net::device::NetworkDevice;
3use std::net::IpAddr;
4
5/// Get default Gateway
6pub fn get_default_gateway() -> Result<NetworkDevice, String> {
7    let local_ip: IpAddr = match crate::net::ip::get_local_ipaddr() {
8        Some(local_ip) => local_ip,
9        None => return Err(String::from("Local IP address not found")),
10    };
11    let interfaces: Vec<Interface> = crate::interface::get_interfaces();
12    for iface in interfaces {
13        match local_ip {
14            IpAddr::V4(local_ipv4) => {
15                if iface.ipv4.iter().any(|x| x.addr() == local_ipv4) {
16                    if let Some(gateway) = iface.gateway {
17                        return Ok(gateway);
18                    }
19                }
20            }
21            IpAddr::V6(local_ipv6) => {
22                if iface.ipv6.iter().any(|x| x.addr() == local_ipv6) {
23                    if let Some(gateway) = iface.gateway {
24                        return Ok(gateway);
25                    }
26                }
27            }
28        }
29    }
30    Err(String::from("Default Gateway not found"))
31}