TCP - Platform

This module includes the platform abstraction for TCP connections and listeners.

Summary

All APIs in this module are applicable only when OPENTHREAD_CONFIG_PLATFORM_TCP_ENABLE feature is enabled.

Enumerations

otPlatTcpDisconnectReason{
  OT_PLAT_TCP_DISCONNECT_REASON_CLOSED,
  OT_PLAT_TCP_DISCONNECT_REASON_TIMEOUT,
  OT_PLAT_TCP_DISCONNECT_REASON_REFUSED,
  OT_PLAT_TCP_DISCONNECT_REASON_RESET,
  OT_PLAT_TCP_DISCONNECT_REASON_ERROR
}
enum
Defines the reason for a TCP connection disconnection.

Typedefs

otPlatTcpConnection typedef
Represents a TCP connection.
otPlatTcpDisconnectReason typedef
Defines the reason for a TCP connection disconnection.
otPlatTcpListener typedef
Represents a TCP listener.
otPlatTcpSockAddr typedef
Represents a TCP socket address.

Functions

otPlatTcpAbort(otPlatTcpConnection *aConn)
void
Aborts the TCP connection.
otPlatTcpAccept(otPlatTcpListener *aListener, const otPlatTcpSockAddr *aPeerSockAddr)
Callback to accept an incoming TCP connection request on an active listener.
otPlatTcpClose(otPlatTcpConnection *aConn)
void
Gracefully closes the TCP connection.
otPlatTcpConnect(otPlatTcpConnection *aConn, const otPlatTcpSockAddr *aPeerSockAddr, const otPlatTcpSockAddr *aLocalSockAddr)
Initiates a TCP connection to a peer.
otPlatTcpDisableListener(otPlatTcpListener *aListener)
void
Disables a TCP listener.
otPlatTcpEnableListener(otPlatTcpListener *aListener, const otPlatTcpSockAddr *aLocalSockAddr)
Enables a TCP listener.
otPlatTcpGetInstanceForConnection(otPlatTcpConnection *aConn)
Gets the OpenThread instance associated with a given TCP connection.
otPlatTcpGetInstanceForListener(otPlatTcpListener *aListener)
Gets the OpenThread instance associated with a given TCP listener.
otPlatTcpHandleConnected(otPlatTcpConnection *aConn)
void
Callback to notify the connection establishment.
otPlatTcpHandleDisconnected(otPlatTcpConnection *aConn, otPlatTcpDisconnectReason aReason)
void
Callback to notify the connection disconnection.
otPlatTcpHandleReceive(otPlatTcpConnection *aConn, const uint8_t *aBuffer, uint16_t aLength)
void
Callback to notify the reception of data on a connection.
otPlatTcpHandleTxReady(otPlatTcpConnection *aConn)
void
Callback to notify that the platform is ready to accept more transmit data.
otPlatTcpIsConnecting(otPlatTcpConnection *aConn)
bool
Indicates whether the TCP connection is currently in the connecting state.
otPlatTcpIsTxPending(otPlatTcpConnection *aConn)
bool
Indicates whether the TCP connection has pending data for transmission.
otPlatTcpIterateConnections(otInstance *aInstance, otPlatTcpConnection *aPrevConn)
Iterates through the active TCP connections.
otPlatTcpIterateListeners(otInstance *aInstance, otPlatTcpListener *aPrevListener)
Iterates through the active TCP listeners.
otPlatTcpNotifyTxPending(otPlatTcpConnection *aConn)
void
Notifies the platform that there is pending data for transmission.
otPlatTcpSend(otPlatTcpConnection *aConn, const uint8_t *aBuffer, uint16_t aLength)
uint16_t
Sends data over an active TCP connection.

Structs

otPlatTcpConnection

Represents a TCP connection.

otPlatTcpListener

Represents a TCP listener.

otPlatTcpSockAddr

Represents a TCP socket address.

Unions

otPlatTcpPlatformData

Represents platform-specific data associated with a connection or a listener.

Enumerations

otPlatTcpDisconnectReason

 otPlatTcpDisconnectReason

Defines the reason for a TCP connection disconnection.

Properties
OT_PLAT_TCP_DISCONNECT_REASON_CLOSED

Connection was gracefully closed.

OT_PLAT_TCP_DISCONNECT_REASON_ERROR

Connection was aborted due to other errors.

OT_PLAT_TCP_DISCONNECT_REASON_REFUSED

Connection was refused by the peer (RST received during handshake).

OT_PLAT_TCP_DISCONNECT_REASON_RESET

Connection was reset by the peer (RST received on established conn).

OT_PLAT_TCP_DISCONNECT_REASON_TIMEOUT

Connection timed out (e.g., failed to connect or keepalive failure).

Typedefs

otPlatTcpConnection

struct otPlatTcpConnection otPlatTcpConnection

Represents a TCP connection.

The OpenThread core owns and manages the otPlatTcpConnection instances. The platform should track the pointers to these instances and pass them when invoking the otPlatTcpHandle* callbacks. The otPlatTcpConnection * can be viewed as a "descriptor" or "handle" to the connection.

The otPlatTcpConnection instance remains valid as long as the connection is active.

otPlatTcpDisconnectReason

enum otPlatTcpDisconnectReason otPlatTcpDisconnectReason

Defines the reason for a TCP connection disconnection.

otPlatTcpListener

struct otPlatTcpListener otPlatTcpListener

Represents a TCP listener.

The OpenThread core owns and manages the otPlatTcpListener instances. The platform should track the pointers to these instances and use them when invoking the callbacks. The otPlatTcpListener * can be viewed as a "descriptor" or "handle" to the listener.

otPlatTcpSockAddr

struct otPlatTcpSockAddr otPlatTcpSockAddr

Represents a TCP socket address.

Functions

otPlatTcpAbort

void otPlatTcpAbort(
  otPlatTcpConnection *aConn
)

Aborts the TCP connection.

This function forcefully terminates the connection. Any unsent data is discarded.

After this call, the platform must forget the aConn. Importantly, it must not invoke any callbacks using the aConn any longer, including otPlatTcpHandleDisconnected. This effectively indicates to the platform that the OpenThread core is de-allocating the aConn instance and it is no longer valid.

Details
Parameters
[in] aConn
The TCP connection.

otPlatTcpAccept

otPlatTcpConnection * otPlatTcpAccept(
  otPlatTcpListener *aListener,
  const otPlatTcpSockAddr *aPeerSockAddr
)

Callback to accept an incoming TCP connection request on an active listener.

This function is implemented and provided by the OpenThread stack for the platform to use.

The callback returns a pointer to an otPlatTcpConnection for the new connection. If the callback returns NULL, the incoming connection is rejected.

Details
Parameters
[in] aListener
The TCP listener.
[in] aPeerSockAddr
The peer's socket address.
Returns
A pointer for the newly accepted connection, or NULL to reject the connection request.

otPlatTcpClose

void otPlatTcpClose(
  otPlatTcpConnection *aConn
)

Gracefully closes the TCP connection.

This function initiates a graceful closure of the connection. The platform should transmit any remaining data before performing the standard TCP connection termination.

Once the connection is fully disconnected, or if it is already closed, or if an error occurs during the close operation, the platform must indicate this by invoking the otPlatTcpHandleDisconnected callback.

The platform must always call otPlatTcpHandleDisconnected() to report the final outcome of the connection. It is permissible for the platform implementation to invoke this callback directly from within otPlatTcpClose() before returning. The OpenThread stack will handle this correctly.

Details
Parameters
[in] aConn
The TCP connection.

otPlatTcpConnect

otError otPlatTcpConnect(
  otPlatTcpConnection *aConn,
  const otPlatTcpSockAddr *aPeerSockAddr,
  const otPlatTcpSockAddr *aLocalSockAddr
)

Initiates a TCP connection to a peer.

The platform should initiate a TCP connection to the aPeerSockAddr.

The aLocalSockAddr specifies the local address and port to bind to before connecting. It can be NULL if the OpenThread stack does not specify a preference. If provided, fields within aLocalSockAddr may still be left unspecified (e.g., the IP address can be all zeros, or the port can be zero). In all such cases, the platform and the underlying TCP stack should automatically select an appropriate local IP address and/or an ephemeral port.

If OT_ERROR_NONE is returned (indicating successful initialization of the connection process), the platform must subsequently report the status. Upon successful connection establishment, the platform must invoke the otPlatTcpHandleConnected callback. If it fails to establish the connection, the otPlatTcpHandleDisconnected callback must be called to indicate the failure.

Details
Parameters
[in] aConn
The TCP connection.
[in] aPeerSockAddr
The peer's socket address.
[in] aLocalSockAddr
The local socket address. Can be NULL.
Return Values
OT_ERROR_NONE
Successfully initiated the connection.
OT_ERROR_FAILED
Failed to initiate the connection.

otPlatTcpDisableListener

void otPlatTcpDisableListener(
  otPlatTcpListener *aListener
)

Disables a TCP listener.

The platform should stop listening for incoming connections on the socket associated with the listener. Any incoming connection requests that have not yet been accepted should be discarded.

Details
Parameters
[in] aListener
The TCP listener.

otPlatTcpEnableListener

otError otPlatTcpEnableListener(
  otPlatTcpListener *aListener,
  const otPlatTcpSockAddr *aLocalSockAddr
)

Enables a TCP listener.

The platform should start listening for incoming TCP connections on the provided aLocalSockAddr. When an incoming connection request is received, the platform must invoke the otPlatTcpAccept() callback to accept the request.

The aLocalSockAddr specifies the local interface, address, and port to bind to. Importantly, the port number within aLocalSockAddr must not be zero. The IP address may be unspecified (all zeros) to indicate that the listener should accept connections on any local address.

Details
Parameters
[in] aListener
The TCP listener.
[in] aLocalSockAddr
The local socket address to listen on.
Return Values
OT_ERROR_NONE
Successfully enabled or disabled the listener.
OT_ERROR_ALREADY
Already listening on the same port/address.
OT_ERROR_FAILED
Failed to enable the listener.

otPlatTcpGetInstanceForConnection

otInstance * otPlatTcpGetInstanceForConnection(
  otPlatTcpConnection *aConn
)

Gets the OpenThread instance associated with a given TCP connection.

This function is provided by OpenThread core. Platform implementations can use it to get the OpenThread instance associated with an active otPlatTcpConnection.

Details
Parameters
[in] aConn
The TCP connection.
Returns
The OpenThread instance.

otPlatTcpGetInstanceForListener

otInstance * otPlatTcpGetInstanceForListener(
  otPlatTcpListener *aListener
)

Gets the OpenThread instance associated with a given TCP listener.

This function is provided by OpenThread core. Platform implementations can use it to get the OpenThread instance associated with an active otPlatTcpListener.

Details
Parameters
[in] aListener
The TCP listener.
Returns
The OpenThread instance.

otPlatTcpHandleConnected

void otPlatTcpHandleConnected(
  otPlatTcpConnection *aConn
)

Callback to notify the connection establishment.

This callback is implemented and provided by the OpenThread stack. It must be invoked by the platform to indicate that the TCP handshake is complete and that the connection is now established.

The platform must call this after a successful call to otPlatTcpConnect() when the connection is established. For incoming connection requests (on an otPlatTcpListener), the platform must call this after the otPlatTcpAccept() callback returns successfully and when the connection is established.

Details
Parameters
[in] aConn
The TCP connection.

otPlatTcpHandleDisconnected

void otPlatTcpHandleDisconnected(
  otPlatTcpConnection *aConn,
  otPlatTcpDisconnectReason aReason
)

Callback to notify the connection disconnection.

This function is implemented and provided by the OpenThread stack for the platform to use.

This callback should be invoked by the platform when it fails to establish a connection, when an established connection is successfully closed (by both endpoints), when it times out, or is reset or aborted.

After this callback is invoked, the otPlatTcpConnection instance is no longer valid. The platform must not use it in any future callbacks.

Details
Parameters
[in] aConn
The TCP connection.
[in] aReason
The reason for the disconnection.

otPlatTcpHandleReceive

void otPlatTcpHandleReceive(
  otPlatTcpConnection *aConn,
  const uint8_t *aBuffer,
  uint16_t aLength
)

Callback to notify the reception of data on a connection.

This function is implemented and provided by the OpenThread stack for the platform to use.

The platform invokes this callback to provide received data to the OpenThread stack. The provided aBuffer only needs to remain valid for the duration of this call. The OpenThread stack will process and copy the bytes as needed, and will not retain the aBuffer pointer after the function returns.

Since TCP is a stream protocol, data can arrive in arbitrarily sized chunks. The platform does not need to buffer or reassemble these; it can invoke this callback immediately as data is received, even if it is expecting more data. The OpenThread stack handles all stream-level behavior, processing, and retention of the received data. This helps simplify the platform implementation.

On certain platforms (such as standard POSIX), a return value of 0 from read() or recv() indicates an End-of-File (EOF) or graceful closure by the peer. The platform implementation must check for this condition and report it by invoking otPlatTcpHandleDisconnected() with the reason set to OT_PLAT_TCP_DISCONNECT_REASON_CLOSED. Importantly, calling otPlatTcpHandleReceive() with aLength set to zero does not signify a graceful closure in the otPlatTcp APIs; such a call is treated as a no-op receive event by the OpenThread stack and is ignored.

Details
Parameters
[in] aConn
The TCP connection.
[in] aBuffer
A pointer to the buffer containing the received data. Must not be NULL if aLength > 0.
[in] aLength
The length (in bytes) of the received data.

otPlatTcpHandleTxReady

void otPlatTcpHandleTxReady(
  otPlatTcpConnection *aConn
)

Callback to notify that the platform is ready to accept more transmit data.

This function is implemented and provided by the OpenThread stack for the platform to use.

The platform should invoke this callback when it is ready to accept more data for transmission over the TCP connection, in response to a prior otPlatTcpNotifyTxPending() call. Upon being called, the OpenThread stack will use otPlatTcpSend() to provide the pending TX data to the platform. The stack may call otPlatTcpSend() multiple times during the execution of this callback.

Details
Parameters
[in] aConn
The TCP connection.

otPlatTcpIsConnecting

bool otPlatTcpIsConnecting(
  otPlatTcpConnection *aConn
)

Indicates whether the TCP connection is currently in the connecting state.

This function is provided by the OpenThread stack. The platform can use it to determine if a TCP connection is still waiting for the TCP handshake to complete.

Details
Parameters
[in] aConn
The TCP connection.
Return Values
TRUE
The connection is currently in the connecting state.
FALSE
The connection is not in the connecting state.

otPlatTcpIsTxPending

bool otPlatTcpIsTxPending(
  otPlatTcpConnection *aConn
)

Indicates whether the TCP connection has pending data for transmission.

This function is provided by the OpenThread stack. The platform can use it to check if there is any pending data for transmission over the TCP connection.

Details
Parameters
[in] aConn
The TCP connection.
Return Values
TRUE
The connection has pending transmit data.
FALSE
The connection does not have pending transmit data.

otPlatTcpIterateConnections

otPlatTcpConnection * otPlatTcpIterateConnections(
  otInstance *aInstance,
  otPlatTcpConnection *aPrevConn
)

Iterates through the active TCP connections.

This function can be used to iterate over all currently active TCP connections associated with the OpenThread instance. It allows platform implementations to process or manage connections without needing to maintain their own list of active connections.

The iteration is guaranteed to remain consistent and safe even if callbacks are invoked during the process. For example, if a connection is reported as disconnected via otPlatTcpHandleDisconnected during iteration, the OpenThread stack ensures that the connection entry remains valid until the iteration is completed.

Details
Parameters
[in] aInstance
The OpenThread instance.
[in] aPrevConn
A pointer to the previous connection, or NULL to start the iteration from the beginning.
Returns
A pointer to the next connection, or NULL if there are no more connections.

otPlatTcpIterateListeners

otPlatTcpListener * otPlatTcpIterateListeners(
  otInstance *aInstance,
  otPlatTcpListener *aPrevListener
)

Iterates through the active TCP listeners.

This function can be used to iterate over all currently active TCP listeners associated with the OpenThread instance. It allows platform implementations to process or manage listeners without needing to maintain their own list of active listeners.

The iteration is guaranteed to remain consistent even if callbacks (e.g., otPlatTcpAccept) are invoked during the process.

Details
Parameters
[in] aInstance
The OpenThread instance.
[in] aPrevListener
A pointer to the previous listener, or NULL to start the iteration from the beginning.
Returns
A pointer to the next listener, or NULL if there are no more listeners.

otPlatTcpNotifyTxPending

void otPlatTcpNotifyTxPending(
  otPlatTcpConnection *aConn
)

Notifies the platform that there is pending data for transmission.

This function is called by the OpenThread stack when it has new data for transmission. After this call, the platform should indicate when it is ready to accept the data by invoking the otPlatTcpHandleTxReady() callback.

The platform can also use otPlatTcpIsTxPending() to check if there is pending data for transmission.

It is permissible for the platform implementation to invoke the otPlatTcpHandleTxReady() callback directly from within otPlatTcpNotifyTxPending() before returning, if the underlying TCP transmit buffer is already available. The OpenThread stack will handle this correctly.

Details
Parameters
[in] aConn
The TCP connection.

otPlatTcpSend

uint16_t otPlatTcpSend(
  otPlatTcpConnection *aConn,
  const uint8_t *aBuffer,
  uint16_t aLength
)

Sends data over an active TCP connection.

This function is called by the OpenThread stack to provide data for the platform to transmit. The data is provided in a buffer. The platform should copy as much data as it can from the given buffer into its underlying platform transmit buffer.

The provided aBuffer is temporary. The platform must not store the pointer or assume the content remains valid after this function returns. All required data must be copied during this call.

The OpenThread stack typically invokes this function from the otPlatTcpHandleTxReady() callback. However, the platform implementation must not assume this and should support being called at any time. If there is no space available to accept any data, the platform can return zero.

The OpenThread stack may call this function multiple times back-to-back to provide all queued transmit content in chunks. The platform should be prepared to handle consecutive calls efficiently.

Details
Parameters
[in] aConn
The TCP connection.
[in] aBuffer
A pointer to the buffer containing the data to send.
[in] aLength
The length (in bytes) of the data in the buffer.
Returns
The actual number of bytes accepted for transmission.

Resources

OpenThread API Reference topics originate from the source code, available on GitHub. For more information, or to contribute to our documentation, refer to Resources.