Struct etherparse::TcpHeader

source ·
pub struct TcpHeader {
Show 17 fields pub source_port: u16, pub destination_port: u16, pub sequence_number: u32, pub acknowledgment_number: u32, pub ns: bool, pub fin: bool, pub syn: bool, pub rst: bool, pub psh: bool, pub ack: bool, pub urg: bool, pub ece: bool, pub cwr: bool, pub window_size: u16, pub checksum: u16, pub urgent_pointer: u16, pub options: TcpOptions,
}
Expand description

TCP header according to rfc 793.

Field descriptions copied from RFC 793 page 15++

Fields§

§source_port: u16

The source port number.

§destination_port: u16

The destination port number.

§sequence_number: u32

The sequence number of the first data octet in this segment (except when SYN is present).

If SYN is present the sequence number is the initial sequence number (ISN) and the first data octet is ISN+1. [copied from RFC 793, page 16]

§acknowledgment_number: u32

If the ACK control bit is set this field contains the value of the next sequence number the sender of the segment is expecting to receive.

Once a connection is established this is always sent.

§ns: bool

ECN-nonce - concealment protection (experimental: see RFC 3540)

§fin: bool

No more data from sender

§syn: bool

Synchronize sequence numbers

§rst: bool

Reset the connection

§psh: bool

Push Function

§ack: bool

Acknowledgment field significant

§urg: bool

Urgent Pointer field significant

§ece: bool

ECN-Echo (RFC 3168)

§cwr: bool

Congestion Window Reduced (CWR) flag

This flag is set by the sending host to indicate that it received a TCP segment with the ECE flag set and had responded in congestion control mechanism (added to header by RFC 3168).

§window_size: u16

The number of data octets beginning with the one indicated in the acknowledgment field which the sender of this segment is willing to accept.

§checksum: u16

Checksum (16 bit one’s complement) of the pseudo ip header, this tcp header and the payload.

§urgent_pointer: u16

This field communicates the current value of the urgent pointer as a positive offset from the sequence number in this segment.

The urgent pointer points to the sequence number of the octet following the urgent data. This field is only be interpreted in segments with the URG control bit set.

§options: TcpOptions

Options in the TCP header.

Implementations§

source§

impl TcpHeader

source

pub const MIN_LEN: usize = 20usize

Minimum length of a TCP header in bytes/octets.

source

pub const MAX_LEN: usize = 60usize

Maximum length of a TCP header in bytes/octets.

The length is obtained by multiplying the maximum value that “data offset” can take (it is a 4 bit number so the max is 0b1111) and multiplying it by 4 as it describes the offset to the data in 4-bytes words.

source

pub const MIN_DATA_OFFSET: u8 = 5u8

The minimum data offset size (size of the tcp header itself).

source

pub const MAX_DATA_OFFSET: u8 = 15u8

The maximum allowed value for the data offset (it is a 4 bit value).

source

pub fn new( source_port: u16, destination_port: u16, sequence_number: u32, window_size: u16 ) -> TcpHeader

Creates a TcpHeader with the given values and the rest initialized with default values.

source

pub fn data_offset(&self) -> u8

The number of 32 bit words in the TCP Header & TCP header options.

This indicates where the data begins relative to the start of an TCP header in multiples of 4 bytes. This number is present in the data_offset field of the header and defines the length of the tcp options present.

§Example
use etherparse::{TcpHeader, TcpOptions};

{
    let header = TcpHeader{
        options: TcpOptions::try_from_slice(&[]).unwrap(),
        .. Default::default()
    };
    // in case there are no options the minimum size of the tcp
    // is returned.
    assert_eq!(5, header.data_offset());
}
{
    let header = TcpHeader{
        options: TcpOptions::try_from_slice(&[1,2,3,4,5,6,7,8]).unwrap(),
        .. Default::default()
    };
    // otherwise the base TCP header size plus the number of 4 byte
    // words in the options is returned
    assert_eq!(5 + 2, header.data_offset());
}
source

pub fn header_len(&self) -> usize

Returns the length of the header including the options.

source

pub fn header_len_u16(&self) -> u16

Returns the length of the header including the options.

source

pub fn options_len(&self) -> usize

👎Deprecated since 0.14.0: Please use options.len() instead

Returns the options size in bytes based on the currently set data_offset. Returns None if the data_offset is smaller then the minimum size or bigger then the maximum supported size.

source

pub fn options(&self) -> &[u8]

👎Deprecated since 0.14.0: Please use options.as_slice() instead

Returns a slice containing the options of the header (size is determined via the data_offset field.

source

pub fn set_options( &mut self, elements: &[TcpOptionElement] ) -> Result<(), TcpOptionWriteError>

Sets the options (overwrites the current options) or returns an error when there is not enough space.

source

pub fn set_options_raw( &mut self, data: &[u8] ) -> Result<(), TcpOptionWriteError>

Sets the options to the data given.

source

pub fn options_iterator(&self) -> TcpOptionsIterator<'_>

Returns an iterator that allows to iterate through all known TCP header options.

source

pub fn read_from_slice( slice: &[u8] ) -> Result<(TcpHeader, &[u8]), HeaderSliceError>

👎Deprecated since 0.10.1: Use TcpHeader::from_slice instead.

Renamed to TcpHeader::from_slice

source

pub fn from_slice(slice: &[u8]) -> Result<(TcpHeader, &[u8]), HeaderSliceError>

Reads a tcp header from a slice

source

pub fn read<T: Read + Sized>( reader: &mut T ) -> Result<TcpHeader, HeaderReadError>

Available on crate feature std only.

Read a tcp header from the current position

source

pub fn write<T: Write + Sized>(&self, writer: &mut T) -> Result<(), Error>

Available on crate feature std only.

Write the tcp header to a stream (does NOT calculate the checksum).

source

pub fn to_bytes(&self) -> ArrayVec<u8, { TcpHeader::MAX_LEN }>

Returns the serialized header.

source

pub fn calc_checksum_ipv4( &self, ip_header: &Ipv4Header, payload: &[u8] ) -> Result<u16, ValueTooBigError<usize>>

Calculates the upd header checksum based on a ipv4 header and returns the result. This does NOT set the checksum.

source

pub fn calc_checksum_ipv4_raw( &self, source_ip: [u8; 4], destination_ip: [u8; 4], payload: &[u8] ) -> Result<u16, ValueTooBigError<usize>>

Calculates the checksum for the current header in ipv4 mode and returns the result. This does NOT set the checksum.

source

pub fn calc_checksum_ipv6( &self, ip_header: &Ipv6Header, payload: &[u8] ) -> Result<u16, ValueTooBigError<usize>>

Calculates the upd header checksum based on a ipv6 header and returns the result. This does NOT set the checksum..

source

pub fn calc_checksum_ipv6_raw( &self, source: [u8; 16], destination: [u8; 16], payload: &[u8] ) -> Result<u16, ValueTooBigError<usize>>

Calculates the checksum for the current header in ipv6 mode and returns the result. This does NOT set the checksum.

Trait Implementations§

source§

impl Clone for TcpHeader

source§

fn clone(&self) -> TcpHeader

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for TcpHeader

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for TcpHeader

source§

fn default() -> TcpHeader

Returns the “default value” for a type. Read more
source§

impl Hash for TcpHeader

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for TcpHeader

source§

fn eq(&self, other: &TcpHeader) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for TcpHeader

source§

impl StructuralPartialEq for TcpHeader

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.