|
1 | 1 | //! Common types and traits shared between client and server TLS implementations. |
2 | 2 |
|
| 3 | +use std::any::TypeId; |
3 | 4 | use std::borrow::Cow; |
| 5 | +use std::pin::Pin; |
| 6 | +use std::task::{Context, Poll}; |
4 | 7 | use std::{fmt, io}; |
5 | 8 |
|
6 | | -use mlua::{IntoLua, Lua, Result, Value}; |
| 9 | +use mlua::{AnyUserData, Error, FromLua, IntoLua, Lua, MaybeSend, Result, Value}; |
| 10 | +use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; |
| 11 | + |
| 12 | +use super::tcp::{TcpListener, TcpStream}; |
| 13 | +#[cfg(feature = "tls")] |
| 14 | +use super::tls::{TlsListener, TlsStream}; |
| 15 | +#[cfg(unix)] |
| 16 | +use super::unix::{UnixListener, UnixStream}; |
7 | 17 |
|
8 | 18 | /// Socket address that can be either TCP or Unix domain socket. |
9 | 19 | pub enum AnySocketAddr { |
@@ -60,3 +70,215 @@ impl AddressProvider for tokio::net::UnixStream { |
60 | 70 | Ok(AnySocketAddr::Unix(self.peer_addr()?)) |
61 | 71 | } |
62 | 72 | } |
| 73 | + |
| 74 | +/// A stream that can be either TCP or Unix domain socket, possibly wrapped in TLS. |
| 75 | +pub enum AnyStream { |
| 76 | + Tcp(TcpStream), |
| 77 | + #[cfg(unix)] |
| 78 | + Unix(UnixStream), |
| 79 | + #[cfg(feature = "tls")] |
| 80 | + TcpTls(TlsStream<TcpStream>), |
| 81 | + #[cfg(all(unix, feature = "tls"))] |
| 82 | + UnixTls(TlsStream<UnixStream>), |
| 83 | +} |
| 84 | + |
| 85 | +impl AsyncRead for AnyStream { |
| 86 | + fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<io::Result<()>> { |
| 87 | + match self.get_mut() { |
| 88 | + AnyStream::Tcp(s) => Pin::new(s).poll_read(cx, buf), |
| 89 | + #[cfg(unix)] |
| 90 | + AnyStream::Unix(s) => Pin::new(s).poll_read(cx, buf), |
| 91 | + #[cfg(feature = "tls")] |
| 92 | + AnyStream::TcpTls(s) => Pin::new(s).poll_read(cx, buf), |
| 93 | + #[cfg(all(unix, feature = "tls"))] |
| 94 | + AnyStream::UnixTls(s) => Pin::new(s).poll_read(cx, buf), |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl AsyncWrite for AnyStream { |
| 100 | + fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> { |
| 101 | + match self.get_mut() { |
| 102 | + AnyStream::Tcp(s) => Pin::new(s).poll_write(cx, buf), |
| 103 | + #[cfg(unix)] |
| 104 | + AnyStream::Unix(s) => Pin::new(s).poll_write(cx, buf), |
| 105 | + #[cfg(feature = "tls")] |
| 106 | + AnyStream::TcpTls(s) => Pin::new(s).poll_write(cx, buf), |
| 107 | + #[cfg(all(unix, feature = "tls"))] |
| 108 | + AnyStream::UnixTls(s) => Pin::new(s).poll_write(cx, buf), |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 113 | + match self.get_mut() { |
| 114 | + AnyStream::Tcp(s) => Pin::new(s).poll_flush(cx), |
| 115 | + #[cfg(unix)] |
| 116 | + AnyStream::Unix(s) => Pin::new(s).poll_flush(cx), |
| 117 | + #[cfg(feature = "tls")] |
| 118 | + AnyStream::TcpTls(s) => Pin::new(s).poll_flush(cx), |
| 119 | + #[cfg(all(unix, feature = "tls"))] |
| 120 | + AnyStream::UnixTls(s) => Pin::new(s).poll_flush(cx), |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 125 | + match self.get_mut() { |
| 126 | + AnyStream::Tcp(s) => Pin::new(s).poll_shutdown(cx), |
| 127 | + #[cfg(unix)] |
| 128 | + AnyStream::Unix(s) => Pin::new(s).poll_shutdown(cx), |
| 129 | + #[cfg(feature = "tls")] |
| 130 | + AnyStream::TcpTls(s) => Pin::new(s).poll_shutdown(cx), |
| 131 | + #[cfg(all(unix, feature = "tls"))] |
| 132 | + AnyStream::UnixTls(s) => Pin::new(s).poll_shutdown(cx), |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +impl IntoLua for AnyStream { |
| 138 | + fn into_lua(self, lua: &Lua) -> Result<Value> { |
| 139 | + match self { |
| 140 | + AnyStream::Tcp(s) => lua.create_userdata(s).map(Value::UserData), |
| 141 | + #[cfg(unix)] |
| 142 | + AnyStream::Unix(s) => lua.create_userdata(s).map(Value::UserData), |
| 143 | + #[cfg(feature = "tls")] |
| 144 | + AnyStream::TcpTls(s) => lua.create_userdata(s).map(Value::UserData), |
| 145 | + #[cfg(all(unix, feature = "tls"))] |
| 146 | + AnyStream::UnixTls(s) => lua.create_userdata(s).map(Value::UserData), |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +impl FromLua for AnyStream { |
| 152 | + fn from_lua(value: Value, lua: &Lua) -> Result<Self> { |
| 153 | + let value = lua.unpack::<AnyUserData>(value)?; |
| 154 | + match value.type_id() { |
| 155 | + Some(id) if id == TypeId::of::<TcpStream>() => { |
| 156 | + let stream = value.take::<TcpStream>()?; |
| 157 | + Ok(AnyStream::Tcp(stream)) |
| 158 | + } |
| 159 | + #[cfg(unix)] |
| 160 | + Some(id) if id == TypeId::of::<UnixStream>() => { |
| 161 | + let stream = value.take::<UnixStream>()?; |
| 162 | + Ok(AnyStream::Unix(stream)) |
| 163 | + } |
| 164 | + #[cfg(feature = "tls")] |
| 165 | + Some(id) if id == TypeId::of::<TlsStream<TcpStream>>() => { |
| 166 | + let stream = value.take::<TlsStream<TcpStream>>()?; |
| 167 | + Ok(AnyStream::TcpTls(stream)) |
| 168 | + } |
| 169 | + #[cfg(all(unix, feature = "tls"))] |
| 170 | + Some(id) if id == TypeId::of::<TlsStream<UnixStream>>() => { |
| 171 | + let stream = value.take::<TlsStream<UnixStream>>()?; |
| 172 | + Ok(AnyStream::UnixTls(stream)) |
| 173 | + } |
| 174 | + _ => { |
| 175 | + let type_name = value.type_name().ok().flatten(); |
| 176 | + let type_name = type_name.as_deref().unwrap_or("unknown"); |
| 177 | + Err(Error::FromLuaConversionError { |
| 178 | + from: "UserData", |
| 179 | + to: "AnyStream".to_string(), |
| 180 | + message: Some(format!("expected TcpStream or UnixStream, got {type_name}",)), |
| 181 | + }) |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +/// A listener that can be either TCP or Unix domain socket, possibly wrapped in TLS. |
| 188 | +pub enum AnyListener { |
| 189 | + Tcp(TcpListener), |
| 190 | + #[cfg(unix)] |
| 191 | + Unix(UnixListener), |
| 192 | + #[cfg(feature = "tls")] |
| 193 | + TcpTls(TlsListener<TcpListener>), |
| 194 | + #[cfg(all(unix, feature = "tls"))] |
| 195 | + UnixTls(TlsListener<UnixListener>), |
| 196 | +} |
| 197 | + |
| 198 | +/// Trait for accepting incoming connections from various listener types. |
| 199 | +pub trait Accept { |
| 200 | + type Stream: AsyncRead + AsyncWrite + Unpin + MaybeSend + 'static; |
| 201 | + |
| 202 | + /// Get the local address that the listener is bound to. |
| 203 | + fn local_addr(&self) -> io::Result<AnySocketAddr>; |
| 204 | + |
| 205 | + /// Accept an incoming connection. |
| 206 | + #[allow(async_fn_in_trait)] |
| 207 | + async fn accept(&self) -> io::Result<(Self::Stream, AnySocketAddr)>; |
| 208 | +} |
| 209 | + |
| 210 | +impl Accept for AnyListener { |
| 211 | + type Stream = AnyStream; |
| 212 | + |
| 213 | + fn local_addr(&self) -> io::Result<AnySocketAddr> { |
| 214 | + match self { |
| 215 | + AnyListener::Tcp(l) => l.local_addr(), |
| 216 | + #[cfg(unix)] |
| 217 | + AnyListener::Unix(l) => l.local_addr(), |
| 218 | + #[cfg(feature = "tls")] |
| 219 | + AnyListener::TcpTls(l) => l.local_addr(), |
| 220 | + #[cfg(all(unix, feature = "tls"))] |
| 221 | + AnyListener::UnixTls(l) => l.local_addr(), |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + async fn accept(&self) -> io::Result<(AnyStream, AnySocketAddr)> { |
| 226 | + match self { |
| 227 | + AnyListener::Tcp(listener) => { |
| 228 | + let (stream, addr) = listener.accept().await?; |
| 229 | + Ok((AnyStream::Tcp(stream), addr)) |
| 230 | + } |
| 231 | + #[cfg(unix)] |
| 232 | + AnyListener::Unix(listener) => { |
| 233 | + let (stream, addr) = listener.accept().await?; |
| 234 | + Ok((AnyStream::Unix(stream), addr)) |
| 235 | + } |
| 236 | + #[cfg(feature = "tls")] |
| 237 | + AnyListener::TcpTls(listener) => { |
| 238 | + let (stream, addr) = listener.accept().await?; |
| 239 | + Ok((AnyStream::TcpTls(stream), addr)) |
| 240 | + } |
| 241 | + #[cfg(all(unix, feature = "tls"))] |
| 242 | + AnyListener::UnixTls(listener) => { |
| 243 | + let (stream, addr) = listener.accept().await?; |
| 244 | + Ok((AnyStream::UnixTls(stream), addr)) |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +impl FromLua for AnyListener { |
| 251 | + fn from_lua(value: Value, lua: &Lua) -> Result<Self> { |
| 252 | + let value = lua.unpack::<AnyUserData>(value)?; |
| 253 | + match value.type_id() { |
| 254 | + Some(id) if id == TypeId::of::<TcpListener>() => { |
| 255 | + let listener = value.take::<TcpListener>()?; |
| 256 | + Ok(AnyListener::Tcp(listener)) |
| 257 | + } |
| 258 | + #[cfg(unix)] |
| 259 | + Some(id) if id == TypeId::of::<UnixListener>() => { |
| 260 | + let listener = value.take::<UnixListener>()?; |
| 261 | + Ok(AnyListener::Unix(listener)) |
| 262 | + } |
| 263 | + #[cfg(feature = "tls")] |
| 264 | + Some(id) if id == TypeId::of::<TlsListener<TcpListener>>() => { |
| 265 | + let listener = value.take::<TlsListener<TcpListener>>()?; |
| 266 | + Ok(AnyListener::TcpTls(listener)) |
| 267 | + } |
| 268 | + #[cfg(all(unix, feature = "tls"))] |
| 269 | + Some(id) if id == TypeId::of::<TlsListener<UnixListener>>() => { |
| 270 | + let listener = value.take::<TlsListener<UnixListener>>()?; |
| 271 | + Ok(AnyListener::UnixTls(listener)) |
| 272 | + } |
| 273 | + _ => { |
| 274 | + let type_name = value.type_name().ok().flatten(); |
| 275 | + let type_name = type_name.as_deref().unwrap_or("unknown"); |
| 276 | + Err(Error::FromLuaConversionError { |
| 277 | + from: "UserData", |
| 278 | + to: "AnyListener".to_string(), |
| 279 | + message: Some(format!("expected TcpListener or UnixListener, got {type_name}")), |
| 280 | + }) |
| 281 | + } |
| 282 | + } |
| 283 | + } |
| 284 | +} |
0 commit comments