@@ -18,13 +18,15 @@ pub struct EncryptedDnsProxyState {
18
18
19
19
#[ derive( Debug ) ]
20
20
pub enum Error {
21
- /// Failed to bind a local listening socket, the one that will be forwarded through the proxy
21
+ /// Failed to initialize tokio runtime.
22
+ TokioRuntime ,
23
+ /// Failed to bind a local listening socket, the one that will be forwarded through the proxy.
22
24
BindLocalSocket ( io:: Error ) ,
23
25
/// Failed to get local listening address of the local listening socket.
24
26
GetBindAddr ( io:: Error ) ,
25
27
/// Failed to initialize forwarder.
26
28
Forwarder ( io:: Error ) ,
27
- /// Failed to fetch new .
29
+ /// Failed to fetch a proxy configuration over DNS .
28
30
FetchConfig ( config_resolver:: Error ) ,
29
31
/// Failed to initialize with a valid configuration.
30
32
NoConfigs ,
@@ -33,6 +35,7 @@ pub enum Error {
33
35
impl From < Error > for i32 {
34
36
fn from ( err : Error ) -> Self {
35
37
match err {
38
+ Error :: TokioRuntime ( _) => -1 ,
36
39
Error :: BindLocalSocket ( _) => -2 ,
37
40
Error :: GetBindAddr ( _) => -3 ,
38
41
Error :: Forwarder ( _) => -4 ,
@@ -59,12 +62,7 @@ impl EncryptedDnsProxyState {
59
62
}
60
63
61
64
async fn start ( & mut self ) -> Result < ProxyHandle , Error > {
62
- // TODO: Consider strong timeout here
63
65
self . fetch_configs ( ) . await ?;
64
- if self . should_reset ( ) {
65
- self . reset ( ) ;
66
- }
67
-
68
66
let proxy_configuration = self . next_configuration ( ) . ok_or ( Error :: NoConfigs ) ?;
69
67
70
68
let local_socket = Self :: bind_local_addr ( )
@@ -92,6 +90,10 @@ impl EncryptedDnsProxyState {
92
90
}
93
91
94
92
fn next_configuration ( & mut self ) -> Option < ProxyConfig > {
93
+ if self . should_reset ( ) {
94
+ self . reset ( ) ;
95
+ }
96
+
95
97
if let Some ( xor_config) = self
96
98
. configurations
97
99
. difference ( & self . tried_configurations )
@@ -156,7 +158,6 @@ pub unsafe extern "C" fn encrytped_dns_proxy_free(ptr: *mut EncryptedDnsProxySta
156
158
///
157
159
/// `proxy_handle` will only contain valid values if the return value is zero. It is still valid to
158
160
/// deallocate the memory.
159
- ///
160
161
#[ no_mangle]
161
162
pub unsafe extern "C" fn encrypted_dns_proxy_start (
162
163
encrypted_dns_proxy : * mut EncryptedDnsProxyState ,
@@ -166,7 +167,7 @@ pub unsafe extern "C" fn encrypted_dns_proxy_start(
166
167
Ok ( handle) => handle,
167
168
Err ( err) => {
168
169
log:: error!( "Cannot instantiate a tokio runtime: {}" , err) ;
169
- return - 1 ;
170
+ return Error :: TokioRuntime . into ( ) ;
170
171
}
171
172
} ;
172
173
@@ -177,6 +178,11 @@ pub unsafe extern "C" fn encrypted_dns_proxy_start(
177
178
match proxy_result {
178
179
Ok ( handle) => unsafe { ptr:: write ( proxy_handle, handle) } ,
179
180
Err ( err) => {
181
+ let empty_handle = ProxyHandle {
182
+ context : ptr:: null ( ) ,
183
+ port : 0 ,
184
+ } ;
185
+ unsafe { ptr:: write ( proxy_handle, empty_handle) }
180
186
log:: error!( "Failed to create a proxy connection: {err:?}" ) ;
181
187
return err. into ( ) ;
182
188
}
@@ -185,12 +191,15 @@ pub unsafe extern "C" fn encrypted_dns_proxy_start(
185
191
0
186
192
}
187
193
188
- /// SAFETY:
194
+ /// # Safety
189
195
/// `proxy_config` must be a valid pointer to a `ProxyHandle` as initialized by
190
196
/// [`encrypted_dns_proxy_start`].
191
197
#[ no_mangle]
192
198
pub unsafe extern "C" fn encrypted_dns_proxy_stop ( proxy_config : * mut ProxyHandle ) -> i32 {
193
- let handle: Box < JoinHandle < ( ) > > = unsafe { Box :: from_raw ( ( * proxy_config) . context . cast ( ) ) } ;
194
- handle. abort ( ) ;
199
+ let ptr = unsafe { * proxy_config. context } ;
200
+ if !ptr. is_null ( ) {
201
+ let handle: Box < JoinHandle < ( ) > > = unsafe { Box :: from_raw ( ptr. cast ( ) ) } ;
202
+ handle. abort ( ) ;
203
+ }
195
204
0i32
196
205
}
0 commit comments