FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Common / ethernet / lwIP_130 / src / include / lwip / tcp.h
blob9912be084b36b71d389daf6f945fa5fa3b09083e
1 /*
2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
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.
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.
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
32 #ifndef __LWIP_TCP_H__
33 #define __LWIP_TCP_H__
35 #include "lwip/opt.h"
37 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
39 #include "lwip/sys.h"
40 #include "lwip/mem.h"
41 #include "lwip/pbuf.h"
42 #include "lwip/ip.h"
43 #include "lwip/icmp.h"
44 #include "lwip/err.h"
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
50 struct tcp_pcb;
52 /* Functions for interfacing with TCP: */
54 /* Lower layer interface to TCP: */
55 #define tcp_init() /* Compatibility define, not init needed. */
56 void tcp_tmr (void); /* Must be called every
57 TCP_TMR_INTERVAL
58 ms. (Typically 250 ms). */
59 /* Application program's interface: */
60 struct tcp_pcb * tcp_new (void);
61 struct tcp_pcb * tcp_alloc (u8_t prio);
63 void tcp_arg (struct tcp_pcb *pcb, void *arg);
64 void tcp_accept (struct tcp_pcb *pcb,
65 err_t (* accept)(void *arg, struct tcp_pcb *newpcb,
66 err_t err));
67 void tcp_recv (struct tcp_pcb *pcb,
68 err_t (* recv)(void *arg, struct tcp_pcb *tpcb,
69 struct pbuf *p, err_t err));
70 void tcp_sent (struct tcp_pcb *pcb,
71 err_t (* sent)(void *arg, struct tcp_pcb *tpcb,
72 u16_t len));
73 void tcp_poll (struct tcp_pcb *pcb,
74 err_t (* poll)(void *arg, struct tcp_pcb *tpcb),
75 u8_t interval);
76 void tcp_err (struct tcp_pcb *pcb,
77 void (* err)(void *arg, err_t err));
79 #define tcp_mss(pcb) ((pcb)->mss)
80 #define tcp_sndbuf(pcb) ((pcb)->snd_buf)
82 #if TCP_LISTEN_BACKLOG
83 #define tcp_accepted(pcb) (((struct tcp_pcb_listen *)(pcb))->accepts_pending--)
84 #else /* TCP_LISTEN_BACKLOG */
85 #define tcp_accepted(pcb)
86 #endif /* TCP_LISTEN_BACKLOG */
88 void tcp_recved (struct tcp_pcb *pcb, u16_t len);
89 err_t tcp_bind (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
90 u16_t port);
91 err_t tcp_connect (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
92 u16_t port, err_t (* connected)(void *arg,
93 struct tcp_pcb *tpcb,
94 err_t err));
96 struct tcp_pcb * tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog);
97 #define tcp_listen(pcb) tcp_listen_with_backlog(pcb, TCP_DEFAULT_LISTEN_BACKLOG)
99 void tcp_abort (struct tcp_pcb *pcb);
100 err_t tcp_close (struct tcp_pcb *pcb);
102 /* Flags for "apiflags" parameter in tcp_write and tcp_enqueue */
103 #define TCP_WRITE_FLAG_COPY 0x01
104 #define TCP_WRITE_FLAG_MORE 0x02
106 err_t tcp_write (struct tcp_pcb *pcb, const void *dataptr, u16_t len,
107 u8_t apiflags);
109 void tcp_setprio (struct tcp_pcb *pcb, u8_t prio);
111 #define TCP_PRIO_MIN 1
112 #define TCP_PRIO_NORMAL 64
113 #define TCP_PRIO_MAX 127
115 /* It is also possible to call these two functions at the right
116 intervals (instead of calling tcp_tmr()). */
117 void tcp_slowtmr (void);
118 void tcp_fasttmr (void);
121 /* Only used by IP to pass a TCP segment to TCP: */
122 void tcp_input (struct pbuf *p, struct netif *inp);
123 /* Used within the TCP code only: */
124 err_t tcp_output (struct tcp_pcb *pcb);
125 void tcp_rexmit (struct tcp_pcb *pcb);
126 void tcp_rexmit_rto (struct tcp_pcb *pcb);
129 * This is the Nagle algorithm: inhibit the sending of new TCP
130 * segments when new outgoing data arrives from the user if any
131 * previously transmitted data on the connection remains
132 * unacknowledged.
134 #define tcp_do_output_nagle(tpcb) ((((tpcb)->unacked == NULL) || \
135 ((tpcb)->flags & TF_NODELAY) || \
136 (((tpcb)->unsent != NULL) && ((tpcb)->unsent->next != NULL))) ? \
137 1 : 0)
138 #define tcp_output_nagle(tpcb) (tcp_do_output_nagle(tpcb) ? tcp_output(tpcb) : ERR_OK)
141 /** This returns a TCP header option for MSS in an u32_t */
142 #define TCP_BUILD_MSS_OPTION() htonl(((u32_t)2 << 24) | \
143 ((u32_t)4 << 16) | \
144 (((u32_t)TCP_MSS / 256) << 8) | \
145 (TCP_MSS & 255))
147 #define TCP_SEQ_LT(a,b) ((s32_t)((a)-(b)) < 0)
148 #define TCP_SEQ_LEQ(a,b) ((s32_t)((a)-(b)) <= 0)
149 #define TCP_SEQ_GT(a,b) ((s32_t)((a)-(b)) > 0)
150 #define TCP_SEQ_GEQ(a,b) ((s32_t)((a)-(b)) >= 0)
151 /* is b<=a<=c? */
152 #if 0 /* see bug #10548 */
153 #define TCP_SEQ_BETWEEN(a,b,c) ((c)-(b) >= (a)-(b))
154 #endif
155 #define TCP_SEQ_BETWEEN(a,b,c) (TCP_SEQ_GEQ(a,b) && TCP_SEQ_LEQ(a,c))
156 #define TCP_FIN 0x01U
157 #define TCP_SYN 0x02U
158 #define TCP_RST 0x04U
159 #define TCP_PSH 0x08U
160 #define TCP_ACK 0x10U
161 #define TCP_URG 0x20U
162 #define TCP_ECE 0x40U
163 #define TCP_CWR 0x80U
165 #define TCP_FLAGS 0x3fU
167 /* Length of the TCP header, excluding options. */
168 #define TCP_HLEN 20
170 #ifndef TCP_TMR_INTERVAL
171 #define TCP_TMR_INTERVAL 250 /* The TCP timer interval in milliseconds. */
172 #endif /* TCP_TMR_INTERVAL */
174 #ifndef TCP_FAST_INTERVAL
175 #define TCP_FAST_INTERVAL TCP_TMR_INTERVAL /* the fine grained timeout in milliseconds */
176 #endif /* TCP_FAST_INTERVAL */
178 #ifndef TCP_SLOW_INTERVAL
179 #define TCP_SLOW_INTERVAL (2*TCP_TMR_INTERVAL) /* the coarse grained timeout in milliseconds */
180 #endif /* TCP_SLOW_INTERVAL */
182 #define TCP_FIN_WAIT_TIMEOUT 20000 /* milliseconds */
183 #define TCP_SYN_RCVD_TIMEOUT 20000 /* milliseconds */
185 #define TCP_OOSEQ_TIMEOUT 6U /* x RTO */
187 #ifndef TCP_MSL
188 #define TCP_MSL 60000U /* The maximum segment lifetime in milliseconds */
189 #endif
191 /* Keepalive values, compliant with RFC 1122. Don't change this unless you know what you're doing */
192 #ifndef TCP_KEEPIDLE_DEFAULT
193 #define TCP_KEEPIDLE_DEFAULT 7200000UL /* Default KEEPALIVE timer in milliseconds */
194 #endif
196 #ifndef TCP_KEEPINTVL_DEFAULT
197 #define TCP_KEEPINTVL_DEFAULT 75000UL /* Default Time between KEEPALIVE probes in milliseconds */
198 #endif
200 #ifndef TCP_KEEPCNT_DEFAULT
201 #define TCP_KEEPCNT_DEFAULT 9U /* Default Counter for KEEPALIVE probes */
202 #endif
204 #define TCP_MAXIDLE TCP_KEEPCNT_DEFAULT * TCP_KEEPINTVL_DEFAULT /* Maximum KEEPALIVE probe time */
206 /* Fields are (of course) in network byte order.
207 * Some fields are converted to host byte order in tcp_input().
209 #ifdef PACK_STRUCT_USE_INCLUDES
210 # include "arch/bpstruct.h"
211 #endif
212 PACK_STRUCT_BEGIN
213 #if (defined(__MWERKS__) || defined(__CWCC__))
214 #pragma options align= packed
215 #endif
216 struct tcp_hdr {
217 PACK_STRUCT_FIELD(u16_t src);
218 PACK_STRUCT_FIELD(u16_t dest);
219 PACK_STRUCT_FIELD(u32_t seqno);
220 PACK_STRUCT_FIELD(u32_t ackno);
221 PACK_STRUCT_FIELD(u16_t _hdrlen_rsvd_flags);
222 PACK_STRUCT_FIELD(u16_t wnd);
223 PACK_STRUCT_FIELD(u16_t chksum);
224 PACK_STRUCT_FIELD(u16_t urgp);
225 } PACK_STRUCT_STRUCT;
226 PACK_STRUCT_END
227 #ifdef PACK_STRUCT_USE_INCLUDES
228 # include "arch/epstruct.h"
229 #endif
231 #define TCPH_OFFSET(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 8)
232 #define TCPH_HDRLEN(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 12)
233 #define TCPH_FLAGS(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS)
235 #define TCPH_OFFSET_SET(phdr, offset) (phdr)->_hdrlen_rsvd_flags = htons(((offset) << 8) | TCPH_FLAGS(phdr))
236 #define TCPH_HDRLEN_SET(phdr, len) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | TCPH_FLAGS(phdr))
237 #define TCPH_FLAGS_SET(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons((ntohs((phdr)->_hdrlen_rsvd_flags) & ~TCP_FLAGS) | (flags))
238 #define TCPH_SET_FLAG(phdr, flags ) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (flags))
239 #define TCPH_UNSET_FLAG(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (TCPH_FLAGS(phdr) & ~(flags)) )
241 #define TCP_TCPLEN(seg) ((seg)->len + ((TCPH_FLAGS((seg)->tcphdr) & TCP_FIN || \
242 TCPH_FLAGS((seg)->tcphdr) & TCP_SYN)? 1: 0))
244 enum tcp_state {
245 CLOSED = 0,
246 LISTEN = 1,
247 SYN_SENT = 2,
248 SYN_RCVD = 3,
249 ESTABLISHED = 4,
250 FIN_WAIT_1 = 5,
251 FIN_WAIT_2 = 6,
252 CLOSE_WAIT = 7,
253 CLOSING = 8,
254 LAST_ACK = 9,
255 TIME_WAIT = 10
258 /** Flags used on input processing, not on pcb->flags
260 #define TF_RESET (u8_t)0x08U /* Connection was reset. */
261 #define TF_CLOSED (u8_t)0x10U /* Connection was sucessfully closed. */
262 #define TF_GOT_FIN (u8_t)0x20U /* Connection was closed by the remote end. */
265 * members common to struct tcp_pcb and struct tcp_listen_pcb
267 #define TCP_PCB_COMMON(type) \
268 type *next; /* for the linked list */ \
269 enum tcp_state state; /* TCP state */ \
270 u8_t prio; \
271 void *callback_arg; \
272 /* ports are in host byte order */ \
273 u16_t local_port
275 /* the TCP protocol control block */
276 struct tcp_pcb {
277 /** common PCB members */
278 IP_PCB;
279 /** protocol specific PCB members */
280 TCP_PCB_COMMON(struct tcp_pcb);
282 /* ports are in host byte order */
283 u16_t remote_port;
285 u8_t flags;
286 #define TF_ACK_DELAY (u8_t)0x01U /* Delayed ACK. */
287 #define TF_ACK_NOW (u8_t)0x02U /* Immediate ACK. */
288 #define TF_INFR (u8_t)0x04U /* In fast recovery. */
289 #define TF_FIN (u8_t)0x20U /* Connection was closed locally (FIN segment enqueued). */
290 #define TF_NODELAY (u8_t)0x40U /* Disable Nagle algorithm */
291 #define TF_NAGLEMEMERR (u8_t)0x80U /* nagle enabled, memerr, try to output to prevent delayed ACK to happen */
293 /* the rest of the fields are in host byte order
294 as we have to do some math with them */
295 /* receiver variables */
296 u32_t rcv_nxt; /* next seqno expected */
297 u16_t rcv_wnd; /* receiver window */
298 u16_t rcv_ann_wnd; /* announced receive window */
300 /* Timers */
301 u32_t tmr;
302 u8_t polltmr, pollinterval;
304 /* Retransmission timer. */
305 s16_t rtime;
307 u16_t mss; /* maximum segment size */
309 /* RTT (round trip time) estimation variables */
310 u32_t rttest; /* RTT estimate in 500ms ticks */
311 u32_t rtseq; /* sequence number being timed */
312 s16_t sa, sv; /* @todo document this */
314 s16_t rto; /* retransmission time-out */
315 u8_t nrtx; /* number of retransmissions */
317 /* fast retransmit/recovery */
318 u32_t lastack; /* Highest acknowledged seqno. */
319 u8_t dupacks;
321 /* congestion avoidance/control variables */
322 u16_t cwnd;
323 u16_t ssthresh;
325 /* sender variables */
326 u32_t snd_nxt, /* next seqno to be sent */
327 snd_max; /* Highest seqno sent. */
328 u16_t snd_wnd; /* sender window */
329 u32_t snd_wl1, snd_wl2, /* Sequence and acknowledgement numbers of last
330 window update. */
331 snd_lbb; /* Sequence number of next byte to be buffered. */
333 u16_t acked;
335 u16_t snd_buf; /* Available buffer space for sending (in bytes). */
336 #define TCP_SNDQUEUELEN_OVERFLOW (0xffff-3)
337 u16_t snd_queuelen; /* Available buffer space for sending (in tcp_segs). */
340 /* These are ordered by sequence number: */
341 struct tcp_seg *unsent; /* Unsent (queued) segments. */
342 struct tcp_seg *unacked; /* Sent but unacknowledged segments. */
343 #if TCP_QUEUE_OOSEQ
344 struct tcp_seg *ooseq; /* Received out of sequence segments. */
345 #endif /* TCP_QUEUE_OOSEQ */
347 struct pbuf *refused_data; /* Data previously received but not yet taken by upper layer */
349 #if LWIP_CALLBACK_API
350 /* Function to be called when more send buffer space is available.
351 * @param arg user-supplied argument (tcp_pcb.callback_arg)
352 * @param pcb the tcp_pcb which has send buffer space available
353 * @param space the amount of bytes available
354 * @return ERR_OK: try to send some data by calling tcp_output
356 err_t (* sent)(void *arg, struct tcp_pcb *pcb, u16_t space);
358 /* Function to be called when (in-sequence) data has arrived.
359 * @param arg user-supplied argument (tcp_pcb.callback_arg)
360 * @param pcb the tcp_pcb for which data has arrived
361 * @param p the packet buffer which arrived
362 * @param err an error argument (TODO: that is current always ERR_OK?)
363 * @return ERR_OK: try to send some data by calling tcp_output
365 err_t (* recv)(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
367 /* Function to be called when a connection has been set up.
368 * @param arg user-supplied argument (tcp_pcb.callback_arg)
369 * @param pcb the tcp_pcb that now is connected
370 * @param err an error argument (TODO: that is current always ERR_OK?)
371 * @return value is currently ignored
373 err_t (* connected)(void *arg, struct tcp_pcb *pcb, err_t err);
375 /* Function to call when a listener has been connected.
376 * @param arg user-supplied argument (tcp_pcb.callback_arg)
377 * @param pcb a new tcp_pcb that now is connected
378 * @param err an error argument (TODO: that is current always ERR_OK?)
379 * @return ERR_OK: accept the new connection,
380 * any other err_t abortsthe new connection
382 err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
384 /* Function which is called periodically.
385 * The period can be adjusted in multiples of the TCP slow timer interval
386 * by changing tcp_pcb.polltmr.
387 * @param arg user-supplied argument (tcp_pcb.callback_arg)
388 * @param pcb the tcp_pcb to poll for
389 * @return ERR_OK: try to send some data by calling tcp_output
391 err_t (* poll)(void *arg, struct tcp_pcb *pcb);
393 /* Function to be called whenever a fatal error occurs.
394 * There is no pcb parameter since most of the times, the pcb is
395 * already deallocated (or there is no pcb) when this function is called.
396 * @param arg user-supplied argument (tcp_pcb.callback_arg)
397 * @param err an indication why the error callback is called:
398 * ERR_ABRT: aborted through tcp_abort or by a TCP timer
399 * ERR_RST: the connection was reset by the remote host
401 void (* errf)(void *arg, err_t err);
402 #endif /* LWIP_CALLBACK_API */
404 /* idle time before KEEPALIVE is sent */
405 u32_t keep_idle;
406 #if LWIP_TCP_KEEPALIVE
407 u32_t keep_intvl;
408 u32_t keep_cnt;
409 #endif /* LWIP_TCP_KEEPALIVE */
411 /* Persist timer counter */
412 u32_t persist_cnt;
413 /* Persist timer back-off */
414 u8_t persist_backoff;
416 /* KEEPALIVE counter */
417 u8_t keep_cnt_sent;
420 struct tcp_pcb_listen {
421 /* Common members of all PCB types */
422 IP_PCB;
423 /* Protocol specific PCB members */
424 TCP_PCB_COMMON(struct tcp_pcb_listen);
426 #if LWIP_CALLBACK_API
427 /* Function to call when a listener has been connected.
428 * @param arg user-supplied argument (tcp_pcb.callback_arg)
429 * @param pcb a new tcp_pcb that now is connected
430 * @param err an error argument (TODO: that is current always ERR_OK?)
431 * @return ERR_OK: accept the new connection,
432 * any other err_t abortsthe new connection
434 err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
435 #endif /* LWIP_CALLBACK_API */
436 #if TCP_LISTEN_BACKLOG
437 u8_t backlog;
438 u8_t accepts_pending;
439 #endif /* TCP_LISTEN_BACKLOG */
442 #if LWIP_EVENT_API
444 enum lwip_event {
445 LWIP_EVENT_ACCEPT,
446 LWIP_EVENT_SENT,
447 LWIP_EVENT_RECV,
448 LWIP_EVENT_CONNECTED,
449 LWIP_EVENT_POLL,
450 LWIP_EVENT_ERR
453 err_t lwip_tcp_event(void *arg, struct tcp_pcb *pcb,
454 enum lwip_event,
455 struct pbuf *p,
456 u16_t size,
457 err_t err);
459 #define TCP_EVENT_ACCEPT(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
460 LWIP_EVENT_ACCEPT, NULL, 0, err)
461 #define TCP_EVENT_SENT(pcb,space,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
462 LWIP_EVENT_SENT, NULL, space, ERR_OK)
463 #define TCP_EVENT_RECV(pcb,p,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
464 LWIP_EVENT_RECV, (p), 0, (err))
465 #define TCP_EVENT_CONNECTED(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
466 LWIP_EVENT_CONNECTED, NULL, 0, (err))
467 #define TCP_EVENT_POLL(pcb,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
468 LWIP_EVENT_POLL, NULL, 0, ERR_OK)
469 #define TCP_EVENT_ERR(errf,arg,err) lwip_tcp_event((arg), NULL, \
470 LWIP_EVENT_ERR, NULL, 0, (err))
471 #else /* LWIP_EVENT_API */
472 #define TCP_EVENT_ACCEPT(pcb,err,ret) \
473 if((pcb)->accept != NULL) \
474 (ret = (pcb)->accept((pcb)->callback_arg,(pcb),(err)))
475 #define TCP_EVENT_SENT(pcb,space,ret) \
476 if((pcb)->sent != NULL) \
477 (ret = (pcb)->sent((pcb)->callback_arg,(pcb),(space)))
478 #define TCP_EVENT_RECV(pcb,p,err,ret) \
479 if((pcb)->recv != NULL) \
480 { ret = (pcb)->recv((pcb)->callback_arg,(pcb),(p),(err)); } else { \
481 ret = ERR_OK; \
482 if (p) pbuf_free(p); }
483 #define TCP_EVENT_CONNECTED(pcb,err,ret) \
484 if((pcb)->connected != NULL) \
485 (ret = (pcb)->connected((pcb)->callback_arg,(pcb),(err)))
486 #define TCP_EVENT_POLL(pcb,ret) \
487 if((pcb)->poll != NULL) \
488 (ret = (pcb)->poll((pcb)->callback_arg,(pcb)))
489 #define TCP_EVENT_ERR(errf,arg,err) \
490 if((errf) != NULL) \
491 (errf)((arg),(err))
492 #endif /* LWIP_EVENT_API */
494 /* This structure represents a TCP segment on the unsent and unacked queues */
495 struct tcp_seg {
496 struct tcp_seg *next; /* used when putting segements on a queue */
497 struct pbuf *p; /* buffer containing data + TCP header */
498 void *dataptr; /* pointer to the TCP data in the pbuf */
499 u16_t len; /* the TCP length of this segment */
500 struct tcp_hdr *tcphdr; /* the TCP header */
503 /* Internal functions and global variables: */
504 struct tcp_pcb *tcp_pcb_copy(struct tcp_pcb *pcb);
505 void tcp_pcb_purge(struct tcp_pcb *pcb);
506 void tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb);
508 u8_t tcp_segs_free(struct tcp_seg *seg);
509 u8_t tcp_seg_free(struct tcp_seg *seg);
510 struct tcp_seg *tcp_seg_copy(struct tcp_seg *seg);
512 #define tcp_ack(pcb) if((pcb)->flags & TF_ACK_DELAY) { \
513 (pcb)->flags &= ~TF_ACK_DELAY; \
514 (pcb)->flags |= TF_ACK_NOW; \
515 tcp_output(pcb); \
516 } else { \
517 (pcb)->flags |= TF_ACK_DELAY; \
520 #define tcp_ack_now(pcb) (pcb)->flags |= TF_ACK_NOW; \
521 tcp_output(pcb)
523 err_t tcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags);
524 err_t tcp_enqueue(struct tcp_pcb *pcb, void *dataptr, u16_t len,
525 u8_t flags, u8_t apiflags,
526 u8_t *optdata, u8_t optlen);
528 void tcp_rexmit_seg(struct tcp_pcb *pcb, struct tcp_seg *seg);
530 void tcp_rst(u32_t seqno, u32_t ackno,
531 struct ip_addr *local_ip, struct ip_addr *remote_ip,
532 u16_t local_port, u16_t remote_port);
534 u32_t tcp_next_iss(void);
536 void tcp_keepalive(struct tcp_pcb *pcb);
537 void tcp_zero_window_probe(struct tcp_pcb *pcb);
539 #if TCP_CALCULATE_EFF_SEND_MSS
540 u16_t tcp_eff_send_mss(u16_t sendmss, struct ip_addr *addr);
541 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
543 extern struct tcp_pcb *tcp_input_pcb;
544 extern u32_t tcp_ticks;
546 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
547 void tcp_debug_print(struct tcp_hdr *tcphdr);
548 void tcp_debug_print_flags(u8_t flags);
549 void tcp_debug_print_state(enum tcp_state s);
550 void tcp_debug_print_pcbs(void);
551 s16_t tcp_pcbs_sane(void);
552 #else
553 # define tcp_debug_print(tcphdr)
554 # define tcp_debug_print_flags(flags)
555 # define tcp_debug_print_state(s)
556 # define tcp_debug_print_pcbs()
557 # define tcp_pcbs_sane() 1
558 #endif /* TCP_DEBUG */
560 #if NO_SYS
561 #define tcp_timer_needed()
562 #else
563 void tcp_timer_needed(void);
564 #endif
566 /* The TCP PCB lists. */
567 union tcp_listen_pcbs_t { /* List of all TCP PCBs in LISTEN state. */
568 struct tcp_pcb_listen *listen_pcbs;
569 struct tcp_pcb *pcbs;
571 extern union tcp_listen_pcbs_t tcp_listen_pcbs;
572 extern struct tcp_pcb *tcp_active_pcbs; /* List of all TCP PCBs that are in a
573 state in which they accept or send
574 data. */
575 extern struct tcp_pcb *tcp_tw_pcbs; /* List of all TCP PCBs in TIME-WAIT. */
577 extern struct tcp_pcb *tcp_tmp_pcb; /* Only used for temporary storage. */
579 /* Axioms about the above lists:
580 1) Every TCP PCB that is not CLOSED is in one of the lists.
581 2) A PCB is only in one of the lists.
582 3) All PCBs in the tcp_listen_pcbs list is in LISTEN state.
583 4) All PCBs in the tcp_tw_pcbs list is in TIME-WAIT state.
586 /* Define two macros, TCP_REG and TCP_RMV that registers a TCP PCB
587 with a PCB list or removes a PCB from a list, respectively. */
588 #if 0
589 #define TCP_REG(pcbs, npcb) do {\
590 LWIP_DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %d\n", npcb, npcb->local_port)); \
591 for(tcp_tmp_pcb = *pcbs; \
592 tcp_tmp_pcb != NULL; \
593 tcp_tmp_pcb = tcp_tmp_pcb->next) { \
594 LWIP_ASSERT("TCP_REG: already registered\n", tcp_tmp_pcb != npcb); \
596 LWIP_ASSERT("TCP_REG: pcb->state != CLOSED", npcb->state != CLOSED); \
597 npcb->next = *pcbs; \
598 LWIP_ASSERT("TCP_REG: npcb->next != npcb", npcb->next != npcb); \
599 *(pcbs) = npcb; \
600 LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
601 tcp_timer_needed(); \
602 } while(0)
603 #define TCP_RMV(pcbs, npcb) do { \
604 LWIP_ASSERT("TCP_RMV: pcbs != NULL", *pcbs != NULL); \
605 LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removing %p from %p\n", npcb, *pcbs)); \
606 if(*pcbs == npcb) { \
607 *pcbs = (*pcbs)->next; \
608 } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
609 if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \
610 tcp_tmp_pcb->next = npcb->next; \
611 break; \
614 npcb->next = NULL; \
615 LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
616 LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removed %p from %p\n", npcb, *pcbs)); \
617 } while(0)
619 #else /* LWIP_DEBUG */
620 #define TCP_REG(pcbs, npcb) do { \
621 npcb->next = *pcbs; \
622 *(pcbs) = npcb; \
623 tcp_timer_needed(); \
624 } while(0)
625 #define TCP_RMV(pcbs, npcb) do { \
626 if(*(pcbs) == npcb) { \
627 (*(pcbs)) = (*pcbs)->next; \
628 } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
629 if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \
630 tcp_tmp_pcb->next = npcb->next; \
631 break; \
634 npcb->next = NULL; \
635 } while(0)
636 #endif /* LWIP_DEBUG */
638 #ifdef __cplusplus
640 #endif
642 #endif /* LWIP_TCP */
644 #endif /* __LWIP_TCP_H__ */