Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] Raw sockets not being closed when Tracer is dropped #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/trace/sock4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use libc::{IPPROTO_TCP, IPPROTO_UDP, c_int};
use log::{debug, error};
use raw_socket::tokio::prelude::*;
use tokio::sync::Mutex;
use tokio::task::JoinSet;
use crate::{Bind, RouteSocket};
use super::probe::{Key, Probe};
use super::reply::Echo;
Expand All @@ -18,6 +19,7 @@ pub struct Sock4 {
tcp: Mutex<Arc<RawSocket>>,
udp: Mutex<Arc<RawSocket>>,
route: Mutex<RouteSocket>,
_join_set: JoinSet<()>,
}

impl Sock4 {
Expand All @@ -37,9 +39,10 @@ impl Sock4 {
tcp.set_sockopt(Level::IPV4, Name::IPV4_HDRINCL, &enable)?;
udp.set_sockopt(Level::IPV4, Name::IPV4_HDRINCL, &enable)?;

let rx = tcp.clone();
let mut join_set = JoinSet::new();

tokio::spawn(async move {
let rx = tcp.clone();
join_set.spawn(async move {
match recv(rx, state).await {
Ok(()) => debug!("recv finished"),
Err(e) => error!("recv failed: {}", e),
Expand All @@ -51,6 +54,7 @@ impl Sock4 {
tcp: Mutex::new(tcp),
udp: Mutex::new(udp),
route: Mutex::new(route),
_join_set: join_set,
})
}

Expand Down
8 changes: 6 additions & 2 deletions src/trace/sock6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use libc::c_int;
use log::{debug, error};
use raw_socket::tokio::prelude::*;
use tokio::sync::Mutex;
use tokio::task::JoinSet;
use crate::{Bind, RouteSocket};
use super::probe::{Key, Probe};
use super::reply::Echo;
Expand All @@ -18,6 +19,7 @@ pub struct Sock6 {
tcp: Mutex<Arc<RawSocket>>,
udp: Mutex<Arc<RawSocket>>,
route: Mutex<RouteSocket>,
_join_set: JoinSet<()>,
}

impl Sock6 {
Expand All @@ -39,9 +41,10 @@ impl Sock6 {
udp.set_sockopt(Level::IPV6, Name::IPV6_CHECKSUM, &offset)?;
udp.bind(bind.sa6()).await?;

let rx = tcp.clone();
let mut join_set = JoinSet::new();

tokio::spawn(async move {
let rx = tcp.clone();
join_set.spawn(async move {
match recv(rx, state).await {
Ok(()) => debug!("recv finished"),
Err(e) => error!("recv failed: {}", e),
Expand All @@ -53,6 +56,7 @@ impl Sock6 {
tcp: Mutex::new(tcp),
udp: Mutex::new(udp),
route: Mutex::new(route),
_join_set: join_set,
})
}

Expand Down