tcp_fsm.c File Reference

TCP finite state machine. More...

#include "core/net.h"
#include "core/ip.h"
#include "core/socket.h"
#include "core/tcp.h"
#include "core/tcp_fsm.h"
#include "core/tcp_misc.h"
#include "core/tcp_timer.h"
#include "ipv4/ipv4.h"
#include "ipv4/ipv4_misc.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

void tcpProcessSegment (NetInterface *interface, const IpPseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary)
 Incoming TCP segment processing. More...
 
void tcpStateClosed (NetInterface *interface, const IpPseudoHeader *pseudoHeader, const TcpHeader *segment, size_t length)
 CLOSED state. More...
 
void tcpStateListen (Socket *socket, NetInterface *interface, const IpPseudoHeader *pseudoHeader, const TcpHeader *segment, size_t length)
 LISTEN state. More...
 
void tcpStateSynSent (Socket *socket, const TcpHeader *segment, size_t length)
 SYN-SENT state. More...
 
void tcpStateSynReceived (Socket *socket, const TcpHeader *segment, const NetBuffer *buffer, size_t offset, size_t length)
 SYN-RECEIVED state. More...
 
void tcpStateEstablished (Socket *socket, const TcpHeader *segment, const NetBuffer *buffer, size_t offset, size_t length)
 ESTABLISHED state. More...
 
void tcpStateCloseWait (Socket *socket, const TcpHeader *segment, size_t length)
 CLOSE-WAIT state. More...
 
void tcpStateLastAck (Socket *socket, const TcpHeader *segment, size_t length)
 LAST-ACK state. More...
 
void tcpStateFinWait1 (Socket *socket, const TcpHeader *segment, const NetBuffer *buffer, size_t offset, size_t length)
 FIN-WAIT-1 state. More...
 
void tcpStateFinWait2 (Socket *socket, const TcpHeader *segment, const NetBuffer *buffer, size_t offset, size_t length)
 FIN-WAIT-2 state. More...
 
void tcpStateClosing (Socket *socket, const TcpHeader *segment, size_t length)
 CLOSING state. More...
 
void tcpStateTimeWait (Socket *socket, const TcpHeader *segment, size_t length)
 TIME-WAIT state. More...
 

Detailed Description

TCP finite state machine.

License

SPDX-License-Identifier: GPL-2.0-or-later

Copyright (C) 2010-2024 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.

Description

The TCP state machine progresses from one state to another in response to events (user calls, incoming segments and timeouts). This file describes the state transitions caused by incoming segments. Refer to the following RFCs for complete details:

  • RFC 793: Transmission Control Protocol
  • RFC 1122: Requirements for Internet Hosts - Communication Layers
Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.4.4

Definition in file tcp_fsm.c.

Macro Definition Documentation

◆ TRACE_LEVEL

#define TRACE_LEVEL   TCP_TRACE_LEVEL

Definition at line 41 of file tcp_fsm.c.

Function Documentation

◆ tcpProcessSegment()

void tcpProcessSegment ( NetInterface interface,
const IpPseudoHeader pseudoHeader,
const NetBuffer buffer,
size_t  offset,
const NetRxAncillary ancillary 
)

Incoming TCP segment processing.

Parameters
[in]interfaceUnderlying network interface
[in]pseudoHeaderTCP pseudo header
[in]bufferMulti-part buffer that holds the incoming TCP segment
[in]offsetOffset to the first byte of the TCP header
[in]ancillaryAdditional options passed to the stack along with the packet

Definition at line 73 of file tcp_fsm.c.

◆ tcpStateClosed()

void tcpStateClosed ( NetInterface interface,
const IpPseudoHeader pseudoHeader,
const TcpHeader segment,
size_t  length 
)

CLOSED state.

This is the default state that each connection starts in before the process of establishing it begins

Parameters
[in]interfaceUnderlying network interface
[in]pseudoHeaderTCP pseudo header
[in]segmentIncoming TCP segment
[in]lengthLength of the segment data

Definition at line 439 of file tcp_fsm.c.

◆ tcpStateCloseWait()

void tcpStateCloseWait ( Socket socket,
const TcpHeader segment,
size_t  length 
)

CLOSE-WAIT state.

The device has received a close request (FIN) from the other device. It must now wait for the application to acknowledge this request and generate a matching request

Parameters
[in]socketHandle referencing the current socket
[in]segmentIncoming TCP segment
[in]lengthLength of the segment data

Definition at line 986 of file tcp_fsm.c.

◆ tcpStateClosing()

void tcpStateClosing ( Socket socket,
const TcpHeader segment,
size_t  length 
)

CLOSING state.

The device has received a FIN from the other device and sent an ACK for it, but not yet received an ACK for its own FIN message

Parameters
[in]socketHandle referencing the current socket
[in]segmentIncoming TCP segment
[in]lengthLength of the segment data

Definition at line 1254 of file tcp_fsm.c.

◆ tcpStateEstablished()

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

ESTABLISHED state.

Data can be exchanged freely once both devices in the connection enter this state. This will continue until the connection is closed

Parameters
[in]socketHandle referencing the current socket
[in]segmentIncoming TCP segment
[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 900 of file tcp_fsm.c.

◆ tcpStateFinWait1()

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

FIN-WAIT-1 state.

A device in this state is waiting for an ACK for a FIN it has sent, or is waiting for a connection termination request from the other device

Parameters
[in]socketHandle referencing the current socket
[in]segmentIncoming TCP segment
[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 1094 of file tcp_fsm.c.

◆ tcpStateFinWait2()

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

FIN-WAIT-2 state.

A device in this state has received an ACK for its request to terminate the connection and is now waiting for a matching FIN from the other device

Parameters
[in]socketHandle referencing the current socket
[in]segmentIncoming TCP segment
[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 1185 of file tcp_fsm.c.

◆ tcpStateLastAck()

void tcpStateLastAck ( Socket socket,
const TcpHeader segment,
size_t  length 
)

LAST-ACK state.

A device that has already received a close request and acknowledged it, has sent its own FIN and is waiting for an ACK to this request

Parameters
[in]socketHandle referencing the current socket
[in]segmentIncoming TCP segment
[in]lengthLength of the segment data

Definition at line 1045 of file tcp_fsm.c.

◆ tcpStateListen()

void tcpStateListen ( Socket socket,
NetInterface interface,
const IpPseudoHeader pseudoHeader,
const TcpHeader segment,
size_t  length 
)

LISTEN state.

A device (normally a server) is waiting to receive a synchronize (SYN) message from a client. It has not yet sent its own SYN message

Parameters
[in]socketHandle referencing the current socket
[in]interfaceUnderlying network interface
[in]pseudoHeaderTCP pseudo header
[in]segmentIncoming TCP segment
[in]lengthLength of the segment data

Definition at line 467 of file tcp_fsm.c.

◆ tcpStateSynReceived()

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

SYN-RECEIVED state.

The device has both received a SYN from its partner and sent its own SYN. It is now waiting for an ACK to its SYN to finish connection setup

Parameters
[in]socketHandle referencing the current socket
[in]segmentIncoming TCP segment
[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 798 of file tcp_fsm.c.

◆ tcpStateSynSent()

void tcpStateSynSent ( Socket socket,
const TcpHeader segment,
size_t  length 
)

SYN-SENT state.

The device (normally a client) has sent a synchronize (SYN) message and is waiting for a matching SYN from the other device (usually a server)

Parameters
[in]socketHandle referencing the current socket
[in]segmentIncoming TCP segment
[in]lengthLength of the segment data

Definition at line 652 of file tcp_fsm.c.

◆ tcpStateTimeWait()

void tcpStateTimeWait ( Socket socket,
const TcpHeader segment,
size_t  length 
)

TIME-WAIT state.

The device has now received a FIN from the other device and acknowledged it, and sent its own FIN and received an ACK for it. We are done, except for waiting to ensure the ACK is received and prevent potential overlap with new connections

Parameters
[in]socketHandle referencing the current socket
[in]segmentIncoming TCP segment
[in]lengthLength of the segment data

Definition at line 1307 of file tcp_fsm.c.