Skip to content
Snippets Groups Projects
Commit 31bc99c9 authored by Federico Lolli's avatar Federico Lolli
Browse files

fixed wrong specification implementation on sender port selection

sender (scanner) now uses a randomly generated port (as specification)
parent dc463a3c
Branches
Tags
No related merge requests found
......@@ -114,6 +114,13 @@ fn scan_on_all_interfaces(continuous_mode: bool, ips: &[Ipv4Addr]) -> anyhow::Re
let interfaces = get_all_interfaces().context("Failed to get network interfaces")?;
info!("Found {} interfaces", interfaces.len());
// randomly selected port for the listener
let listener_port = {
let mut rng = rand::rng();
rng.random_range(10000..65535)
};
debug!("Using port {}", listener_port.yellow());
let mut task_handlers = Vec::new();
// spawn listening thread
let listen_lifetime = if continuous_mode {
......@@ -121,7 +128,11 @@ fn scan_on_all_interfaces(continuous_mode: bool, ips: &[Ipv4Addr]) -> anyhow::Re
} else {
DEFAULT_WAIT_DISCOVERY_FOR
};
task_handlers.push(spawn_msg_listener(listen_lifetime, handle_scan_replies));
task_handlers.push(spawn_msg_listener(
listen_lifetime,
listener_port,
handle_scan_replies,
));
// send requests to all interfaces
let target_ips = interfaces
......@@ -129,7 +140,11 @@ fn scan_on_all_interfaces(continuous_mode: bool, ips: &[Ipv4Addr]) -> anyhow::Re
.filter_map(|itf| itf.broadcast)
.chain(ips.iter().copied()); // if specific IPs are provided, send requests to them
for ip in target_ips {
task_handlers.push(spawn_sender_scan_through(ip, continuous_mode));
task_handlers.push(spawn_sender_scan_through(
ip,
listener_port,
continuous_mode,
));
}
// wait for all tasks to finish
......@@ -154,11 +169,12 @@ fn scan_on_all_interfaces(continuous_mode: bool, ips: &[Ipv4Addr]) -> anyhow::Re
///
/// # Arguments
/// * `duration` - Duration to wait for messages.
/// * `listener_port` - Port to listen on.
/// * `handler` - Function to handle received messages.
///
/// # Returns
/// * `Ok(())` if successful, `Err(anyhow::Error)` if an error occurs.
fn spawn_msg_listener<F>(lifetime: Duration, handler: F) -> JoinHandle<TaskResult>
fn spawn_msg_listener<F>(lifetime: Duration, port: u16, handler: F) -> JoinHandle<TaskResult>
where
F: Fn(Result<(MavHeader, MavMessage), MessageReadError>) -> anyhow::Result<()>
+ Send
......@@ -166,16 +182,12 @@ where
+ 'static,
{
thread::spawn(move || {
let recv_endpoint = format!("udpin:0.0.0.0:{}", DEFAULT_MAVLINK_PORT);
let mut recv_connection = mavlink::connect::<MavMessage>(&recv_endpoint).context(
format!("Failed to connect to 0.0.0.0:{}", DEFAULT_MAVLINK_PORT),
)?;
let recv_endpoint = format!("udpin:0.0.0.0:{}", port);
let mut recv_connection = mavlink::connect::<MavMessage>(&recv_endpoint)
.context(format!("Failed to connect to 0.0.0.0:{}", port))?;
recv_connection
.set_read_timeout(Some(STANDARD_UDP_IO_TIMEOUT))
.context(format!(
"Failed to set read timeout for 0.0.0.0:{}",
DEFAULT_MAVLINK_PORT
))?;
.context(format!("Failed to set read timeout for 0.0.0.0:{}", port))?;
let mut scheduler = repeat(
move || {
......@@ -216,6 +228,7 @@ where
/// * `JoinHandle<TaskResult>` - Handle to the spawned thread.
fn spawn_sender_scan_through(
bcast_address: Ipv4Addr,
listener_port: u16,
continuous_mode: bool,
) -> JoinHandle<TaskResult> {
thread::spawn(move || {
......@@ -238,7 +251,7 @@ fn spawn_sender_scan_through(
abort_if_necessary!();
let request_data = GS_DISCOVERY_REQUEST_DATA {
source_ip: local_address.to_bits(),
source_port: DEFAULT_MAVLINK_PORT,
source_port: listener_port,
};
let request = MavMessage::GS_DISCOVERY_REQUEST(request_data.clone());
if let Err(e) = send_connection.send(&MavHeader::default(), &request) {
......@@ -400,7 +413,10 @@ fn handle_scan_requests(
match res {
Ok((_, msg)) => match msg {
MavMessage::GS_DISCOVERY_REQUEST(
data @ GS_DISCOVERY_REQUEST_DATA { source_ip, .. },
data @ GS_DISCOVERY_REQUEST_DATA {
source_ip,
source_port,
},
) => {
let source_ip = Ipv4Addr::from(source_ip);
debug!("Received {}", data.to_pretty_string());
......@@ -412,9 +428,9 @@ fn handle_scan_requests(
debug!(
"Sending DISCOVERY_RESPONSE to {}:{}",
source_ip.bold().yellow(),
DEFAULT_MAVLINK_PORT.yellow()
source_port.yellow()
);
let send_endpoint = format!("udpout:{}:{}", source_ip, DEFAULT_MAVLINK_PORT);
let send_endpoint = format!("udpout:{}:{}", source_ip, source_port);
let mut send_connection = mavlink::connect::<MavMessage>(&send_endpoint)
.context("Failed to connect to send endpoint")?;
send_connection
......@@ -439,13 +455,13 @@ fn handle_scan_requests(
)
.context(format!(
"Failed to send DISCOVERY_RESPONSE to {}:{}",
source_ip, DEFAULT_MAVLINK_PORT
source_ip, source_port
))?;
debug!(
"Sent {} to {}:{}",
response.to_pretty_string(),
source_ip.bold().yellow(),
DEFAULT_MAVLINK_PORT.yellow()
source_port.yellow()
);
Ok(())
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment