Skip to content

Commit cee16a2

Browse files
committed
refactor: restructure and reuse code
1 parent 009c257 commit cee16a2

27 files changed

+1214
-910
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ smol-hyper = { version = "0.1.1", optional = true, default-features = false, fea
3131
"async-io",
3232
] }
3333
futures-lite = { version = "2.6.0", optional = true }
34-
# vsock sockets
34+
# VSOCK sockets
3535
vsock = { version = "0.5.1", optional = true }
3636

3737
[dev-dependencies]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
## Hyper Client Sockets
22

3-
Before hyper v1, hyperlocal was the most convenient solution to use Unix sockets for both client and server. With hyper v1, server socket support is no longer needed (just use `UnixListener` or `VsockListener` instead of `TcpListener`), yet hyperlocal still has it and hasn't received a release since several years.
3+
Before hyper v1, hyperlocal was the most convenient solution to use Unix Domain sockets for both client and server. With hyper v1, server socket support is no longer needed (just use `UnixListener` or `VsockListener` instead of `TcpListener`), yet hyperlocal still has it and hasn't received a release since several years.
44

55
This library provides hyper v1 client support for:
66

77
- Unix (`AF_UNIX`) sockets (`HyperUnixStream` implementing hyper traits)
88
- VSock (`AF_VSOCK`) sockets (`HyperVsockStream` implementing hyper traits)
9-
- Firecracker Unix sockets that need `CONNECT` commands in order to establish a tunnel (`HyperFirecrackerStream` implementing hyper traits)
9+
- Firecracker Unix Domain sockets that need `CONNECT` commands in order to establish a tunnel (`HyperFirecrackerStream` implementing hyper traits)
1010

1111
Additionally, the library supports different async I/O backends:
1212

src/async_io.rs

Lines changed: 0 additions & 235 deletions
This file was deleted.

src/async_io/firecracker.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::{
2+
io::Result,
3+
ops::{Deref, DerefMut},
4+
os::unix::net::UnixStream,
5+
path::Path,
6+
};
7+
8+
use async_io::Async;
9+
use futures_lite::{io::BufReader, AsyncBufReadExt as _, AsyncWriteExt as _, StreamExt as _};
10+
use smol_hyper::rt::FuturesIo;
11+
12+
use crate::utils::{
13+
firecracker::{format_request, parse_connection_response},
14+
hyper_io_by_deref,
15+
};
16+
17+
pub type AsyncFirecrackerIoInner = FuturesIo<Async<UnixStream>>;
18+
19+
pub struct AsyncFirecrackerIo(AsyncFirecrackerIoInner);
20+
21+
impl AsyncFirecrackerIo {
22+
pub(super) async fn connect<P>(host_socket_path: P, guest_port: u32) -> Result<Self>
23+
where
24+
P: AsRef<Path>,
25+
{
26+
let mut stream = Async::<UnixStream>::connect(host_socket_path).await?;
27+
stream.write_all(format_request(guest_port).as_bytes()).await?;
28+
let response = BufReader::new(&mut stream).lines().next().await.transpose();
29+
parse_connection_response(stream, response)
30+
.map(FuturesIo::new)
31+
.map(Self)
32+
}
33+
}
34+
35+
impl Deref for AsyncFirecrackerIo {
36+
type Target = AsyncFirecrackerIoInner;
37+
38+
fn deref(&self) -> &Self::Target {
39+
&self.0
40+
}
41+
}
42+
43+
impl DerefMut for AsyncFirecrackerIo {
44+
fn deref_mut(&mut self) -> &mut Self::Target {
45+
&mut self.0
46+
}
47+
}
48+
49+
hyper_io_by_deref!(AsyncFirecrackerIo);

src/async_io/mod.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#[cfg(feature = "firecracker")]
2+
#[cfg_attr(docsrs, doc(cfg(feature = "firecracker")))]
3+
pub mod firecracker;
4+
5+
#[cfg(feature = "unix")]
6+
#[cfg_attr(docsrs, doc(cfg(feature = "unix")))]
7+
pub mod unix;
8+
9+
#[cfg(feature = "vsock")]
10+
#[cfg_attr(docsrs, doc(cfg(feature = "vsock")))]
11+
pub mod vsock;
12+
13+
#[cfg(any(feature = "firecracker", feature = "unix", feature = "vsock"))]
14+
use std::io::Result;
15+
16+
#[cfg(any(feature = "firecracker", feature = "unix"))]
17+
use std::path::Path;
18+
19+
#[cfg(feature = "vsock")]
20+
use ::vsock::VsockAddr;
21+
22+
#[cfg(feature = "firecracker")]
23+
#[cfg_attr(docsrs, doc(cfg(feature = "firecracker")))]
24+
pub use self::firecracker::{AsyncFirecrackerIo, AsyncFirecrackerIoInner};
25+
26+
#[cfg(feature = "unix")]
27+
#[cfg_attr(docsrs, doc(cfg(feature = "unix")))]
28+
pub use self::unix::{AsyncUnixIo, AsyncUnixIoInner};
29+
30+
#[cfg(feature = "vsock")]
31+
#[cfg_attr(docsrs, doc(cfg(feature = "vsock")))]
32+
pub use self::vsock::{AsyncVsockIo, AsyncVsockIoInner};
33+
34+
use crate::Backend;
35+
36+
/// [`Backend`] for hyper-client-sockets that is implemented via the async-io crate's reactor.
37+
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
38+
pub struct AsyncIoBackend;
39+
40+
impl Backend for AsyncIoBackend {
41+
#[cfg(feature = "firecracker")]
42+
#[cfg_attr(docsrs, doc(cfg(feature = "firecracker")))]
43+
type FirecrackerIo = AsyncFirecrackerIo;
44+
45+
#[cfg(feature = "unix")]
46+
#[cfg_attr(docsrs, doc(cfg(feature = "unix")))]
47+
type UnixIo = AsyncUnixIo;
48+
49+
#[cfg(feature = "vsock")]
50+
#[cfg_attr(docsrs, doc(cfg(feature = "vsock")))]
51+
type VsockIo = AsyncVsockIo;
52+
53+
#[cfg(feature = "firecracker")]
54+
#[cfg_attr(docsrs, doc(cfg(feature = "firecracker")))]
55+
async fn connect_to_firecracker_socket(host_socket_path: &Path, guest_port: u32) -> Result<Self::FirecrackerIo> {
56+
Self::FirecrackerIo::connect(host_socket_path, guest_port).await
57+
}
58+
59+
#[cfg(feature = "unix")]
60+
#[cfg_attr(docsrs, doc(cfg(feature = "unix")))]
61+
async fn connect_to_unix_socket(socket_path: &Path) -> Result<Self::UnixIo> {
62+
Self::UnixIo::connect(socket_path).await
63+
}
64+
65+
#[cfg(feature = "vsock")]
66+
#[cfg_attr(docsrs, doc(cfg(feature = "vsock")))]
67+
async fn connect_to_vsock_socket(addr: VsockAddr) -> Result<Self::VsockIo> {
68+
Self::VsockIo::connect(addr).await
69+
}
70+
}

0 commit comments

Comments
 (0)