File tree Expand file tree Collapse file tree 2 files changed +20
-4
lines changed
src/rust/catpowder/runtime Expand file tree Collapse file tree 2 files changed +20
-4
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ use crate::{
26
26
types:: MacAddress ,
27
27
} ,
28
28
Runtime ,
29
+ SharedObject ,
29
30
} ,
30
31
} ;
31
32
use :: std:: {
@@ -49,7 +50,7 @@ pub struct LinuxRuntime {
49
50
link_addr : MacAddress ,
50
51
ipv4_addr : Ipv4Addr ,
51
52
ifindex : i32 ,
52
- socket : RawSocket ,
53
+ socket : SharedObject < RawSocket > ,
53
54
}
54
55
55
56
//==============================================================================
@@ -85,7 +86,7 @@ impl LinuxRuntime {
85
86
link_addr : config. local_link_addr ( ) ,
86
87
ipv4_addr : config. local_ipv4_addr ( ) ,
87
88
ifindex,
88
- socket,
89
+ socket : SharedObject :: < RawSocket > :: new ( socket ) ,
89
90
}
90
91
}
91
92
Original file line number Diff line number Diff line change @@ -24,7 +24,6 @@ use ::std::{
24
24
//======================================================================================================================
25
25
26
26
/// Raw socket.
27
- #[ derive( Clone ) ]
28
27
pub struct RawSocket ( libc:: c_int ) ;
29
28
30
29
//======================================================================================================================
@@ -44,7 +43,7 @@ impl RawSocket {
44
43
if sockfd == -1 {
45
44
return Err ( Fail :: new ( libc:: EAGAIN , "failed to create raw socket" ) ) ;
46
45
}
47
-
46
+ trace ! ( "Creating raw socket with fd={:?}" , sockfd ) ;
48
47
Ok ( RawSocket ( sockfd) )
49
48
}
50
49
@@ -108,3 +107,19 @@ impl RawSocket {
108
107
Ok ( ( nbytes as usize , rawaddr) )
109
108
}
110
109
}
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
+ }
You can’t perform that action at this time.
0 commit comments