Skip to content

Commit

Permalink
Merge pull request #1249 from microsoft/bugfix-catpowder-close-raw-so…
Browse files Browse the repository at this point in the history
…cket

[catpowder] Close Raw Socket
  • Loading branch information
ppenna committed Apr 27, 2024
2 parents ebe18f9 + 547bc5b commit ba498f1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/rust/catpowder/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::{
types::MacAddress,
},
Runtime,
SharedObject,
},
};
use ::std::{
Expand All @@ -49,7 +50,7 @@ pub struct LinuxRuntime {
link_addr: MacAddress,
ipv4_addr: Ipv4Addr,
ifindex: i32,
socket: RawSocket,
socket: SharedObject<RawSocket>,
}

//==============================================================================
Expand Down Expand Up @@ -85,7 +86,7 @@ impl LinuxRuntime {
link_addr: config.local_link_addr(),
ipv4_addr: config.local_ipv4_addr(),
ifindex,
socket,
socket: SharedObject::<RawSocket>::new(socket),
}
}

Expand Down
19 changes: 17 additions & 2 deletions src/rust/catpowder/runtime/rawsocket/rawsocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use ::std::{
//======================================================================================================================

/// Raw socket.
#[derive(Clone)]
pub struct RawSocket(libc::c_int);

//======================================================================================================================
Expand All @@ -44,7 +43,7 @@ impl RawSocket {
if sockfd == -1 {
return Err(Fail::new(libc::EAGAIN, "failed to create raw socket"));
}

trace!("Creating raw socket with fd={:?}", sockfd);
Ok(RawSocket(sockfd))
}

Expand Down Expand Up @@ -108,3 +107,19 @@ impl RawSocket {
Ok((nbytes as usize, rawaddr))
}
}

//======================================================================================================================
// Trait Implementations
//======================================================================================================================

/// Closes the raw socket.
impl Drop for RawSocket {
fn drop(&mut self) {
if unsafe { libc::close(self.0) } < 0 {
let errno: libc::c_int = unsafe { *libc::__errno_location() };
warn!("could not close raw socket (fd={:?}): {:?}", self.0, errno);
} else {
trace!("Closing raw socket fd={:?}", self.0)
}
}
}

0 comments on commit ba498f1

Please sign in to comment.