@@ -10,21 +10,21 @@ use nix::{ioctl_readwrite, sys::socket::AddressFamily};
10
10
11
11
use super :: { create_socket, IoError } ;
12
12
13
- // FIXME: `WgDataIo` has to be declared as public
14
- ioctl_readwrite ! ( write_wireguard_data, b'i' , 210 , WgDataIo ) ;
15
- ioctl_readwrite ! ( read_wireguard_data, b'i' , 211 , WgDataIo ) ;
13
+ // FIXME: `WgReadIo` and `WgWriteIo` have to be declared public.
14
+ ioctl_readwrite ! ( write_wireguard_data, b'i' , 210 , WgWriteIo ) ;
15
+ ioctl_readwrite ! ( read_wireguard_data, b'i' , 211 , WgReadIo ) ;
16
16
17
17
/// Represent `struct wg_data_io` defined in
18
18
/// https://github.com/freebsd/freebsd-src/blob/main/sys/dev/wg/if_wg.h
19
19
#[ repr( C ) ]
20
- pub struct WgDataIo {
21
- pub ( super ) wgd_name : [ u8 ; IF_NAMESIZE ] ,
22
- pub ( super ) wgd_data : * mut u8 , // *void
23
- pub ( super ) wgd_size : usize ,
20
+ pub struct WgReadIo {
21
+ wgd_name : [ u8 ; IF_NAMESIZE ] ,
22
+ wgd_data : * mut u8 , // *void
23
+ wgd_size : usize ,
24
24
}
25
25
26
- impl WgDataIo {
27
- /// Create `WgDataIo ` without data buffer.
26
+ impl WgReadIo {
27
+ /// Create `WgReadIo ` without data buffer.
28
28
#[ must_use]
29
29
pub fn new ( if_name : & str ) -> Self {
30
30
let mut wgd_name = [ 0u8 ; IF_NAMESIZE ] ;
@@ -63,41 +63,67 @@ impl WgDataIo {
63
63
unsafe {
64
64
// First do ioctl with empty `wg_data` to obtain buffer size.
65
65
if let Err ( err) = read_wireguard_data ( socket. as_raw_fd ( ) , self ) {
66
- error ! ( "WgDataIo first read error {err}" ) ;
66
+ error ! ( "WgReadIo first read error {err}" ) ;
67
67
return Err ( IoError :: ReadIo ( err) ) ;
68
68
}
69
69
// Allocate buffer.
70
70
self . alloc_data ( ) ?;
71
71
// Second call to ioctl with allocated buffer.
72
72
if let Err ( err) = read_wireguard_data ( socket. as_raw_fd ( ) , self ) {
73
- error ! ( "WgDataIo second read error {err}" ) ;
73
+ error ! ( "WgReadIo second read error {err}" ) ;
74
74
return Err ( IoError :: ReadIo ( err) ) ;
75
75
}
76
76
}
77
77
78
78
Ok ( ( ) )
79
79
}
80
+ }
81
+
82
+ impl Drop for WgReadIo {
83
+ fn drop ( & mut self ) {
84
+ if self . wgd_size != 0 {
85
+ let layout = Layout :: array :: < u8 > ( self . wgd_size ) . expect ( "Bad layout" ) ;
86
+ unsafe {
87
+ dealloc ( self . wgd_data , layout) ;
88
+ }
89
+ }
90
+ }
91
+ }
92
+
93
+ /// Same data layout as `WgReadIo`, but avoid `Drop`.
94
+ #[ repr( C ) ]
95
+ pub struct WgWriteIo {
96
+ wgd_name : [ u8 ; IF_NAMESIZE ] ,
97
+ wgd_data : * mut u8 , // *void
98
+ wgd_size : usize ,
99
+ }
100
+
101
+ impl WgWriteIo {
102
+ /// Create `WgWriteIo` from slice.
103
+ #[ must_use]
104
+ pub fn new ( if_name : & str , buf : & mut [ u8 ] ) -> Self {
105
+ let mut wgd_name = [ 0u8 ; IF_NAMESIZE ] ;
106
+ if_name
107
+ . bytes ( )
108
+ . take ( IF_NAMESIZE - 1 )
109
+ . enumerate ( )
110
+ . for_each ( |( i, b) | wgd_name[ i] = b) ;
111
+ Self {
112
+ wgd_name,
113
+ wgd_data : buf. as_mut_ptr ( ) ,
114
+ wgd_size : buf. len ( ) ,
115
+ }
116
+ }
80
117
81
118
pub ( super ) fn write_data ( & mut self ) -> Result < ( ) , IoError > {
82
119
let socket = create_socket ( AddressFamily :: Unix ) . map_err ( IoError :: WriteIo ) ?;
83
120
unsafe {
84
121
if let Err ( err) = write_wireguard_data ( socket. as_raw_fd ( ) , self ) {
85
- error ! ( "WgDataIo write error {err}" ) ;
122
+ error ! ( "WgWriteIo write error {err}" ) ;
86
123
return Err ( IoError :: WriteIo ( err) ) ;
87
124
}
88
125
}
89
126
90
127
Ok ( ( ) )
91
128
}
92
129
}
93
-
94
- impl Drop for WgDataIo {
95
- fn drop ( & mut self ) {
96
- if self . wgd_size != 0 {
97
- let layout = Layout :: array :: < u8 > ( self . wgd_size ) . expect ( "Bad layout" ) ;
98
- unsafe {
99
- dealloc ( self . wgd_data , layout) ;
100
- }
101
- }
102
- }
103
- }
0 commit comments