1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2023 宋昊文
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

extern crate libc;

use std::{
    ffi::{c_void, CStr},
    net::SocketAddr,
    ptr::NonNull,
};

#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
use libc::c_char;

pub struct NetworkRequestCallbackWrapper {
    callback: Option<Box<dyn FnOnce(bool) + Send + Sync + 'static>>,
}

#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
extern "C" fn network_activation_callback(ptr: *mut c_void, activated: bool) {
    let data = ptr as *mut NetworkRequestCallbackWrapper;

    unsafe {
        if let Some(wrapper) = data.as_mut() {
            if let Some(callback) = wrapper.callback.take() {
                callback(activated);
            }
        }
    }
}

#[repr(C)]
pub struct NetworkRequestCHandle {
    _data: [u8; 0],
    _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}

pub struct NetworkRequestCHandleWrapper(NonNull<NetworkRequestCHandle>);

impl Drop for NetworkRequestCHandleWrapper {
    fn drop(&mut self) {
        #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
        let c_handle = self.0.as_ptr();
        #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
        unsafe {
            platform_drop_network_request(c_handle);
        }
    }
}

unsafe impl Send for NetworkRequestCHandleWrapper {}

#[repr(C)]
pub struct NetworkInfoCHandle {
    _data: [u8; 0],
    _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}

pub struct NetworkInfoCHandleWrapper(NonNull<NetworkInfoCHandle>);

impl Drop for NetworkInfoCHandleWrapper {
    fn drop(&mut self) {
        #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
        let c_handle = self.0.as_ptr();
        #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
        unsafe {
            platform_drop_network_info(c_handle);
        }
    }
}

unsafe impl Send for NetworkInfoCHandleWrapper {}

#[repr(C)]
pub struct DnsInfoCHandle {
    _data: [u8; 0],
    _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}

pub struct DnsInfoCHandleWrapper(NonNull<DnsInfoCHandle>);

impl Drop for DnsInfoCHandleWrapper {
    fn drop(&mut self) {
        #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
        let c_handle = self.0.as_ptr();
        #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
        unsafe {
            platform_drop_dns_info(c_handle);
        }
    }
}

unsafe impl Send for DnsInfoCHandleWrapper {}

#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
extern "C" {
    fn platform_activate_cellular_network(
        ptr: *mut c_void,
        callback: Option<extern "C" fn(*mut c_void, bool)>,
    ) -> *mut NetworkRequestCHandle;
    fn platform_drop_network_request(c_handle: *mut NetworkRequestCHandle);
    fn platform_get_active_network_info() -> *mut NetworkInfoCHandle;
    fn platform_get_network_type(network_info: *mut NetworkInfoCHandle) -> i32;
    fn platform_get_network_dns_info(network_info: *mut NetworkInfoCHandle) -> *mut DnsInfoCHandle;
    fn platform_get_dns_server(dns_info: *mut DnsInfoCHandle) -> *const c_char;
    fn platform_drop_dns_info(dns_info: *mut DnsInfoCHandle);
    fn platform_drop_network_info(network_info: *mut NetworkInfoCHandle);
}

pub fn activate_cellular_network<T>(callback: T) -> Option<NetworkRequestCHandleWrapper>
where
    T: 'static + FnOnce(bool) + Send + Sync,
{
    let wrapper = NetworkRequestCallbackWrapper {
        callback: Some(Box::new(callback)),
    };

    let data = Box::into_raw(Box::new(wrapper));
    let ptr = data as *mut c_void;

    #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
    unsafe {
        if let Some(c_handle) =
            platform_activate_cellular_network(ptr, Some(network_activation_callback)).as_mut()
        {
            return Some(NetworkRequestCHandleWrapper(
                NonNull::new(c_handle).unwrap(),
            ));
        }
    }

    None
}

pub fn get_active_network_info() -> Option<NetworkInfoCHandleWrapper> {
    #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
    unsafe {
        if let Some(network_info) = platform_get_active_network_info().as_mut() {
            return Some(NetworkInfoCHandleWrapper(
                NonNull::new(network_info).unwrap(),
            ));
        }
    }

    None
}

pub fn get_network_type(network_info: &NetworkInfoCHandleWrapper) -> i32 {
    let network_info_c_handle = network_info.0.as_ptr();
    #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
    unsafe {
        platform_get_network_type(network_info_c_handle)
    }
    #[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))]
    0
}

pub fn get_dns_info(network_info: &NetworkInfoCHandleWrapper) -> Option<DnsInfoCHandleWrapper> {
    let network_info_c_handle = network_info.0.as_ptr();

    #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
    unsafe {
        if let Some(dns_info) = platform_get_network_dns_info(network_info_c_handle).as_mut() {
            return Some(DnsInfoCHandleWrapper(NonNull::new(dns_info).unwrap()));
        }
    }

    None
}

pub fn get_dns_servers(dns_info: &DnsInfoCHandleWrapper) -> Vec<SocketAddr> {
    let dns_info_c_handle = dns_info.0.as_ptr();

    let mut v = Vec::new();

    #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
    unsafe {
        while let Some(ptr) = platform_get_dns_server(dns_info_c_handle).as_ref() {
            let str = CStr::from_ptr(ptr).to_string_lossy().into_owned();
            if let Ok(dns_server) = str.parse::<SocketAddr>() {
                v.push(dns_server);
            }
        }
    }

    v
}

pub fn get_active_dns_servers() -> Vec<SocketAddr> {
    let mut v = Vec::new();

    #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
    unsafe {
        if let Some(network_info) = platform_get_active_network_info().as_mut() {
            if let Some(dns_info) = platform_get_network_dns_info(network_info).as_mut() {
                while let Some(ptr) = platform_get_dns_server(dns_info).as_ref() {
                    let str = CStr::from_ptr(ptr).to_string_lossy().into_owned();
                    if let Ok(dns_server) = str.parse::<SocketAddr>() {
                        v.push(dns_server);
                    }
                }
                platform_drop_dns_info(dns_info);
            }
        }
    }

    v
}