Ensure IpEchoServerMessage is not fragmented (#7214)

automerge
This commit is contained in:
Michael Vines 2019-12-02 23:32:43 -07:00 committed by Grimes
parent 5ac435325b
commit d38f3f664f
2 changed files with 20 additions and 6 deletions

View File

@ -70,6 +70,19 @@ pub fn ip_echo_server(tcp: std::net::TcpListener) -> IpEchoServer {
));
}
let expected_len =
bincode::serialized_size(&IpEchoServerMessage::default()).unwrap() as usize;
let actual_len = data[4..].len();
if actual_len < expected_len {
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"Request too short, actual {} < expected {}",
actual_len, expected_len
),
));
}
bincode::deserialize::<IpEchoServerMessage>(&data[4..])
.map(Some)
.or_else(|err| {

View File

@ -29,14 +29,15 @@ fn ip_echo_server_request(
let timeout = Duration::new(5, 0);
TcpStream::connect_timeout(ip_echo_server_addr, timeout)
.and_then(|mut stream| {
let msg = bincode::serialize(&msg).expect("serialize IpEchoServerMessage");
// Start with 4 null bytes to avoid looking like an HTTP GET/POST request
stream.write_all(&[0; 4])?;
let mut bytes = vec![0; 4]; // Start with 4 null bytes to avoid looking like an HTTP GET/POST request
stream.write_all(&msg)?;
bytes.append(&mut bincode::serialize(&msg).expect("serialize IpEchoServerMessage"));
// Send a '\n' to make this request look HTTP-ish and tickle an error response back from an HTTP server
stream.write_all(b"\n")?;
// End with '\n' to make this request look HTTP-ish and tickle an error response back
// from an HTTP server
bytes.push(b'\n');
stream.write_all(&bytes)?;
stream.shutdown(std::net::Shutdown::Write)?;
stream
.set_read_timeout(Some(Duration::new(10, 0)))