Skip to content

Commit

Permalink
[Lib] Fix memory allocation for DNS query
Browse files Browse the repository at this point in the history
Signed-off-by: KernelErr <[email protected]>
  • Loading branch information
KernelErr authored and hydai committed Feb 17, 2022
1 parent ee7ae9f commit 7358ee1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasmedge_wasi_socket"
version = "0.3.1"
version = "0.3.2"
authors = [ "Yi <[email protected]>" ]
edition = "2021"
license = "Apache-2.0"
Expand Down
64 changes: 46 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ pub struct WasiAddress {

unsafe impl Send for WasiAddress {}

#[derive(Debug)]
#[derive(Debug, Clone)]
#[repr(C)]
pub struct WasiSockaddr {
pub family: AddressFamily,
pub sa_data_len: u32,
pub sa_data: *mut libc::c_uchar,
pub sa_data: *mut u8,
}

impl WasiSockaddr {
Expand All @@ -73,6 +73,16 @@ impl WasiSockaddr {
}
}

impl Default for WasiSockaddr {
fn default() -> WasiSockaddr {
WasiSockaddr {
family: AddressFamily::Inet4,
sa_data_len: 14,
sa_data: std::ptr::null_mut(),
}
}
}

#[link(wasm_import_module = "wasi_snapshot_preview1")]
extern "C" {
pub fn sock_open(addr_family: u8, sock_type: u8, fd: *mut u32) -> u32;
Expand Down Expand Up @@ -154,7 +164,7 @@ pub fn unset_fdflag(fd: u32, fdflag: Fdflags) -> io::Result<()> {
}

#[derive(Copy, Clone, Debug)]
#[repr(u8)]
#[repr(u8, align(1))]
pub enum AddressFamily {
Inet4,
Inet6,
Expand All @@ -180,14 +190,14 @@ impl AddressFamily {
}

#[derive(Copy, Clone, Debug)]
#[repr(u8)]
#[repr(u8, align(1))]
pub enum SocketType {
Datagram,
Stream,
}

#[derive(Copy, Clone, Debug)]
#[repr(u16)]
#[repr(u16, align(2))]
pub enum AiFlags {
AiPassive,
AiCanonname,
Expand All @@ -199,14 +209,14 @@ pub enum AiFlags {
}

#[derive(Copy, Clone, Debug)]
#[repr(u8)]
#[repr(u8, align(1))]
pub enum AiProtocol {
IPProtoTCP,
IPProtoUDP,
}

#[derive(Debug, Clone)]
#[repr(C)]
#[repr(C, packed(4))]
pub struct WasiAddrinfo {
pub ai_flags: AiFlags,
pub ai_family: AddressFamily,
Expand Down Expand Up @@ -246,25 +256,43 @@ impl WasiAddrinfo {
sockbuff: &mut Vec<Vec<u8>>,
ai_canonname: &mut Vec<String>,
) -> io::Result<Vec<WasiAddrinfo>> {
let node = node.to_string();
let service = service.to_string();
let mut node = node.to_string();
let mut service = service.to_string();

if !node.ends_with('\0') {
node.push('\0');
}

if !service.ends_with('\0') {
service.push('\0');
}

let mut res_len: u32 = 0;
let mut wasiaddrinfo_array: Vec<WasiAddrinfo> = Vec::<WasiAddrinfo>::new();
for i in 0..max_reslen {
sockbuff.push(vec![0u8; 14]);
sockaddr.push(WasiSockaddr::new(AddressFamily::Inet4, &mut sockbuff[i]));
ai_canonname.push(String::with_capacity(30));
wasiaddrinfo_array.push(WasiAddrinfo {
sockbuff.resize(max_reslen, vec![0u8; 14]);
ai_canonname.resize(max_reslen, String::with_capacity(30));
sockaddr.resize(max_reslen, WasiSockaddr::default());
let mut wasiaddrinfo_array: Vec<WasiAddrinfo> = Vec::new();
wasiaddrinfo_array.resize(
max_reslen,
WasiAddrinfo {
ai_flags: AiFlags::AiPassive,
ai_family: AddressFamily::Inet6,
ai_socktype: SocketType::Datagram,
ai_protocol: AiProtocol::IPProtoTCP,
ai_addr: &mut sockaddr[i],
ai_addr: std::ptr::null_mut(),
ai_addrlen: 0,
ai_canonname: ai_canonname[i].as_mut_ptr(),
ai_canonname: std::ptr::null_mut(),
ai_canonnamelen: 30,
ai_next: std::ptr::null_mut(),
});
},
);
for i in 0..max_reslen {
sockaddr[i].sa_data = sockbuff[i].as_mut_ptr();
wasiaddrinfo_array[i].ai_addr = &mut sockaddr[i];
wasiaddrinfo_array[i].ai_canonname = ai_canonname[i].as_mut_ptr();
if i.gt(&0) {
wasiaddrinfo_array[i - 1].ai_next = &mut wasiaddrinfo_array[i];
}
}
let mut res = wasiaddrinfo_array.as_mut_ptr() as u32;
unsafe {
Expand Down

0 comments on commit 7358ee1

Please sign in to comment.