johnyburd / net-route

Rust crate providing a cross platform interface for interacting with the routing table
https://docs.rs/net-route/
21 stars 14 forks source link

How to route all traffic to a specified tun device by using this library? #8

Closed xmh0511 closed 8 months ago

xmh0511 commented 11 months ago

How do I achieve this target by using this crate? Honestly, the documentation is lack, I don't know how to do.

johnyburd commented 10 months ago

You might first want to determine the ifindex since the library doesn't currently support specifying interfaces by name #7, for example with this function: https://www.man7.org/linux/man-pages/man3/if_nametoindex.3.html

let tun_ifindex: u32 = 3;

let handle = Handle::new()?;
let route = Route::new("0.0.0.0".parse().unwrap(), 0).with_ifindex(tun_ifindex);
handle.add(&route).await
xmh0511 commented 10 months ago

You might first want to determine the ifindex since the library doesn't currently support specifying interfaces by name #7, for example with this function: https://www.man7.org/linux/man-pages/man3/if_nametoindex.3.html

let tun_ifindex: u32 = 3;

let handle = Handle::new()?;
let route = Route::new("0.0.0.0".parse().unwrap(), 0).with_ifindex(tun_ifindex);
handle.add(&route).await

Thanks. ~But, what should I do on the Windows platform? IMO, Windows does not have an interface index.~

Does with_ifindex still work on the Windows platform when geting if_index with https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/NetworkManagement/IpHelper/fn.if_nametoindex.html

xmh0511 commented 10 months ago

@johnyburd handle.add(&route).await just return the error:

Err(Custom { kind: AlreadyExists, error: "rtm_errno 17" })
index.crates.io-6f17d22bba15001f/net-route-0.2.11/src/platform_impl/macos/macos.rs:254:25:
index out of bounds: the len is 12 but the index is 12
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
johnyburd commented 10 months ago

yes Route::with_ifindex is supported on windows.

I'm not able to tell what's wrong in the snipped you posted from macos. I would need a minimum reproduction of the error and perhaps a dump of your routing table.

xmh0511 commented 10 months ago

yes Route::with_ifindex is supported on windows.

I'm not able to tell what's wrong in the snipped you posted from macos. I would need a minimum reproduction of the error and perhaps a dump of your routing table.

It's here https://github.com/johnyburd/net-route/issues/15#issuecomment-1890300339

johnyburd commented 8 months ago

Closing in favor of #7