Skip to content

Commit

Permalink
ip-echo-server: Add helper to compute reply length
Browse files Browse the repository at this point in the history
  • Loading branch information
t-nelson authored and mergify[bot] committed Nov 12, 2020
1 parent aab5f24 commit 7481ba5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 2 additions & 6 deletions net-utils/src/ip_echo_server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::HEADER_LENGTH;
use crate::{ip_echo_server_reply_length, HEADER_LENGTH};
use bytes::Bytes;
use log::*;
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -177,11 +177,7 @@ pub fn ip_echo_server(tcp: std::net::TcpListener) -> IpEchoServer {
} else {
// "\0\0\0\0" header is added to ensure a valid response will never
// conflict with the first four bytes of a valid HTTP response.
let mut bytes = vec![
0;
HEADER_LENGTH + bincode::serialized_size(&peer_addr.ip()).unwrap()
as usize
];
let mut bytes = vec![0u8; ip_echo_server_reply_length()];
bincode::serialize_into(&mut bytes[HEADER_LENGTH..], &peer_addr.ip())
.unwrap();
Ok(Bytes::from(bytes))
Expand Down
4 changes: 4 additions & 0 deletions net-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub struct UdpSocketPair {
pub type PortRange = (u16, u16);

pub(crate) const HEADER_LENGTH: usize = 4;
pub(crate) fn ip_echo_server_reply_length() -> usize {
let largest_ip_addr = IpAddr::from([0u16; 8]); // IPv6 variant
HEADER_LENGTH + bincode::serialized_size(&largest_ip_addr).unwrap() as usize
}

fn ip_echo_server_request(
ip_echo_server_addr: &SocketAddr,
Expand Down

0 comments on commit 7481ba5

Please sign in to comment.