Make libforwarder buildable
Bug: 340126051
Test: m libforwarder
Change-Id: Ie427efeb5f326ec4bd1dfb84ac7ee8c03b9e0248
diff --git a/libs/libforwarder/Android.bp b/libs/libforwarder/Android.bp
new file mode 100644
index 0000000..48307e7
--- /dev/null
+++ b/libs/libforwarder/Android.bp
@@ -0,0 +1,15 @@
+package {
+ default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+rust_library {
+ name: "libforwarder",
+ crate_name: "forwarder",
+ edition: "2021",
+ srcs: ["src/lib.rs"],
+ rustlibs: [
+ "liblibc",
+ "libvsock",
+ ],
+ proc_macros: ["libremain"],
+}
diff --git a/libs/libforwarder/src/forwarder.rs b/libs/libforwarder/src/forwarder.rs
index 89c75d5..3600ab2 100644
--- a/libs/libforwarder/src/forwarder.rs
+++ b/libs/libforwarder/src/forwarder.rs
@@ -15,6 +15,8 @@
// Copied from ChromiumOS with relicensing:
// src/platform2/vm_tools/chunnel/src/forwarder.rs
+//! This module contains forwarding mechanism between stream sockets.
+
use std::fmt;
use std::io::{self, Read, Write};
use std::result;
@@ -62,19 +64,13 @@
fn forward(from_stream: &mut StreamSocket, to_stream: &mut StreamSocket) -> Result<bool> {
let mut buf = [0u8; MAX_FRAME_SIZE];
- let count = from_stream
- .read(&mut buf)
- .map_err(ForwarderError::ReadFromStream)?;
+ let count = from_stream.read(&mut buf).map_err(ForwarderError::ReadFromStream)?;
if count == 0 {
- to_stream
- .shut_down_write()
- .map_err(ForwarderError::ShutDownStream)?;
+ to_stream.shut_down_write().map_err(ForwarderError::ShutDownStream)?;
return Ok(true);
}
- to_stream
- .write_all(&buf[..count])
- .map_err(ForwarderError::WriteToStream)?;
+ to_stream.write_all(&buf[..count]).map_err(ForwarderError::WriteToStream)?;
Ok(false)
}
diff --git a/libs/libforwarder/src/lib.rs b/libs/libforwarder/src/lib.rs
index 9c2c7a3..bcce689 100644
--- a/libs/libforwarder/src/lib.rs
+++ b/libs/libforwarder/src/lib.rs
@@ -15,5 +15,7 @@
// Copied from ChromiumOS with relicensing:
// src/platform2/vm_tools/chunnel/src/lib.rs
+//! Library for stream socket forwarding.
+
pub mod forwarder;
pub mod stream;
diff --git a/libs/libforwarder/src/stream.rs b/libs/libforwarder/src/stream.rs
index 2b48498..d8c7f51 100644
--- a/libs/libforwarder/src/stream.rs
+++ b/libs/libforwarder/src/stream.rs
@@ -15,6 +15,8 @@
// Copied from ChromiumOS with relicensing:
// src/platform2/vm_tools/chunnel/src/stream.rs
+//! This module provides abstraction of various stream socket type.
+
use std::fmt;
use std::io;
use std::net::TcpStream;
@@ -35,12 +37,8 @@
}
Ok(VsockAddr::new(
- components[1]
- .parse()
- .map_err(|_| io::Error::from_raw_os_error(libc::EINVAL))?,
- components[2]
- .parse()
- .map_err(|_| io::Error::from_raw_os_error(libc::EINVAL))?,
+ components[1].parse().map_err(|_| io::Error::from_raw_os_error(libc::EINVAL))?,
+ components[2].parse().map_err(|_| io::Error::from_raw_os_error(libc::EINVAL))?,
))
}
@@ -79,6 +77,7 @@
/// Shuts down writes to the socket using shutdown(2).
pub fn shut_down_write(&mut self) -> io::Result<()> {
+ // SAFETY:
// Safe because no memory is modified and the return value is checked.
let ret = unsafe { shutdown(self.fd, SHUT_WR) };
if ret < 0 {
@@ -97,6 +96,7 @@
impl io::Read for StreamSocket {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ // SAFETY:
// Safe because this will only modify the contents of |buf| and we check the return value.
let ret = unsafe { libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, buf.len()) };
if ret < 0 {
@@ -109,6 +109,7 @@
impl io::Write for StreamSocket {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ // SAFETY:
// Safe because this doesn't modify any memory and we check the return value.
let ret = unsafe { libc::write(self.fd, buf.as_ptr() as *const c_void, buf.len()) };
if ret < 0 {
@@ -140,42 +141,31 @@
impl From<TcpStream> for StreamSocket {
fn from(stream: TcpStream) -> Self {
- StreamSocket {
- fd: stream.into_raw_fd(),
- shut_down: false,
- }
+ StreamSocket { fd: stream.into_raw_fd(), shut_down: false }
}
}
impl From<UnixStream> for StreamSocket {
fn from(stream: UnixStream) -> Self {
- StreamSocket {
- fd: stream.into_raw_fd(),
- shut_down: false,
- }
+ StreamSocket { fd: stream.into_raw_fd(), shut_down: false }
}
}
impl From<VsockStream> for StreamSocket {
fn from(stream: VsockStream) -> Self {
- StreamSocket {
- fd: stream.into_raw_fd(),
- shut_down: false,
- }
+ StreamSocket { fd: stream.into_raw_fd(), shut_down: false }
}
}
impl FromRawFd for StreamSocket {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
- StreamSocket {
- fd,
- shut_down: false,
- }
+ StreamSocket { fd, shut_down: false }
}
}
impl Drop for StreamSocket {
fn drop(&mut self) {
+ // SAFETY:
// Safe because this doesn't modify any memory and we are the only
// owner of the file descriptor.
unsafe { libc::close(self.fd) };
@@ -186,8 +176,11 @@
#[remain::sorted]
#[derive(Debug)]
pub enum StreamSocketError {
+ /// Error on connecting TCP socket.
ConnectTcp(String, io::Error),
+ /// Error on connecting unix socket.
ConnectUnix(String, io::Error),
+ /// Error on connecting vsock socket.
ConnectVsock(String, io::Error),
}