| @@ -37,7 +37,7 @@ use crate::int::saturating; |
| 37 | 37 | use crate::otp::Wait; |
| 38 | 38 | use crate::resource::{tl_resource_get, tl_resource_new}; |
| 39 | 39 | use crate::sched::{park, this}; |
| 40 | | −use crate::values::{bytes_from, bytes_of, error, ok, text}; |
| 40 | +use crate::values::{bytes_from, bytes_of, error, list, ok, string, text}; |
| 41 | 41 | |
| 42 | 42 | enum Socket { |
| 43 | 43 | Listener(TcpListener), |
| @@ -141,7 +141,34 @@ pub(crate) unsafe fn wait_for(ctx: *mut Ctx, token: Token, seen: u64) -> bool { |
| 141 | 141 | } |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | | −/// `listen(host, port)`: a listening socket. |
| 144 | +/// `resolve(host)`: the addresses `host` names, as text (`127.0.0.1`, |
| 145 | +/// `::1`), in the resolver's order. A lookup can take seconds, so this is |
| 146 | +/// a `blocking` extern: it runs on the blocking pool while its actor waits |
| 147 | +/// (ADR 0012, Revision 2), and must not panic. |
| 148 | +/// |
| 149 | +/// # Safety |
| 150 | +/// `host` must be a live string. |
| 151 | +#[unsafe(no_mangle)] |
| 152 | +pub unsafe extern "C" fn tl_net_resolve(ctx: *mut Ctx, host: Value) -> Value { |
| 153 | + // SAFETY: caller guarantees a live string. |
| 154 | + let host = unsafe { text(host) }; |
| 155 | + match (host, 0).to_socket_addrs() { |
| 156 | + Ok(addrs) => { |
| 157 | + let addrs: Vec<Value> = addrs.map(|a| string(ctx, &a.ip().to_string())).collect(); |
| 158 | + ok(ctx, list(ctx, addrs.into_iter())) |
| 159 | + } |
| 160 | + Err(e) => error(ctx, &e.to_string()), |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +/// A port given as an Int: 0 to 65535, else the error. |
| 165 | +fn port_number(port: i64) -> Result<u16, String> { |
| 166 | + u16::try_from(port).map_err(|_| format!("port out of range: {port}")) |
| 167 | +} |
| 168 | + |
| 169 | +/// `listen(address, port)`: a listening socket. `std/net` resolves the |
| 170 | +/// host first ([`tl_net_resolve`]), so `address` is an IP address, which |
| 171 | +/// needs no lookup here. |
| 145 | 172 | /// |
| 146 | 173 | /// # Safety |
| 147 | 174 | /// `ctx` must be the running actor's context; `host` a live string. |
| @@ -149,7 +176,11 @@ pub(crate) unsafe fn wait_for(ctx: *mut Ctx, token: Token, seen: u64) -> bool { |
| 149 | 176 | pub unsafe extern "C" fn tl_net_listen(ctx: *mut Ctx, host: Value, port: Value) -> Value { |
| 150 | 177 | // SAFETY: caller guarantees a live string and a live Int. |
| 151 | 178 | let (host, port) = unsafe { (text(host), saturating(port)) }; |
| 152 | | − let addr = match (host, port as u16).to_socket_addrs().map(|mut a| a.next()) { |
| 179 | + let port = match port_number(port) { |
| 180 | + Ok(p) => p, |
| 181 | + Err(e) => return error(ctx, &e), |
| 182 | + }; |
| 183 | + let addr = match (host, port).to_socket_addrs().map(|mut a| a.next()) { |
| 153 | 184 | Ok(Some(a)) => a, |
| 154 | 185 | Ok(None) => return error(ctx, "no address"), |
| 155 | 186 | Err(e) => return error(ctx, &e.to_string()), |
| @@ -210,10 +241,9 @@ pub unsafe extern "C" fn tl_net_accept(ctx: *mut Ctx, listener: Value) -> Value |
| 210 | 241 | } |
| 211 | 242 | } |
| 212 | 243 | |
| 213 | | −/// `connect(host, port)`: a connection to a server, at the first of the |
| 214 | | −/// addresses `host` resolves to that accepts, tried in order (`localhost` |
| 215 | | −/// is often `::1` first, then `127.0.0.1`, and a server may listen on |
| 216 | | −/// only one). The error is the last address's. |
| 244 | +/// `connect(address, port)`: a connection to a server. `std/net` |
| 245 | +/// resolves the host first ([`tl_net_resolve`]) and tries each address in |
| 246 | +/// turn, so `address` is an IP address, which needs no lookup here. |
| 217 | 247 | /// |
| 218 | 248 | /// # Safety |
| 219 | 249 | /// `ctx` must be the running actor's context; `host` a live string. |
| @@ -221,7 +251,11 @@ pub unsafe extern "C" fn tl_net_accept(ctx: *mut Ctx, listener: Value) -> Value |
| 221 | 251 | pub unsafe extern "C" fn tl_net_connect(ctx: *mut Ctx, host: Value, port: Value) -> Value { |
| 222 | 252 | // SAFETY: caller guarantees a live string and a live Int. |
| 223 | 253 | let (host, port) = unsafe { (text(host), saturating(port)) }; |
| 224 | | − let addrs: Vec<SocketAddr> = match (host, port as u16).to_socket_addrs() { |
| 254 | + let port = match port_number(port) { |
| 255 | + Ok(p) => p, |
| 256 | + Err(e) => return error(ctx, &e), |
| 257 | + }; |
| 258 | + let addrs: Vec<SocketAddr> = match (host, port).to_socket_addrs() { |
| 225 | 259 | Ok(a) => a.collect(), |
| 226 | 260 | Err(e) => return error(ctx, &e.to_string()), |
| 227 | 261 | }; |