Skip to content

Commit 6425afa

Browse files
committed
reorganize modules
1 parent aa3b840 commit 6425afa

File tree

6 files changed

+67
-64
lines changed

6 files changed

+67
-64
lines changed

src/client/generate.rs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use anyhow::Result;
2+
use caracat::models::Probe;
3+
use ipnet::IpNet;
4+
use rand::seq::SliceRandom;
5+
use rand::thread_rng;
6+
7+
use crate::client::target::Target;
8+
9+
pub fn generate_probes(target: &Target) -> Result<Vec<Probe>> {
10+
// TODO: We should pass an iterator instead of a vector.
11+
let mut probes = vec![];
12+
13+
// First start by dividing the prefix into /24s or /64s, if necessary.
14+
let subnets = match target.prefix {
15+
IpNet::V4(_) => {
16+
let prefix_len = target.prefix.prefix_len();
17+
let target_len = if prefix_len > 24 { prefix_len } else { 24 };
18+
target.prefix.subnets(target_len)
19+
}
20+
IpNet::V6(_) => {
21+
let prefix_len = target.prefix.prefix_len();
22+
let target_len = if prefix_len > 64 { prefix_len } else { 64 };
23+
target.prefix.subnets(target_len)
24+
}
25+
}?;
26+
27+
// Iterate over the subnets and generate the probes.
28+
for subnet in subnets {
29+
// Right now the probe generation is simplistic, we just iterate over the hosts.
30+
// If we need more flows than hosts, we will we explicitely fail.
31+
// TODO: implement mapper-like generator such as the ones in diamond-miner.
32+
// https://github.com/dioptra-io/diamond-miner/blob/main/diamond_miner/mappers.py
33+
let mut prefix_hosts = subnet.hosts();
34+
if target.n_flows > prefix_hosts.count().try_into()? {
35+
return Err(anyhow::anyhow!("Not enough hosts in the prefix"));
36+
}
37+
38+
for _ in 0..target.n_flows {
39+
let dst_addr = prefix_hosts.next().unwrap();
40+
41+
// Randomize the probes order within a flow.
42+
// In YARRP we randomize the probes over the entire probing space.
43+
// This is of course a very big simplication, but it's not that silly.
44+
// The rational is to avoid results errors due to path changes.
45+
// So, for now, probes belonging to the same traceroute flow will be sent close in time.
46+
// TODO: is this shuffle fast?
47+
let mut ttls: Vec<u8> = (target.min_ttl..target.max_ttl).collect();
48+
ttls.shuffle(&mut thread_rng());
49+
50+
for i in ttls {
51+
probes.push(Probe {
52+
dst_addr,
53+
src_port: 24000,
54+
dst_port: 33434,
55+
ttl: i,
56+
protocol: target.protocol.clone(),
57+
});
58+
}
59+
}
60+
}
61+
62+
Ok(probes)
63+
}

src/client/handler.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use anyhow::Result;
22
use log::trace;
33

44
use crate::auth::{KafkaAuth, SaslAuth};
5+
use crate::client::generate::generate_probes;
56
use crate::client::producer::produce;
7+
use crate::client::target::decode_target;
68
use crate::config::AppConfig;
7-
use crate::probe::generate_probes;
8-
use crate::target::decode_target;
99

1010
pub async fn handle(config: &AppConfig, agents: &str, target: &str) -> Result<()> {
1111
trace!("Client handler");

src/client/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
mod generate;
12
mod handler;
23
mod producer;
4+
mod target;
35

46
pub use handler::handle;

src/target.rs src/client/target.rs

File renamed without changes.

src/main.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ mod auth;
33
mod client;
44
mod config;
55
mod probe;
6-
mod target;
76
mod utils;
87

98
use anyhow::Result;

src/probe.rs

-61
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,5 @@
11
use anyhow::Result;
22
use caracat::models::Probe;
3-
use ipnet::IpNet;
4-
use rand::seq::SliceRandom;
5-
use rand::thread_rng;
6-
7-
use crate::target::Target;
8-
9-
pub fn generate_probes(target: &Target) -> Result<Vec<Probe>> {
10-
// TODO: We should pass an iterator instead of a vector.
11-
let mut probes = vec![];
12-
13-
// First start by dividing the prefix into /24s or /64s, if necessary.
14-
let subnets = match target.prefix {
15-
IpNet::V4(_) => {
16-
let prefix_len = target.prefix.prefix_len();
17-
let target_len = if prefix_len > 24 { prefix_len } else { 24 };
18-
target.prefix.subnets(target_len)
19-
}
20-
IpNet::V6(_) => {
21-
let prefix_len = target.prefix.prefix_len();
22-
let target_len = if prefix_len > 64 { prefix_len } else { 64 };
23-
target.prefix.subnets(target_len)
24-
}
25-
}?;
26-
27-
// Iterate over the subnets and generate the probes.
28-
for subnet in subnets {
29-
// Right now the probe generation is simplistic, we just iterate over the hosts.
30-
// If we need more flows than hosts, we will we explicitely fail.
31-
// TODO: implement mapper-like generator such as the ones in diamond-miner.
32-
// https://github.com/dioptra-io/diamond-miner/blob/main/diamond_miner/mappers.py
33-
let mut prefix_hosts = subnet.hosts();
34-
if target.n_flows > prefix_hosts.count().try_into()? {
35-
return Err(anyhow::anyhow!("Not enough hosts in the prefix"));
36-
}
37-
38-
for _ in 0..target.n_flows {
39-
let dst_addr = prefix_hosts.next().unwrap();
40-
41-
// Randomize the probes order within a flow.
42-
// In YARRP we randomize the probes over the entire probing space.
43-
// This is of course a very big simplication, but it's not that silly.
44-
// The rational is to avoid results errors due to path changes.
45-
// So, for now, probes belonging to the same traceroute flow will be sent close in time.
46-
// TODO: is this shuffle fast?
47-
let mut ttls: Vec<u8> = (target.min_ttl..target.max_ttl).collect();
48-
ttls.shuffle(&mut thread_rng());
49-
50-
for i in ttls {
51-
probes.push(Probe {
52-
dst_addr,
53-
src_port: 24000,
54-
dst_port: 33434,
55-
ttl: i,
56-
protocol: target.protocol.clone(),
57-
});
58-
}
59-
}
60-
}
61-
62-
Ok(probes)
63-
}
643

654
pub fn encode_protocol(protocol: caracat::models::L4) -> String {
665
match protocol {

0 commit comments

Comments
 (0)