32 #ifndef __LWIP_TCP_IMPL_H__
33 #define __LWIP_TCP_IMPL_H__
44 #include "lwip/icmp.h"
60 void tcp_slowtmr (
void);
61 void tcp_fasttmr (
void);
65 void tcp_input (
struct pbuf *p,
struct netif *inp);
67 struct tcp_pcb * tcp_alloc (
u8_t prio);
68 void tcp_abandon (
struct tcp_pcb *pcb,
int reset);
69 err_t tcp_send_empty_ack(
struct tcp_pcb *pcb);
70 void tcp_rexmit (
struct tcp_pcb *pcb);
71 void tcp_rexmit_rto (
struct tcp_pcb *pcb);
72 void tcp_rexmit_fast (
struct tcp_pcb *pcb);
73 u32_t tcp_update_rcv_ann_wnd(
struct tcp_pcb *pcb);
84 #define tcp_do_output_nagle(tpcb) ((((tpcb)->unacked == NULL) || \
85 ((tpcb)->flags & (TF_NODELAY | TF_INFR)) || \
86 (((tpcb)->unsent != NULL) && (((tpcb)->unsent->next != NULL) || \
87 ((tpcb)->unsent->len >= (tpcb)->mss))) \
89 #define tcp_output_nagle(tpcb) (tcp_do_output_nagle(tpcb) ? tcp_output(tpcb) : ERR_OK)
92 #define TCP_SEQ_LT(a,b) ((s32_t)((a)-(b)) < 0)
93 #define TCP_SEQ_LEQ(a,b) ((s32_t)((a)-(b)) <= 0)
94 #define TCP_SEQ_GT(a,b) ((s32_t)((a)-(b)) > 0)
95 #define TCP_SEQ_GEQ(a,b) ((s32_t)((a)-(b)) >= 0)
98 #define TCP_SEQ_BETWEEN(a,b,c) ((c)-(b) >= (a)-(b))
100 #define TCP_SEQ_BETWEEN(a,b,c) (TCP_SEQ_GEQ(a,b) && TCP_SEQ_LEQ(a,c))
101 #define TCP_FIN 0x01U
102 #define TCP_SYN 0x02U
103 #define TCP_RST 0x04U
104 #define TCP_PSH 0x08U
105 #define TCP_ACK 0x10U
106 #define TCP_URG 0x20U
107 #define TCP_ECE 0x40U
108 #define TCP_CWR 0x80U
110 #define TCP_FLAGS 0x3fU
115 #ifndef TCP_TMR_INTERVAL
116 #define TCP_TMR_INTERVAL 250
119 #ifndef TCP_FAST_INTERVAL
120 #define TCP_FAST_INTERVAL TCP_TMR_INTERVAL
123 #ifndef TCP_SLOW_INTERVAL
124 #define TCP_SLOW_INTERVAL (2*TCP_TMR_INTERVAL)
127 #define TCP_FIN_WAIT_TIMEOUT 20000
128 #define TCP_SYN_RCVD_TIMEOUT 20000
130 #define TCP_OOSEQ_TIMEOUT 6U
133 #define TCP_MSL 60000UL
137 #ifndef TCP_KEEPIDLE_DEFAULT
138 #define TCP_KEEPIDLE_DEFAULT 7200000UL
141 #ifndef TCP_KEEPINTVL_DEFAULT
142 #define TCP_KEEPINTVL_DEFAULT 75000UL
145 #ifndef TCP_KEEPCNT_DEFAULT
146 #define TCP_KEEPCNT_DEFAULT 9U
149 #define TCP_MAXIDLE TCP_KEEPCNT_DEFAULT * TCP_KEEPINTVL_DEFAULT
154 #ifdef PACK_STRUCT_USE_INCLUDES
155 # include "arch/bpstruct.h"
169 #ifdef PACK_STRUCT_USE_INCLUDES
170 # include "arch/epstruct.h"
173 #define TCPH_OFFSET(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 8)
174 #define TCPH_HDRLEN(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 12)
175 #define TCPH_FLAGS(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS)
177 #define TCPH_OFFSET_SET(phdr, offset) (phdr)->_hdrlen_rsvd_flags = htons(((offset) << 8) | TCPH_FLAGS(phdr))
178 #define TCPH_HDRLEN_SET(phdr, len) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | TCPH_FLAGS(phdr))
179 #define TCPH_FLAGS_SET(phdr, flags) (phdr)->_hdrlen_rsvd_flags = (((phdr)->_hdrlen_rsvd_flags & PP_HTONS((u16_t)(~(u16_t)(TCP_FLAGS)))) | htons(flags))
180 #define TCPH_HDRLEN_FLAGS_SET(phdr, len, flags) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | (flags))
182 #define TCPH_SET_FLAG(phdr, flags ) (phdr)->_hdrlen_rsvd_flags = ((phdr)->_hdrlen_rsvd_flags | htons(flags))
183 #define TCPH_UNSET_FLAG(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (TCPH_FLAGS(phdr) & ~(flags)) )
185 #define TCP_TCPLEN(seg) ((seg)->len + ((TCPH_FLAGS((seg)->tcphdr) & (TCP_FIN | TCP_SYN)) != 0))
189 #define TF_RESET (u8_t)0x08U
190 #define TF_CLOSED (u8_t)0x10U
191 #define TF_GOT_FIN (u8_t)0x20U
196 #define TCP_EVENT_ACCEPT(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
197 LWIP_EVENT_ACCEPT, NULL, 0, err)
198 #define TCP_EVENT_SENT(pcb,space,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
199 LWIP_EVENT_SENT, NULL, space, ERR_OK)
200 #define TCP_EVENT_RECV(pcb,p,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
201 LWIP_EVENT_RECV, (p), 0, (err))
202 #define TCP_EVENT_CLOSED(pcb,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
203 LWIP_EVENT_RECV, NULL, 0, ERR_OK)
204 #define TCP_EVENT_CONNECTED(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
205 LWIP_EVENT_CONNECTED, NULL, 0, (err))
206 #define TCP_EVENT_POLL(pcb,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
207 LWIP_EVENT_POLL, NULL, 0, ERR_OK)
208 #define TCP_EVENT_ERR(errf,arg,err) lwip_tcp_event((arg), NULL, \
209 LWIP_EVENT_ERR, NULL, 0, (err))
213 #define TCP_EVENT_ACCEPT(pcb,err,ret) \
215 if((pcb)->accept != NULL) \
216 (ret) = (pcb)->accept((pcb)->callback_arg,(pcb),(err)); \
217 else (ret) = ERR_ARG; \
220 #define TCP_EVENT_SENT(pcb,space,ret) \
222 if((pcb)->sent != NULL) \
223 (ret) = (pcb)->sent((pcb)->callback_arg,(pcb),(space)); \
224 else (ret) = ERR_OK; \
227 #define TCP_EVENT_RECV(pcb,p,err,ret) \
229 if((pcb)->recv != NULL) { \
230 (ret) = (pcb)->recv((pcb)->callback_arg,(pcb),(p),(err));\
232 (ret) = tcp_recv_null(NULL, (pcb), (p), (err)); \
236 #define TCP_EVENT_CLOSED(pcb,ret) \
238 if(((pcb)->recv != NULL)) { \
239 (ret) = (pcb)->recv((pcb)->callback_arg,(pcb),NULL,ERR_OK);\
245 #define TCP_EVENT_CONNECTED(pcb,err,ret) \
247 if((pcb)->connected != NULL) \
248 (ret) = (pcb)->connected((pcb)->callback_arg,(pcb),(err)); \
249 else (ret) = ERR_OK; \
252 #define TCP_EVENT_POLL(pcb,ret) \
254 if((pcb)->poll != NULL) \
255 (ret) = (pcb)->poll((pcb)->callback_arg,(pcb)); \
256 else (ret) = ERR_OK; \
259 #define TCP_EVENT_ERR(errf,arg,err) \
262 (errf)((arg),(err)); \
268 #if TCP_OVERSIZE && defined(LWIP_DEBUG)
269 #define TCP_OVERSIZE_DBGCHECK 1
271 #define TCP_OVERSIZE_DBGCHECK 0
275 #define TCP_CHECKSUM_ON_COPY (LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_TCP)
279 struct tcp_seg *next;
283 #if TCP_OVERSIZE_DBGCHECK
288 #if TCP_CHECKSUM_ON_COPY
293 #define TF_SEG_OPTS_MSS (u8_t)0x01U
294 #define TF_SEG_OPTS_TS (u8_t)0x02U
295 #define TF_SEG_DATA_CHECKSUMMED (u8_t)0x04U
297 struct tcp_hdr *tcphdr;
300 #define LWIP_TCP_OPT_LENGTH(flags) \
301 (flags & TF_SEG_OPTS_MSS ? 4 : 0) + \
302 (flags & TF_SEG_OPTS_TS ? 12 : 0)
305 #define TCP_BUILD_MSS_OPTION(x) (x) = PP_HTONL(((u32_t)2 << 24) | \
307 (((u32_t)TCP_MSS / 256) << 8) | \
311 extern struct tcp_pcb *tcp_input_pcb;
312 extern u32_t tcp_ticks;
315 union tcp_listen_pcbs_t {
316 struct tcp_pcb_listen *listen_pcbs;
317 struct tcp_pcb *pcbs;
319 extern struct tcp_pcb *tcp_bound_pcbs;
320 extern union tcp_listen_pcbs_t tcp_listen_pcbs;
321 extern struct tcp_pcb *tcp_active_pcbs;
324 extern struct tcp_pcb *tcp_tw_pcbs;
326 extern struct tcp_pcb *tcp_tmp_pcb;
336 #ifndef TCP_DEBUG_PCB_LISTS
337 #define TCP_DEBUG_PCB_LISTS 0
339 #if TCP_DEBUG_PCB_LISTS
340 #define TCP_REG(pcbs, npcb) do {\
341 LWIP_DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %d\n", (npcb), (npcb)->local_port)); \
342 for(tcp_tmp_pcb = *(pcbs); \
343 tcp_tmp_pcb != NULL; \
344 tcp_tmp_pcb = tcp_tmp_pcb->next) { \
345 LWIP_ASSERT("TCP_REG: already registered\n", tcp_tmp_pcb != (npcb)); \
347 LWIP_ASSERT("TCP_REG: pcb->state != CLOSED", ((pcbs) == &tcp_bound_pcbs) || ((npcb)->state != CLOSED)); \
348 (npcb)->next = *(pcbs); \
349 LWIP_ASSERT("TCP_REG: npcb->next != npcb", (npcb)->next != (npcb)); \
351 LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
352 tcp_timer_needed(); \
354 #define TCP_RMV(pcbs, npcb) do { \
355 LWIP_ASSERT("TCP_RMV: pcbs != NULL", *(pcbs) != NULL); \
356 LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removing %p from %p\n", (npcb), *(pcbs))); \
357 if(*(pcbs) == (npcb)) { \
358 *(pcbs) = (*pcbs)->next; \
359 } else for(tcp_tmp_pcb = *(pcbs); tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
360 if(tcp_tmp_pcb->next == (npcb)) { \
361 tcp_tmp_pcb->next = (npcb)->next; \
365 (npcb)->next = NULL; \
366 LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
367 LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removed %p from %p\n", (npcb), *(pcbs))); \
372 #define TCP_REG(pcbs, npcb) \
374 (npcb)->next = *pcbs; \
376 tcp_timer_needed(); \
379 #define TCP_RMV(pcbs, npcb) \
381 if(*(pcbs) == (npcb)) { \
382 (*(pcbs)) = (*pcbs)->next; \
385 for(tcp_tmp_pcb = *pcbs; \
386 tcp_tmp_pcb != NULL; \
387 tcp_tmp_pcb = tcp_tmp_pcb->next) { \
388 if(tcp_tmp_pcb->next == (npcb)) { \
389 tcp_tmp_pcb->next = (npcb)->next; \
394 (npcb)->next = NULL; \
401 struct tcp_pcb *tcp_pcb_copy(
struct tcp_pcb *pcb);
402 void tcp_pcb_purge(
struct tcp_pcb *pcb);
403 void tcp_pcb_remove(
struct tcp_pcb **pcblist,
struct tcp_pcb *pcb);
405 void tcp_segs_free(
struct tcp_seg *seg);
406 void tcp_seg_free(
struct tcp_seg *seg);
407 struct tcp_seg *tcp_seg_copy(
struct tcp_seg *seg);
409 #define tcp_ack(pcb) \
411 if((pcb)->flags & TF_ACK_DELAY) { \
412 (pcb)->flags &= ~TF_ACK_DELAY; \
413 (pcb)->flags |= TF_ACK_NOW; \
416 (pcb)->flags |= TF_ACK_DELAY; \
420 #define tcp_ack_now(pcb) \
422 (pcb)->flags |= TF_ACK_NOW; \
425 err_t tcp_send_fin(
struct tcp_pcb *pcb);
426 err_t tcp_enqueue_flags(
struct tcp_pcb *pcb,
u8_t flags);
428 void tcp_rexmit_seg(
struct tcp_pcb *pcb,
struct tcp_seg *seg);
434 u32_t tcp_next_iss(
void);
436 void tcp_keepalive(
struct tcp_pcb *pcb);
437 void tcp_zero_window_probe(
struct tcp_pcb *pcb);
439 #if TCP_CALCULATE_EFF_SEND_MSS
443 #if LWIP_CALLBACK_API
444 err_t tcp_recv_null(
void *arg,
struct tcp_pcb *pcb,
struct pbuf *p,
err_t err);
447 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
448 void tcp_debug_print(
struct tcp_hdr *tcphdr);
449 void tcp_debug_print_flags(
u8_t flags);
450 void tcp_debug_print_state(
enum tcp_state s);
451 void tcp_debug_print_pcbs(
void);
452 s16_t tcp_pcbs_sane(
void);
454 # define tcp_debug_print(tcphdr)
455 # define tcp_debug_print_flags(flags)
456 # define tcp_debug_print_state(s)
457 # define tcp_debug_print_pcbs()
458 # define tcp_pcbs_sane() 1