我正在使用nftnl-rs构建一个 nftables 规则表,并取得了巨大的成功。但是现在我想知道系统中是否已经存在同名的表。我在 nftnl-rs 中发现了两个nftnl/src/table.rs
似乎正是为此而设计的函数:get_tables_nlmsg()和get_tables_cb()。但是我无法成功使用它们。get_tables_cb()
的文档字符串说它是用来处理前者的输出,但get_tables_nlmsg()
返回 a Vec<u8>
,显然包含创建的 nlmsghdr 的内存地址,而get_tables_cb()
将 a&nlmsghdr
作为参数。
具体来说,我试过:
use std::collections::HashSet;
use std::ffi::CString;
use nftnl{self, nftnl_sys::libc};
fn dump_tables() -> Result<(), Box<dyn std::error::Error> {
let mut tables: &mut HashSet<CString> = &mut HashSet::new();
let mut buffer = nftnl::table::get_tables_nlmsg(0);
let buffer = buffer.as_ptr() as *const libc::nlmsghdr;
nftnl::table::get_tables_cb(&&buffer, &mut tables);
println!("{:?}", tables);
Ok(())
}
rustc 抱怨它期望 an&nlmsghdr
并得到 a &&*const nlmsghdr
,而我无法将前者转换为后者。我浏览了源代码和示例,没有看到这个铸造案例出现。在源代码中来回跳转,我发现 Nomicon 的一部分谈论似乎适用于的不透明结构nlmsghdr
,但也不能解决我的问题。事实上,我对函数声明有点困惑:
pub fn get_tables_cb(header: &libc::nlmsghdr, tables: &mut HashSet<CString>) -> libc::c_int
如果*const libc::nlmsghdr
是外来的 Struct,到底能&libc::nlmsghdr
是什么?会不会是 nftnl-rs 的源代码中有错字?