/// Add a route in the OS's routing table to get traffic flowing through this interface.
/// Returns an error if the process doesn't exit successfully, otherwise returns
/// true if the route was changed, false if the route already exists.
Add a route in the OS's routing table to get traffic flowing through this interface.
Returns an error if the process doesn't exit successfully, otherwise returns
true if the route was changed, false if the route already exists.
Add a route in the OS ' s routing table to get traffic flowing through this interface . Returns an error if the process doesn ' t exit successfully otherwise returns true if the route was changed false if the route already exists .
pub fn add_route(interface: &str, cidr: IpNetwork) -> Result<bool, Error> {
    if cfg!(target_os = "macos") {
        let real_interface =
            wgctrl::backends::userspace::resolve_tun(interface).with_str(interface)?;
        let output = cmd(
            "route",
            &[
                "-n",
                "add",
                if cidr.is_ipv4() { "-inet" } else { "-inet6" },
                &cidr.to_string(),
                "-interface",
                &real_interface,
            ],
        )?;
        let stderr = String::from_utf8_lossy(&output.stderr);
        if !output.status.success() {
            Err(format!(
                "failed to add route for device {} ({}): {}",
                &interface, real_interface, stderr
            )
            .into())
        } else {
            Ok(!stderr.contains("File exists"))
        }
    } else {
        // TODO(mcginty): use the netlink interface on linux to modify routing table.
        let _ = cmd(
            "ip",
            &["route", "add", &cidr.to_string(), "dev", &interface],
        );
        Ok(false)
    }
}
pub fn add_route ( interface : & str , cidr : IpNetwork ) -> Result < bool , Error > { if cfg ! ( target_os " " ) { let real_interface = wgctrl :: backends :: userspace :: resolve_tun ( interface ) . with_str ( interface ) ? ; let output = cmd ( " " , & [ " " , " " , if cidr . is_ipv4 ( ) { " " } else { " " } , & cidr . to_string ( ) , " " , & real_interface , ] , ) ? ; let stderr = String :: from_utf8_lossy ( & output . stderr ) ; if ! output . status . success ( ) { Err ( format ! ( " " interface real_interface stderr ) . into ( ) ) } else { Ok ( ! stderr . contains ( " " ) ) } } else { // TODO(mcginty): use the netlink interface on linux to modify routing table. let _ = cmd ( " " , & [ " " , " " , & cidr . to_string ( ) , " " , & interface ] , ) ; Ok ( false ) } }
Add a route in the OS's routing table to get traffic flowing through this interface.
Add a route in the OS ' s routing table to get traffic flowing through this interface .
interface
&str
cidr
IpNetwork