Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/canndrew/netsim
Network simulation in Rust
https://github.com/canndrew/netsim
Last synced: 6 days ago
JSON representation
Network simulation in Rust
- Host: GitHub
- URL: https://github.com/canndrew/netsim
- Owner: canndrew
- Created: 2017-12-29T13:05:38.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-12-18T07:08:59.000Z (26 days ago)
- Last Synced: 2024-12-31T04:07:24.800Z (13 days ago)
- Language: Rust
- Size: 522 KB
- Stars: 192
- Watchers: 6
- Forks: 21
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE-BSD
Awesome Lists containing this project
- awesome-rust-cn - candrew/netsim - ci.org/canndrew/netsim.svg?branch=master">](https://travis-ci.org/canndrew/netsim) (Libraries / Network programming)
- awesome-rust - candrew/netsim - ci.org/canndrew/netsim.svg?branch=master">](https://travis-ci.org/canndrew/netsim) (Libraries / Network programming)
- awesome-rust - candrew/netsim
- awesome-rust-cn - candrew/netsim
- awesome-rust-zh - candrew/netsim - 用于网络模拟和测试的 Rust 库[<img src="https://api.travis-ci.org/canndrew/netsim.svg?branch=master">](https://travis-ci.org/canndrew/netsim) (库 / 网络编程)
README
# netsim
`netsim` is a Rust library which allows you to:
* Run tests in network-isolated threads.
* Test networking code on simulated networks.
* Capture and inspect packets produced by your code.
* Inject and meddle with network packets.[Documentation](https://docs.rs/netsim/)
## Examples
### Example 1: Isolating tests.
Suppose you have multiple tests that need to bind to the same port for some reason. By using
`#[netsim::isolate]` you can run your test suite without having to use `--test-threads=1` and
without having to stop any daemons running on your dev machine.```no_run
#[test]
#[netsim::isolate]
fn a_test() {
let _listener = std::net::TcpListener::bind("0.0.0.0:80").unwrap();
}#[test]
#[netsim::isolate]
fn another_test_that_runs_in_parallel() {
let _listener = std::net::TcpListener::bind("0.0.0.0:80").unwrap();
}
```### Example 2: Capturing a packet.
The `#[netsim::isolate]` attribute showcased above is just a convenient way to setup a `Machine`
and spawn a task onto it. To capture a packet you'll need to do these steps yourself. You'll also
need to give the machine a network interface to send the packet on.In this example we create a UDP socket and use it to send the message "hello" towards an arbitary
address. The packet then arrives on our `IpIface`, hoping to be routed somewhere, and we can check
that it still contains the correct message.```rust
let local_addr: SocketAddrV4 = "10.1.2.3:5555".parse().unwrap();
let remote_addr: SocketAddrV4 = "1.1.1.1:53".parse().unwrap();
let machine = Machine::new().unwrap();
let mut iface = {
machine
.add_ip_iface()
.ipv4_addr(*local_addr.ip())
.ipv4_default_route()
.build()
.unwrap()
};
machine.spawn(async move {
let socket = UdpSocket::bind(local_addr).await.unwrap();
socket.send_to(b"hello", remote_addr).await.unwrap();
}).await.unwrap();let packet = loop {
let packet = iface.next().await.unwrap().unwrap();
let IpPacketVersion::V4(packet) = packet.version_box() else { continue };
let Ipv4PacketProtocol::Udp(packet) = packet.protocol_box() else { continue };
break packet;
};
assert_eq!(packet.data(), b"hello");
```### More, longer examples.
Check out the [examples](https://github.com/canndrew/netsim/tree/master/examples) directory in
this repo.## Limitations
`netsim` currently only supports Linux since it makes use of the Linux containerization APIs.
## License
MIT or BSD-3-Clause at your option.