johnyburd / net-route Goto Github PK
View Code? Open in Web Editor NEWRust crate providing a cross platform interface for interacting with the routing table
Home Page: https://docs.rs/net-route/
Rust crate providing a cross platform interface for interacting with the routing table
Home Page: https://docs.rs/net-route/
The way of setting routes to make all traffic reach the specified tun interface is suggested to be:
let if_name = CString::new("utun6")?;
let tun_ifindex = unsafe{
libc::if_nametoindex(if_name.as_ptr())
};
let handle = Handle::new()?;
let route = Route::new("0.0.0.0".parse().unwrap(), 0).with_ifindex(tun_ifindex);
handle.add(&route).await // #1
However, #1
just returns an error Err(Custom { kind: AlreadyExists, error: "rtm_errno 17" })
. If set the routing with Route::new("20.0.0.0".parse().unwrap(), 24).with_ifindex(tun_ifindex);
then it's OK.
In both case, there is a panic at
.cargo/registry/src/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
IPv6 LAN-local routes come back with either /0 or /128 prefix and no interface index. I'm investigating to see if I can do a PR.
I found that the routing table cannot be recovered once the program setting the routing table with net-route
exits without being correct to call delete
API, the only approach is to restart the system. net-route
seems can record the original associated setting to disk such that the routing table can be recoverable after the next time the program run.
OS: macOS 14.2.1 (23C71)
CPU: Apple M1
error[E0560]: struct `bind::rt_metrics` has no field named `rmx_state`
--> /Users/frost/.cargo/registry/src/index.crates.io-6f17d22bba15001f/net-route-0.2.8/src/platform_impl/macos/macos.rs:231:13
|
231 | rmx_state: 0,
| ^^^^^^^^^ `bind::rt_metrics` does not have this field
|
= note: all struct fields are already assigned
error[E0308]: mismatched types
--> /Users/frost/.cargo/registry/src/index.crates.io-6f17d22bba15001f/net-route-0.2.8/src/platform_impl/macos/macos.rs:232:25
|
232 | rmx_filler: [0u32; 3],
| ^^^^^^^^^ expected an array with a fixed size of 4 elements, found one with 3 elements
new rt_metrics struct
struct rt_metrics {
u_int32_t rmx_locks; /* Kernel leaves these values alone */
u_int32_t rmx_mtu; /* MTU for this path */
u_int32_t rmx_hopcount; /* max hops expected */
int32_t rmx_expire; /* lifetime for route, e.g. redirect */
u_int32_t rmx_recvpipe; /* inbound delay-bandwidth product */
u_int32_t rmx_sendpipe; /* outbound delay-bandwidth product */
u_int32_t rmx_ssthresh; /* outbound gateway buffer limit */
u_int32_t rmx_rtt; /* estimated round trip time */
u_int32_t rmx_rttvar; /* estimated rtt variance */
u_int32_t rmx_pksent; /* packets sent using this route */
u_int32_t rmx_filler[4]; /* will be used for TCP's peer-MSS cache */
};
When setting 0.0.0.0/1
, almost traffic are routing to tun
, and bind_device_by_index_v4
is used to make the socket binding to the specified network interface such that the traffic of the socket does not pass through the routing table. However, it seems not work when using net-route
, the approach does work when using tproxy-config
.
The error is
called
Result::unwrap()
on anErr
value: Os { code: 51, kind: NetworkUnreachable, message: "Network is unreachable" }
How do I achieve this target by using this crate? Honestly, the documentation is lack, I don't know how to do.
use futures::StreamExt;
use net_route::Handle;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let handle = Handle::new()?;
let stream = handle.route_listen_stream();
futures::pin_mut!(stream);
println!("Listening for route events, press Ctrl+C to cancel...");
while let Some(value) = stream.next().await {
println!("{:?}", value);
}
Ok(())
}
this is code from examples/listen.rs
, It would block when you switch the wifi, this bug is 100% reproducible
I'm new to this, but it seems like rt_msghdr->rtm_index in macOS is the interface index
I want to get the actual interface used by each route, just like netstat -nr
I used Go before and it did the same thing https://cs.opensource.google/go/x/net/+/master:route/route_classic.go;drc=d23d9bc549229fd1a9d375dc91141fcf1385d257;l=47
target/debug/build/net-route-02f35d3dceb1c1e4/out/bindings.rs: No such file or directory (os error 2)
|
5 | include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `include` (in Nightly builds, run with -Z macro-backtrace for more info)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.