# TCP

This module includes functions that control TCP communication.

## Summary

| ### Enumerations ||
|---|---|
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaf9bdc3014f3d54c426b6d2df10de4960` | enumDefines flags passed to [otTcpConnect()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga3bc39aeb3514e836dfb1d74e824e1cce). |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gadb49720dc49f7d4e4cf9adbf2948e409` | enumDefines flags passed to `otTcpSendByReference`. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga2dea5fb99241c1b9efe73dc79dbe30ea` | enum |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaad21b37b5f4d9fbc97e86b9a026237b0{ https://openthread.io/reference/group/api-tcp#group__api-tcp_1ggaad21b37b5f4d9fbc97e86b9a026237b0a3c412f0e31a6e5d4dc90cce7388cf5c4, https://openthread.io/reference/group/api-tcp#group__api-tcp_1ggaad21b37b5f4d9fbc97e86b9a026237b0a73f6209e2015242f880c65634f671449, https://openthread.io/reference/group/api-tcp#group__api-tcp_1ggaad21b37b5f4d9fbc97e86b9a026237b0a2194e1854e3f1c7d51039b25bbfa7044 }` | enumDefines incoming connection actions. |

| ### Typedefs ||
|---|---|
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga49f3950b5fe6b1f466f8b6ade923cfb4` | typedef `struct https://openthread.io/reference/struct/ot-linked-buffer` A linked buffer structure for use with TCP. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga399f95bb0ad56cbe6f317127fdf2e7be)(otTcpListener *aListener, otTcpEndpoint *aEndpoint, const otSockAddr *aPeer)` | typedef `void(*` This callback indicates that the TCP connection is now ready for two-way communication. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaf53a2e6d1a995ed5f7e09f6799bc828c)(otTcpListener *aListener, const otSockAddr *aPeer, otTcpEndpoint **aAcceptInto)` | typedef `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaad21b37b5f4d9fbc97e86b9a026237b0(*` This callback indicates that an incoming connection that matches this TCP listener has arrived. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga03a1e75e384d545c95cf5acb51f8728b)(otTcpEndpoint *aEndpoint, otTcpDisconnectedReason aReason)` | typedef `void(*` This callback indicates that the connection was broken and should no longer be used, or that a connection has entered the TIME-WAIT state. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaba3d813dd1a8968bf0f60312e32a4263` | typedef `enum https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga2dea5fb99241c1b9efe73dc79dbe30ea` |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga9b25068e9277d21122c7421e0efb909d` | typedef `struct https://openthread.io/reference/struct/ot-tcp-endpoint` |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gafe706402a5c796bd699f0046aa9632b3` | typedef `struct https://openthread.io/reference/struct/ot-tcp-endpoint-initialize-args` Contains arguments to the [otTcpEndpointInitialize()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga46ca008dbb9657f10692612f3051dd55) function. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga25c56312aca352fb3f023fab53687558)(otTcpEndpoint *aEndpoint)` | typedef `void(*` This callback informs the application that the TCP 3-way handshake is complete and that the connection is now established. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga69935ba978a5984e8b38b5fc8854c6fb)(otTcpEndpoint *aEndpoint, size_t aInSendBuffer, size_t aBacklog)` | typedef `void(*` This callback informs the application if forward progress has been made in transferring data from the send buffer to the recipient. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaabbf4ac01a179c45fb595462fb2a7673` | typedef `enum https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaad21b37b5f4d9fbc97e86b9a026237b0` Defines incoming connection actions. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga525b81e6a10ffac54fa6d86b39841611` | typedef `struct https://openthread.io/reference/struct/ot-tcp-listener` |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga86fdfebf1988c213634a8c6768c977ff` | typedef `struct https://openthread.io/reference/struct/ot-tcp-listener-initialize-args` Contains arguments to the [otTcpListenerInitialize()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga1415bd34cc64f1745ca4b3364d7804bb) function. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaaad9c380c56bfe2dfee43b8f45bfe499)(otTcpEndpoint *aEndpoint, size_t aBytesAvailable, bool aEndOfStream, size_t aBytesRemaining)` | typedef `void(*` This callback indicates the number of bytes available for consumption from the receive buffer. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gab49b3db0f5d27c85e72672468b6c425b)(otTcpEndpoint *aEndpoint, otLinkedBuffer *aData)` | typedef `void(*` This callback informs the application that data in the provided `aData` have been acknowledged by the connection peer and that `aData` and the data it contains can be reclaimed by the application. |

| ### Functions ||
|---|---|
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga11edfbb1feaab02ba9e8cb65bfa39491(https://openthread.io/reference/struct/ot-tcp-endpoint *aEndpoint)` | `https://openthread.io/reference/group/api-error#group__api-error_1gabaaa90aaa35da8f9d394a227a23a6a49` Forcibly ends the TCP connection associated with this TCP endpoint. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga5d0811860150c56632ad641a71100a5a(https://openthread.io/reference/struct/ot-tcp-endpoint *aEndpoint, const https://openthread.io/reference/struct/ot-sock-addr *aSockName)` | `https://openthread.io/reference/group/api-error#group__api-error_1gabaaa90aaa35da8f9d394a227a23a6a49` Binds the TCP endpoint to an IP address and port. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga0ee5105795fcdf332bb255e252eeed74(https://openthread.io/reference/struct/ot-tcp-endpoint *aEndpoint, size_t aNumBytes, uint32_t aFlags)` | `https://openthread.io/reference/group/api-error#group__api-error_1gabaaa90aaa35da8f9d394a227a23a6a49` Informs the TCP stack that the application has finished processing `aNumBytes` bytes of data at the start of the receive buffer and that the TCP stack need not continue maintaining those bytes in the receive buffer. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga3bc39aeb3514e836dfb1d74e824e1cce(https://openthread.io/reference/struct/ot-tcp-endpoint *aEndpoint, const https://openthread.io/reference/struct/ot-sock-addr *aSockName, uint32_t aFlags)` | `https://openthread.io/reference/group/api-error#group__api-error_1gabaaa90aaa35da8f9d394a227a23a6a49` Records the remote host and port for this connection. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gac3d5c30636461e320a8fd77c8da49881(https://openthread.io/reference/struct/ot-tcp-endpoint *aEndpoint)` | `https://openthread.io/reference/group/api-error#group__api-error_1gabaaa90aaa35da8f9d394a227a23a6a49` Deinitializes this TCP endpoint. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga4da77ed858448152d8f02f9e01f4e5f4(https://openthread.io/reference/struct/ot-tcp-endpoint *aEndpoint)` | `void *` Obtains the context pointer that was associated with `aEndpoint` upon initialization. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga58b8afb5269f4d3491cd413d219dc5f2(https://openthread.io/reference/struct/ot-tcp-endpoint *aEndpoint)` | `https://openthread.io/reference/group/api-instance#group__api-instance_1ga25440551213fb5b40869725378584960 *` Obtains the otInstance that was associated with `aEndpoint` upon initialization. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga46ca008dbb9657f10692612f3051dd55(https://openthread.io/reference/group/api-instance#group__api-instance_1ga25440551213fb5b40869725378584960 *aInstance, https://openthread.io/reference/struct/ot-tcp-endpoint *aEndpoint, const https://openthread.io/reference/struct/ot-tcp-endpoint-initialize-args *aArgs)` | `https://openthread.io/reference/group/api-error#group__api-error_1gabaaa90aaa35da8f9d394a227a23a6a49` Initializes a TCP endpoint. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga9cde3ffdf74d08820bb1f80345929d33(const https://openthread.io/reference/struct/ot-tcp-endpoint *aEndpoint)` | `const https://openthread.io/reference/struct/ot-sock-addr *` Obtains a pointer to a TCP endpoint's local host and port. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gac348d019a2252ecb2d00344c84360ce6(const https://openthread.io/reference/struct/ot-tcp-endpoint *aEndpoint)` | `const https://openthread.io/reference/struct/ot-sock-addr *` Obtains a pointer to a TCP endpoint's peer's host and port. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga51b2d66fc32d24aab66868383a089a1e(https://openthread.io/reference/struct/ot-tcp-listener *aListener, const https://openthread.io/reference/struct/ot-sock-addr *aSockName)` | `https://openthread.io/reference/group/api-error#group__api-error_1gabaaa90aaa35da8f9d394a227a23a6a49` Causes incoming TCP connections that match the specified IP address and port to trigger this TCP listener's callbacks. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gae4035a8f92bab14eb1adeb8c0a768f60(https://openthread.io/reference/struct/ot-tcp-listener *aListener)` | `https://openthread.io/reference/group/api-error#group__api-error_1gabaaa90aaa35da8f9d394a227a23a6a49` Deinitializes this TCP listener. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga71933392bec496e86903c9de2758e6c3(https://openthread.io/reference/struct/ot-tcp-listener *aListener)` | `void *` Obtains the context pointer that was associated with `aListener` upon initialization. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gadd51d612e6c5290c1eba030265599fc4(https://openthread.io/reference/struct/ot-tcp-listener *aListener)` | `https://openthread.io/reference/group/api-instance#group__api-instance_1ga25440551213fb5b40869725378584960 *` Obtains the otInstance that was associated with `aListener` upon initialization. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga1415bd34cc64f1745ca4b3364d7804bb(https://openthread.io/reference/group/api-instance#group__api-instance_1ga25440551213fb5b40869725378584960 *aInstance, https://openthread.io/reference/struct/ot-tcp-listener *aListener, const https://openthread.io/reference/struct/ot-tcp-listener-initialize-args *aArgs)` | `https://openthread.io/reference/group/api-error#group__api-error_1gabaaa90aaa35da8f9d394a227a23a6a49` Initializes a TCP listener. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga3773930ab3cade5ff57dfe6da7726847(https://openthread.io/reference/struct/ot-tcp-endpoint *aEndpoint, const https://openthread.io/reference/struct/ot-linked-buffer **aBuffer)` | `https://openthread.io/reference/group/api-error#group__api-error_1gabaaa90aaa35da8f9d394a227a23a6a49` Provides the application with a linked buffer chain referencing data currently in the TCP receive buffer. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaf4755a0481eb9e7c279cfb031641c128(https://openthread.io/reference/struct/ot-tcp-endpoint *aEndpoint)` | `https://openthread.io/reference/group/api-error#group__api-error_1gabaaa90aaa35da8f9d394a227a23a6a49` Reorganizes the receive buffer to be entirely contiguous in memory. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaf4c8ad89d113b0b40778be6ca5d3cde8(https://openthread.io/reference/struct/ot-tcp-endpoint *aEndpoint, size_t aNumBytes, uint32_t aFlags)` | `https://openthread.io/reference/group/api-error#group__api-error_1gabaaa90aaa35da8f9d394a227a23a6a49` Adds data to the send buffer by extending the length of the final [otLinkedBuffer](https://openthread.io/reference/struct/ot-linked-buffer) in the send buffer by the specified amount. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaa75f4bd44478f8a09a41b49544a96dfb(https://openthread.io/reference/struct/ot-tcp-endpoint *aEndpoint, https://openthread.io/reference/struct/ot-linked-buffer *aBuffer, uint32_t aFlags)` | `https://openthread.io/reference/group/api-error#group__api-error_1gabaaa90aaa35da8f9d394a227a23a6a49` Adds data referenced by the linked buffer pointed to by `aBuffer` to the send buffer. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga9e928aa3c9957c5d5f636c6e4c9f6e6d(https://openthread.io/reference/struct/ot-tcp-endpoint *aEndpoint)` | `https://openthread.io/reference/group/api-error#group__api-error_1gabaaa90aaa35da8f9d394a227a23a6a49` Informs the connection peer that this TCP endpoint will not send more data. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaa1ec44353fa7f24229b772298863f198(https://openthread.io/reference/struct/ot-tcp-listener *aListener)` | `https://openthread.io/reference/group/api-error#group__api-error_1gabaaa90aaa35da8f9d394a227a23a6a49` Causes this TCP listener to stop listening for incoming connections. |

| ### Macros ||
|---|---|
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga880da2e4907c94d271ce28fb3cccae22 36` |   |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaa82629c770c4bb6a2919cd1aed657b69 392` | OT_TCP_ENDPOINT_TCB_SIZE_BASE and OT_TCP_ENDPOINT_TCB_NUM_POINTERS are chosen such that the mTcb field of [otTcpEndpoint](https://openthread.io/reference/struct/ot-tcp-endpoint) has the same size as struct tcpcb in TCPlp. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga2fe6ab4e74e7fe7760e08e5a9e53b31c 3` |   |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga28d78d4a040b42dce00443cf8f704065 16` | OT_TCP_LISTENER_TCB_SIZE_BASE and OT_TCP_LISTENER_TCB_NUM_POINTERS are chosen such that the mTcbListener field of [otTcpListener](https://openthread.io/reference/struct/ot-tcp-listener) has the same size as struct tcpcb_listen in TCPlp. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga88304ad88257ec1ec57c124d486ed8d1 2598` | Recommended buffer size for TCP connections that traverse about 3 wireless hops or fewer. |
| `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaaee4e4eba102333eb69a3641896c0116 4157` | Recommended buffer size for TCP connections that traverse many wireless hops. |

| ### Structs ||
|---|---|
| [otLinkedBuffer](https://openthread.io/reference/struct/ot-linked-buffer) | A linked buffer structure for use with TCP. |
| [otTcpEndpoint](https://openthread.io/reference/struct/ot-tcp-endpoint) | Represents a TCP endpoint. |
| [otTcpEndpointInitializeArgs](https://openthread.io/reference/struct/ot-tcp-endpoint-initialize-args) | Contains arguments to the [otTcpEndpointInitialize()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga46ca008dbb9657f10692612f3051dd55) function. |
| [otTcpListener](https://openthread.io/reference/struct/ot-tcp-listener) | Represents a TCP listener. |
| [otTcpListenerInitializeArgs](https://openthread.io/reference/struct/ot-tcp-listener-initialize-args) | Contains arguments to the [otTcpListenerInitialize()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga1415bd34cc64f1745ca4b3364d7804bb) function. |

## Enumerations

### Anonymous Enum 19

```
 Anonymous Enum 19
```
Defines flags passed to [otTcpConnect()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga3bc39aeb3514e836dfb1d74e824e1cce).

### Anonymous Enum 20

```
 Anonymous Enum 20
```
Defines flags passed to `otTcpSendByReference`.

### otTcpDisconnectedReason

```
 otTcpDisconnectedReason
```

### otTcpIncomingConnectionAction

```
 otTcpIncomingConnectionAction
```
Defines incoming connection actions.

This is used in [otTcpAcceptReady()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaf53a2e6d1a995ed5f7e09f6799bc828c) callback.

| Properties ||
|---|---|
| `OT_TCP_INCOMING_CONNECTION_ACTION_ACCEPT` | Accept the incoming connection. |
| `OT_TCP_INCOMING_CONNECTION_ACTION_DEFER` | Defer (silently ignore) the incoming connection. |
| `OT_TCP_INCOMING_CONNECTION_ACTION_REFUSE` | Refuse the incoming connection. |

## Typedefs

### otLinkedBuffer

```
struct otLinkedBuffer otLinkedBuffer
```
A linked buffer structure for use with TCP.

A single [otLinkedBuffer](https://openthread.io/reference/struct/ot-linked-buffer) structure references an array of bytes in memory, via mData and mLength. The mNext field is used to form a chain of [otLinkedBuffer](https://openthread.io/reference/struct/ot-linked-buffer) structures.

### otTcpAcceptDone

```
void(* otTcpAcceptDone)(otTcpListener *aListener, otTcpEndpoint *aEndpoint, const otSockAddr *aPeer)
```
This callback indicates that the TCP connection is now ready for two-way communication.

In the case of TCP Fast Open, this may be before the TCP connection handshake has actually completed. The application is provided with the context pointers both for the TCP listener that accepted the connection and the TCP endpoint into which it was accepted. The provided context is the one associated with the TCP listener.

<br />

Details Parameters `[in] aListener` The TCP listener that matches the incoming connection. `[in] aEndpoint` The TCP endpoint into which the incoming connection was accepted. `[in] aPeer` the host and port from which the incoming connection originated.

### otTcpAcceptReady

```
otTcpIncomingConnectionAction(* otTcpAcceptReady)(otTcpListener *aListener, const otSockAddr *aPeer, otTcpEndpoint **aAcceptInto)
```
This callback indicates that an incoming connection that matches this TCP listener has arrived.

The typical response is for the application to accept the incoming connection. It does so by populating `aAcceptInto` with a pointer to the [otTcpEndpoint](https://openthread.io/reference/struct/ot-tcp-endpoint) into which to accept the incoming connection. This [otTcpEndpoint](https://openthread.io/reference/struct/ot-tcp-endpoint) must already be initialized using [otTcpEndpointInitialize()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga46ca008dbb9657f10692612f3051dd55). Then, the application returns OT_TCP_INCOMING_CONNECTION_ACTION_ACCEPT.

Alternatively, the application can decline to accept the incoming connection. There are two ways for the application to do this. First, if the application returns OT_TCP_INCOMING_CONNECTION_ACTION_DEFER, then OpenThread silently ignores the connection establishment request; the connection peer will likely retransmit the request, at which point the callback will be called again. This is valuable if resources are not presently available to accept the connection, but they may be available when the connection peer retransmits its connection establishment attempt. Second, if the application returns OT_TCP_INCOMING_CONNECTION_ACTION_REFUSE, then OpenThread sends a "connection refused" message to the host that attempted to establish a connection. If the application declines the incoming connection, it is not required to populate `aAcceptInto`.

<br />

Details Parameters `[in] aListener` The TCP listener that matches the incoming connection. `[in] aPeer` The host and port from which the incoming connection originates. `[out] aAcceptInto` The TCP endpoint into which to accept the incoming connection. **Returns** Description of how to handle the incoming connection.

### otTcpDisconnected

```
void(* otTcpDisconnected)(otTcpEndpoint *aEndpoint, otTcpDisconnectedReason aReason)
```
This callback indicates that the connection was broken and should no longer be used, or that a connection has entered the TIME-WAIT state.

It can occur if a connection establishment attempt (initiated by calling [otTcpConnect()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga3bc39aeb3514e836dfb1d74e824e1cce)) fails, or any point thereafter (e.g., if the connection times out or an RST segment is received from the connection peer). Once this callback fires, all resources that the application provided for this connection (i.e., any `otLinkedBuffers` and memory they reference, but not the TCP endpoint itself or space for the receive buffers) can be reclaimed. In the case of a connection entering the TIME-WAIT state, this callback is called twice, once upon entry into the TIME-WAIT state (with OT_TCP_DISCONNECTED_REASON_TIME_WAIT, and again when the TIME-WAIT state expires (with OT_TCP_DISCONNECTED_REASON_NORMAL).

<br />

Details Parameters `[in] aEndpoint` The TCP endpoint whose connection has been lost. `[in] aReason` The reason why the connection was lost.

### otTcpDisconnectedReason

```
enum otTcpDisconnectedReason otTcpDisconnectedReason
```

### otTcpEndpoint

```
struct otTcpEndpoint otTcpEndpoint
```

### otTcpEndpointInitializeArgs

```
struct otTcpEndpointInitializeArgs otTcpEndpointInitializeArgs
```
Contains arguments to the [otTcpEndpointInitialize()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga46ca008dbb9657f10692612f3051dd55) function.

### otTcpEstablished

```
void(* otTcpEstablished)(otTcpEndpoint *aEndpoint)
```
This callback informs the application that the TCP 3-way handshake is complete and that the connection is now established.

<br />

Details Parameters `[in] aEndpoint` The TCP endpoint whose connection is now established.

### otTcpForwardProgress

```
void(* otTcpForwardProgress)(otTcpEndpoint *aEndpoint, size_t aInSendBuffer, size_t aBacklog)
```
This callback informs the application if forward progress has been made in transferring data from the send buffer to the recipient.

This callback is not necessary for correct TCP operation. Most applications can just rely on the [otTcpSendDone()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gab49b3db0f5d27c85e72672468b6c425b) callback to reclaim linked buffers once the TCP stack is done using them. The purpose of this callback is to support advanced applications that benefit from finer-grained information about how the the connection is making forward progress in transferring data to the connection peer.

This callback's operation is closely tied to TCP's send buffer. The send buffer can be understood as having two regions. First, there is the "in-flight" region at the head (front) of the send buffer. It corresponds to data which has been sent to the recipient, but is not yet acknowledged. Second, there is the "backlog" region, which consists of all data in the send buffer that is not in the "in-flight" region. The "backlog" region corresponds to data that is queued for sending, but has not yet been sent.

The callback is invoked in response to two types of events. First, the "in-flight" region of the send buffer may shrink (e.g., when the recipient acknowledges data that we sent earlier). Second, the "backlog" region of the send buffer may shrink (e.g., new data was sent out). These two conditions often occur at the same time, in response to an ACK segment from the connection peer, which is why they are combined in a single callback.

The TCP stack only uses the `aInSendBuffer` bytes at the tail of the send buffer; when `aInSendBuffer` decreases by an amount x, it means that x additional bytes that were formerly at the head of the send buffer are no longer part of the send buffer and can now be reclaimed (i.e., overwritten) by the application. Note that the [otLinkedBuffer](https://openthread.io/reference/struct/ot-linked-buffer) structure itself can only be reclaimed once all bytes that it references are no longer part of the send buffer.

This callback subsumes [otTcpSendDone()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gab49b3db0f5d27c85e72672468b6c425b), in the following sense: applications can determine when linked buffers can be reclaimed by comparing `aInSendBuffer` with how many bytes are in each linked buffer. However, we expect [otTcpSendDone()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gab49b3db0f5d27c85e72672468b6c425b), which directly conveys which otLinkedBuffers can be reclaimed, to be much simpler to use. If both callbacks are registered and are triggered by the same event (e.g., the same ACK segment received), then the [otTcpSendDone()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gab49b3db0f5d27c85e72672468b6c425b) callback will be triggered first, followed by this callback.

Additionally, this callback provides `aBacklog`, which indicates how many bytes of data in the send buffer are not yet in flight. For applications that only want to add data to the send buffer when there is an assurance that it will be sent out soon, it may be desirable to only send out data when `aBacklog` is suitably small (0 or close to 0). For example, an application may use `aBacklog` so that it can react to queue buildup by dropping or aggregating data to avoid creating a backlog of data.

After a call to [otTcpSendByReference()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaa75f4bd44478f8a09a41b49544a96dfb) or [otTcpSendByExtension()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaf4c8ad89d113b0b40778be6ca5d3cde8) with a positive number of bytes, the [otTcpForwardProgress()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga69935ba978a5984e8b38b5fc8854c6fb) callback is guaranteed to be called, to indicate when the bytes that were added to the send buffer are sent out. The call to [otTcpForwardProgress()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga69935ba978a5984e8b38b5fc8854c6fb) may be made immediately after the bytes are added to the send buffer (if some of those bytes are immediately sent out, reducing the backlog), or sometime in the future (once the connection sends out some or all of the data, reducing the backlog). By "immediately," we mean that the callback is immediately scheduled for execution in a tasklet; to avoid reentrancy-related complexity, the [otTcpForwardProgress()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga69935ba978a5984e8b38b5fc8854c6fb) callback is never directly called from the [otTcpSendByReference()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaa75f4bd44478f8a09a41b49544a96dfb) or [otTcpSendByExtension()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaf4c8ad89d113b0b40778be6ca5d3cde8) functions.

<br />

Details Parameters `[in] aEndpoint` The TCP endpoint for the connection. `[in] aInSendBuffer` The number of bytes in the send buffer (sum of "in-flight" and "backlog" regions). `[in] aBacklog` The number of bytes that are queued for sending but have not yet been sent (the "backlog" region).

### otTcpIncomingConnectionAction

```
enum otTcpIncomingConnectionAction otTcpIncomingConnectionAction
```
Defines incoming connection actions.

This is used in [otTcpAcceptReady()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaf53a2e6d1a995ed5f7e09f6799bc828c) callback.

### otTcpListener

```
struct otTcpListener otTcpListener
```

### otTcpListenerInitializeArgs

```
struct otTcpListenerInitializeArgs otTcpListenerInitializeArgs
```
Contains arguments to the [otTcpListenerInitialize()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga1415bd34cc64f1745ca4b3364d7804bb) function.

### otTcpReceiveAvailable

```
void(* otTcpReceiveAvailable)(otTcpEndpoint *aEndpoint, size_t aBytesAvailable, bool aEndOfStream, size_t aBytesRemaining)
```
This callback indicates the number of bytes available for consumption from the receive buffer.

It is called whenever bytes are added to the receive buffer and when the end of stream is reached. If the end of the stream has been reached (i.e., if no more data will become available to read because the connection peer has closed their end of the connection for writing), then `aEndOfStream` is true. Finally, `aBytesRemaining` indicates how much capacity is left in the receive buffer to hold additional data that arrives.

<br />

Details Parameters `[in] aEndpoint` The TCP endpoint for the connection. `[in] aBytesAvailable` The number of bytes in the connection's receive buffer. `[in] aEndOfStream` Indicates if additional data, beyond what is already in the connection's receive buffer, can be received. `[in] aBytesRemaining` The number of additional bytes that can be received before the receive buffer becomes full.

### otTcpSendDone

```
void(* otTcpSendDone)(otTcpEndpoint *aEndpoint, otLinkedBuffer *aData)
```
This callback informs the application that data in the provided `aData` have been acknowledged by the connection peer and that `aData` and the data it contains can be reclaimed by the application.

The `aData` are guaranteed to be identical to those passed in to TCP via [otTcpSendByReference()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaa75f4bd44478f8a09a41b49544a96dfb), including any extensions effected via [otTcpSendByExtension()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaf4c8ad89d113b0b40778be6ca5d3cde8).

<br />

Details Parameters `[in] aEndpoint` The TCP endpoint for the connection. `[in] aData` A pointer to the [otLinkedBuffer](https://openthread.io/reference/struct/ot-linked-buffer) that can be reclaimed.

## Functions

### otTcpAbort

```
otError otTcpAbort(
  otTcpEndpoint *aEndpoint
)
```
Forcibly ends the TCP connection associated with this TCP endpoint.

This immediately makes the TCP endpoint free for use for another connection and empties the send and receive buffers, transferring ownership of any data provided by the application in [otTcpSendByReference()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaa75f4bd44478f8a09a41b49544a96dfb) and [otTcpSendByExtension()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaf4c8ad89d113b0b40778be6ca5d3cde8) calls back to the application. The TCP endpoint's callbacks and memory for the receive buffer remain associated with the TCP endpoint.

<br />

Details Parameters `[in] aEndpoint` A pointer to the TCP endpoint structure representing the TCP endpoint to abort. Return Values `OT_ERROR_NONE` Successfully aborted the TCP endpoint's connection. `OT_ERROR_FAILED` Failed to abort the TCP endpoint's connection.

### otTcpBind

```
otError otTcpBind(
  otTcpEndpoint *aEndpoint,
  const otSockAddr *aSockName
)
```
Binds the TCP endpoint to an IP address and port.

<br />

Details Parameters `[in] aEndpoint` A pointer to the TCP endpoint structure to bind. `[in] aSockName` The address and port to which to bind this TCP endpoint. Return Values `OT_ERROR_NONE` Successfully bound the TCP endpoint. `OT_ERROR_FAILED` Failed to bind the TCP endpoint.

### otTcpCommitReceive

```
otError otTcpCommitReceive(
  otTcpEndpoint *aEndpoint,
  size_t aNumBytes,
  uint32_t aFlags
)
```
Informs the TCP stack that the application has finished processing `aNumBytes` bytes of data at the start of the receive buffer and that the TCP stack need not continue maintaining those bytes in the receive buffer.

<br />

Details Parameters `[in] aEndpoint` A pointer to the TCP endpoint structure representing the TCP endpoint on which to receive data. `[in] aNumBytes` The number of bytes consumed. `[in] aFlags` Flags specifying options for this operation (none yet). Return Values `OT_ERROR_NONE` Successfully completed the receive operation. `OT_ERROR_FAILED` Failed to complete the receive operation.

### otTcpConnect

```
otError otTcpConnect(
  otTcpEndpoint *aEndpoint,
  const otSockAddr *aSockName,
  uint32_t aFlags
)
```
Records the remote host and port for this connection.

TCP Fast Open must be enabled or disabled using `aFlags`. If it is disabled, then the TCP connection establishment handshake is initiated immediately. If it is enabled, then this function merely records the the remote host and port, and the TCP connection establishment handshake only happens on the first call to `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaa75f4bd44478f8a09a41b49544a96dfb`.

If TCP Fast Open is disabled, then the caller must wait for the `otTcpEstablished` callback indicating that TCP connection establishment handshake is done before it can start sending data e.g., by calling `https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaa75f4bd44478f8a09a41b49544a96dfb`.

<br />

Details Parameters `[in] aEndpoint` A pointer to the TCP endpoint structure to connect. `[in] aSockName` The IP address and port of the host to which to connect. `[in] aFlags` Flags specifying options for this operation (see enumeration above). Return Values `OT_ERROR_NONE` Successfully completed the operation. `OT_ERROR_FAILED` Failed to complete the operation.

### otTcpEndpointDeinitialize

```
otError otTcpEndpointDeinitialize(
  otTcpEndpoint *aEndpoint
)
```
Deinitializes this TCP endpoint.

This means that OpenThread no longer keeps track of this TCP endpoint and deallocates all resources it has internally allocated for this TCP endpoint. The application can reuse the memory backing the TCP endpoint as it sees fit.

If it corresponds to a live TCP connection, the connection is terminated unceremoniously (as in [otTcpAbort()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga11edfbb1feaab02ba9e8cb65bfa39491)). All resources the application has provided for this TCP endpoint (linked buffers for the send buffer, memory for the receive buffer, the `aEndpoint` structure itself, etc.) are immediately returned to the application.

<br />

Details Parameters `[in] aEndpoint` A pointer to the TCP endpoint structure to deinitialize. Return Values `OT_ERROR_NONE` Successfully deinitialized the TCP endpoint. `OT_ERROR_FAILED` Failed to deinitialize the TCP endpoint.

### otTcpEndpointGetContext

```
void * otTcpEndpointGetContext(
  otTcpEndpoint *aEndpoint
)
```
Obtains the context pointer that was associated with `aEndpoint` upon initialization.

<br />

Details Parameters `[in] aEndpoint` The TCP endpoint whose context to obtain. **Returns** The context pointer associated with `aEndpoint`.

### otTcpEndpointGetInstance

```
otInstance * otTcpEndpointGetInstance(
  otTcpEndpoint *aEndpoint
)
```
Obtains the otInstance that was associated with `aEndpoint` upon initialization.

<br />

Details Parameters `[in] aEndpoint` The TCP endpoint whose instance to obtain. **Returns** The otInstance pointer associated with `aEndpoint`.

### otTcpEndpointInitialize

```
otError otTcpEndpointInitialize(
  otInstance *aInstance,
  otTcpEndpoint *aEndpoint,
  const otTcpEndpointInitializeArgs *aArgs
)
```
Initializes a TCP endpoint.

Calling this function causes OpenThread to keep track of the TCP endpoint and store and retrieve TCP data inside the `aEndpoint`. The application should refrain from directly accessing or modifying the fields in `aEndpoint`. If the application needs to reclaim the memory backing `aEndpoint`, it should call [otTcpEndpointDeinitialize()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gac3d5c30636461e320a8fd77c8da49881).

<br />

Details Parameters `[in] aInstance` A pointer to an OpenThread instance. `[in] aEndpoint` A pointer to a TCP endpoint structure. `[in] aArgs` A pointer to a structure of arguments. Return Values `OT_ERROR_NONE` Successfully opened the TCP endpoint. `OT_ERROR_FAILED` Failed to open the TCP endpoint.

### otTcpGetLocalAddress

```
const otSockAddr * otTcpGetLocalAddress(
  const otTcpEndpoint *aEndpoint
)
```
Obtains a pointer to a TCP endpoint's local host and port.

The contents of the host and port may be stale if this socket is not in a connected state and has not been bound after it was last disconnected.

<br />

Details Parameters `[in] aEndpoint` The TCP endpoint whose local host and port to obtain. **Returns** The local host and port of `aEndpoint`.

### otTcpGetPeerAddress

```
const otSockAddr * otTcpGetPeerAddress(
  const otTcpEndpoint *aEndpoint
)
```
Obtains a pointer to a TCP endpoint's peer's host and port.

The contents of the host and port may be stale if this socket is not in a connected state.

<br />

Details Parameters `[in] aEndpoint` The TCP endpoint whose peer's host and port to obtain. **Returns** The host and port of the connection peer of `aEndpoint`.

### otTcpListen

```
otError otTcpListen(
  otTcpListener *aListener,
  const otSockAddr *aSockName
)
```
Causes incoming TCP connections that match the specified IP address and port to trigger this TCP listener's callbacks.

<br />

Details Parameters `[in] aListener` A pointer to the TCP listener structure that should begin listening. `[in] aSockName` The address and port on which to listen for incoming connections. Return Values `OT_ERROR_NONE` Successfully initiated listening on the TCP listener. `OT_ERROR_FAILED` Failed to initiate listening on the TCP listener.

### otTcpListenerDeinitialize

```
otError otTcpListenerDeinitialize(
  otTcpListener *aListener
)
```
Deinitializes this TCP listener.

This means that OpenThread no longer keeps track of this TCP listener and deallocates all resources it has internally allocated for this TCP listener. The application can reuse the memory backing the TCP listener as it sees fit.

If the TCP listener is currently listening, it stops listening.

<br />

Details Parameters `[in] aListener` A pointer to the TCP listener structure to deinitialize. Return Values `OT_ERROR_NONE` Successfully deinitialized the TCP listener. `OT_ERROR_FAILED` Failed to deinitialize the TCP listener.

### otTcpListenerGetContext

```
void * otTcpListenerGetContext(
  otTcpListener *aListener
)
```
Obtains the context pointer that was associated with `aListener` upon initialization.

<br />

Details Parameters `[in] aListener` The TCP listener whose context to obtain. **Returns** The context pointer associated with `aListener`.

### otTcpListenerGetInstance

```
otInstance * otTcpListenerGetInstance(
  otTcpListener *aListener
)
```
Obtains the otInstance that was associated with `aListener` upon initialization.

<br />

Details Parameters `[in] aListener` The TCP listener whose instance to obtain. **Returns** The otInstance pointer associated with `aListener`.

### otTcpListenerInitialize

```
otError otTcpListenerInitialize(
  otInstance *aInstance,
  otTcpListener *aListener,
  const otTcpListenerInitializeArgs *aArgs
)
```
Initializes a TCP listener.

Calling this function causes OpenThread to keep track of the TCP listener and store and retrieve TCP data inside `aListener`. The application should refrain from directly accessing or modifying the fields in `aListener`. If the application needs to reclaim the memory backing `aListener`, it should call [otTcpListenerDeinitialize()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gae4035a8f92bab14eb1adeb8c0a768f60).

<br />

Details Parameters `[in] aInstance` A pointer to an OpenThread instance. `[in] aListener` A pointer to a TCP listener structure. `[in] aArgs` A pointer to a structure of arguments. Return Values `OT_ERROR_NONE` Successfully opened the TCP listener. `OT_ERROR_FAILED` Failed to open the TCP listener.

### otTcpReceiveByReference

```
otError otTcpReceiveByReference(
  otTcpEndpoint *aEndpoint,
  const otLinkedBuffer **aBuffer
)
```
Provides the application with a linked buffer chain referencing data currently in the TCP receive buffer.

The linked buffer chain is valid until the "receive ready" callback is next invoked, or until the next call to [otTcpReceiveContiguify()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1gaf4755a0481eb9e7c279cfb031641c128) or [otTcpCommitReceive()](https://openthread.io/reference/group/api-tcp#group__api-tcp_1ga0ee5105795fcdf332bb255e252eeed74).

<br />

Details Parameters `[in] aEndpoint` A pointer to the TCP endpoint structure representing the TCP endpoint on which to receive data. `[out] aBuffer` A pointer to the linked buffer chain referencing data currently in the receive buffer. Return Values `OT_ERROR_NONE` Successfully completed the operation. `OT_ERROR_FAILED` Failed to complete the operation.

### otTcpReceiveContiguify

```
otError otTcpReceiveContiguify(
  otTcpEndpoint *aEndpoint
)
```
Reorganizes the receive buffer to be entirely contiguous in memory.

This is optional; an application can simply traverse the linked buffer chain obtained by calling `otTcpReceiveByReference`. Some applications may wish to call this function to make the receive buffer contiguous to simplify their data processing, but this comes at the expense of CPU time to reorganize the data in the receive buffer.

<br />

Details Parameters `[in] aEndpoint` A pointer to the TCP endpoint whose receive buffer to reorganize. Return Values `OT_ERROR_NONE` Successfully completed the operation. `OT_ERROR_FAILED` Failed to complete the operation.

### otTcpSendByExtension

```
otError otTcpSendByExtension(
  otTcpEndpoint *aEndpoint,
  size_t aNumBytes,
  uint32_t aFlags
)
```
Adds data to the send buffer by extending the length of the final [otLinkedBuffer](https://openthread.io/reference/struct/ot-linked-buffer) in the send buffer by the specified amount.

If the send buffer is empty, then the operation fails.

<br />

Details Parameters `[in] aEndpoint` A pointer to the TCP endpoint structure representing the TCP endpoint on which to send data. `[in] aNumBytes` The number of bytes by which to extend the length of the final linked buffer. `[in] aFlags` Flags specifying options for this operation (see enumeration above). Return Values `OT_ERROR_NONE` Successfully added data to the send buffer. `OT_ERROR_FAILED` Failed to add data to the send buffer.

### otTcpSendByReference

```
otError otTcpSendByReference(
  otTcpEndpoint *aEndpoint,
  otLinkedBuffer *aBuffer,
  uint32_t aFlags
)
```
Adds data referenced by the linked buffer pointed to by `aBuffer` to the send buffer.

Upon a successful call to this function, the linked buffer and data it references are owned by the TCP stack; they should not be modified by the application until a "send done" callback returns ownership of those objects to the application. It is acceptable to call this function to add another linked buffer to the send queue, even if the "send done" callback for a previous invocation of this function has not yet fired.

Note that `aBuffer` should not be chained; its mNext field should be NULL. If additional data will be added right after this call, then the OT_TCP_SEND_MORE_TO_COME flag should be used as a hint to the TCP implementation.

<br />

Details Parameters `[in] aEndpoint` A pointer to the TCP endpoint structure representing the TCP endpoint on which to send data. `[in] aBuffer` A pointer to the linked buffer chain referencing data to add to the send buffer. `[in] aFlags` Flags specifying options for this operation (see enumeration above). Return Values `OT_ERROR_NONE` Successfully added data to the send buffer. `OT_ERROR_FAILED` Failed to add data to the send buffer.

### otTcpSendEndOfStream

```
otError otTcpSendEndOfStream(
  otTcpEndpoint *aEndpoint
)
```
Informs the connection peer that this TCP endpoint will not send more data.

This should be used when the application has no more data to send to the connection peer. For this connection, future reads on the connection peer will result in the "end of stream" condition, and future writes on this connection endpoint will fail.

The "end of stream" condition only applies after any data previously provided to the TCP stack to send out has been received by the connection peer.

<br />

Details Parameters `[in] aEndpoint` A pointer to the TCP endpoint structure representing the TCP endpoint to shut down. Return Values `OT_ERROR_NONE` Successfully queued the "end of stream" condition for transmission. `OT_ERROR_FAILED` Failed to queue the "end of stream" condition for transmission.

### otTcpStopListening

```
otError otTcpStopListening(
  otTcpListener *aListener
)
```
Causes this TCP listener to stop listening for incoming connections.

<br />

Details Parameters `[in] aListener` A pointer to the TCP listener structure that should stop listening. Return Values `OT_ERROR_NONE` Successfully stopped listening on the TCP listener. `OT_ERROR_FAILED` Failed to stop listening on the TCP listener.

## Macros

### OT_TCP_ENDPOINT_TCB_NUM_PTR

```
 OT_TCP_ENDPOINT_TCB_NUM_PTR 36
```

### OT_TCP_ENDPOINT_TCB_SIZE_BASE

```
 OT_TCP_ENDPOINT_TCB_SIZE_BASE 392
```
OT_TCP_ENDPOINT_TCB_SIZE_BASE and OT_TCP_ENDPOINT_TCB_NUM_POINTERS are chosen such that the mTcb field of [otTcpEndpoint](https://openthread.io/reference/struct/ot-tcp-endpoint) has the same size as struct tcpcb in TCPlp.

This is necessary because the mTcb field, although opaque in its declaration, is treated as struct tcpcb in the TCP implementation.

### OT_TCP_LISTENER_TCB_NUM_PTR

```
 OT_TCP_LISTENER_TCB_NUM_PTR 3
```

### OT_TCP_LISTENER_TCB_SIZE_BASE

```
 OT_TCP_LISTENER_TCB_SIZE_BASE 16
```
OT_TCP_LISTENER_TCB_SIZE_BASE and OT_TCP_LISTENER_TCB_NUM_POINTERS are chosen such that the mTcbListener field of [otTcpListener](https://openthread.io/reference/struct/ot-tcp-listener) has the same size as struct tcpcb_listen in TCPlp.

This is necessary because the mTcbListen field, though opaque in its declaration, is treated as struct tcpcb in the TCP implementation.

### OT_TCP_RECEIVE_BUFFER_SIZE_FEW_HOPS

```
 OT_TCP_RECEIVE_BUFFER_SIZE_FEW_HOPS 2598
```
Recommended buffer size for TCP connections that traverse about 3 wireless hops or fewer.

On platforms where memory is particularly constrained and in situations where high bandwidth is not necessary, it may be desirable to manually select a smaller buffer size.

### OT_TCP_RECEIVE_BUFFER_SIZE_MANY_HOPS

```
 OT_TCP_RECEIVE_BUFFER_SIZE_MANY_HOPS 4157
```
Recommended buffer size for TCP connections that traverse many wireless hops.

If the TCP connection traverses a very large number of hops (more than 6 or so), then it may be advisable to select a large buffer size manually.

## Resources

OpenThread API Reference topics originate from the source code, available
on [GitHub](https://github.com/openthread/openthread/tree/main/include/openthread).
For more information, or to contribute to our documentation, refer to
[Resources](https://openthread.io/resources).