32 #ifndef LWIP_HDR_TCP_IMPL_H 33 #define LWIP_HDR_TCP_IMPL_H 61 void tcp_slowtmr (
void);
62 void tcp_fasttmr (
void);
68 void tcp_txnow (
void);
71 void tcp_input (
struct pbuf *p,
struct netif *inp);
73 struct tcp_pcb * tcp_alloc (
u8_t prio);
74 void tcp_abandon (
struct tcp_pcb *pcb,
int reset);
75 err_t tcp_send_empty_ack(
struct tcp_pcb *pcb);
76 void tcp_rexmit (
struct tcp_pcb *pcb);
77 void tcp_rexmit_rto (
struct tcp_pcb *pcb);
78 void tcp_rexmit_fast (
struct tcp_pcb *pcb);
79 u32_t tcp_update_rcv_ann_wnd(
struct tcp_pcb *pcb);
80 err_t tcp_process_refused_data(
struct tcp_pcb *pcb);
91 #define tcp_do_output_nagle(tpcb) ((((tpcb)->unacked == NULL) || \ 92 ((tpcb)->flags & (TF_NODELAY | TF_INFR)) || \ 93 (((tpcb)->unsent != NULL) && (((tpcb)->unsent->next != NULL) || \ 94 ((tpcb)->unsent->len >= (tpcb)->mss))) || \ 95 ((tcp_sndbuf(tpcb) == 0) || (tcp_sndqueuelen(tpcb) >= TCP_SND_QUEUELEN)) \ 97 #define tcp_output_nagle(tpcb) (tcp_do_output_nagle(tpcb) ? tcp_output(tpcb) : ERR_OK) 100 #define TCP_SEQ_LT(a,b) ((s32_t)((u32_t)(a) - (u32_t)(b)) < 0) 101 #define TCP_SEQ_LEQ(a,b) ((s32_t)((u32_t)(a) - (u32_t)(b)) <= 0) 102 #define TCP_SEQ_GT(a,b) ((s32_t)((u32_t)(a) - (u32_t)(b)) > 0) 103 #define TCP_SEQ_GEQ(a,b) ((s32_t)((u32_t)(a) - (u32_t)(b)) >= 0) 106 #define TCP_SEQ_BETWEEN(a,b,c) ((c)-(b) >= (a)-(b)) 108 #define TCP_SEQ_BETWEEN(a,b,c) (TCP_SEQ_GEQ(a,b) && TCP_SEQ_LEQ(a,c)) 109 #define TCP_FIN 0x01U 110 #define TCP_SYN 0x02U 111 #define TCP_RST 0x04U 112 #define TCP_PSH 0x08U 113 #define TCP_ACK 0x10U 114 #define TCP_URG 0x20U 115 #define TCP_ECE 0x40U 116 #define TCP_CWR 0x80U 118 #define TCP_FLAGS 0x3fU 123 #ifndef TCP_TMR_INTERVAL 124 #define TCP_TMR_INTERVAL 250 127 #ifndef TCP_FAST_INTERVAL 128 #define TCP_FAST_INTERVAL TCP_TMR_INTERVAL 131 #ifndef TCP_SLOW_INTERVAL 132 #define TCP_SLOW_INTERVAL (2*TCP_TMR_INTERVAL) 135 #define TCP_FIN_WAIT_TIMEOUT 20000 136 #define TCP_SYN_RCVD_TIMEOUT 20000 138 #define TCP_OOSEQ_TIMEOUT 6U 141 #define TCP_MSL 60000UL 145 #ifndef TCP_KEEPIDLE_DEFAULT 146 #define TCP_KEEPIDLE_DEFAULT 7200000UL 149 #ifndef TCP_KEEPINTVL_DEFAULT 150 #define TCP_KEEPINTVL_DEFAULT 75000UL 153 #ifndef TCP_KEEPCNT_DEFAULT 154 #define TCP_KEEPCNT_DEFAULT 9U 157 #define TCP_MAXIDLE TCP_KEEPCNT_DEFAULT * TCP_KEEPINTVL_DEFAULT 162 #ifdef PACK_STRUCT_USE_INCLUDES 177 #ifdef PACK_STRUCT_USE_INCLUDES 181 #define TCPH_HDRLEN(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 12) 182 #define TCPH_FLAGS(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS) 184 #define TCPH_HDRLEN_SET(phdr, len) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | TCPH_FLAGS(phdr)) 185 #define TCPH_FLAGS_SET(phdr, flags) (phdr)->_hdrlen_rsvd_flags = (((phdr)->_hdrlen_rsvd_flags & PP_HTONS(~TCP_FLAGS)) | htons(flags)) 186 #define TCPH_HDRLEN_FLAGS_SET(phdr, len, flags) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | (flags)) 188 #define TCPH_SET_FLAG(phdr, flags ) (phdr)->_hdrlen_rsvd_flags = ((phdr)->_hdrlen_rsvd_flags | htons(flags)) 189 #define TCPH_UNSET_FLAG(phdr, flags) (phdr)->_hdrlen_rsvd_flags = ((phdr)->_hdrlen_rsvd_flags & ~htons(flags)) 191 #define TCP_TCPLEN(seg) ((seg)->len + (((TCPH_FLAGS((seg)->tcphdr) & (TCP_FIN | TCP_SYN)) != 0) ? 1U : 0U)) 195 #define TF_RESET (u8_t)0x08U 196 #define TF_CLOSED (u8_t)0x10U 197 #define TF_GOT_FIN (u8_t)0x20U 202 #define TCP_EVENT_ACCEPT(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\ 203 LWIP_EVENT_ACCEPT, NULL, 0, err) 204 #define TCP_EVENT_SENT(pcb,space,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\ 205 LWIP_EVENT_SENT, NULL, space, ERR_OK) 206 #define TCP_EVENT_RECV(pcb,p,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\ 207 LWIP_EVENT_RECV, (p), 0, (err)) 208 #define TCP_EVENT_CLOSED(pcb,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\ 209 LWIP_EVENT_RECV, NULL, 0, ERR_OK) 210 #define TCP_EVENT_CONNECTED(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\ 211 LWIP_EVENT_CONNECTED, NULL, 0, (err)) 212 #define TCP_EVENT_POLL(pcb,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\ 213 LWIP_EVENT_POLL, NULL, 0, ERR_OK) 214 #define TCP_EVENT_ERR(errf,arg,err) lwip_tcp_event((arg), NULL, \ 215 LWIP_EVENT_ERR, NULL, 0, (err)) 219 #define TCP_EVENT_ACCEPT(pcb,err,ret) \ 221 if((pcb)->accept != NULL) \ 222 (ret) = (pcb)->accept((pcb)->callback_arg,(pcb),(err)); \ 223 else (ret) = ERR_ARG; \ 226 #define TCP_EVENT_SENT(pcb,space,ret) \ 228 if((pcb)->sent != NULL) \ 229 (ret) = (pcb)->sent((pcb)->callback_arg,(pcb),(space)); \ 230 else (ret) = ERR_OK; \ 233 #define TCP_EVENT_RECV(pcb,p,err,ret) \ 235 if((pcb)->recv != NULL) { \ 236 (ret) = (pcb)->recv((pcb)->callback_arg,(pcb),(p),(err));\ 238 (ret) = tcp_recv_null(NULL, (pcb), (p), (err)); \ 242 #define TCP_EVENT_CLOSED(pcb,ret) \ 244 if(((pcb)->recv != NULL)) { \ 245 (ret) = (pcb)->recv((pcb)->callback_arg,(pcb),NULL,ERR_OK);\ 251 #define TCP_EVENT_CONNECTED(pcb,err,ret) \ 253 if((pcb)->connected != NULL) \ 254 (ret) = (pcb)->connected((pcb)->callback_arg,(pcb),(err)); \ 255 else (ret) = ERR_OK; \ 258 #define TCP_EVENT_POLL(pcb,ret) \ 260 if((pcb)->poll != NULL) \ 261 (ret) = (pcb)->poll((pcb)->callback_arg,(pcb)); \ 262 else (ret) = ERR_OK; \ 265 #define TCP_EVENT_ERR(errf,arg,err) \ 268 (errf)((arg),(err)); \ 274 #if TCP_OVERSIZE && defined(LWIP_DEBUG) 275 #define TCP_OVERSIZE_DBGCHECK 1 277 #define TCP_OVERSIZE_DBGCHECK 0 281 #define TCP_CHECKSUM_ON_COPY (LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_TCP) 285 struct tcp_seg *next;
288 #if TCP_OVERSIZE_DBGCHECK 293 #if TCP_CHECKSUM_ON_COPY 298 #define TF_SEG_OPTS_MSS (u8_t)0x01U 299 #define TF_SEG_OPTS_TS (u8_t)0x02U 300 #define TF_SEG_DATA_CHECKSUMMED (u8_t)0x04U 302 #define TF_SEG_OPTS_WND_SCALE (u8_t)0x08U 303 struct tcp_hdr *tcphdr;
306 #define LWIP_TCP_OPT_EOL 0 307 #define LWIP_TCP_OPT_NOP 1 308 #define LWIP_TCP_OPT_MSS 2 309 #define LWIP_TCP_OPT_WS 3 310 #define LWIP_TCP_OPT_TS 8 312 #define LWIP_TCP_OPT_LEN_MSS 4 313 #if LWIP_TCP_TIMESTAMPS 314 #define LWIP_TCP_OPT_LEN_TS 10 315 #define LWIP_TCP_OPT_LEN_TS_OUT 12 317 #define LWIP_TCP_OPT_LEN_TS_OUT 0 320 #define LWIP_TCP_OPT_LEN_WS 3 321 #define LWIP_TCP_OPT_LEN_WS_OUT 4 323 #define LWIP_TCP_OPT_LEN_WS_OUT 0 326 #define LWIP_TCP_OPT_LENGTH(flags) \ 327 (flags & TF_SEG_OPTS_MSS ? LWIP_TCP_OPT_LEN_MSS : 0) + \ 328 (flags & TF_SEG_OPTS_TS ? LWIP_TCP_OPT_LEN_TS_OUT : 0) + \ 329 (flags & TF_SEG_OPTS_WND_SCALE ? LWIP_TCP_OPT_LEN_WS_OUT : 0) 332 #define TCP_BUILD_MSS_OPTION(mss) htonl(0x02040000 | ((mss) & 0xFFFF)) 335 #define TCPWNDSIZE_F U32_F 336 #define TCPWND_MAX 0xFFFFFFFFU 337 #define TCPWND_CHECK16(x) LWIP_ASSERT("window size > 0xFFFF", (x) <= 0xFFFF) 338 #define TCPWND_MIN16(x) ((u16_t)LWIP_MIN((x), 0xFFFF)) 340 #define TCPWNDSIZE_F U16_F 341 #define TCPWND_MAX 0xFFFFU 342 #define TCPWND_CHECK16(x) 343 #define TCPWND_MIN16(x) x 347 extern struct tcp_pcb *tcp_input_pcb;
348 extern u32_t tcp_ticks;
349 extern u8_t tcp_active_pcbs_changed;
352 union tcp_listen_pcbs_t {
353 struct tcp_pcb_listen *listen_pcbs;
354 struct tcp_pcb *pcbs;
356 extern struct tcp_pcb *tcp_bound_pcbs;
357 extern union tcp_listen_pcbs_t tcp_listen_pcbs;
358 extern struct tcp_pcb *tcp_active_pcbs;
361 extern struct tcp_pcb *tcp_tw_pcbs;
363 #define NUM_TCP_PCB_LISTS_NO_TIME_WAIT 3 364 #define NUM_TCP_PCB_LISTS 4 365 extern struct tcp_pcb **
const tcp_pcb_lists[NUM_TCP_PCB_LISTS];
375 #ifndef TCP_DEBUG_PCB_LISTS 376 #define TCP_DEBUG_PCB_LISTS 0 378 #if TCP_DEBUG_PCB_LISTS 379 #define TCP_REG(pcbs, npcb) do {\ 380 struct tcp_pcb *tcp_tmp_pcb; \ 381 LWIP_DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %d\n", (npcb), (npcb)->local_port)); \ 382 for (tcp_tmp_pcb = *(pcbs); \ 383 tcp_tmp_pcb != NULL; \ 384 tcp_tmp_pcb = tcp_tmp_pcb->next) { \ 385 LWIP_ASSERT("TCP_REG: already registered\n", tcp_tmp_pcb != (npcb)); \ 387 LWIP_ASSERT("TCP_REG: pcb->state != CLOSED", ((pcbs) == &tcp_bound_pcbs) || ((npcb)->state != CLOSED)); \ 388 (npcb)->next = *(pcbs); \ 389 LWIP_ASSERT("TCP_REG: npcb->next != npcb", (npcb)->next != (npcb)); \ 391 LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \ 392 tcp_timer_needed(); \ 394 #define TCP_RMV(pcbs, npcb) do { \ 395 struct tcp_pcb *tcp_tmp_pcb; \ 396 LWIP_ASSERT("TCP_RMV: pcbs != NULL", *(pcbs) != NULL); \ 397 LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removing %p from %p\n", (npcb), *(pcbs))); \ 398 if(*(pcbs) == (npcb)) { \ 399 *(pcbs) = (*pcbs)->next; \ 400 } else for (tcp_tmp_pcb = *(pcbs); tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \ 401 if(tcp_tmp_pcb->next == (npcb)) { \ 402 tcp_tmp_pcb->next = (npcb)->next; \ 406 (npcb)->next = NULL; \ 407 LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \ 408 LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removed %p from %p\n", (npcb), *(pcbs))); \ 413 #define TCP_REG(pcbs, npcb) \ 415 (npcb)->next = *pcbs; \ 417 tcp_timer_needed(); \ 420 #define TCP_RMV(pcbs, npcb) \ 422 if(*(pcbs) == (npcb)) { \ 423 (*(pcbs)) = (*pcbs)->next; \ 426 struct tcp_pcb *tcp_tmp_pcb; \ 427 for (tcp_tmp_pcb = *pcbs; \ 428 tcp_tmp_pcb != NULL; \ 429 tcp_tmp_pcb = tcp_tmp_pcb->next) { \ 430 if(tcp_tmp_pcb->next == (npcb)) { \ 431 tcp_tmp_pcb->next = (npcb)->next; \ 436 (npcb)->next = NULL; \ 441 #define TCP_REG_ACTIVE(npcb) \ 443 TCP_REG(&tcp_active_pcbs, npcb); \ 444 tcp_active_pcbs_changed = 1; \ 447 #define TCP_RMV_ACTIVE(npcb) \ 449 TCP_RMV(&tcp_active_pcbs, npcb); \ 450 tcp_active_pcbs_changed = 1; \ 453 #define TCP_PCB_REMOVE_ACTIVE(pcb) \ 455 tcp_pcb_remove(&tcp_active_pcbs, pcb); \ 456 tcp_active_pcbs_changed = 1; \ 461 struct tcp_pcb *tcp_pcb_copy(
struct tcp_pcb *pcb);
462 void tcp_pcb_purge(
struct tcp_pcb *pcb);
463 void tcp_pcb_remove(
struct tcp_pcb **pcblist,
struct tcp_pcb *pcb);
465 void tcp_segs_free(
struct tcp_seg *seg);
466 void tcp_seg_free(
struct tcp_seg *seg);
467 struct tcp_seg *tcp_seg_copy(
struct tcp_seg *seg);
469 #define tcp_ack(pcb) \ 471 if((pcb)->flags & TF_ACK_DELAY) { \ 472 (pcb)->flags &= ~TF_ACK_DELAY; \ 473 (pcb)->flags |= TF_ACK_NOW; \ 476 (pcb)->flags |= TF_ACK_DELAY; \ 480 #define tcp_ack_now(pcb) \ 482 (pcb)->flags |= TF_ACK_NOW; \ 485 err_t tcp_send_fin(
struct tcp_pcb *pcb);
486 err_t tcp_enqueue_flags(
struct tcp_pcb *pcb,
u8_t flags);
488 void tcp_rexmit_seg(
struct tcp_pcb *pcb,
struct tcp_seg *seg);
494 u32_t tcp_next_iss(
void);
496 err_t tcp_keepalive(
struct tcp_pcb *pcb);
497 err_t tcp_zero_window_probe(
struct tcp_pcb *pcb);
498 void tcp_trigger_input_pcb_close(
void);
500 #if TCP_CALCULATE_EFF_SEND_MSS 509 #if LWIP_IPV4 && LWIP_IPV6 510 #define tcp_eff_send_mss(sendmss, src, dest, isipv6) tcp_eff_send_mss_impl(sendmss, dest, src, isipv6) 511 #elif LWIP_IPV6 || LWIP_IPV4_SRC_ROUTING 512 #define tcp_eff_send_mss(sendmss, src, dest, isipv6) tcp_eff_send_mss_impl(sendmss, dest, src) 514 #define tcp_eff_send_mss(sendmss, src, dest, isipv6) tcp_eff_send_mss_impl(sendmss, dest) 518 #if LWIP_CALLBACK_API 519 err_t tcp_recv_null(
void *arg,
struct tcp_pcb *pcb,
struct pbuf *p,
err_t err);
522 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG 523 void tcp_debug_print(
struct tcp_hdr *tcphdr);
524 void tcp_debug_print_flags(
u8_t flags);
525 void tcp_debug_print_state(
enum tcp_state s);
526 void tcp_debug_print_pcbs(
void);
527 s16_t tcp_pcbs_sane(
void);
529 # define tcp_debug_print(tcphdr) 530 # define tcp_debug_print_flags(flags) 531 # define tcp_debug_print_state(s) 532 # define tcp_debug_print_pcbs() 533 # define tcp_pcbs_sane() 1 541 void tcp_netif_ipv4_addr_changed(
const ip4_addr_t* old_addr,
const ip4_addr_t* new_addr);
#define PACK_STRUCT_STRUCT
void tcp_timer_needed(void)
#define PACK_STRUCT_FIELD(x)
#define PACK_STRUCT_BEGIN