tcp_misc.c File Reference

Helper functions for TCP. More...

#include <stdlib.h>
#include <string.h>
#include "core/net.h"
#include "core/socket.h"
#include "core/tcp.h"
#include "core/tcp_misc.h"
#include "core/tcp_timer.h"
#include "core/ip.h"
#include "ipv4/ipv4.h"
#include "ipv6/ipv6.h"
#include "mibs/mib2_module.h"
#include "mibs/tcp_mib_module.h"
#include "date_time.h"
#include "debug.h"

Go to the source code of this file.

Macros

#define TRACE_LEVEL   TCP_TRACE_LEVEL
 

Functions

error_t tcpSendSegment (Socket *socket, uint8_t flags, uint32_t seqNum, uint32_t ackNum, size_t length, bool_t addToQueue)
 Send a TCP segment. More...
 
error_t tcpSendResetSegment (NetInterface *interface, IpPseudoHeader *pseudoHeader, TcpHeader *segment, size_t length)
 Send a TCP reset in response to an invalid segment. More...
 
error_t tcpAddOption (TcpHeader *segment, uint8_t kind, const void *value, uint8_t length)
 Append an option to a TCP segment. More...
 
TcpOptiontcpGetOption (TcpHeader *segment, uint8_t kind)
 Find a specified option in a TCP segment. More...
 
error_t tcpCheckSequenceNumber (Socket *socket, TcpHeader *segment, size_t length)
 Test the sequence number of an incoming segment. More...
 
error_t tcpCheckSyn (Socket *socket, TcpHeader *segment, size_t length)
 Check the SYN bit of an incoming segment. More...
 
error_t tcpCheckAck (Socket *socket, TcpHeader *segment, size_t length)
 Test the ACK field of an incoming segment. More...
 
bool_t tcpIsDuplicateSyn (Socket *socket, IpPseudoHeader *pseudoHeader, TcpHeader *segment)
 Test whether the incoming SYN segment is a duplicate. More...
 
bool_t tcpIsDuplicateAck (Socket *socket, TcpHeader *segment, size_t length)
 Test whether the incoming acknowledgment is a duplicate. More...
 
void tcpFastRetransmit (Socket *socket)
 Fast retransmit procedure. More...
 
void tcpFastRecovery (Socket *socket, TcpHeader *segment, uint_t n)
 Fast recovery procedure. More...
 
void tcpFastLossRecovery (Socket *socket, TcpHeader *segment)
 Fast loss recovery procedure. More...
 
void tcpProcessSegmentData (Socket *socket, TcpHeader *segment, const NetBuffer *buffer, size_t offset, size_t length)
 Process the segment text. More...
 
void tcpDeleteControlBlock (Socket *socket)
 Delete TCB structure. More...
 
void tcpUpdateRetransmitQueue (Socket *socket)
 Remove acknowledged segments from retransmission queue. More...
 
void tcpFlushRetransmitQueue (Socket *socket)
 Flush retransmission queue. More...
 
void tcpFlushSynQueue (Socket *socket)
 Flush SYN queue. More...
 
void tcpUpdateSackBlocks (Socket *socket, uint32_t *leftEdge, uint32_t *rightEdge)
 Update the list of non-contiguous blocks that have been received. More...
 
void tcpUpdateSendWindow (Socket *socket, TcpHeader *segment)
 Update send window. More...
 
void tcpUpdateReceiveWindow (Socket *socket)
 Update receive window so as to avoid Silly Window Syndrome. More...
 
bool_t tcpComputeRto (Socket *socket)
 Compute retransmission timeout. More...
 
error_t tcpRetransmitSegment (Socket *socket)
 TCP segment retransmission. More...
 
error_t tcpNagleAlgo (Socket *socket, uint_t flags)
 Nagle algorithm implementation. More...
 
void tcpChangeState (Socket *socket, TcpState newState)
 Update TCP FSM current state. More...
 
void tcpUpdateEvents (Socket *socket)
 Update TCP related events. More...
 
uint_t tcpWaitForEvents (Socket *socket, uint_t eventMask, systime_t timeout)
 Wait for a particular TCP event. More...
 
void tcpWriteTxBuffer (Socket *socket, uint32_t seqNum, const uint8_t *data, size_t length)
 Copy incoming data to the send buffer. More...
 
error_t tcpReadTxBuffer (Socket *socket, uint32_t seqNum, NetBuffer *buffer, size_t length)
 Copy data from the send buffer. More...
 
void tcpWriteRxBuffer (Socket *socket, uint32_t seqNum, const NetBuffer *data, size_t dataOffset, size_t length)
 Copy incoming data to the receive buffer. More...
 
void tcpReadRxBuffer (Socket *socket, uint32_t seqNum, uint8_t *data, size_t length)
 Copy data from the receive buffer. More...
 
void tcpDumpHeader (const TcpHeader *segment, size_t length, uint32_t iss, uint32_t irs)
 Dump TCP header for debugging purpose. More...
 

Detailed Description

Helper functions for TCP.

License

Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneTCP Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
1.9.0

Definition in file tcp_misc.c.

Macro Definition Documentation

◆ TRACE_LEVEL

#define TRACE_LEVEL   TCP_TRACE_LEVEL

Definition at line 30 of file tcp_misc.c.

Function Documentation

◆ tcpAddOption()

error_t tcpAddOption ( TcpHeader segment,
uint8_t  kind,
const void *  value,
uint8_t  length 
)

Append an option to a TCP segment.

Parameters
[in]segmentPointer to the TCP header
[in]kindOption code
[in]valueOption value
[in]lengthLength of the option value
Returns
Error code

Definition at line 419 of file tcp_misc.c.

◆ tcpChangeState()

void tcpChangeState ( Socket socket,
TcpState  newState 
)

Update TCP FSM current state.

Parameters
[in]socketHandle referencing the socket
[in]newStateNew TCP state to switch to

Definition at line 1723 of file tcp_misc.c.

◆ tcpCheckAck()

error_t tcpCheckAck ( Socket socket,
TcpHeader segment,
size_t  length 
)

Test the ACK field of an incoming segment.

Parameters
[in]socketHandle referencing the current socket
[in]segmentPointer to the TCP segment to check
[in]lengthLength of the segment data
Returns
NO_ERROR if the acknowledgment is acceptable, ERROR_FAILURE otherwise

Definition at line 626 of file tcp_misc.c.

◆ tcpCheckSequenceNumber()

error_t tcpCheckSequenceNumber ( Socket socket,
TcpHeader segment,
size_t  length 
)

Test the sequence number of an incoming segment.

Parameters
[in]socketHandle referencing the current socket
[in]segmentPointer to the TCP segment to check
[in]lengthLength of the segment data
Returns
NO_ERROR if the incoming segment is acceptable, ERROR_FAILURE otherwise

Definition at line 523 of file tcp_misc.c.

◆ tcpCheckSyn()

error_t tcpCheckSyn ( Socket socket,
TcpHeader segment,
size_t  length 
)

Check the SYN bit of an incoming segment.

Parameters
[in]socketHandle referencing the current socket
[in]segmentPointer to the TCP segment to check
[in]lengthLength of the segment data
Returns
ERROR_FAILURE if the SYN is in the window, NO_ERROR otherwise

Definition at line 592 of file tcp_misc.c.

◆ tcpComputeRto()

bool_t tcpComputeRto ( Socket socket)

Compute retransmission timeout.

Parameters
[in]socketHandle referencing the socket
Returns
TRUE if the RTT measurement is complete, else FALSE

Definition at line 1422 of file tcp_misc.c.

◆ tcpDeleteControlBlock()

void tcpDeleteControlBlock ( Socket socket)

Delete TCB structure.

Parameters
[in]socketHandle referencing the socket

Definition at line 1128 of file tcp_misc.c.

◆ tcpDumpHeader()

void tcpDumpHeader ( const TcpHeader segment,
size_t  length,
uint32_t  iss,
uint32_t  irs 
)

Dump TCP header for debugging purpose.

Parameters
[in]segmentPointer to the TCP header
[in]lengthLength of the segment data
[in]issInitial send sequence number (needed to compute relative SEQ number)
[in]irsInitial receive sequence number (needed to compute relative ACK number)

Definition at line 2065 of file tcp_misc.c.

◆ tcpFastLossRecovery()

void tcpFastLossRecovery ( Socket socket,
TcpHeader segment 
)

Fast loss recovery procedure.

Parameters
[in]socketHandle referencing the current socket
[in]segmentPointer to the incoming TCP segment

Definition at line 1023 of file tcp_misc.c.

◆ tcpFastRecovery()

void tcpFastRecovery ( Socket socket,
TcpHeader segment,
uint_t  n 
)

Fast recovery procedure.

Parameters
[in]socketHandle referencing the current socket
[in]segmentPointer to the incoming TCP segment
[in]nNumber of bytes acknowledged by the incoming ACK

Definition at line 974 of file tcp_misc.c.

◆ tcpFastRetransmit()

void tcpFastRetransmit ( Socket socket)

Fast retransmit procedure.

Parameters
[in]socketHandle referencing the current socket

Definition at line 935 of file tcp_misc.c.

◆ tcpFlushRetransmitQueue()

void tcpFlushRetransmitQueue ( Socket socket)

Flush retransmission queue.

Parameters
[in]socketHandle referencing the socket

Definition at line 1225 of file tcp_misc.c.

◆ tcpFlushSynQueue()

void tcpFlushSynQueue ( Socket socket)

Flush SYN queue.

Parameters
[in]socketHandle referencing the socket

Definition at line 1254 of file tcp_misc.c.

◆ tcpGetOption()

TcpOption* tcpGetOption ( TcpHeader segment,
uint8_t  kind 
)

Find a specified option in a TCP segment.

Parameters
[in]segmentPointer to the TCP header
[in]kindCode of the option to find
Returns
If the specified option is found, a pointer to the corresponding option is returned. Otherwise NULL pointer is returned

Definition at line 467 of file tcp_misc.c.

◆ tcpIsDuplicateAck()

bool_t tcpIsDuplicateAck ( Socket socket,
TcpHeader segment,
size_t  length 
)

Test whether the incoming acknowledgment is a duplicate.

Parameters
[in]socketHandle referencing the current socket
[in]segmentPointer to the TCP segment to check
[in]lengthLength of the segment data
Returns
TRUE if the ACK is duplicate, else FALSE

Definition at line 893 of file tcp_misc.c.

◆ tcpIsDuplicateSyn()

bool_t tcpIsDuplicateSyn ( Socket socket,
IpPseudoHeader pseudoHeader,
TcpHeader segment 
)

Test whether the incoming SYN segment is a duplicate.

Parameters
[in]socketHandle referencing the current socket
[in]pseudoHeaderTCP pseudo header
[in]segmentPointer to the TCP segment to check
Returns
TRUE if the SYN segment is duplicate, else FALSE

Definition at line 817 of file tcp_misc.c.

◆ tcpNagleAlgo()

error_t tcpNagleAlgo ( Socket socket,
uint_t  flags 
)

Nagle algorithm implementation.

Parameters
[in]socketHandle referencing the socket
[in]flagsSet of flags that influences the behavior of this function
Returns
Error code

Definition at line 1594 of file tcp_misc.c.

◆ tcpProcessSegmentData()

void tcpProcessSegmentData ( Socket socket,
TcpHeader segment,
const NetBuffer buffer,
size_t  offset,
size_t  length 
)

Process the segment text.

Parameters
[in]socketHandle referencing the current socket
[in]segmentPointer to the TCP header
[in]bufferMulti-part buffer containing the incoming TCP segment
[in]offsetOffset to the first data byte
[in]lengthLength of the segment data

Definition at line 1061 of file tcp_misc.c.

◆ tcpReadRxBuffer()

void tcpReadRxBuffer ( Socket socket,
uint32_t  seqNum,
uint8_t *  data,
size_t  length 
)

Copy data from the receive buffer.

Parameters
[in]socketHandle referencing the socket
[in]seqNumSequence number of the first data to read
[out]dataPointer to the output buffer
[in]lengthNumber of data to read

Definition at line 2032 of file tcp_misc.c.

◆ tcpReadTxBuffer()

error_t tcpReadTxBuffer ( Socket socket,
uint32_t  seqNum,
NetBuffer buffer,
size_t  length 
)

Copy data from the send buffer.

Parameters
[in]socketHandle referencing the socket
[in]seqNumSequence number of the first data to read
[out]bufferPointer to the output buffer
[in]lengthNumber of data to read
Returns
Error code

Definition at line 1955 of file tcp_misc.c.

◆ tcpRetransmitSegment()

error_t tcpRetransmitSegment ( Socket socket)

TCP segment retransmission.

Parameters
[in]socketHandle referencing the socket
Returns
Error code

Definition at line 1491 of file tcp_misc.c.

◆ tcpSendResetSegment()

error_t tcpSendResetSegment ( NetInterface interface,
IpPseudoHeader pseudoHeader,
TcpHeader segment,
size_t  length 
)

Send a TCP reset in response to an invalid segment.

Parameters
[in]interfaceUnderlying network interface
[in]pseudoHeaderTCP pseudo header describing the incoming segment
[in]segmentIncoming TCP segment
[in]lengthLength of the incoming segment data
Returns
Error code

Definition at line 283 of file tcp_misc.c.

◆ tcpSendSegment()

error_t tcpSendSegment ( Socket socket,
uint8_t  flags,
uint32_t  seqNum,
uint32_t  ackNum,
size_t  length,
bool_t  addToQueue 
)

Send a TCP segment.

Parameters
[in]socketHandle referencing a socket
[in]flagsValue that contains bitwise OR of flags (see TcpFlags enumeration)
[in]seqNumSequence number
[in]ackNumAcknowledgment number
[in]lengthLength of the segment data
[in]addToQueueAdd the segment to retransmission queue
Returns
Error code

Definition at line 63 of file tcp_misc.c.

◆ tcpUpdateEvents()

void tcpUpdateEvents ( Socket socket)

Update TCP related events.

Parameters
[in]socketHandle referencing the socket

Definition at line 1754 of file tcp_misc.c.

◆ tcpUpdateReceiveWindow()

void tcpUpdateReceiveWindow ( Socket socket)

Update receive window so as to avoid Silly Window Syndrome.

Parameters
[in]socketHandle referencing the socket

Definition at line 1385 of file tcp_misc.c.

◆ tcpUpdateRetransmitQueue()

void tcpUpdateRetransmitQueue ( Socket socket)

Remove acknowledged segments from retransmission queue.

Parameters
[in]socketHandle referencing the socket

Definition at line 1149 of file tcp_misc.c.

◆ tcpUpdateSackBlocks()

void tcpUpdateSackBlocks ( Socket socket,
uint32_t *  leftEdge,
uint32_t *  rightEdge 
)

Update the list of non-contiguous blocks that have been received.

Parameters
[in]socketHandle referencing the socket
[in,out]leftEdgeFirst sequence number occupied by the incoming data
[in,out]rightEdgeSequence number immediately following the incoming data

Definition at line 1282 of file tcp_misc.c.

◆ tcpUpdateSendWindow()

void tcpUpdateSendWindow ( Socket socket,
TcpHeader segment 
)

Update send window.

Parameters
[in]socketHandle referencing the socket
[in]segmentPointer to the incoming TCP segment

Definition at line 1335 of file tcp_misc.c.

◆ tcpWaitForEvents()

uint_t tcpWaitForEvents ( Socket socket,
uint_t  eventMask,
systime_t  timeout 
)

Wait for a particular TCP event.

Parameters
[in]socketHandle referencing the socket
[in]eventMaskLogic OR of all the TCP events that will complete the wait
[in]timeoutMaximum time to wait
Returns
Logic OR of all the TCP events that satisfied the wait

Definition at line 1883 of file tcp_misc.c.

◆ tcpWriteRxBuffer()

void tcpWriteRxBuffer ( Socket socket,
uint32_t  seqNum,
const NetBuffer data,
size_t  dataOffset,
size_t  length 
)

Copy incoming data to the receive buffer.

Parameters
[in]socketHandle referencing the socket
[in]seqNumFirst sequence number occupied by the incoming data
[in]dataMulti-part buffer containing the incoming data
[in]dataOffsetOffset to the first data byte
[in]lengthNumber of data to write

Definition at line 1999 of file tcp_misc.c.

◆ tcpWriteTxBuffer()

void tcpWriteTxBuffer ( Socket socket,
uint32_t  seqNum,
const uint8_t *  data,
size_t  length 
)

Copy incoming data to the send buffer.

Parameters
[in]socketHandle referencing the socket
[in]seqNumFirst sequence number occupied by the incoming data
[in]dataData to write
[in]lengthNumber of data to write

Definition at line 1921 of file tcp_misc.c.