canndrew / netsim

Network simulation in Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why not allow Ipv4Range have a single IP?

povilasb opened this issue · comments

Say I have this code snippet:

#[macro_use]
extern crate net_literals;
extern crate netsim;
extern crate tokio_core;
#[macro_use]
extern crate unwrap;

use netsim::{node, spawn, Ipv4Range, Network};
use tokio_core::reactor::Core;

fn main() {
    let mut evloop = unwrap!(Core::new());
    let network = Network::new(&evloop.handle());

    let server_recipe = node::ipv4::machine(|ip| {
        println!("[server] ip = {}", ip);
    });
    let (spawn_complete, _ipv4_plug) =
        spawn::ipv4_tree(&network.handle(), Ipv4Range::new(ipv4!("78.100.10.1"), 31), server_recipe);

    unwrap!(evloop.run(spawn_complete));
}

Which is not doing much, just spawning a single IPv4 machine. In such case maybe I'd like to assign a specific address to my machine. But if you run this code, netsim panics because of https://github.com/canndrew/netsim/blob/master/src/range/v4.rs#L112
Is there any particular reason we don't want to allow this behavior?

Static IPs also might be useful when we connect 2 nodes with router, then we don't need to exchange the IP addressess - less code in tests :)

It's because ipv4_tree tells the node that it's on the network 78.100.10.0/31, of which 78.100.10.1 is reserved for the default gateway (not that there is one in this case), and so there's no IP address left for the machine itself.

ipv4_tree is really for if you want netsim to pick IP addresses for you, because then it also sets up routing tables and gateways for you as well. If you want to use a specific IP then you need to use MachineBuilder directly.

got it, thx :)