Crate netns_rs
SourceExpand description
This crate provides an ultra-simple interface for handling network namespaces in Rust. Changing namespaces requires elevated privileges, so in most cases this code needs to be run as root.
We can simply create a NetNs using NetNs::new
. Once created, the netns
instance can be used.
§Examples
use netns_rs::NetNs;
// create a new netns in `/var/run/netns` by default.
let mut ns = NetNs::new("my_netns").unwrap();
ns.run(|_| {
// do something in the new netns. eg. ip link add.
}).unwrap();
// removes netns.
ns.remove().unwrap();
To get a Netns that already exists, you can use the NetNs::get
series of functions.
use netns_rs::NetNs;
let ns = NetNs::get("my_netns").unwrap();
Or use get_from_current_thread
to get the netns of the current thread.
use netns_rs::get_from_current_thread;
let ns = get_from_current_thread().unwrap();
Structs§
- Default
Env - A default network namespace environment.
- NetNs
- A network namespace type.
Enums§
Traits§
- Env
- Defines a NetNs environment behavior.
Functions§
- get_
from_ current_ thread - Returns the NetNs of current thread.
- get_
from_ path - Returns the NetNs with the spectified path.