lwIP
2.1.0
Lightweight IP stack
|
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) |
Transmission Control Protocol for IP
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.
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 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 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.
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.
#define tcp_listen | ( | pcb | ) | tcp_listen_with_backlog(pcb, TCP_DEFAULT_LISTEN_BACKLOG) |
#define tcp_mss | ( | pcb | ) | ((pcb)->mss) |
#define tcp_nagle_disable | ( | pcb | ) | tcp_set_flags(pcb, TF_NODELAY) |
#define tcp_nagle_disabled | ( | pcb | ) | tcp_is_flag_set(pcb, TF_NODELAY) |
#define tcp_nagle_enable | ( | pcb | ) | tcp_clear_flags(pcb, TF_NODELAY) |
#define tcp_sndbuf | ( | pcb | ) | (TCPWND16((pcb)->snd_buf)) |
#define tcp_sndqueuelen | ( | pcb | ) | ((pcb)->snd_queuelen) |
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!
pcb | the tcp pcb to abort |
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.
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 |
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.
pcb | tcp_pcb to set the callback argument |
arg | void pointer argument to pass to callback functions |
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!
pcb | the connection pcb which is now fully accepted (or closed/aborted) |
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!
pcb | the connection pcb which is not fully accepted yet |
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.
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 |
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.
pcb | the tcp_pcb to bind. |
netif | the netif to bind to. Can be NULL. |
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.
pcb | the tcp_pcb to close |
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.
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) |
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.
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 |
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.
pcb | the original tcp_pcb |
backlog | the incoming connections queue limit |
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.
pcb | the original tcp_pcb |
backlog | the incoming connections queue limit |
err | when NULL is returned, this contains the error reason |
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.
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().
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. |
Find out what we can send and send it
pcb | Protocol control block for the TCP connection to send data |
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.
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.
pcb | tcp_pcb to set the recv callback |
recv | callback function to call for this pcb when data is received |
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.
pcb | the tcp_pcb for which data is read |
len | the amount of bytes that have been read by the application |
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.
pcb | tcp_pcb to set the sent callback |
sent | callback function to call for this pcb when data is successfully sent |
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!
pcb | PCB to shutdown |
shut_rx | shut down receive side if this is != 0 |
shut_tx | shut down send side if this is != 0 |
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:
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.
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 :
|