浏览代码

webui: generator C API with bindgen

Nick Peng 1 年之前
父节点
当前提交
a10e2f737f

+ 4 - 1
plugin/smartdns-ui/Cargo.toml

@@ -34,4 +34,7 @@ build-release = []
 reqwest = {version = "0.12.5", features = ["blocking"]}
 tungstenite = "0.23.0"
 tokio-tungstenite = "0.23.1"
-tempfile = "3.10.0"
+tempfile = "3.10.0"
+
+[build-dependencies]
+bindgen = "0.69.4"

+ 35 - 2
plugin/smartdns-ui/build.rs

@@ -1,11 +1,45 @@
-
 use std::env;
+use std::path::PathBuf;
+use std::collections::HashSet;
+
+#[derive(Debug)]
+struct IgnoreMacros(HashSet<String>);
+
+impl bindgen::callbacks::ParseCallbacks for IgnoreMacros {
+    fn will_parse_macro(&self, name: &str) -> bindgen::callbacks::MacroParsingBehavior {
+        if self.0.contains(name) {
+            bindgen::callbacks::MacroParsingBehavior::Ignore
+        } else {
+            bindgen::callbacks::MacroParsingBehavior::Default
+        }
+    }
+}
 
 fn link_smartdns_lib() {
     let curr_source_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
     let smartdns_src_dir = format!("{}/../../src", curr_source_dir);
     let smartdns_lib_file = format!("{}/libsmartdns-test.a", smartdns_src_dir);
 
+    let ignored_macros = IgnoreMacros(
+        vec![
+            "IPPORT_RESERVED".into(),
+        ]
+        .into_iter()
+        .collect(),
+    );
+
+    let bindings = bindgen::Builder::default()
+        .header(format!("{}/smartdns.h", smartdns_src_dir))
+        .clang_arg(format!("-I{}/include", smartdns_src_dir))
+        .parse_callbacks(Box::new(ignored_macros))
+        .blocklist_file("/usr/include/.*")
+        .generate()
+        .expect("Unable to generate bindings");
+
+    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
+    bindings
+        .write_to_file(out_path.join("smartdns_bindings.rs"))
+        .expect("Couldn't write bindings!");
     /*
     to run tests, please run the following command:
     make test-prepare
@@ -16,7 +50,6 @@ fn link_smartdns_lib() {
         println!("cargo:rustc-link-lib=ssl");
         println!("cargo:rustc-link-lib=crypto");
         println!("cargo:rustc-link-search=native={}", smartdns_src_dir);
-        println!("cargo:warning=link smartdns-test library");
     }
 }
 

+ 1 - 2
plugin/smartdns-ui/src/data_server.rs

@@ -36,8 +36,7 @@ impl DataServerConfig {
     pub fn new() -> Self {
         DataServerConfig {
             data_root: Plugin::dns_conf_data_dir() + "/ui.db",
-            // max_log_age_ms: 7 * 24 * 60 * 60 * 1000,
-            max_log_age_ms: 60 * 60 * 1000,
+            max_log_age_ms: 7 * 24 * 60 * 60 * 1000,
         }
     }
 }

+ 66 - 138
plugin/smartdns-ui/src/smartdns.rs

@@ -16,11 +16,58 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#![allow(non_upper_case_globals)]
+#![allow(non_camel_case_types)]
+#![allow(non_snake_case)]
+#![allow(dead_code)]
+#![allow(unused_imports)]
+mod smartdns_c {
+    use libc::gid_t;
+    use libc::in6_addr;
+    use libc::in_addr;
+    use libc::sockaddr;
+    use libc::sockaddr_storage;
+    use libc::socklen_t;
+    use libc::time_t;
+    use libc::timeval;
+    use libc::tm;
+    use libc::uid_t;
+    use u32 as u_int;
+    include!(concat!(env!("OUT_DIR"), "/smartdns_bindings.rs"));
+
+}
+
 extern crate libc;
 use std::error::Error;
 use std::ffi::CString;
 
-pub use smartdns_c::LogLevel;
+#[repr(C)]
+#[derive(Copy, Clone, Debug, PartialEq)]
+#[allow(dead_code)]
+pub enum LogLevel {
+    DEBUG = 0,
+    INFO = 1,
+    NOTICE = 2,
+    WARN = 3,
+    ERROR = 4,
+    FATAL = 5,
+}
+
+impl TryFrom<u32> for LogLevel {
+    type Error = ();
+
+    fn try_from(value: u32) -> Result<Self, Self::Error> {
+        match value {
+            0 => Ok(LogLevel::DEBUG),
+            1 => Ok(LogLevel::INFO),
+            2 => Ok(LogLevel::NOTICE),
+            3 => Ok(LogLevel::WARN),
+            4 => Ok(LogLevel::ERROR),
+            5 => Ok(LogLevel::FATAL),
+            _ => Err(()),
+        }
+    }
+}
 
 #[macro_export]
 macro_rules! dns_log {
@@ -31,17 +78,20 @@ macro_rules! dns_log {
     };
 }
 pub fn dns_can_log(level: LogLevel) -> bool {
-    unsafe { smartdns_c::smartdns_plugin_can_log(level) != 0 }
+    unsafe { smartdns_c::smartdns_plugin_can_log(level as u32) != 0 }
 }
 
 pub fn dns_log_set_level(level: LogLevel) {
     unsafe {
-        smartdns_c::smartdns_plugin_log_setlevel(level);
+        smartdns_c::smartdns_plugin_log_setlevel(level as u32);
     }
 }
 
 pub fn dns_log_get_level() -> LogLevel {
-    unsafe { smartdns_c::smartdns_plugin_log_getlevel() }
+    unsafe {
+        let leve = smartdns_c::smartdns_plugin_log_getlevel();
+        LogLevel::try_from(leve as u32).unwrap()
+    }
 }
 
 pub fn dns_log_out(level: LogLevel, file: &str, line: u32, message: &str) {
@@ -54,133 +104,15 @@ pub fn dns_log_out(level: LogLevel, file: &str, line: u32, message: &str) {
 
     unsafe {
         smartdns_c::smartdns_plugin_log(
-            level,
+            level as u32,
             file_cstring.as_ptr(),
-            line,
+            line as i32,
             std::ptr::null(),
             message_cstring.as_ptr(),
         );
     }
 }
 
-mod smartdns_c {
-
-    #[repr(C)]
-    #[derive(Copy, Clone, Debug, PartialEq)]
-    #[allow(dead_code)]
-    pub enum LogLevel {
-        DEBUG = 0,
-        INFO = 1,
-        NOTICE = 2,
-        WARN = 3,
-        ERROR = 4,
-        FATAL = 5,
-    }
-
-    #[repr(C)]
-    pub struct _SmartdnsOperations {
-        pub server_recv: Option<
-            extern "C" fn(
-                packet: *mut _DnsPacket,
-                inpacket: *mut u8,
-                inpacket_len: libc::c_int,
-                local: *mut libc::sockaddr_storage,
-                local_len: libc::socklen_t,
-                from: *mut libc::sockaddr_storage,
-                from_len: libc::socklen_t,
-            ) -> libc::c_int,
-        >,
-        pub server_query_complete: Option<extern "C" fn(request: *mut _DnsRequest)>,
-    }
-
-    #[repr(C)]
-    pub struct _DnsPlugin {
-        _dummy: [u8; 0],
-    }
-
-    #[repr(C)]
-    pub struct _DnsRequest {
-        _dummy: [u8; 0],
-    }
-
-    #[repr(C)]
-    pub struct _DnsPacket {
-        _dummy: [u8; 0],
-    }
-
-    extern "C" {
-        pub fn dns_plugin_get_argc(plugin: *mut _DnsPlugin) -> i32;
-        pub fn dns_plugin_get_argv(plugin: *mut _DnsPlugin) -> *const *const libc::c_char;
-        pub fn dns_server_request_get_group_name(request: *mut _DnsRequest) -> *const libc::c_char;
-        pub fn dns_server_request_get_domain(request: *mut _DnsRequest) -> *const libc::c_char;
-        pub fn dns_server_request_get_qtype(request: *mut _DnsRequest) -> i32;
-        pub fn dns_server_request_get_qclass(request: *mut _DnsRequest) -> i32;
-        pub fn dns_server_request_get_id(request: *mut _DnsRequest) -> u16;
-        pub fn dns_server_request_get_rcode(request: *mut _DnsRequest) -> i32;
-        pub fn dns_server_request_get_query_time(request: *mut _DnsRequest) -> u64;
-        pub fn dns_server_request_get_remote_addr(
-            request: *mut _DnsRequest,
-        ) -> *const libc::sockaddr_storage;
-
-        pub fn dns_server_request_get_local_addr(
-            request: *mut _DnsRequest,
-        ) -> *const libc::sockaddr_storage;
-
-        pub fn get_host_by_addr(
-            host: *mut libc::c_char,
-            maxsize: i32,
-            addr: *const libc::sockaddr_storage,
-        ) -> *const libc::c_char;
-
-        pub fn dns_server_request_get(request: *mut _DnsRequest);
-
-        pub fn dns_server_request_put(request: *mut _DnsRequest);
-
-        pub fn smartdns_operations_register(operations: *const _SmartdnsOperations) -> i32;
-        pub fn smartdns_operations_unregister(operations: *const _SmartdnsOperations) -> i32;
-
-        pub fn smartdns_exit(status: i32);
-
-        pub fn smartdns_restart();
-
-        pub fn smartdns_get_cert(key: *mut libc::c_char, cert: *mut libc::c_char) -> i32;
-
-        pub fn dns_cache_flush();
-
-        pub fn dns_cache_total_num() -> i32;
-
-        pub fn smartdns_plugin_log(
-            level: LogLevel,
-            file: *const libc::c_char,
-            line: u32,
-            func: *const libc::c_char,
-            msg: *const libc::c_char,
-        );
-
-        pub fn smartdns_plugin_log_setlevel(level: LogLevel);
-
-        pub fn smartdns_plugin_log_getlevel() -> LogLevel;
-
-        pub fn smartdns_plugin_can_log(level: LogLevel) -> i32;
-
-        pub fn dns_conf_get_cache_dir() -> *const libc::c_char;
-
-        pub fn dns_conf_get_data_dir() -> *const libc::c_char;
-
-        pub fn smartdns_plugin_get_config(key: *const libc::c_char) -> *const libc::c_char;
-
-        pub fn smartdns_plugin_clear_all_config();
-
-        pub fn smartdns_server_run(file: *const libc::c_char) -> i32;
-
-        pub fn smartdns_server_stop();
-
-        pub fn get_utc_time_ms() -> u64;
-
-        pub fn smartdns_version() -> *const libc::c_char;
-    }
-}
-
 pub fn smartdns_version() -> String {
     unsafe {
         let version = smartdns_c::smartdns_version();
@@ -218,13 +150,13 @@ pub fn get_utc_time_ms() -> u64 {
     unsafe { smartdns_c::get_utc_time_ms() }
 }
 
-static SMARTDNS_OPS: smartdns_c::_SmartdnsOperations = smartdns_c::_SmartdnsOperations {
+static SMARTDNS_OPS: smartdns_c::smartdns_operations = smartdns_c::smartdns_operations {
     server_recv: None,
     server_query_complete: Some(dns_request_complete),
 };
 
 #[no_mangle]
-extern "C" fn dns_request_complete(request: *mut smartdns_c::_DnsRequest) {
+extern "C" fn dns_request_complete(request: *mut smartdns_c::dns_request) {
     unsafe {
         let ops = PLUGIN.ops.as_ref();
         if let None = ops {
@@ -238,7 +170,7 @@ extern "C" fn dns_request_complete(request: *mut smartdns_c::_DnsRequest) {
 }
 
 #[no_mangle]
-extern "C" fn dns_plugin_init(plugin: *mut smartdns_c::_DnsPlugin) -> i32 {
+extern "C" fn dns_plugin_init(plugin: *mut smartdns_c::dns_plugin) -> i32 {
     unsafe {
         PLUGIN.parser_args(plugin).unwrap();
         smartdns_c::smartdns_operations_register(&SMARTDNS_OPS);
@@ -253,7 +185,7 @@ extern "C" fn dns_plugin_init(plugin: *mut smartdns_c::_DnsPlugin) -> i32 {
 }
 
 #[no_mangle]
-extern "C" fn dns_plugin_exit(_plugin: *mut smartdns_c::_DnsPlugin) -> i32 {
+extern "C" fn dns_plugin_exit(_plugin: *mut smartdns_c::dns_plugin) -> i32 {
     unsafe {
         smartdns_c::smartdns_operations_unregister(&SMARTDNS_OPS);
         PLUGIN.ops.as_mut().unwrap().server_exit();
@@ -262,12 +194,12 @@ extern "C" fn dns_plugin_exit(_plugin: *mut smartdns_c::_DnsPlugin) -> i32 {
 }
 
 pub struct DnsRequest {
-    request: *mut smartdns_c::_DnsRequest,
+    request: *mut smartdns_c::dns_request,
 }
 
 #[allow(dead_code)]
 impl DnsRequest {
-    fn new(request: *mut smartdns_c::_DnsRequest) -> DnsRequest {
+    fn new(request: *mut smartdns_c::dns_request) -> DnsRequest {
         unsafe {
             smartdns_c::dns_server_request_get(request);
         }
@@ -309,7 +241,7 @@ impl DnsRequest {
     }
 
     pub fn get_id(&self) -> u16 {
-        unsafe { smartdns_c::dns_server_request_get_id(self.request) }
+        unsafe { smartdns_c::dns_server_request_get_id(self.request) as u16 }
     }
 
     pub fn get_rcode(&self) -> u16 {
@@ -323,13 +255,11 @@ impl DnsRequest {
     pub fn get_remote_addr(&self) -> String {
         unsafe {
             let addr = smartdns_c::dns_server_request_get_remote_addr(self.request);
-            let addr =
-                std::mem::transmute::<*const libc::sockaddr_storage, *const libc::sockaddr>(addr);
             let mut buf = [0u8; 1024];
             let retstr = smartdns_c::get_host_by_addr(
                 buf.as_mut_ptr(),
                 buf.len() as i32,
-                addr as *const libc::sockaddr_storage,
+                addr as *const libc::sockaddr,
             );
             if retstr.is_null() {
                 return String::new();
@@ -345,13 +275,11 @@ impl DnsRequest {
     pub fn get_local_addr(&self) -> String {
         unsafe {
             let addr = smartdns_c::dns_server_request_get_local_addr(self.request);
-            let addr =
-                std::mem::transmute::<*const libc::sockaddr_storage, *const libc::sockaddr>(addr);
             let mut buf = [0u8; 1024];
             let retstr = smartdns_c::get_host_by_addr(
                 buf.as_mut_ptr(),
                 buf.len() as i32,
-                addr as *const libc::sockaddr_storage,
+                addr as *const libc::sockaddr,
             );
             if retstr.is_null() {
                 return String::new();
@@ -507,7 +435,7 @@ impl Plugin {
         }
     }
 
-    fn parser_args(&mut self, plugin: *mut smartdns_c::_DnsPlugin) -> Result<(), String> {
+    fn parser_args(&mut self, plugin: *mut smartdns_c::dns_plugin) -> Result<(), String> {
         let argc = unsafe { smartdns_c::dns_plugin_get_argc(plugin) };
         let args: Vec<String> = unsafe {
             let argv = smartdns_c::dns_plugin_get_argv(plugin);

+ 1 - 1
src/dns_conf.c

@@ -461,7 +461,7 @@ static void _config_group_table_destroy(void)
 	}
 }
 
-struct dns_proxy_names *dns_server_get_proxy_nams(const char *proxyname)
+struct dns_proxy_names *dns_server_get_proxy_names(const char *proxyname)
 {
 	uint32_t key = 0;
 	struct dns_proxy_names *proxy = NULL;

+ 1 - 1
src/dns_conf.h

@@ -762,7 +762,7 @@ int dns_server_load_conf(const char *file);
 
 int dns_server_check_update_hosts(void);
 
-struct dns_proxy_names *dns_server_get_proxy_nams(const char *proxyname);
+struct dns_proxy_names *dns_server_get_proxy_names(const char *proxyname);
 
 struct dns_srv_records *dns_server_get_srv_record(const char *domain);
 

+ 2 - 2
src/dns_plugin.c

@@ -94,7 +94,7 @@ void smartdns_plugin_func_server_complete_request(struct dns_request *request)
 	return;
 }
 
-int smartdns_operations_register(struct smartdns_operations *operations)
+int smartdns_operations_register(const struct smartdns_operations *operations)
 {
 	struct dns_plugin_ops *chain = NULL;
 
@@ -109,7 +109,7 @@ int smartdns_operations_register(struct smartdns_operations *operations)
 	return 0;
 }
 
-int smartdns_operations_unregister(struct smartdns_operations *operations)
+int smartdns_operations_unregister(const struct smartdns_operations *operations)
 {
 	struct dns_plugin_ops *chain = NULL;
 	struct dns_plugin_ops *tmp = NULL;

+ 2 - 2
src/dns_plugin.h

@@ -88,9 +88,9 @@ struct smartdns_operations {
 	void (*server_query_complete)(struct dns_request *request);
 };
 
-int smartdns_operations_register(struct smartdns_operations *operations);
+int smartdns_operations_register(const struct smartdns_operations *operations);
 
-int smartdns_operations_unregister(struct smartdns_operations *operations);
+int smartdns_operations_unregister(const struct smartdns_operations *operations);
 
 #ifdef __cplusplus
 }

+ 2 - 2
src/dns_server.c

@@ -2940,12 +2940,12 @@ static void _dns_server_request_get(struct dns_request *request)
 	}
 }
 
-struct sockaddr *dns_server_request_get_remote_addr(struct dns_request *request)
+const struct sockaddr *dns_server_request_get_remote_addr(struct dns_request *request)
 {
 	return &request->addr;
 }
 
-struct sockaddr *dns_server_request_get_local_addr(struct dns_request *request)
+const struct sockaddr *dns_server_request_get_local_addr(struct dns_request *request)
 {
 	return (struct sockaddr *)&request->localaddr;
 }

+ 2 - 2
src/dns_server.h

@@ -71,9 +71,9 @@ int dns_server_query(const char *domain, int qtype, struct dns_server_query_opti
 
 struct dns_request;
 
-struct sockaddr *dns_server_request_get_remote_addr(struct dns_request *request);
+const struct sockaddr *dns_server_request_get_remote_addr(struct dns_request *request);
 
-struct sockaddr *dns_server_request_get_local_addr(struct dns_request *request);
+const struct sockaddr *dns_server_request_get_local_addr(struct dns_request *request);
 
 const char *dns_server_request_get_group_name(struct dns_request *request);
 

+ 3 - 7
src/smartdns.c

@@ -17,21 +17,17 @@
  */
 
 #define _GNU_SOURCE
+
 #include "smartdns.h"
+
 #include "art.h"
 #include "atomic.h"
-#include "dns_cache.h"
-#include "dns_client.h"
-#include "dns_conf.h"
-#include "dns_plugin.h"
-#include "dns_server.h"
-#include "fast_ping.h"
 #include "hashtable.h"
 #include "list.h"
 #include "rbtree.h"
 #include "timer.h"
 #include "tlog.h"
-#include "util.h"
+
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>

+ 9 - 0
src/smartdns.h

@@ -19,6 +19,15 @@
 #ifndef SMART_DNS_H
 #define SMART_DNS_H
 
+
+#include "dns_cache.h"
+#include "dns_client.h"
+#include "dns_conf.h"
+#include "dns_plugin.h"
+#include "dns_server.h"
+#include "fast_ping.h"
+#include "util.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif /*__cplusplus */

+ 2 - 2
src/util.c

@@ -280,7 +280,7 @@ int create_dir_with_perm(const char *dir_path)
 	return 0;
 }
 
-char *get_host_by_addr(char *host, int maxsize, struct sockaddr *addr)
+char *get_host_by_addr(char *host, int maxsize, const struct sockaddr *addr)
 {
 	struct sockaddr_storage *addr_store = (struct sockaddr_storage *)addr;
 	host[0] = 0;
@@ -388,7 +388,7 @@ int is_private_addr(const unsigned char *addr, int addr_len)
 	return 0;
 }
 
-int is_private_addr_sockaddr(struct sockaddr *addr, socklen_t addr_len)
+int is_private_addr_sockaddr(const struct sockaddr *addr, socklen_t addr_len)
 {
 	switch (addr->sa_family) {
 	case AF_INET: {

+ 2 - 2
src/util.h

@@ -65,7 +65,7 @@ int drop_root_privilege(void);
 
 int create_dir_with_perm(const char *dir_path);
 
-char *get_host_by_addr(char *host, int maxsize, struct sockaddr *addr);
+char *get_host_by_addr(char *host, int maxsize, const struct sockaddr *addr);
 
 int generate_random_addr(unsigned char *addr, int addr_len, int mask);
 
@@ -74,7 +74,7 @@ int generate_addr_map(const unsigned char *addr_from, const unsigned char *addr_
 
 int is_private_addr(const unsigned char *addr, int addr_len);
 
-int is_private_addr_sockaddr(struct sockaddr *addr, socklen_t addr_len);
+int is_private_addr_sockaddr(const struct sockaddr *addr, socklen_t addr_len);
 
 int getaddr_by_host(const char *host, struct sockaddr *addr, socklen_t *addr_len);