STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
tcp.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  *
29  * Author: Adam Dunkels <adam@sics.se>
30  *
31  */
32 #ifndef LWIP_HDR_TCP_H
33 #define LWIP_HDR_TCP_H
34 
35 #include "lwip/opt.h"
36 
37 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
38 
39 #include "lwip/mem.h"
40 #include "lwip/pbuf.h"
41 #include "lwip/ip.h"
42 #include "lwip/icmp.h"
43 #include "lwip/err.h"
44 #include "lwip/ip6.h"
45 #include "lwip/ip6_addr.h"
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 struct tcp_pcb;
52 
62 typedef err_t (*tcp_accept_fn)(void *arg, struct tcp_pcb *newpcb, err_t err);
63 
74 typedef err_t (*tcp_recv_fn)(void *arg, struct tcp_pcb *tpcb,
75  struct pbuf *p, err_t err);
76 
88 typedef err_t (*tcp_sent_fn)(void *arg, struct tcp_pcb *tpcb,
89  u16_t len);
90 
100 typedef err_t (*tcp_poll_fn)(void *arg, struct tcp_pcb *tpcb);
101 
112 typedef void (*tcp_err_fn)(void *arg, err_t err);
113 
126 typedef err_t (*tcp_connected_fn)(void *arg, struct tcp_pcb *tpcb, err_t err);
127 
128 #if LWIP_WND_SCALE
129 #define RCV_WND_SCALE(pcb, wnd) (((wnd) >> (pcb)->rcv_scale))
130 #define SND_WND_SCALE(pcb, wnd) (((wnd) << (pcb)->snd_scale))
131 #define TCPWND16(x) ((u16_t)LWIP_MIN((x), 0xFFFF))
132 #define TCP_WND_MAX(pcb) ((tcpwnd_size_t)(((pcb)->flags & TF_WND_SCALE) ? TCP_WND : TCPWND16(TCP_WND)))
133 typedef u32_t tcpwnd_size_t;
134 typedef u16_t tcpflags_t;
135 #else
136 #define RCV_WND_SCALE(pcb, wnd) (wnd)
137 #define SND_WND_SCALE(pcb, wnd) (wnd)
138 #define TCPWND16(x) (x)
139 #define TCP_WND_MAX(pcb) TCP_WND
140 typedef u16_t tcpwnd_size_t;
141 typedef u8_t tcpflags_t;
142 #endif
143 
144 enum tcp_state {
145  CLOSED = 0,
146  LISTEN = 1,
147  SYN_SENT = 2,
148  SYN_RCVD = 3,
149  ESTABLISHED = 4,
150  FIN_WAIT_1 = 5,
151  FIN_WAIT_2 = 6,
152  CLOSE_WAIT = 7,
153  CLOSING = 8,
154  LAST_ACK = 9,
155  TIME_WAIT = 10
156 };
157 
158 #if LWIP_CALLBACK_API
159  /* Function to call when a listener has been connected.
160  * @param arg user-supplied argument (tcp_pcb.callback_arg)
161  * @param pcb a new tcp_pcb that now is connected
162  * @param err an error argument (TODO: that is current always ERR_OK?)
163  * @return ERR_OK: accept the new connection,
164  * any other err_t aborts the new connection
165  */
166 #define DEF_ACCEPT_CALLBACK tcp_accept_fn accept;
167 #else /* LWIP_CALLBACK_API */
168 #define DEF_ACCEPT_CALLBACK
169 #endif /* LWIP_CALLBACK_API */
170 
174 #define TCP_PCB_COMMON(type) \
175  type *next; /* for the linked list */ \
176  void *callback_arg; \
177  /* the accept callback for listen- and normal pcbs, if LWIP_CALLBACK_API */ \
178  DEF_ACCEPT_CALLBACK \
179  enum tcp_state state; /* TCP state */ \
180  u8_t prio; \
181  /* ports are in host byte order */ \
182  u16_t local_port
183 
184 
185 /* the TCP protocol control block */
186 struct tcp_pcb {
188  IP_PCB;
190  TCP_PCB_COMMON(struct tcp_pcb);
191 
192  /* ports are in host byte order */
193  u16_t remote_port;
194 
195  tcpflags_t flags;
196 #define TF_ACK_DELAY 0x01U /* Delayed ACK. */
197 #define TF_ACK_NOW 0x02U /* Immediate ACK. */
198 #define TF_INFR 0x04U /* In fast recovery. */
199 #define TF_TIMESTAMP 0x08U /* Timestamp option enabled */
200 #define TF_RXCLOSED 0x10U /* rx closed by tcp_shutdown */
201 #define TF_FIN 0x20U /* Connection was closed locally (FIN segment enqueued). */
202 #define TF_NODELAY 0x40U /* Disable Nagle algorithm */
203 #define TF_NAGLEMEMERR 0x80U /* nagle enabled, memerr, try to output to prevent delayed ACK to happen */
204 #if LWIP_WND_SCALE
205 #define TF_WND_SCALE 0x0100U /* Window Scale option enabled */
206 #endif
207 
208  /* the rest of the fields are in host byte order
209  as we have to do some math with them */
210 
211  /* Timers */
212  u8_t polltmr, pollinterval;
213  u8_t last_timer;
214  u32_t tmr;
215 
216  /* receiver variables */
217  u32_t rcv_nxt; /* next seqno expected */
218  tcpwnd_size_t rcv_wnd; /* receiver window available */
219  tcpwnd_size_t rcv_ann_wnd; /* receiver window to announce */
220  u32_t rcv_ann_right_edge; /* announced right edge of window */
221 
222  /* Retransmission timer. */
223  s16_t rtime;
224 
225  u16_t mss; /* maximum segment size */
226 
227  /* RTT (round trip time) estimation variables */
228  u32_t rttest; /* RTT estimate in 500ms ticks */
229  u32_t rtseq; /* sequence number being timed */
230  s16_t sa, sv; /* @todo document this */
231 
232  s16_t rto; /* retransmission time-out */
233  u8_t nrtx; /* number of retransmissions */
234 
235  /* fast retransmit/recovery */
236  u8_t dupacks;
237  u32_t lastack; /* Highest acknowledged seqno. */
238 
239  /* congestion avoidance/control variables */
240  tcpwnd_size_t cwnd;
241  tcpwnd_size_t ssthresh;
242 
243  /* sender variables */
244  u32_t snd_nxt; /* next new seqno to be sent */
245  u32_t snd_wl1, snd_wl2; /* Sequence and acknowledgement numbers of last
246  window update. */
247  u32_t snd_lbb; /* Sequence number of next byte to be buffered. */
248  tcpwnd_size_t snd_wnd; /* sender window */
249  tcpwnd_size_t snd_wnd_max; /* the maximum sender window announced by the remote host */
250 
251  tcpwnd_size_t acked;
252 
253  tcpwnd_size_t snd_buf; /* Available buffer space for sending (in bytes). */
254 #define TCP_SNDQUEUELEN_OVERFLOW (0xffffU-3)
255  u16_t snd_queuelen; /* Number of pbufs currently in the send buffer. */
256 
257 #if TCP_OVERSIZE
258  /* Extra bytes available at the end of the last pbuf in unsent. */
259  u16_t unsent_oversize;
260 #endif /* TCP_OVERSIZE */
261 
262  /* These are ordered by sequence number: */
263  struct tcp_seg *unsent; /* Unsent (queued) segments. */
264  struct tcp_seg *unacked; /* Sent but unacknowledged segments. */
265 #if TCP_QUEUE_OOSEQ
266  struct tcp_seg *ooseq; /* Received out of sequence segments. */
267 #endif /* TCP_QUEUE_OOSEQ */
268 
269  struct pbuf *refused_data; /* Data previously received but not yet taken by upper layer */
270 
271 #if LWIP_CALLBACK_API
272  /* Function to be called when more send buffer space is available. */
273  tcp_sent_fn sent;
274  /* Function to be called when (in-sequence) data has arrived. */
275  tcp_recv_fn recv;
276  /* Function to be called when a connection has been set up. */
277  tcp_connected_fn connected;
278  /* Function which is called periodically. */
279  tcp_poll_fn poll;
280  /* Function to be called whenever a fatal error occurs. */
281  tcp_err_fn errf;
282 #endif /* LWIP_CALLBACK_API */
283 
284 #if LWIP_TCP_TIMESTAMPS
285  u32_t ts_lastacksent;
286  u32_t ts_recent;
287 #endif /* LWIP_TCP_TIMESTAMPS */
288 
289  /* idle time before KEEPALIVE is sent */
290  u32_t keep_idle;
291 #if LWIP_TCP_KEEPALIVE
292  u32_t keep_intvl;
293  u32_t keep_cnt;
294 #endif /* LWIP_TCP_KEEPALIVE */
295 
296  /* Persist timer counter */
297  u8_t persist_cnt;
298  /* Persist timer back-off */
299  u8_t persist_backoff;
300 
301  /* KEEPALIVE counter */
302  u8_t keep_cnt_sent;
303 
304 #if LWIP_WND_SCALE
305  u8_t snd_scale;
306  u8_t rcv_scale;
307 #endif
308 };
309 
310 struct tcp_pcb_listen {
311 /* Common members of all PCB types */
312  IP_PCB;
313 /* Protocol specific PCB members */
314  TCP_PCB_COMMON(struct tcp_pcb_listen);
315 
316 #if TCP_LISTEN_BACKLOG
317  u8_t backlog;
318  u8_t accepts_pending;
319 #endif /* TCP_LISTEN_BACKLOG */
320 #if LWIP_IPV4 && LWIP_IPV6
321  u8_t accept_any_ip_version;
322 #endif /* LWIP_IPV4 && LWIP_IPV6 */
323 };
324 
325 #if LWIP_EVENT_API
326 
327 enum lwip_event {
328  LWIP_EVENT_ACCEPT,
329  LWIP_EVENT_SENT,
330  LWIP_EVENT_RECV,
331  LWIP_EVENT_CONNECTED,
332  LWIP_EVENT_POLL,
333  LWIP_EVENT_ERR
334 };
335 
336 err_t lwip_tcp_event(void *arg, struct tcp_pcb *pcb,
337  enum lwip_event,
338  struct pbuf *p,
339  u16_t size,
340  err_t err);
341 
342 #endif /* LWIP_EVENT_API */
343 
344 /* Application program's interface: */
345 struct tcp_pcb * tcp_new (void);
346 
347 void tcp_arg (struct tcp_pcb *pcb, void *arg);
348 void tcp_accept (struct tcp_pcb *pcb, tcp_accept_fn accept);
349 void tcp_recv (struct tcp_pcb *pcb, tcp_recv_fn recv);
350 void tcp_sent (struct tcp_pcb *pcb, tcp_sent_fn sent);
351 void tcp_poll (struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval);
352 void tcp_err (struct tcp_pcb *pcb, tcp_err_fn err);
353 
354 #define tcp_mss(pcb) (((pcb)->flags & TF_TIMESTAMP) ? ((pcb)->mss - 12) : (pcb)->mss)
355 #define tcp_sndbuf(pcb) (TCPWND16((pcb)->snd_buf))
356 #define tcp_sndqueuelen(pcb) ((pcb)->snd_queuelen)
357 #define tcp_nagle_disable(pcb) ((pcb)->flags |= TF_NODELAY)
358 #define tcp_nagle_enable(pcb) ((pcb)->flags = (tcpflags_t)((pcb)->flags & ~TF_NODELAY))
359 #define tcp_nagle_disabled(pcb) (((pcb)->flags & TF_NODELAY) != 0)
360 
361 #if TCP_LISTEN_BACKLOG
362 #define tcp_accepted(pcb) do { \
363  LWIP_ASSERT("pcb->state == LISTEN (called for wrong pcb?)", pcb->state == LISTEN); \
364  (((struct tcp_pcb_listen *)(pcb))->accepts_pending--); } while(0)
365 #else /* TCP_LISTEN_BACKLOG */
366 #define tcp_accepted(pcb) LWIP_ASSERT("pcb->state == LISTEN (called for wrong pcb?)", \
367  (pcb)->state == LISTEN)
368 #endif /* TCP_LISTEN_BACKLOG */
369 
370 void tcp_recved (struct tcp_pcb *pcb, u16_t len);
371 err_t tcp_bind (struct tcp_pcb *pcb, const ip_addr_t *ipaddr,
372  u16_t port);
373 err_t tcp_connect (struct tcp_pcb *pcb, const ip_addr_t *ipaddr,
374  u16_t port, tcp_connected_fn connected);
375 
376 struct tcp_pcb * tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog);
377 #define tcp_listen(pcb) tcp_listen_with_backlog(pcb, TCP_DEFAULT_LISTEN_BACKLOG)
378 
379 void tcp_abort (struct tcp_pcb *pcb);
380 err_t tcp_close (struct tcp_pcb *pcb);
381 err_t tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx);
382 
383 /* Flags for "apiflags" parameter in tcp_write */
384 #define TCP_WRITE_FLAG_COPY 0x01
385 #define TCP_WRITE_FLAG_MORE 0x02
386 
387 err_t tcp_write (struct tcp_pcb *pcb, const void *dataptr, u16_t len,
388  u8_t apiflags);
389 
390 void tcp_setprio (struct tcp_pcb *pcb, u8_t prio);
391 
392 #define TCP_PRIO_MIN 1
393 #define TCP_PRIO_NORMAL 64
394 #define TCP_PRIO_MAX 127
395 
396 err_t tcp_output (struct tcp_pcb *pcb);
397 
398 
399 const char* tcp_debug_state_str(enum tcp_state s);
400 
401 #if LWIP_IPV6
402 struct tcp_pcb * tcp_new_ip6 (void);
403 #endif /* LWIP_IPV6 */
404 #if LWIP_IPV4 && LWIP_IPV6
405 struct tcp_pcb * tcp_listen_dual_with_backlog(struct tcp_pcb *pcb, u8_t backlog);
406 #define tcp_listen_dual(pcb) tcp_listen_dual_with_backlog(pcb, TCP_DEFAULT_LISTEN_BACKLOG)
407 #else /* LWIP_IPV4 && LWIP_IPV6 */
408 #define tcp_listen_dual_with_backlog(pcb, backlog) tcp_listen_with_backlog(pcb, backlog)
409 #define tcp_listen_dual(pcb) tcp_listen(pcb)
410 #endif /* LWIP_IPV4 && LWIP_IPV6 */
411 
412 
413 #ifdef __cplusplus
414 }
415 #endif
416 
417 #endif /* LWIP_TCP */
418 
419 #endif /* LWIP_HDR_TCP_H */
420 
signed short s16_t
Definition: cc.h:41
#define IP_PCB
Definition: ip.h:96
unsigned long u32_t
Definition: cc.h:42
Definition: pbuf.h:108
s8_t err_t
Definition: err.h:47
unsigned char u8_t
Definition: cc.h:38
ip6_addr_t ip_addr_t
Definition: ip_addr.h:194
unsigned short u16_t
Definition: cc.h:40