pub struct Tube<T>{
pub inner: T,
pub timeout: Duration,
/* private fields */
}Expand description
A wrapper to provide extra methods. Note that the API from this crate is different from pwntools.
Fields§
§inner: TThe inner struct, usually a BufReader containing the original struct.
timeout: DurationThis field is only used by methods directly provided by this struct and not methods from
traits like AsyncRead.
This is due to the fact that during the polling, there is no way to keep track of the futures involved. If 2 calls to the poll functions occurs, there is not enough information in the argument to deduce whether it come from the same future or the previous future is dropped and another future has started polling. As a result, the API will be producing inconsistent timeout if it is implemented.
Luckily, tokio::time::timeout provides an easy way to add timeout to a future (which is
how timeout is implemented in this library) so you can still have timeout behaviour on
functions that doesn’t support them.
Hence, timeout can only be reliably implemented for async fn (which returns a future under the hood) or fn that return a future.
Implementations§
Source§impl<T> Tube<BufReader<T>>
impl<T> Tube<BufReader<T>>
Sourcepub fn with_timeout(inner: T, timeout: Duration) -> Self
pub fn with_timeout(inner: T, timeout: Duration) -> Self
Construct a new Tube<T> with the supplied timeout argument. Note that timeout is only
implemented for methods directly provided by this struct and not methods from traits.
use io_tubes::tubes::{ProcessTube, Tube};
use std::{io, time::Duration};
#[tokio::main]
async fn create_with_timeout() -> io::Result<()> {
let mut p = Tube::process("/usr/bin/cat")?;
p.timeout = Duration::from_millis(50);
// Equivalent to
let mut p =
Tube::with_timeout(ProcessTube::new("/usr/bin/cat")?, Duration::from_millis(50));
Ok(())
}
create_with_timeout();Source§impl Tube<BufReader<ProcessTube>>
impl Tube<BufReader<ProcessTube>>
Sourcepub fn process<S: AsRef<OsStr>>(program: S) -> Result<Self>
pub fn process<S: AsRef<OsStr>>(program: S) -> Result<Self>
Create a process with supplied path to program.
use io_tubes::tubes::Tube;
use std::io;
#[tokio::main]
async fn create_process() -> io::Result<()> {
let mut p = Tube::process("/usr/bin/cat")?;
p.send("abcdHi!").await?;
let result = p.recv_until("Hi").await?;
assert_eq!(result, b"abcdHi");
Ok(())
}
create_process();Source§impl Tube<BufReader<TcpStream>>
impl Tube<BufReader<TcpStream>>
Sourcepub async fn remote<A: ToSocketAddrs>(addr: A) -> Result<Self>
pub async fn remote<A: ToSocketAddrs>(addr: A) -> Result<Self>
Create a tube by connecting to the remote address.
use io_tubes::tubes::{Listener, Tube};
use std::{
io,
net::{IpAddr, Ipv4Addr, SocketAddr},
};
#[tokio::main]
async fn create_remote() -> io::Result<()> {
let l = Listener::listen().await?;
let mut p =
Tube::remote(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), l.port()?)).await?;
let mut server = l.accept().await?;
p.send("Client Hello").await?;
server.send("Server Hello").await?;
assert_eq!(p.recv_until("Hello").await?, b"Server Hello");
assert_eq!(server.recv_until("Hello").await?, b"Client Hello");
Ok(())
}
create_remote();Source§impl<T> Tube<T>
impl<T> Tube<T>
Sourcepub fn from_buffered(inner: T) -> Self
pub fn from_buffered(inner: T) -> Self
Construct a tube from any custom buffered type.
Sourcepub async fn recv_line(&mut self) -> Result<Vec<u8>>
pub async fn recv_line(&mut self) -> Result<Vec<u8>>
Receive until new line (0xA byte) is reached or EOF is reached.
Sourcepub async fn recv_until<A: AsRef<[u8]>>(&mut self, delims: A) -> Result<Vec<u8>>
pub async fn recv_until<A: AsRef<[u8]>>(&mut self, delims: A) -> Result<Vec<u8>>
Receive until the delims are found or EOF is reached.
A lookup table will be built to enable efficient matching of long patterns.
Sourcepub async fn send_line<A: AsRef<[u8]>>(&mut self, data: A) -> Result<()>
pub async fn send_line<A: AsRef<[u8]>>(&mut self, data: A) -> Result<()>
Same as send, but add new line (0xA byte).
Sourcepub async fn send_line_after<A: AsRef<[u8]>, B: AsRef<[u8]>>(
&mut self,
pattern: A,
data: B,
) -> Result<Vec<u8>>
pub async fn send_line_after<A: AsRef<[u8]>, B: AsRef<[u8]>>( &mut self, pattern: A, data: B, ) -> Result<Vec<u8>>
Send line after receiving the pattern from read.
use io_tubes::tubes::Tube;
use std::io;
#[tokio::main]
async fn send_line_after() -> io::Result<()> {
let mut p = Tube::process("/usr/bin/cat")?;
p.send("Hello, what's your name? ").await?;
assert_eq!(
p.send_line_after("name", "test").await?,
b"Hello, what's your name"
);
assert_eq!(p.recv_line().await?, b"? test\n");
Ok(())
}
send_line_after();Sourcepub async fn interactive(&mut self) -> Result<()>
pub async fn interactive(&mut self) -> Result<()>
Connect the tube to stdin and stdout so you can interact with it directly.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consume the tube to get back the underlying BufReader
Trait Implementations§
Source§impl<T> AsyncBufRead for Tube<T>
impl<T> AsyncBufRead for Tube<T>
Source§impl<T> AsyncWrite for Tube<T>
impl<T> AsyncWrite for Tube<T>
Source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize>>
buf into the object. Read moreSource§fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
Source§fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
Source§fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>],
) -> Poll<Result<usize, Error>>
fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>], ) -> Poll<Result<usize, Error>>
poll_write, except that it writes from a slice of buffers. Read moreSource§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
poll_write_vectored
implementation. Read moreAuto Trait Implementations§
impl<T> Freeze for Tube<T>where
T: Freeze,
impl<T> RefUnwindSafe for Tube<T>where
T: RefUnwindSafe,
impl<T> Send for Tube<T>where
T: Send,
impl<T> Sync for Tube<T>where
T: Sync,
impl<T> Unpin for Tube<T>
impl<T> UnwindSafe for Tube<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<R> AsyncBufReadExt for Rwhere
R: AsyncBufRead + ?Sized,
impl<R> AsyncBufReadExt for Rwhere
R: AsyncBufRead + ?Sized,
Source§fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>,
) -> ReadUntil<'a, Self>where
Self: Unpin,
fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>,
) -> ReadUntil<'a, Self>where
Self: Unpin,
Source§fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLine<'a, Self>where
Self: Unpin,
fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLine<'a, Self>where
Self: Unpin,
Source§fn split(self, byte: u8) -> Split<Self>
fn split(self, byte: u8) -> Split<Self>
byte. Read moreSource§fn fill_buf(&mut self) -> FillBuf<'_, Self>where
Self: Unpin,
fn fill_buf(&mut self) -> FillBuf<'_, Self>where
Self: Unpin,
Source§impl<R> AsyncReadExt for R
impl<R> AsyncReadExt for R
Source§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
Source§fn read_buf<'a, B>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B>
fn read_buf<'a, B>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B>
Source§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where
Self: Unpin,
buf. Read moreSource§fn read_u8(&mut self) -> ReadU8<&mut Self>where
Self: Unpin,
fn read_u8(&mut self) -> ReadU8<&mut Self>where
Self: Unpin,
Source§fn read_i8(&mut self) -> ReadI8<&mut Self>where
Self: Unpin,
fn read_i8(&mut self) -> ReadI8<&mut Self>where
Self: Unpin,
Source§fn read_u16(&mut self) -> ReadU16<&mut Self>where
Self: Unpin,
fn read_u16(&mut self) -> ReadU16<&mut Self>where
Self: Unpin,
Source§fn read_i16(&mut self) -> ReadI16<&mut Self>where
Self: Unpin,
fn read_i16(&mut self) -> ReadI16<&mut Self>where
Self: Unpin,
Source§fn read_u32(&mut self) -> ReadU32<&mut Self>where
Self: Unpin,
fn read_u32(&mut self) -> ReadU32<&mut Self>where
Self: Unpin,
Source§fn read_i32(&mut self) -> ReadI32<&mut Self>where
Self: Unpin,
fn read_i32(&mut self) -> ReadI32<&mut Self>where
Self: Unpin,
Source§fn read_u64(&mut self) -> ReadU64<&mut Self>where
Self: Unpin,
fn read_u64(&mut self) -> ReadU64<&mut Self>where
Self: Unpin,
Source§fn read_i64(&mut self) -> ReadI64<&mut Self>where
Self: Unpin,
fn read_i64(&mut self) -> ReadI64<&mut Self>where
Self: Unpin,
Source§fn read_u128(&mut self) -> ReadU128<&mut Self>where
Self: Unpin,
fn read_u128(&mut self) -> ReadU128<&mut Self>where
Self: Unpin,
Source§fn read_i128(&mut self) -> ReadI128<&mut Self>where
Self: Unpin,
fn read_i128(&mut self) -> ReadI128<&mut Self>where
Self: Unpin,
Source§fn read_f32(&mut self) -> ReadF32<&mut Self>where
Self: Unpin,
fn read_f32(&mut self) -> ReadF32<&mut Self>where
Self: Unpin,
Source§fn read_f64(&mut self) -> ReadF64<&mut Self>where
Self: Unpin,
fn read_f64(&mut self) -> ReadF64<&mut Self>where
Self: Unpin,
Source§fn read_u16_le(&mut self) -> ReadU16Le<&mut Self>where
Self: Unpin,
fn read_u16_le(&mut self) -> ReadU16Le<&mut Self>where
Self: Unpin,
Source§fn read_i16_le(&mut self) -> ReadI16Le<&mut Self>where
Self: Unpin,
fn read_i16_le(&mut self) -> ReadI16Le<&mut Self>where
Self: Unpin,
Source§fn read_u32_le(&mut self) -> ReadU32Le<&mut Self>where
Self: Unpin,
fn read_u32_le(&mut self) -> ReadU32Le<&mut Self>where
Self: Unpin,
Source§fn read_i32_le(&mut self) -> ReadI32Le<&mut Self>where
Self: Unpin,
fn read_i32_le(&mut self) -> ReadI32Le<&mut Self>where
Self: Unpin,
Source§fn read_u64_le(&mut self) -> ReadU64Le<&mut Self>where
Self: Unpin,
fn read_u64_le(&mut self) -> ReadU64Le<&mut Self>where
Self: Unpin,
Source§fn read_i64_le(&mut self) -> ReadI64Le<&mut Self>where
Self: Unpin,
fn read_i64_le(&mut self) -> ReadI64Le<&mut Self>where
Self: Unpin,
Source§fn read_u128_le(&mut self) -> ReadU128Le<&mut Self>where
Self: Unpin,
fn read_u128_le(&mut self) -> ReadU128Le<&mut Self>where
Self: Unpin,
Source§fn read_i128_le(&mut self) -> ReadI128Le<&mut Self>where
Self: Unpin,
fn read_i128_le(&mut self) -> ReadI128Le<&mut Self>where
Self: Unpin,
Source§fn read_f32_le(&mut self) -> ReadF32Le<&mut Self>where
Self: Unpin,
fn read_f32_le(&mut self) -> ReadF32Le<&mut Self>where
Self: Unpin,
Source§fn read_f64_le(&mut self) -> ReadF64Le<&mut Self>where
Self: Unpin,
fn read_f64_le(&mut self) -> ReadF64Le<&mut Self>where
Self: Unpin,
Source§fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>where
Self: Unpin,
buf. Read more