Skip to content

Commit

Permalink
Refactor ListenerAddress::Vsock to not depend on tokio_vsock::VsockAddr
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander V. Nikolaev <[email protected]>
  • Loading branch information
avnik committed Sep 11, 2024
1 parent 058499f commit d2ff11d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ tokio-util = {version = "0.7", optional=true, features=["net","codec"]}
tokio-vsock = {version = "0.5", optional=true}

[target.'cfg(unix)'.dependencies]
nix = { version = "0.26.2", default-features = false, features = ["user", "fs", "socket"], optional=true }
nix = { version = "0.26.2", default-features = false, features = ["user", "fs"], optional=true }


[features]
Expand Down
7 changes: 4 additions & 3 deletions src/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ fn listen_abstract(a: &String, usr_opts: &UserOptions) -> Result<ListenerImpl, s
}

#[cfg(all(target_os = "linux", feature = "vsock"))]
fn listen_vsock(vs: &tokio_vsock::VsockAddr) -> Result<ListenerImpl, std::io::Error> {
use tokio_vsock::VsockListener;
let listener = VsockListener::bind(vs.to_owned())?;
fn listen_vsock((cid, port): &(u32, u32)) -> Result<ListenerImpl, std::io::Error> {
use tokio_vsock::{VsockAddr, VsockListener};
let vs = VsockAddr::new(*cid, *port);
let listener = VsockListener::bind(vs)?;
Ok(ListenerImpl::Vsock(ListenerImplVsock{ s: listener}))
}

Expand Down
25 changes: 19 additions & 6 deletions src/listener_address.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::{fmt::Display, net::SocketAddr, path::PathBuf, str::FromStr};

#[cfg(all(unix, feature = "vsock"))]
use tokio_vsock::VsockAddr;

/// Abstraction over socket address that instructs in which way and at what address (if any) [`Listener`]
/// should listen for incoming stream connections.
///
Expand Down Expand Up @@ -31,7 +28,7 @@ use tokio_vsock::VsockAddr;
feature = "serde",
derive(serde_with::DeserializeFromStr, serde_with::SerializeDisplay)
)]
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ListenerAddress {
/// Usual server TCP socket. Triggered by specifying IPv4 or IPv6 address and port pair.
/// Example: `127.0.0.1:8080`.
Expand All @@ -55,7 +52,8 @@ pub enum ListenerAddress {
///
/// Special name `*` means to bind all passed addresses simultaneously, if `multi-listener` crate feature is enabled.
FromFdNamed(String),
Vsock(VsockAddr),
/// Pair of CID and port
Vsock((u32, u32)),
}

pub(crate) const SD_LISTEN_FDS_START: i32 = 3;
Expand Down Expand Up @@ -86,6 +84,12 @@ impl FromStr for ListenerAddress {
return Err("Invalid tokio-listener sd-listen: name");
}
Ok(ListenerAddress::FromFdNamed(x.to_owned()))
} else if let Some(vsock) = s.strip_prefix("vsock:") {
if let Some((Ok(cid), Ok(port))) = vsock.split_once(":").map(|(cid, port)| (cid.parse(), port.parse())) {
Ok(ListenerAddress::Vsock((cid, port)))
} else {
Err("Invalid tokio-listener vsock: cid:port pair expected")
}
} else if let Ok(a) = s.parse() {
Ok(ListenerAddress::Tcp(a))
} else {
Expand Down Expand Up @@ -125,7 +129,9 @@ impl Display for ListenerAddress {
ListenerAddress::FromFdNamed(name) => {
write!(f, "sd-listen:{name}")
}
ListenerAddress::Vsock(a) => a.fmt(f),
ListenerAddress::Vsock((cid, port)) => {
write!(f, "vsock:{cid}:{port}")
}
}
}
}
Expand Down Expand Up @@ -156,3 +162,10 @@ pub(crate) fn check_env_for_fd(fdnum: i32) -> Option<()> {

Some(())
}

#[cfg(all(unix, feature = "vsock"))]
impl std::convert::From<tokio_vsock::VsockAddr> for ListenerAddress {
fn from(vs: tokio_vsock::VsockAddr) -> Self {
ListenerAddress::Vsock((vs.cid(), vs.port()))
}
}

0 comments on commit d2ff11d

Please sign in to comment.