3 * TCP API (to be used from TCPIP thread)\n
4 * See also @ref tcp_raw
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
38 #ifndef LWIP_HDR_TCP_H
39 #define LWIP_HDR_TCP_H
43 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
46 #include "lwip/pbuf.h"
48 #include "lwip/icmp.h"
51 #include "lwip/ip6_addr.h"
59 /** Function prototype for tcp accept callback functions. Called when a new
60 * connection can be accepted on a listening pcb.
62 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
63 * @param newpcb The new connection pcb
64 * @param err An error code if there has been an error accepting.
65 * Only return ERR_ABRT if you have called tcp_abort from within the
68 typedef err_t (*tcp_accept_fn)(void *arg, struct tcp_pcb *newpcb, err_t err);
70 /** Function prototype for tcp receive callback functions. Called when data has
73 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
74 * @param tpcb The connection pcb which received data
75 * @param p The received data (or NULL when the connection has been closed!)
76 * @param err An error code if there has been an error receiving
77 * Only return ERR_ABRT if you have called tcp_abort from within the
80 typedef err_t (*tcp_recv_fn)(void *arg, struct tcp_pcb *tpcb,
81 struct pbuf *p, err_t err);
83 /** Function prototype for tcp sent callback functions. Called when sent data has
84 * been acknowledged by the remote side. Use it to free corresponding resources.
85 * This also means that the pcb has now space available to send new data.
87 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
88 * @param tpcb The connection pcb for which data has been acknowledged
89 * @param len The amount of bytes acknowledged
90 * @return ERR_OK: try to send some data by calling tcp_output
91 * Only return ERR_ABRT if you have called tcp_abort from within the
94 typedef err_t (*tcp_sent_fn)(void *arg, struct tcp_pcb *tpcb,
97 /** Function prototype for tcp poll callback functions. Called periodically as
98 * specified by @see tcp_poll.
100 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
101 * @param tpcb tcp pcb
102 * @return ERR_OK: try to send some data by calling tcp_output
103 * Only return ERR_ABRT if you have called tcp_abort from within the
106 typedef err_t (*tcp_poll_fn)(void *arg, struct tcp_pcb *tpcb);
108 /** Function prototype for tcp error callback functions. Called when the pcb
109 * receives a RST or is unexpectedly closed for any other reason.
111 * @note The corresponding pcb is already freed when this callback is called!
113 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
114 * @param err Error code to indicate why the pcb has been closed
115 * ERR_ABRT: aborted through tcp_abort or by a TCP timer
116 * ERR_RST: the connection was reset by the remote host
118 typedef void (*tcp_err_fn)(void *arg, err_t err);
120 /** Function prototype for tcp connected callback functions. Called when a pcb
121 * is connected to the remote side after initiating a connection attempt by
122 * calling tcp_connect().
124 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
125 * @param tpcb The connection pcb which is connected
126 * @param err An unused error code, always ERR_OK currently ;-) @todo!
127 * Only return ERR_ABRT if you have called tcp_abort from within the
130 * @note When a connection attempt fails, the error callback is currently called!
132 typedef err_t (*tcp_connected_fn)(void *arg, struct tcp_pcb *tpcb, err_t err);
135 #define RCV_WND_SCALE(pcb, wnd) (((wnd) >> (pcb)->rcv_scale))
136 #define SND_WND_SCALE(pcb, wnd) (((wnd) << (pcb)->snd_scale))
137 #define TCPWND16(x) ((u16_t)LWIP_MIN((x), 0xFFFF))
138 #define TCP_WND_MAX(pcb) ((tcpwnd_size_t)(((pcb)->flags & TF_WND_SCALE) ? TCP_WND : TCPWND16(TCP_WND)))
139 typedef u32_t tcpwnd_size_t;
141 #define RCV_WND_SCALE(pcb, wnd) (wnd)
142 #define SND_WND_SCALE(pcb, wnd) (wnd)
143 #define TCPWND16(x) (x)
144 #define TCP_WND_MAX(pcb) TCP_WND
145 typedef u16_t tcpwnd_size_t;
148 #if LWIP_WND_SCALE || TCP_LISTEN_BACKLOG || LWIP_TCP_TIMESTAMPS
149 typedef u16_t tcpflags_t;
151 typedef u8_t tcpflags_t;
169 * members common to struct tcp_pcb and struct tcp_listen_pcb
171 #define TCP_PCB_COMMON(type) \
172 type *next; /* for the linked list */ \
173 void *callback_arg; \
174 enum tcp_state state; /* TCP state */ \
176 /* ports are in host byte order */ \
180 /** the TCP protocol control block for listening pcbs */
181 struct tcp_pcb_listen {
182 /** Common members of all PCB types */
184 /** Protocol specific PCB members */
185 TCP_PCB_COMMON(struct tcp_pcb_listen);
187 #if LWIP_CALLBACK_API
188 /* Function to call when a listener has been connected. */
189 tcp_accept_fn accept;
190 #endif /* LWIP_CALLBACK_API */
192 #if TCP_LISTEN_BACKLOG
194 u8_t accepts_pending;
195 #endif /* TCP_LISTEN_BACKLOG */
199 /** the TCP protocol control block */
201 /** common PCB members */
203 /** protocol specific PCB members */
204 TCP_PCB_COMMON(struct tcp_pcb);
206 /* ports are in host byte order */
210 #define TF_ACK_DELAY 0x01U /* Delayed ACK. */
211 #define TF_ACK_NOW 0x02U /* Immediate ACK. */
212 #define TF_INFR 0x04U /* In fast recovery. */
213 #define TF_CLOSEPEND 0x08U /* If this is set, tcp_close failed to enqueue the FIN (retried in tcp_tmr) */
214 #define TF_RXCLOSED 0x10U /* rx closed by tcp_shutdown */
215 #define TF_FIN 0x20U /* Connection was closed locally (FIN segment enqueued). */
216 #define TF_NODELAY 0x40U /* Disable Nagle algorithm */
217 #define TF_NAGLEMEMERR 0x80U /* nagle enabled, memerr, try to output to prevent delayed ACK to happen */
219 #define TF_WND_SCALE 0x0100U /* Window Scale option enabled */
221 #if TCP_LISTEN_BACKLOG
222 #define TF_BACKLOGPEND 0x0200U /* If this is set, a connection pcb has increased the backlog on its listener */
224 #if LWIP_TCP_TIMESTAMPS
225 #define TF_TIMESTAMP 0x0400U /* Timestamp option enabled */
228 /* the rest of the fields are in host byte order
229 as we have to do some math with them */
232 u8_t polltmr, pollinterval;
236 /* receiver variables */
237 u32_t rcv_nxt; /* next seqno expected */
238 tcpwnd_size_t rcv_wnd; /* receiver window available */
239 tcpwnd_size_t rcv_ann_wnd; /* receiver window to announce */
240 u32_t rcv_ann_right_edge; /* announced right edge of window */
242 /* Retransmission timer. */
245 u16_t mss; /* maximum segment size */
247 /* RTT (round trip time) estimation variables */
248 u32_t rttest; /* RTT estimate in 500ms ticks */
249 u32_t rtseq; /* sequence number being timed */
250 s16_t sa, sv; /* @todo document this */
252 s16_t rto; /* retransmission time-out */
253 u8_t nrtx; /* number of retransmissions */
255 /* fast retransmit/recovery */
257 u32_t lastack; /* Highest acknowledged seqno. */
259 /* congestion avoidance/control variables */
261 tcpwnd_size_t ssthresh;
263 /* sender variables */
264 u32_t snd_nxt; /* next new seqno to be sent */
265 u32_t snd_wl1, snd_wl2; /* Sequence and acknowledgement numbers of last
267 u32_t snd_lbb; /* Sequence number of next byte to be buffered. */
268 tcpwnd_size_t snd_wnd; /* sender window */
269 tcpwnd_size_t snd_wnd_max; /* the maximum sender window announced by the remote host */
271 tcpwnd_size_t snd_buf; /* Available buffer space for sending (in bytes). */
272 #define TCP_SNDQUEUELEN_OVERFLOW (0xffffU-3)
273 u16_t snd_queuelen; /* Number of pbufs currently in the send buffer. */
276 /* Extra bytes available at the end of the last pbuf in unsent. */
277 u16_t unsent_oversize;
278 #endif /* TCP_OVERSIZE */
280 /* These are ordered by sequence number: */
281 struct tcp_seg *unsent; /* Unsent (queued) segments. */
282 struct tcp_seg *unacked; /* Sent but unacknowledged segments. */
284 struct tcp_seg *ooseq; /* Received out of sequence segments. */
285 #endif /* TCP_QUEUE_OOSEQ */
287 struct pbuf *refused_data; /* Data previously received but not yet taken by upper layer */
289 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
290 struct tcp_pcb_listen* listener;
291 #endif /* LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG */
293 #if LWIP_CALLBACK_API
294 /* Function to be called when more send buffer space is available. */
296 /* Function to be called when (in-sequence) data has arrived. */
298 /* Function to be called when a connection has been set up. */
299 tcp_connected_fn connected;
300 /* Function which is called periodically. */
302 /* Function to be called whenever a fatal error occurs. */
304 #endif /* LWIP_CALLBACK_API */
306 #if LWIP_TCP_TIMESTAMPS
307 u32_t ts_lastacksent;
309 #endif /* LWIP_TCP_TIMESTAMPS */
311 /* idle time before KEEPALIVE is sent */
313 #if LWIP_TCP_KEEPALIVE
316 #endif /* LWIP_TCP_KEEPALIVE */
318 /* Persist timer counter */
320 /* Persist timer back-off */
321 u8_t persist_backoff;
323 /* KEEPALIVE counter */
338 LWIP_EVENT_CONNECTED,
343 err_t lwip_tcp_event(void *arg, struct tcp_pcb *pcb,
349 #endif /* LWIP_EVENT_API */
351 /* Application program's interface: */
352 struct tcp_pcb * tcp_new (void);
353 struct tcp_pcb * tcp_new_ip_type (u8_t type);
355 void tcp_arg (struct tcp_pcb *pcb, void *arg);
356 #if LWIP_CALLBACK_API
357 void tcp_recv (struct tcp_pcb *pcb, tcp_recv_fn recv);
358 void tcp_sent (struct tcp_pcb *pcb, tcp_sent_fn sent);
359 void tcp_err (struct tcp_pcb *pcb, tcp_err_fn err);
360 void tcp_accept (struct tcp_pcb *pcb, tcp_accept_fn accept);
361 #endif /* LWIP_CALLBACK_API */
362 void tcp_poll (struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval);
364 #define tcp_set_flags(pcb, set_flags) do { (pcb)->flags = (tcpflags_t)((pcb)->flags | (set_flags)); } while(0)
365 #define tcp_clear_flags(pcb, clr_flags) do { (pcb)->flags = (tcpflags_t)((pcb)->flags & ~(clr_flags)); } while(0)
366 #define tcp_is_flag_set(pcb, flag) (((pcb)->flags & (flag)) != 0)
368 #if LWIP_TCP_TIMESTAMPS
369 #define tcp_mss(pcb) (((pcb)->flags & TF_TIMESTAMP) ? ((pcb)->mss - 12) : (pcb)->mss)
370 #else /* LWIP_TCP_TIMESTAMPS */
371 #define tcp_mss(pcb) ((pcb)->mss)
372 #endif /* LWIP_TCP_TIMESTAMPS */
373 #define tcp_sndbuf(pcb) (TCPWND16((pcb)->snd_buf))
374 #define tcp_sndqueuelen(pcb) ((pcb)->snd_queuelen)
375 /** @ingroup tcp_raw */
376 #define tcp_nagle_disable(pcb) tcp_set_flags(pcb, TF_NODELAY)
377 /** @ingroup tcp_raw */
378 #define tcp_nagle_enable(pcb) tcp_clear_flags(pcb, TF_NODELAY)
379 /** @ingroup tcp_raw */
380 #define tcp_nagle_disabled(pcb) tcp_is_flag_set(pcb, TF_NODELAY)
382 #if TCP_LISTEN_BACKLOG
383 #define tcp_backlog_set(pcb, new_backlog) do { \
384 LWIP_ASSERT("pcb->state == LISTEN (called for wrong pcb?)", (pcb)->state == LISTEN); \
385 ((struct tcp_pcb_listen *)(pcb))->backlog = ((new_backlog) ? (new_backlog) : 1); } while(0)
386 void tcp_backlog_delayed(struct tcp_pcb* pcb);
387 void tcp_backlog_accepted(struct tcp_pcb* pcb);
388 #else /* TCP_LISTEN_BACKLOG */
389 #define tcp_backlog_set(pcb, new_backlog)
390 #define tcp_backlog_delayed(pcb)
391 #define tcp_backlog_accepted(pcb)
392 #endif /* TCP_LISTEN_BACKLOG */
393 #define tcp_accepted(pcb) /* compatibility define, not needed any more */
395 void tcp_recved (struct tcp_pcb *pcb, u16_t len);
396 err_t tcp_bind (struct tcp_pcb *pcb, const ip_addr_t *ipaddr,
398 void tcp_bind_netif(struct tcp_pcb *pcb, const struct netif *netif);
399 err_t tcp_connect (struct tcp_pcb *pcb, const ip_addr_t *ipaddr,
400 u16_t port, tcp_connected_fn connected);
402 struct tcp_pcb * tcp_listen_with_backlog_and_err(struct tcp_pcb *pcb, u8_t backlog, err_t *err);
403 struct tcp_pcb * tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog);
404 /** @ingroup tcp_raw */
405 #define tcp_listen(pcb) tcp_listen_with_backlog(pcb, TCP_DEFAULT_LISTEN_BACKLOG)
407 void tcp_abort (struct tcp_pcb *pcb);
408 err_t tcp_close (struct tcp_pcb *pcb);
409 err_t tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx);
411 /* Flags for "apiflags" parameter in tcp_write */
412 #define TCP_WRITE_FLAG_COPY 0x01
413 #define TCP_WRITE_FLAG_MORE 0x02
415 err_t tcp_write (struct tcp_pcb *pcb, const void *dataptr, u16_t len,
418 void tcp_setprio (struct tcp_pcb *pcb, u8_t prio);
420 #define TCP_PRIO_MIN 1
421 #define TCP_PRIO_NORMAL 64
422 #define TCP_PRIO_MAX 127
424 err_t tcp_output (struct tcp_pcb *pcb);
427 const char* tcp_debug_state_str(enum tcp_state s);
429 /* for compatibility with older implementation */
430 #define tcp_new_ip6() tcp_new_ip_type(IPADDR_TYPE_V6)
436 #endif /* LWIP_TCP */
438 #endif /* LWIP_HDR_TCP_H */