lwIP 2.1.1 Lightweight IP stack

Modules | Macros | Functions
TCP
APIs » "raw" APIs

## Modules
| ext arguments

## Macros
#define | tcp_mss(pcb) ((pcb)->mss)
#define | tcp_sndbuf(pcb) (TCPWND16((pcb)->snd_buf))
#define | tcp_sndqueuelen(pcb) ((pcb)->snd_queuelen)
#define | tcp_nagle_disable(pcb) tcp_set_flags(pcb, TF_NODELAY)
#define | tcp_nagle_enable(pcb) tcp_clear_flags(pcb, TF_NODELAY)
#define | tcp_nagle_disabled(pcb) tcp_is_flag_set(pcb, TF_NODELAY)
#define | tcp_listen(pcb) tcp_listen_with_backlog(pcb, TCP_DEFAULT_LISTEN_BACKLOG)

## Functions
void | tcp_backlog_delayed (struct tcp_pcb *pcb)
void | tcp_backlog_accepted (struct tcp_pcb *pcb)
err_t | tcp_close (struct tcp_pcb *pcb)
err_t | tcp_shutdown (struct tcp_pcb *pcb, int shut_rx, int shut_tx)
void | tcp_abort (struct tcp_pcb *pcb)
err_t | tcp_bind (struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
void | tcp_bind_netif (struct tcp_pcb *pcb, const struct netif *netif)
struct tcp_pcb * | tcp_listen_with_backlog (struct tcp_pcb *pcb, u8_t backlog)
struct tcp_pcb * | tcp_listen_with_backlog_and_err (struct tcp_pcb *pcb, u8_t backlog, err_t *err)
void | tcp_recved (struct tcp_pcb *pcb, u16_t len)
err_t | tcp_connect (struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port, tcp_connected_fn connected)
struct tcp_pcb * | tcp_new (void)
struct tcp_pcb * | tcp_new_ip_type (u8_t type)
void | tcp_arg (struct tcp_pcb *pcb, void *arg)
void | tcp_recv (struct tcp_pcb *pcb, tcp_recv_fn recv)
void | tcp_sent (struct tcp_pcb *pcb, tcp_sent_fn sent)
void | tcp_err (struct tcp_pcb *pcb, tcp_err_fn err)
void | tcp_accept (struct tcp_pcb *pcb, tcp_accept_fn accept)
void | tcp_poll (struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval)
err_t | tcp_write (struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags)
err_t | tcp_output (struct tcp_pcb *pcb)

## Detailed Description

Transmission Control Protocol for IP

See also
  APIs

Common functions for the TCP implementation, such as functions for manipulating the data structures and the TCP timer functions. TCP functions related to input and output is found in tcp_in.c and tcp_out.c respectively.

## TCP connection setup

The functions used for setting up connections is similar to that of the sequential API and of the BSD socket API. A new TCP connection identifier (i.e., a protocol control block - PCB) is created with the tcp_new() function. This PCB can then be either set to listen for new incoming connections or be explicitly connected to another host.

- tcp_new()
- tcp_bind()
- tcp_listen() and tcp_listen_with_backlog()
- tcp_accept()
- tcp_connect()

## Sending TCP data

TCP data is sent by enqueueing the data with a call to tcp_write() and triggering to send by calling tcp_output(). When the data is successfully transmitted to the remote host, the application will be notified with a call to a specified callback function.

- tcp_write()
- tcp_output()
- tcp_sent()

## Receiving TCP data

TCP data reception is callback based - an application specified callback function is called when new data arrives. When the application has taken the data, it has to call the tcp_recved() function to indicate that TCP can advertise increase the receive window.

- tcp_recv()
- tcp_recved()

## Application polling

When a connection is idle (i.e., no data is either transmitted or received), lwIP will repeatedly poll the application by calling a specified callback function. This can be used either as a watchdog timer for killing connections that have stayed idle for too long, or as a method of waiting for memory to become available. For instance, if a call to tcp_write() has failed because memory wasn't available, the application may use the polling functionality to call tcp_write() again when the connection has been idle for a while.

- tcp_poll()

## Closing and aborting connections

- tcp_close()
- tcp_abort()
- tcp_err()

## Macro Definition Documentation

## ◆ tcp_listen

#define tcp_listen | ( |  | pcb | ) | tcp_listen_with_backlog(pcb, TCP_DEFAULT_LISTEN_BACKLOG)

## ◆ tcp_mss

#define tcp_mss | ( |  | pcb | ) | ((pcb)->mss)

## ◆ tcp_nagle_disable

#define tcp_nagle_disable | ( |  | pcb | ) | tcp_set_flags(pcb, TF_NODELAY)

## ◆ tcp_nagle_disabled

#define tcp_nagle_disabled | ( |  | pcb | ) | tcp_is_flag_set(pcb, TF_NODELAY)

## ◆ tcp_nagle_enable

#define tcp_nagle_enable | ( |  | pcb | ) | tcp_clear_flags(pcb, TF_NODELAY)

## ◆ tcp_sndbuf

#define tcp_sndbuf | ( |  | pcb | ) | (TCPWND16((pcb)->snd_buf))

## ◆ tcp_sndqueuelen

#define tcp_sndqueuelen | ( |  | pcb | ) | ((pcb)->snd_queuelen)

## Function Documentation

## ◆ tcp_abort()

void tcp_abort | ( | struct tcp_pcb * | pcb | ) |

Aborts the connection by sending a RST (reset) segment to the remote host. The pcb is deallocated. This function never fails.

ATTENTION: When calling this from one of the TCP callbacks, make sure you always return ERR_ABRT (and never return ERR_ABRT otherwise or you will risk accessing deallocated memory or memory leaks!

Parameters

  pcb | the tcp pcb to abort

## ◆ tcp_accept()

void tcp_accept | ( | struct tcp_pcb * | pcb,
|  | tcp_accept_fn | accept
| ) |  |

Used for specifying the function that should be called when a LISTENing connection has been connected to another host.

Parameters

  pcb | tcp_pcb to set the accept callback
  accept | callback function to call for this pcb when LISTENing connection has been connected to another host

## ◆ tcp_arg()

void tcp_arg | ( | struct tcp_pcb * | pcb,
|  | void * | arg
| ) |  |

Specifies the program specific state that should be passed to all other callback functions. The "pcb" argument is the current TCP connection control block, and the "arg" argument is the argument that will be passed to the callbacks.

Parameters

  pcb | tcp_pcb to set the callback argument
  arg | void pointer argument to pass to callback functions

## ◆ tcp_backlog_accepted()

void tcp_backlog_accepted | ( | struct tcp_pcb * | pcb | ) |

A delayed-accept a connection is accepted (or closed/aborted): decreases the number of outstanding connections after calling tcp_backlog_delayed().

ATTENTION: the caller is responsible for calling tcp_backlog_accepted() or else the backlog feature will get out of sync!

Parameters

  pcb | the connection pcb which is now fully accepted (or closed/aborted)

## ◆ tcp_backlog_delayed()

void tcp_backlog_delayed | ( | struct tcp_pcb * | pcb | ) |

Delay accepting a connection in respect to the listen backlog: the number of outstanding connections is increased until tcp_backlog_accepted() is called.

ATTENTION: the caller is responsible for calling tcp_backlog_accepted() or else the backlog feature will get out of sync!

Parameters

  pcb | the connection pcb which is not fully accepted yet

## ◆ tcp_bind()

err_t tcp_bind | ( | struct tcp_pcb * | pcb,
|  | const ip_addr_t * | ipaddr,
|  | u16_t | port
| ) |  |

Binds the connection to a local port number and IP address. If the IP address is not given (i.e., ipaddr == IP_ANY_TYPE), the connection is bound to all local IP addresses. If another connection is bound to the same port, the function will return ERR_USE, otherwise ERR_OK is returned.

Parameters

  pcb | the tcp_pcb to bind (no check is done whether this pcb is already bound!)
  ipaddr | the local ip address to bind to (use IPx_ADDR_ANY to bind to any local address
  port | the local port to bind to

Returns
  ERR_USE if the port is already in use ERR_VAL if bind failed because the PCB is not in a valid state ERR_OK if bound

## ◆ tcp_bind_netif()

void tcp_bind_netif | ( | struct tcp_pcb * | pcb,
|  | const struct netif * | netif
| ) |  |

Binds the connection to a netif and IP address. After calling this function, all packets received via this PCB are guaranteed to have come in via the specified netif, and all outgoing packets will go out via the specified netif.

Parameters

  pcb | the tcp_pcb to bind.
  netif | the netif to bind to. Can be NULL.

## ◆ tcp_close()

err_t tcp_close | ( | struct tcp_pcb * | pcb | ) |

Closes the connection held by the PCB.

Listening pcbs are freed and may not be referenced any more. Connection pcbs are freed if not yet connected and may not be referenced any more. If a connection is established (at least SYN received or in a closing state), the connection is closed, and put in a closing state. The pcb is then automatically freed in tcp_slowtmr(). It is therefore unsafe to reference it (unless an error is returned).

The function may return ERR_MEM if no memory was available for closing the connection. If so, the application should wait and try again either by using the acknowledgment callback or the polling functionality. If the close succeeds, the function returns ERR_OK.

Parameters

  pcb | the tcp_pcb to close

Returns
  ERR_OK if connection has been closed another err_t if closing failed and pcb is not freed

## ◆ tcp_connect()

err_t tcp_connect | ( | struct tcp_pcb * | pcb,
|  | const ip_addr_t * | ipaddr,
|  | u16_t | port,
|  | tcp_connected_fn | connected
| ) |  |

Connects to another host. The function given as the "connected" argument will be called when the connection has been established. Sets up the pcb to connect to the remote host and sends the initial SYN segment which opens the connection.

The tcp_connect() function returns immediately; it does not wait for the connection to be properly setup. Instead, it will call the function specified as the fourth argument (the "connected" argument) when the connection is established. If the connection could not be properly established, either because the other host refused the connection or because the other host didn't answer, the "err" callback function of this pcb (registered with tcp_err, see below) will be called.

The tcp_connect() function can return ERR_MEM if no memory is available for enqueueing the SYN segment. If the SYN indeed was enqueued successfully, the tcp_connect() function returns ERR_OK.

Parameters

  pcb | the tcp_pcb used to establish the connection
  ipaddr | the remote ip address to connect to
  port | the remote tcp port to connect to
  connected | callback function to call when connected (on error, the err calback will be called)

Returns
  ERR_VAL if invalid arguments are given ERR_OK if connect request has been sent other err_t values if connect request couldn't be sent

## ◆ tcp_err()

void tcp_err | ( | struct tcp_pcb * | pcb,
|  | tcp_err_fn | err
| ) |  |

Used to specify the function that should be called when a fatal error has occurred on the connection.

If a connection is aborted because of an error, the application is alerted of this event by the err callback. Errors that might abort a connection are when there is a shortage of memory. The callback function to be called is set using the tcp_err() function.

Note
  The corresponding pcb is already freed when this callback is called!

Parameters

  pcb | tcp_pcb to set the err callback
  err | callback function to call for this pcb when a fatal error has occurred on the connection

## ◆ tcp_listen_with_backlog()

struct tcp_pcb* tcp_listen_with_backlog | ( | struct tcp_pcb * | pcb,
|  | u8_t | backlog
| ) |  |

Set the state of the connection to be LISTEN, which means that it is able to accept incoming connections. The protocol control block is reallocated in order to consume less memory. Setting the connection to LISTEN is an irreversible process. When an incoming connection is accepted, the function specified with the tcp_accept() function will be called. The pcb has to be bound to a local port with the tcp_bind() function.

The tcp_listen() function returns a new connection identifier, and the one passed as an argument to the function will be deallocated. The reason for this behavior is that less memory is needed for a connection that is listening, so tcp_listen() will reclaim the memory needed for the original connection and allocate a new smaller memory block for the listening connection.

tcp_listen() may return NULL if no memory was available for the listening connection. If so, the memory associated with the pcb passed as an argument to tcp_listen() will not be deallocated.

The backlog limits the number of outstanding connections in the listen queue to the value specified by the backlog argument. To use it, your need to set TCP_LISTEN_BACKLOG=1 in your lwipopts.h.

Parameters

  pcb | the original tcp_pcb
  backlog | the incoming connections queue limit

Returns
  tcp_pcb used for listening, consumes less memory.

Note
  The original tcp_pcb is freed. This function therefore has to be called like this: tpcb = tcp_listen_with_backlog(tpcb, backlog);

## ◆ tcp_listen_with_backlog_and_err()

struct tcp_pcb* tcp_listen_with_backlog_and_err | ( | struct tcp_pcb * | pcb,
|  | u8_t | backlog,
|  | err_t * | err
| ) |  |

Set the state of the connection to be LISTEN, which means that it is able to accept incoming connections. The protocol control block is reallocated in order to consume less memory. Setting the connection to LISTEN is an irreversible process.

Parameters

  pcb | the original tcp_pcb
  backlog | the incoming connections queue limit
  err | when NULL is returned, this contains the error reason

Returns
  tcp_pcb used for listening, consumes less memory.

Note
  The original tcp_pcb is freed. This function therefore has to be called like this: tpcb = tcp_listen_with_backlog_and_err(tpcb, backlog, &err);

## ◆ tcp_new()

struct tcp_pcb* tcp_new | ( | void |  | ) |

Creates a new TCP protocol control block but doesn't place it on any of the TCP PCB lists. The pcb is not put on any list until binding using tcp_bind(). If memory is not available for creating the new pcb, NULL is returned.

## ◆ tcp_new_ip_type()

struct tcp_pcb* tcp_new_ip_type | ( | u8_t | type | ) |

Creates a new TCP protocol control block but doesn't place it on any of the TCP PCB lists. The pcb is not put on any list until binding using tcp_bind().

Parameters

  type | IP address type, see lwip_ip_addr_type definitions. If you want to listen to IPv4 and IPv6 (dual-stack) connections, supply IPADDR_TYPE_ANY as argument and bind to IP_ANY_TYPE.

Returns
  a new tcp_pcb that initially is in state CLOSED

## ◆ tcp_output()

err_t tcp_output | ( | struct tcp_pcb * | pcb | ) |

Find out what we can send and send it

Parameters

  pcb | Protocol control block for the TCP connection to send data

Returns
  ERR_OK if data has been sent or nothing to send another err_t on error

## ◆ tcp_poll()

void tcp_poll | ( | struct tcp_pcb * | pcb,
|  | tcp_poll_fn | poll,
|  | u8_t | interval
| ) |  |

Specifies the polling interval and the callback function that should be called to poll the application. The interval is specified in number of TCP coarse grained timer shots, which typically occurs twice a second. An interval of 10 means that the application would be polled every 5 seconds.

When a connection is idle (i.e., no data is either transmitted or received), lwIP will repeatedly poll the application by calling a specified callback function. This can be used either as a watchdog timer for killing connections that have stayed idle for too long, or as a method of waiting for memory to become available. For instance, if a call to tcp_write() has failed because memory wasn't available, the application may use the polling functionality to call tcp_write() again when the connection has been idle for a while.

## ◆ tcp_recv()

void tcp_recv | ( | struct tcp_pcb * | pcb,
|  | tcp_recv_fn | recv
| ) |  |

Sets the callback function that will be called when new data arrives. The callback function will be passed a NULL pbuf to indicate that the remote host has closed the connection. If the callback function returns ERR_OK or ERR_ABRT it must have freed the pbuf, otherwise it must not have freed it.

Parameters

  pcb | tcp_pcb to set the recv callback
  recv | callback function to call for this pcb when data is received

## ◆ tcp_recved()

void tcp_recved | ( | struct tcp_pcb * | pcb,
|  | u16_t | len
| ) |  |

This function should be called by the application when it has processed the data. The purpose is to advertise a larger window when the data has been processed.

Parameters

  pcb | the tcp_pcb for which data is read
  len | the amount of bytes that have been read by the application

## ◆ tcp_sent()

void tcp_sent | ( | struct tcp_pcb * | pcb,
|  | tcp_sent_fn | sent
| ) |  |

Specifies the callback function that should be called when data has successfully been received (i.e., acknowledged) by the remote host. The len argument passed to the callback function gives the amount bytes that was acknowledged by the last acknowledgment.

Parameters

  pcb | tcp_pcb to set the sent callback
  sent | callback function to call for this pcb when data is successfully sent

## ◆ tcp_shutdown()

err_t tcp_shutdown | ( | struct tcp_pcb * | pcb,
|  | int | shut_rx,
|  | int | shut_tx
| ) |  |

Causes all or part of a full-duplex connection of this PCB to be shut down. This doesn't deallocate the PCB unless shutting down both sides! Shutting down both sides is the same as calling tcp_close, so if it succeds (i.e. returns ER_OK), the PCB must not be referenced any more!

Parameters

  pcb | PCB to shutdown
  shut_rx | shut down receive side if this is != 0
  shut_tx | shut down send side if this is != 0

Returns
  ERR_OK if shutdown succeeded (or the PCB has already been shut down) another err_t on error.

## ◆ tcp_write()

err_t tcp_write | ( | struct tcp_pcb * | pcb,
|  | const void * | arg,
|  | u16_t | len,
|  | u8_t | apiflags
| ) |  |

Write data for sending (but does not send it immediately).

It waits in the expectation of more data being sent soon (as it can send them more efficiently by combining them together). To prompt the system to send data now, call tcp_output() after calling tcp_write().

This function enqueues the data pointed to by the argument dataptr. The length of the data is passed as the len parameter. The apiflags can be one or more of:

- TCP_WRITE_FLAG_COPY: indicates whether the new memory should be allocated for the data to be copied into. If this flag is not given, no new memory should be allocated and the data should only be referenced by pointer. This also means that the memory behind dataptr must not change until the data is ACKed by the remote host
- TCP_WRITE_FLAG_MORE: indicates that more data follows. If this is omitted, the PSH flag is set in the last segment created by this call to tcp_write. If this flag is given, the PSH flag is not set.

The tcp_write() function will fail and return ERR_MEM if the length of the data exceeds the current send buffer size or if the length of the queue of outgoing segment is larger than the upper limit defined in lwipopts.h. The number of bytes available in the output queue can be retrieved with the tcp_sndbuf() function.

The proper way to use this function is to call the function with at most tcp_sndbuf() bytes of data. If the function returns ERR_MEM, the application should wait until some of the currently enqueued data has been successfully received by the other host and try again.

Parameters

  pcb | Protocol control block for the TCP connection to enqueue data for.
  arg | Pointer to the data to be enqueued for sending.
  len | Data length in bytes
  apiflags | combination of following flags : TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will not be set on last segment sent,

Returns
  ERR_OK if enqueued, another err_t on error