use std::io::{ErrorKind, Read, Write};
use std::net::SocketAddr;
use std::time::{Duration, Instant};
use crate::config::limits::CONNECT_PROBE_TIMEOUT;
use crate::fingerprint::Tunnel;
use crate::protocols::http::message_end as http_message_end;
use crate::system::descriptors;
use crate::transport::dial::Egress;
const LARGEST_DATAGRAM: u64 = 65_535;
pub(crate) struct Reply {
pub(crate) bytes: Vec<u8>,
pub(crate) complete: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ExchangeError {
TimedOut,
ConnectionRefused,
Reset,
Starved,
}
impl ExchangeError {
fn of(error: &std::io::Error) -> Self {
if descriptors::exhausted(error) {
return Self::Starved;
}
match error.kind() {
ErrorKind::TimedOut | ErrorKind::WouldBlock => Self::TimedOut,
ErrorKind::ConnectionRefused => Self::ConnectionRefused,
_ => Self::Reset,
}
}
}
pub(crate) fn remaining(deadline: Instant) -> Option<Duration> {
deadline
.checked_duration_since(Instant::now())
.filter(|left| !left.is_zero())
}
pub(crate) fn tcp(
addr: SocketAddr,
egress: Egress,
tunnel: Option<Tunnel>,
bytes: &[u8],
deadline: Instant,
cap: u64,
) -> Result<Reply, ExchangeError> {
let left = remaining(deadline).ok_or(ExchangeError::TimedOut)?;
let tcp = egress
.connect_within(addr, left.min(CONNECT_PROBE_TIMEOUT), left)
.map_err(|error| ExchangeError::of(&error))?;
tcp.set_read_timeout(Some(remaining(deadline).ok_or(ExchangeError::TimedOut)?))
.map_err(|error| ExchangeError::of(&error))?;
let mut stream = super::tls::wrap(tcp, addr.ip(), tunnel).ok_or(ExchangeError::Reset)?;
let sent = Instant::now();
stream
.write_all(bytes)
.map_err(|error| ExchangeError::of(&error))?;
let mut reply = Vec::new();
let mut buffer = [0u8; 4096];
let mut whole = false;
let mut gap = None;
while (reply.len() as u64) < cap {
let want = ((cap - reply.len() as u64) as usize).min(buffer.len());
match stream.read(&mut buffer[..want]) {
Ok(0) => {
whole = true;
break;
}
Ok(read) => {
reply.extend_from_slice(&buffer[..read]);
if http_message_end(&reply).is_some() {
whole = true;
break;
}
let Some(left) = remaining(deadline) else {
break;
};
let gap = *gap.get_or_insert_with(|| idle_gap(sent.elapsed(), left));
if stream
.socket()
.set_read_timeout(Some(gap.min(left)))
.is_err()
{
break;
}
}
Err(_) => break,
}
}
Ok(Reply {
bytes: reply,
complete: whole,
})
}
fn idle_gap(first_byte: Duration, left: Duration) -> Duration {
const FLOOR: Duration = Duration::from_millis(300);
first_byte.saturating_mul(2).max(left / 4).max(FLOOR)
}
pub(crate) fn udp(
addr: SocketAddr,
egress: Egress,
bytes: &[u8],
deadline: Instant,
cap: u64,
) -> Result<Reply, ExchangeError> {
let left = remaining(deadline).ok_or(ExchangeError::TimedOut)?;
let socket = egress
.udp_blocking(addr.ip(), left)
.map_err(|error| ExchangeError::of(&error))?;
socket
.connect(addr)
.map_err(|error| ExchangeError::of(&error))?;
socket
.set_read_timeout(Some(remaining(deadline).ok_or(ExchangeError::TimedOut)?))
.map_err(|error| ExchangeError::of(&error))?;
socket
.send(bytes)
.map_err(|error| ExchangeError::of(&error))?;
let mut buffer = vec![0u8; cap.min(LARGEST_DATAGRAM) as usize];
match socket.recv(&mut buffer) {
Ok(read) => {
buffer.truncate(read);
Ok(Reply {
bytes: buffer,
complete: true,
})
}
Err(error)
if error.kind() == ErrorKind::TimedOut || error.kind() == ErrorKind::WouldBlock =>
{
Ok(Reply {
bytes: Vec::new(),
complete: true,
})
}
Err(error) => Err(ExchangeError::of(&error)),
}
}
#[cfg(test)]
mod tests {
use super::{http_message_end, idle_gap};
use std::time::Duration;
#[cfg(unix)]
#[test]
fn an_exchange_holds_one_descriptor_and_goes_through_on_a_table_with_room_for_it() {
use crate::system::descriptors::testing::{exhaust, in_a_process_of_its_own};
use std::io::{Read, Write};
use std::time::Instant;
if !in_a_process_of_its_own(
module_path!(),
"an_exchange_holds_one_descriptor_and_goes_through_on_a_table_with_room_for_it",
) {
return;
}
const REPLY: &[u8] = b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok";
let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("a loopback listener");
let addr = listener.local_addr().expect("its address");
let server = std::thread::spawn(move || {
let (mut stream, _) = loop {
match listener.accept() {
Ok(accepted) => break accepted,
Err(_) => std::thread::sleep(Duration::from_millis(10)),
}
};
let mut request = Vec::new();
let mut buffer = [0u8; 256];
while !request.ends_with(b"\r\n\r\n") {
match stream.read(&mut buffer) {
Ok(0) | Err(_) => return,
Ok(read) => request.extend_from_slice(&buffer[..read]),
}
}
let _ = stream.write_all(REPLY);
std::thread::sleep(Duration::from_secs(2));
});
let mut held = exhaust(64);
held.truncate(held.len() - 2);
let reply = super::tcp(
addr,
crate::transport::dial::Egress::KERNEL,
None,
b"GET / HTTP/1.1\r\n\r\n",
Instant::now() + Duration::from_secs(5),
4096,
)
.map(|reply| reply.bytes);
assert_eq!(
reply.as_deref(),
Ok(REPLY),
"the exchange needed more than its one socket"
);
drop(held);
let _ = server.join();
}
#[test]
fn the_idle_gap_is_the_largest_of_its_three_terms() {
let ms = Duration::from_millis;
assert_eq!(idle_gap(ms(900), ms(2_000)), ms(1_800));
assert_eq!(idle_gap(ms(1), ms(3_000)), ms(750));
assert_eq!(idle_gap(ms(1), ms(400)), ms(300));
}
#[test]
fn a_response_with_a_length_ends_once_that_many_body_bytes_are_in() {
let whole = b"HTTP/1.1 404 Not Found\r\nContent-Length: 9\r\n\r\nnot found";
assert_eq!(http_message_end(whole), Some(whole.len()));
assert_eq!(http_message_end(&whole[..whole.len() - 1]), None);
assert_eq!(
http_message_end(b"HTTP/1.1 404 Not Found\r\nContent-Le"),
None
);
}
#[test]
fn header_names_are_matched_whatever_their_case() {
let whole = b"HTTP/1.0 200 OK\r\ncontent-length:2\r\n\r\nok";
assert_eq!(http_message_end(whole), Some(whole.len()));
}
#[test]
fn a_chunked_response_ends_after_its_closing_chunk_and_trailers() {
let whole =
b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n4;x=y\r\nwiki\r\n0\r\n\r\n";
assert_eq!(http_message_end(whole), Some(whole.len()));
assert_eq!(http_message_end(&whole[..whole.len() - 2]), None);
let trailed = b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n0\r\nX-T: 1\r\n\r\n";
assert_eq!(http_message_end(trailed), Some(trailed.len()));
}
#[test]
fn chunked_framing_outranks_a_length_given_beside_it() {
let reply = b"HTTP/1.1 200 OK\r\nContent-Length: 3\r\nTransfer-Encoding: chunked\r\n\r\n3\r\nabc\r\n";
assert_eq!(http_message_end(reply), None);
}
#[test]
fn a_status_that_carries_no_body_ends_with_its_headers() {
let reply = b"HTTP/1.1 304 Not Modified\r\nContent-Length: 1234\r\n\r\n";
assert_eq!(http_message_end(reply), Some(reply.len()));
}
#[test]
fn a_reply_that_does_not_say_where_it_ends_is_left_to_the_close() {
for reply in [
&b"HTTP/1.1 200 OK\r\nServer: x\r\n\r\nbody until close"[..],
b"HTTP/1.1 100 Continue\r\n\r\n",
b"HTTP/1.1 200 OK\r\nContent-Length: 3\r\nContent-Length: 4\r\n\r\nabcd",
b"HTTP/1.1 200 OK\r\nContent-Length: many\r\n\r\nabcd",
b"+OK redis\r\n\r\n",
b"SSH-2.0-OpenSSH_9.6\r\n",
] {
assert_eq!(
http_message_end(reply),
None,
"{:?}",
String::from_utf8_lossy(reply)
);
}
}
}