Skip to content

Commit

Permalink
Added PacketBuilder::tcp_header()
Browse files Browse the repository at this point in the history
Allows caller to fux with the full tcp_header and not just
the four parameters that go into TcpHeader::new().

Doc code passes test.
  • Loading branch information
robs-zeynet committed Jul 4, 2023
1 parent 5078f13 commit aca4242
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion etherparse/src/packet_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ impl PacketBuilderStep<IpHeader> {
}
}

/// Adds an TCP header.
/// Adds a simple TCP header.
///
/// # Example
///
Expand Down Expand Up @@ -1260,6 +1260,56 @@ impl PacketBuilderStep<IpHeader> {
}
}

/// Adds a more complicated TCP header.
///
/// # Example
///
/// Basic usage:
///
/// ```
/// # use etherparse::PacketBuilder;
/// use etherparse::TcpHeader;
///
/// let mut tcp_header = TcpHeader::new(
/// 21, // source port
/// 12, // destination port
/// 12345, // sequence number
/// 4000, // window size
/// );
/// tcp_header.psh = true;
/// tcp_header.ack = true;
/// tcp_header.acknowledgment_number = 1;
///
/// let builder = PacketBuilder::
/// ethernet2([1,2,3,4,5,6], // source mac
/// [7,8,9,10,11,12]) // destionation mac
/// .ipv4([192,168,1,1], // source ip
/// [192,168,1,2], // desitionation ip
/// 20) // time to life
/// .tcp_header(tcp_header);
///
/// //payload of the udp packet
/// let payload = [1,2,3,4,5,6,7,8];
///
/// //get some memory to store the result
/// let mut result = Vec::<u8>::with_capacity(
/// builder.size(payload.len()));
///
/// //serialize
/// builder.write(&mut result, &payload).unwrap();
/// ```
pub fn tcp_header(
mut self,
tcp_header: TcpHeader,
) -> PacketBuilderStep<TcpHeader> {
self.state.transport_header = Some(TransportHeader::Tcp(tcp_header));
//return for next step
PacketBuilderStep {
state: self.state,
_marker: marker::PhantomData::<TcpHeader> {},
}
}

/// Write all the headers and the payload with the given ip number.
///
/// `last_next_header_ip_number` will be set in the last extension header
Expand Down

0 comments on commit aca4242

Please sign in to comment.