|
| 1 | +use crate::preview2::bindings::io::poll::Pollable; |
| 2 | +use crate::preview2::bindings::sockets::ip_name_lookup::{Host, HostResolveAddressStream}; |
| 3 | +use crate::preview2::bindings::sockets::network::{ |
| 4 | + Error, ErrorCode, IpAddress, IpAddressFamily, Network, |
| 5 | +}; |
| 6 | +use crate::preview2::network::TableNetworkExt; |
| 7 | +use crate::preview2::poll::{Subscribe, TablePollableExt}; |
| 8 | +use crate::preview2::{AbortOnDropJoinHandle, WasiView}; |
| 9 | +use anyhow::Result; |
| 10 | +use std::io; |
| 11 | +use std::mem; |
| 12 | +use std::net::{SocketAddr, ToSocketAddrs}; |
| 13 | +use std::vec; |
| 14 | +use wasmtime::component::Resource; |
| 15 | + |
| 16 | +pub enum ResolveAddressStream { |
| 17 | + Waiting(AbortOnDropJoinHandle<io::Result<Vec<IpAddress>>>), |
| 18 | + Done(io::Result<vec::IntoIter<IpAddress>>), |
| 19 | +} |
| 20 | + |
| 21 | +#[async_trait::async_trait] |
| 22 | +impl<T: WasiView> Host for T { |
| 23 | + fn resolve_addresses( |
| 24 | + &mut self, |
| 25 | + network: Resource<Network>, |
| 26 | + name: String, |
| 27 | + family: Option<IpAddressFamily>, |
| 28 | + include_unavailable: bool, |
| 29 | + ) -> Result<Resource<ResolveAddressStream>, Error> { |
| 30 | + if !self.table().get_network(&network)?.allow_ip_name_lookup { |
| 31 | + return Err(ErrorCode::PermanentResolverFailure.into()); |
| 32 | + } |
| 33 | + |
| 34 | + // ignored for now, should probably have a future PR to actually take |
| 35 | + // this into account. This would require invoking `getaddrinfo` directly |
| 36 | + // rather than using the standard library to do it for us. |
| 37 | + let _ = include_unavailable; |
| 38 | + |
| 39 | + // For now use the standard library to perform actual resolution through |
| 40 | + // the usage of the `ToSocketAddrs` trait. This blocks the current |
| 41 | + // thread, so use `spawn_blocking`. Finally note that this is only |
| 42 | + // resolving names, not ports, so force the port to be 0. |
| 43 | + let task = tokio::task::spawn_blocking(move || -> io::Result<Vec<_>> { |
| 44 | + let result = (name.as_str(), 0).to_socket_addrs()?; |
| 45 | + Ok(result |
| 46 | + .filter_map(|addr| { |
| 47 | + // In lieu of preventing these addresses from being resolved |
| 48 | + // in the first place, filter them out here. |
| 49 | + match addr { |
| 50 | + SocketAddr::V4(addr) => match family { |
| 51 | + None | Some(IpAddressFamily::Ipv4) => { |
| 52 | + let [a, b, c, d] = addr.ip().octets(); |
| 53 | + Some(IpAddress::Ipv4((a, b, c, d))) |
| 54 | + } |
| 55 | + Some(IpAddressFamily::Ipv6) => None, |
| 56 | + }, |
| 57 | + SocketAddr::V6(addr) => match family { |
| 58 | + None | Some(IpAddressFamily::Ipv6) => { |
| 59 | + let [a, b, c, d, e, f, g, h] = addr.ip().segments(); |
| 60 | + Some(IpAddress::Ipv6((a, b, c, d, e, f, g, h))) |
| 61 | + } |
| 62 | + Some(IpAddressFamily::Ipv4) => None, |
| 63 | + }, |
| 64 | + } |
| 65 | + }) |
| 66 | + .collect()) |
| 67 | + }); |
| 68 | + let task = AbortOnDropJoinHandle(task); |
| 69 | + let resource = self |
| 70 | + .table_mut() |
| 71 | + .push_resource(ResolveAddressStream::Waiting(task))?; |
| 72 | + Ok(resource) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +#[async_trait::async_trait] |
| 77 | +impl<T: WasiView> HostResolveAddressStream for T { |
| 78 | + fn resolve_next_address( |
| 79 | + &mut self, |
| 80 | + resource: Resource<ResolveAddressStream>, |
| 81 | + ) -> Result<Option<IpAddress>, Error> { |
| 82 | + let stream = self.table_mut().get_resource_mut(&resource)?; |
| 83 | + loop { |
| 84 | + match stream { |
| 85 | + ResolveAddressStream::Waiting(future) => match crate::preview2::poll_noop(future) { |
| 86 | + Some(result) => { |
| 87 | + *stream = ResolveAddressStream::Done(result.map(|v| v.into_iter())); |
| 88 | + } |
| 89 | + None => return Err(ErrorCode::WouldBlock.into()), |
| 90 | + }, |
| 91 | + ResolveAddressStream::Done(slot @ Err(_)) => { |
| 92 | + // TODO: this `?` is what converts `io::Error` into `Error` |
| 93 | + // and the conversion is not great right now. The standard |
| 94 | + // library doesn't expose a ton of information through the |
| 95 | + // return value of `getaddrinfo` right now so supporting a |
| 96 | + // richer conversion here will probably require calling |
| 97 | + // `getaddrinfo` directly. |
| 98 | + mem::replace(slot, Ok(Vec::new().into_iter()))?; |
| 99 | + unreachable!(); |
| 100 | + } |
| 101 | + ResolveAddressStream::Done(Ok(iter)) => return Ok(iter.next()), |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + fn subscribe( |
| 107 | + &mut self, |
| 108 | + resource: Resource<ResolveAddressStream>, |
| 109 | + ) -> Result<Resource<Pollable>> { |
| 110 | + Ok(self.table_mut().push_host_pollable_resource(&resource)?) |
| 111 | + } |
| 112 | + |
| 113 | + fn drop(&mut self, resource: Resource<ResolveAddressStream>) -> Result<()> { |
| 114 | + self.table_mut().delete_resource(resource)?; |
| 115 | + Ok(()) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +#[async_trait::async_trait] |
| 120 | +impl Subscribe for ResolveAddressStream { |
| 121 | + async fn ready(&mut self) -> Result<()> { |
| 122 | + if let ResolveAddressStream::Waiting(future) = self { |
| 123 | + *self = ResolveAddressStream::Done(future.await.map(|v| v.into_iter())); |
| 124 | + } |
| 125 | + Ok(()) |
| 126 | + } |
| 127 | +} |
0 commit comments