Skip to content

Commit

Permalink
add syscall getaddrbyname to determine the ip address by name
Browse files Browse the repository at this point in the history
  • Loading branch information
stlankes committed May 20, 2024
1 parent b218a40 commit 538c7a0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 48 deletions.
80 changes: 32 additions & 48 deletions src/executor/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,31 +114,35 @@ async fn network_run() {
.await
}

/*#[cfg(feature = "dns")]
#[allow(dead_code)]
async fn dns_test() {
#[cfg(feature = "dns")]
pub(crate) async fn get_query_result(query: QueryHandle) -> Result<Vec<IpAddress>, IoError> {
use core::future::Future;
info!("Start DNS test");

let query = {
future::poll_fn(|cx| {
let mut guard = NIC.lock();
let nic = guard.as_nic_mut().unwrap();
let query = nic.start_query("rust-lang.org", DnsQueryType::A).unwrap();
nic.poll_common(now());
query
};
let socket = nic.get_mut_dns_socket()?;
match socket.get_query_result(query) {
Ok(addrs) => {
let mut ips = Vec::new();
for x in &addrs {
ips.push(*x);
}

let result = future::poll_fn(|cx| {
let mut guard = NIC.lock();
let nic = guard.as_nic_mut().unwrap();
let future = core::pin::pin!(nic.get_query_result(query));
future.poll(cx)
Poll::Ready(Ok(ips))
}
Err(GetQueryResultError::Pending) => {
socket.register_query_waker(query, cx.waker());
Poll::Pending
}
Err(e) => {
warn!("DNS query failed: {e:?}");
Poll::Ready(Err(IoError::ENOENT))
}
}
})
.await;
info!("result {:?}", result);
}*/
.await
}

pub(crate) fn init() {
info!("Try to initialize network!");
Expand All @@ -159,8 +163,6 @@ pub(crate) fn init() {
crate::core_scheduler().add_network_timer(wakeup_time);

spawn(network_run());
//#[cfg(feature = "dns")]
//spawn(dns_test());
}
}

Expand Down Expand Up @@ -283,7 +285,6 @@ impl<'a> NetworkInterface<'a> {
}

#[cfg(feature = "dns")]
#[allow(dead_code)]
pub(crate) fn start_query(
&mut self,
name: &str,
Expand All @@ -296,34 +297,17 @@ impl<'a> NetworkInterface<'a> {
.map_err(|_| IoError::EIO)
}

#[cfg(feature = "dns")]
#[allow(dead_code)]
pub(crate) async fn get_query_result(
&mut self,
query: QueryHandle,
) -> Result<Vec<IpAddress>, IoError> {
future::poll_fn(|_cx| {
let dns_handle = self.dns_handle.ok_or(IoError::EINVAL)?;
self.poll_common(now());

let socket: &mut dns::Socket<'a> = self.sockets.get_mut(dns_handle);
match socket.get_query_result(query) {
Ok(addrs) => {
let mut ips = Vec::new();
for x in &addrs {
ips.push(*x);
}
#[cfg(feature = "dns")]
pub(crate) fn get_dns_socket(&self) -> Result<&dns::Socket<'a>, IoError> {
let dns_handle = self.dns_handle.ok_or(IoError::EINVAL)?;
Ok(self.sockets.get(dns_handle))
}

Poll::Ready(Ok(ips))
}
Err(GetQueryResultError::Pending) => Poll::Pending,
Err(e) => {
warn!("DNS query failed: {e:?}");
Poll::Ready(Err(IoError::ENOENT))
}
}
})
.await
#[cfg(feature = "dns")]
pub(crate) fn get_mut_dns_socket(&mut self) -> Result<&mut dns::Socket<'a>, IoError> {
let dns_handle = self.dns_handle.ok_or(IoError::EINVAL)?;
Ok(self.sockets.get_mut(dns_handle))
}
}

Expand Down
49 changes: 49 additions & 0 deletions src/syscalls/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,55 @@ pub struct linger {
pub l_linger: i32,
}

#[cfg(feature = "dns")]
#[hermit_macro::system]
pub extern "C" fn sys_getaddrbyname(name: *const c_char, inaddr: *mut u8, len: usize) -> i32 {
use alloc::borrow::ToOwned;

use smoltcp::wire::DnsQueryType;

use crate::executor::block_on;
use crate::executor::network::get_query_result;

if len != size_of::<in_addr>().try_into().unwrap()
&& len != size_of::<in6_addr>().try_into().unwrap()
{
return -EINVAL;
}

if inaddr.is_null() {
return -EINVAL;
}

let query_type = if len == size_of::<in6_addr>().try_into().unwrap() {
DnsQueryType::Aaaa
} else {
DnsQueryType::A
};

let name = unsafe { core::ffi::CStr::from_ptr(name) };
let name = name.to_str().expect("Bad encoding").to_owned();

let query = {
let mut guard = NIC.lock();
let nic = guard.as_nic_mut().unwrap();
let query = nic.start_query(&name, query_type).unwrap();
nic.poll_common(crate::executor::network::now());

query
};

match block_on(get_query_result(query), None) {
Ok(addr_vec) => {
let slice = unsafe { core::slice::from_raw_parts_mut(inaddr, len) };
slice.copy_from_slice(addr_vec[0].as_bytes());

0
}
Err(e) => -num::ToPrimitive::to_i32(&e).unwrap(),
}
}

#[hermit_macro::system]
pub extern "C" fn sys_socket(domain: i32, type_: SockType, protocol: i32) -> i32 {
debug!(
Expand Down

0 comments on commit 538c7a0

Please sign in to comment.