Skip to content

Commit ba498f1

Browse files
authored
Merge pull request #1249 from microsoft/bugfix-catpowder-close-raw-socket
[catpowder] Close Raw Socket
2 parents ebe18f9 + 547bc5b commit ba498f1

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/rust/catpowder/runtime/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::{
2626
types::MacAddress,
2727
},
2828
Runtime,
29+
SharedObject,
2930
},
3031
};
3132
use ::std::{
@@ -49,7 +50,7 @@ pub struct LinuxRuntime {
4950
link_addr: MacAddress,
5051
ipv4_addr: Ipv4Addr,
5152
ifindex: i32,
52-
socket: RawSocket,
53+
socket: SharedObject<RawSocket>,
5354
}
5455

5556
//==============================================================================
@@ -85,7 +86,7 @@ impl LinuxRuntime {
8586
link_addr: config.local_link_addr(),
8687
ipv4_addr: config.local_ipv4_addr(),
8788
ifindex,
88-
socket,
89+
socket: SharedObject::<RawSocket>::new(socket),
8990
}
9091
}
9192

src/rust/catpowder/runtime/rawsocket/rawsocket.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use ::std::{
2424
//======================================================================================================================
2525

2626
/// Raw socket.
27-
#[derive(Clone)]
2827
pub struct RawSocket(libc::c_int);
2928

3029
//======================================================================================================================
@@ -44,7 +43,7 @@ impl RawSocket {
4443
if sockfd == -1 {
4544
return Err(Fail::new(libc::EAGAIN, "failed to create raw socket"));
4645
}
47-
46+
trace!("Creating raw socket with fd={:?}", sockfd);
4847
Ok(RawSocket(sockfd))
4948
}
5049

@@ -108,3 +107,19 @@ impl RawSocket {
108107
Ok((nbytes as usize, rawaddr))
109108
}
110109
}
110+
111+
//======================================================================================================================
112+
// Trait Implementations
113+
//======================================================================================================================
114+
115+
/// Closes the raw socket.
116+
impl Drop for RawSocket {
117+
fn drop(&mut self) {
118+
if unsafe { libc::close(self.0) } < 0 {
119+
let errno: libc::c_int = unsafe { *libc::__errno_location() };
120+
warn!("could not close raw socket (fd={:?}): {:?}", self.0, errno);
121+
} else {
122+
trace!("Closing raw socket fd={:?}", self.0)
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)