84 #ifdef LWIP_HOOK_FILENAME 85 #include LWIP_HOOK_FILENAME 88 #ifndef TCP_LOCAL_PORT_RANGE_START 91 #define TCP_LOCAL_PORT_RANGE_START 0xc000 92 #define TCP_LOCAL_PORT_RANGE_END 0xffff 93 #define TCP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & ~TCP_LOCAL_PORT_RANGE_START) + TCP_LOCAL_PORT_RANGE_START)) 96 #if LWIP_TCP_KEEPALIVE 97 #define TCP_KEEP_DUR(pcb) ((pcb)->keep_cnt * (pcb)->keep_intvl) 98 #define TCP_KEEP_INTVL(pcb) ((pcb)->keep_intvl) 100 #define TCP_KEEP_DUR(pcb) TCP_MAXIDLE 101 #define TCP_KEEP_INTVL(pcb) TCP_KEEPINTVL_DEFAULT 106 #define INITIAL_MSS 536 108 #define INITIAL_MSS TCP_MSS 111 static const char *
const tcp_state_str[] = {
126 static u16_t tcp_port = TCP_LOCAL_PORT_RANGE_START;
130 static const u8_t tcp_backoff[13] =
131 { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7};
133 static const u8_t tcp_persist_backoff[7] = { 3, 6, 12, 24, 48, 96, 120 };
138 struct tcp_pcb *tcp_bound_pcbs;
140 union tcp_listen_pcbs_t tcp_listen_pcbs;
143 struct tcp_pcb *tcp_active_pcbs;
145 struct tcp_pcb *tcp_tw_pcbs;
148 struct tcp_pcb **
const tcp_pcb_lists[] = {&tcp_listen_pcbs.pcbs, &tcp_bound_pcbs,
149 &tcp_active_pcbs, &tcp_tw_pcbs};
151 u8_t tcp_active_pcbs_changed;
154 static u8_t tcp_timer;
155 static u8_t tcp_timer_ctr;
156 static u16_t tcp_new_port(
void);
158 static err_t tcp_close_shutdown_fin(
struct tcp_pcb *pcb);
166 #if LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) 167 tcp_port = TCP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND());
180 if (++tcp_timer & 1) {
187 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG 192 tcp_remove_listener(
struct tcp_pcb *list,
struct tcp_pcb_listen *lpcb)
195 for (pcb = list; pcb != NULL; pcb = pcb->next) {
196 if (pcb->listener == lpcb) {
197 pcb->listener = NULL;
207 tcp_listen_closed(
struct tcp_pcb *pcb)
209 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG 211 LWIP_ASSERT(
"pcb != NULL", pcb != NULL);
212 LWIP_ASSERT(
"pcb->state == LISTEN", pcb->state == LISTEN);
213 for (i = 1; i < LWIP_ARRAYSIZE(tcp_pcb_lists); i++) {
214 tcp_remove_listener(*tcp_pcb_lists[i], (
struct tcp_pcb_listen*)pcb);
220 #if TCP_LISTEN_BACKLOG 232 tcp_backlog_delayed(
struct tcp_pcb* pcb)
234 LWIP_ASSERT(
"pcb != NULL", pcb != NULL);
235 if ((pcb->flags & TF_BACKLOGPEND) == 0) {
236 if (pcb->listener != NULL) {
237 pcb->listener->accepts_pending++;
238 LWIP_ASSERT(
"accepts_pending != 0", pcb->listener->accepts_pending != 0);
239 pcb->flags |= TF_BACKLOGPEND;
254 tcp_backlog_accepted(
struct tcp_pcb* pcb)
256 LWIP_ASSERT(
"pcb != NULL", pcb != NULL);
257 if ((pcb->flags & TF_BACKLOGPEND) != 0) {
258 if (pcb->listener != NULL) {
259 LWIP_ASSERT(
"accepts_pending != 0", pcb->listener->accepts_pending != 0);
260 pcb->listener->accepts_pending--;
261 pcb->flags &= ~TF_BACKLOGPEND;
284 tcp_close_shutdown(
struct tcp_pcb *pcb, u8_t rst_on_unacked_data)
286 if (rst_on_unacked_data && ((pcb->state == ESTABLISHED) || (pcb->state == CLOSE_WAIT))) {
287 if ((pcb->refused_data != NULL) || (pcb->rcv_wnd != TCP_WND_MAX(pcb))) {
290 LWIP_ASSERT(
"pcb->flags & TF_RXCLOSED", pcb->flags & TF_RXCLOSED);
294 tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
295 pcb->local_port, pcb->remote_port);
299 if (pcb->state == ESTABLISHED) {
301 pcb->state = TIME_WAIT;
302 TCP_REG(&tcp_tw_pcbs, pcb);
305 if (tcp_input_pcb == pcb) {
307 tcp_trigger_input_pcb_close();
318 switch (pcb->state) {
327 if (pcb->local_port != 0) {
328 TCP_RMV(&tcp_bound_pcbs, pcb);
333 tcp_listen_closed(pcb);
334 tcp_pcb_remove(&tcp_listen_pcbs.pcbs, pcb);
338 TCP_PCB_REMOVE_ACTIVE(pcb);
340 MIB2_STATS_INC(mib2.tcpattemptfails);
343 return tcp_close_shutdown_fin(pcb);
349 tcp_close_shutdown_fin(
struct tcp_pcb *pcb)
352 LWIP_ASSERT(
"pcb != NULL", pcb != NULL);
354 switch (pcb->state) {
356 err = tcp_send_fin(pcb);
358 tcp_backlog_accepted(pcb);
359 MIB2_STATS_INC(mib2.tcpattemptfails);
360 pcb->state = FIN_WAIT_1;
364 err = tcp_send_fin(pcb);
366 MIB2_STATS_INC(mib2.tcpestabresets);
367 pcb->state = FIN_WAIT_1;
371 err = tcp_send_fin(pcb);
373 MIB2_STATS_INC(mib2.tcpestabresets);
374 pcb->state = LAST_ACK;
392 pcb->flags |= TF_CLOSEPEND;
413 tcp_close(
struct tcp_pcb *pcb)
416 tcp_debug_print_state(pcb->state);
418 if (pcb->state != LISTEN) {
420 pcb->flags |= TF_RXCLOSED;
423 return tcp_close_shutdown(pcb, 1);
440 tcp_shutdown(
struct tcp_pcb *pcb,
int shut_rx,
int shut_tx)
442 if (pcb->state == LISTEN) {
447 pcb->flags |= TF_RXCLOSED;
450 return tcp_close_shutdown(pcb, 1);
453 if (pcb->refused_data != NULL) {
455 pcb->refused_data = NULL;
461 switch (pcb->state) {
465 return tcp_close_shutdown(pcb, (u8_t)shut_rx);
484 tcp_abandon(
struct tcp_pcb *pcb,
int reset)
487 #if LWIP_CALLBACK_API 493 LWIP_ASSERT(
"don't call tcp_abort/tcp_abandon for listen-pcbs",
494 pcb->state != LISTEN);
498 if (pcb->state == TIME_WAIT) {
499 tcp_pcb_remove(&tcp_tw_pcbs, pcb);
503 u16_t local_port = 0;
504 enum tcp_state last_state;
505 seqno = pcb->snd_nxt;
506 ackno = pcb->rcv_nxt;
507 #if LWIP_CALLBACK_API 510 errf_arg = pcb->callback_arg;
511 if (pcb->state == CLOSED) {
512 if (pcb->local_port != 0) {
514 TCP_RMV(&tcp_bound_pcbs, pcb);
518 local_port = pcb->local_port;
519 TCP_PCB_REMOVE_ACTIVE(pcb);
521 if (pcb->unacked != NULL) {
522 tcp_segs_free(pcb->unacked);
524 if (pcb->unsent != NULL) {
525 tcp_segs_free(pcb->unsent);
528 if (pcb->ooseq != NULL) {
529 tcp_segs_free(pcb->ooseq);
532 tcp_backlog_accepted(pcb);
535 tcp_rst(seqno, ackno, &pcb->local_ip, &pcb->remote_ip, local_port, pcb->remote_port);
537 last_state = pcb->state;
539 TCP_EVENT_ERR(last_state, errf, errf_arg,
ERR_ABRT);
555 tcp_abort(
struct tcp_pcb *pcb)
576 tcp_bind(
struct tcp_pcb *pcb,
const ip_addr_t *ipaddr, u16_t port)
579 int max_pcb_list = NUM_TCP_PCB_LISTS;
580 struct tcp_pcb *cpcb;
584 if (ipaddr == NULL) {
585 ipaddr = IP4_ADDR_ANY;
590 if ((pcb == NULL) || (ipaddr == NULL)) {
594 LWIP_ERROR(
"tcp_bind: can only bind in state CLOSED", pcb->state == CLOSED,
return ERR_VAL);
603 max_pcb_list = NUM_TCP_PCB_LISTS_NO_TIME_WAIT;
608 port = tcp_new_port();
614 for (i = 0; i < max_pcb_list; i++) {
615 for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
616 if (cpcb->local_port == port) {
626 if ((IP_IS_V6(ipaddr) == IP_IS_V6_VAL(cpcb->local_ip)) &&
627 (ip_addr_isany(&cpcb->local_ip) ||
628 ip_addr_isany(ipaddr) ||
629 ip_addr_cmp(&cpcb->local_ip, ipaddr))) {
638 if (!ip_addr_isany(ipaddr)) {
639 ip_addr_set(&pcb->local_ip, ipaddr);
641 pcb->local_port = port;
642 TCP_REG(&tcp_bound_pcbs, pcb);
646 #if LWIP_CALLBACK_API 651 tcp_accept_null(
void *arg,
struct tcp_pcb *pcb,
err_t err)
678 tcp_listen_with_backlog(
struct tcp_pcb *pcb, u8_t backlog)
680 return tcp_listen_with_backlog_and_err(pcb, backlog, NULL);
700 tcp_listen_with_backlog_and_err(
struct tcp_pcb *pcb, u8_t backlog,
err_t *err)
702 struct tcp_pcb_listen *lpcb = NULL;
706 LWIP_ERROR(
"tcp_listen: pcb already connected", pcb->state == CLOSED, res =
ERR_CLSD;
goto done);
709 if (pcb->state == LISTEN) {
710 lpcb = (
struct tcp_pcb_listen*)pcb;
719 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
720 if ((lpcb->local_port == pcb->local_port) &&
721 ip_addr_cmp(&lpcb->local_ip, &pcb->local_ip)) {
730 lpcb = (
struct tcp_pcb_listen *)
memp_malloc(MEMP_TCP_PCB_LISTEN);
735 lpcb->callback_arg = pcb->callback_arg;
736 lpcb->local_port = pcb->local_port;
737 lpcb->state = LISTEN;
738 lpcb->prio = pcb->prio;
739 lpcb->so_options = pcb->so_options;
740 lpcb->ttl = pcb->ttl;
741 lpcb->tos = pcb->tos;
742 #if LWIP_IPV4 && LWIP_IPV6 743 IP_SET_TYPE_VAL(lpcb->remote_ip, pcb->local_ip.type);
745 ip_addr_copy(lpcb->local_ip, pcb->local_ip);
746 if (pcb->local_port != 0) {
747 TCP_RMV(&tcp_bound_pcbs, pcb);
750 #if LWIP_CALLBACK_API 751 lpcb->accept = tcp_accept_null;
753 #if TCP_LISTEN_BACKLOG 754 lpcb->accepts_pending = 0;
755 tcp_backlog_set(lpcb, backlog);
757 TCP_REG(&tcp_listen_pcbs.pcbs, (
struct tcp_pcb *)lpcb);
763 return (
struct tcp_pcb *)lpcb;
773 tcp_update_rcv_ann_wnd(
struct tcp_pcb *pcb)
775 u32_t new_right_edge = pcb->rcv_nxt + pcb->rcv_wnd;
777 if (TCP_SEQ_GEQ(new_right_edge, pcb->rcv_ann_right_edge + LWIP_MIN((TCP_WND / 2), pcb->mss))) {
779 pcb->rcv_ann_wnd = pcb->rcv_wnd;
780 return new_right_edge - pcb->rcv_ann_right_edge;
782 if (TCP_SEQ_GT(pcb->rcv_nxt, pcb->rcv_ann_right_edge)) {
785 pcb->rcv_ann_wnd = 0;
788 u32_t new_rcv_ann_wnd = pcb->rcv_ann_right_edge - pcb->rcv_nxt;
790 LWIP_ASSERT(
"new_rcv_ann_wnd <= 0xffff", new_rcv_ann_wnd <= 0xffff);
792 pcb->rcv_ann_wnd = (tcpwnd_size_t)new_rcv_ann_wnd;
808 tcp_recved(
struct tcp_pcb *pcb, u16_t len)
813 LWIP_ASSERT(
"don't call tcp_recved for listen-pcbs",
814 pcb->state != LISTEN);
817 if (pcb->rcv_wnd > TCP_WND_MAX(pcb)) {
818 pcb->rcv_wnd = TCP_WND_MAX(pcb);
819 }
else if (pcb->rcv_wnd == 0) {
821 if ((pcb->state == CLOSE_WAIT) || (pcb->state == LAST_ACK)) {
825 pcb->rcv_wnd = TCP_WND_MAX(pcb);
827 LWIP_ASSERT(
"tcp_recved: len wrapped rcv_wnd\n", 0);
831 wnd_inflation = tcp_update_rcv_ann_wnd(pcb);
842 LWIP_DEBUGF(
TCP_DEBUG, (
"tcp_recved: received %"U16_F
" bytes, wnd %"TCPWNDSIZE_F
" (%"TCPWNDSIZE_F
").\n",
843 len, pcb->rcv_wnd, (u16_t)(TCP_WND_MAX(pcb) - pcb->rcv_wnd)));
859 if (tcp_port++ == TCP_LOCAL_PORT_RANGE_END) {
860 tcp_port = TCP_LOCAL_PORT_RANGE_START;
863 for (i = 0; i < NUM_TCP_PCB_LISTS; i++) {
864 for (pcb = *tcp_pcb_lists[i]; pcb != NULL; pcb = pcb->next) {
865 if (pcb->local_port == tcp_port) {
866 if (++n > (TCP_LOCAL_PORT_RANGE_END - TCP_LOCAL_PORT_RANGE_START)) {
891 tcp_connect(
struct tcp_pcb *pcb,
const ip_addr_t *ipaddr, u16_t port,
892 tcp_connected_fn connected)
896 u16_t old_local_port;
898 if ((pcb == NULL) || (ipaddr == NULL)) {
902 LWIP_ERROR(
"tcp_connect: can only connect from state CLOSED", pcb->state == CLOSED,
return ERR_ISCONN);
905 ip_addr_set(&pcb->remote_ip, ipaddr);
906 pcb->remote_port = port;
909 if (ip_addr_isany(&pcb->local_ip)) {
912 const ip_addr_t *local_ip;
913 ip_route_get_local_ip(&pcb->local_ip, &pcb->remote_ip, netif, local_ip);
914 if ((netif == NULL) || (local_ip == NULL)) {
920 ip_addr_copy(pcb->local_ip, *local_ip);
923 old_local_port = pcb->local_port;
924 if (pcb->local_port == 0) {
925 pcb->local_port = tcp_new_port();
926 if (pcb->local_port == 0) {
934 struct tcp_pcb *cpcb;
937 for (i = 2; i < NUM_TCP_PCB_LISTS; i++) {
938 for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
939 if ((cpcb->local_port == pcb->local_port) &&
940 (cpcb->remote_port == port) &&
941 ip_addr_cmp(&cpcb->local_ip, &pcb->local_ip) &&
942 ip_addr_cmp(&cpcb->remote_ip, ipaddr)) {
952 iss = tcp_next_iss(pcb);
955 pcb->lastack = iss - 1;
956 pcb->snd_wl2 = iss - 1;
957 pcb->snd_lbb = iss - 1;
960 pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_WND);
961 pcb->rcv_ann_right_edge = pcb->rcv_nxt;
962 pcb->snd_wnd = TCP_WND;
965 pcb->mss = INITIAL_MSS;
966 #if TCP_CALCULATE_EFF_SEND_MSS 967 pcb->mss = tcp_eff_send_mss(pcb->mss, &pcb->local_ip, &pcb->remote_ip);
970 #if LWIP_CALLBACK_API 971 pcb->connected = connected;
977 ret = tcp_enqueue_flags(pcb, TCP_SYN);
980 pcb->state = SYN_SENT;
981 if (old_local_port != 0) {
982 TCP_RMV(&tcp_bound_pcbs, pcb);
985 MIB2_STATS_INC(mib2.tcpactiveopens);
1002 struct tcp_pcb *pcb, *prev;
1003 tcpwnd_size_t eff_wnd;
1016 pcb = tcp_active_pcbs;
1020 while (pcb != NULL) {
1022 LWIP_ASSERT(
"tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED);
1023 LWIP_ASSERT(
"tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN);
1024 LWIP_ASSERT(
"tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT);
1025 if (pcb->last_timer == tcp_timer_ctr) {
1030 pcb->last_timer = tcp_timer_ctr;
1043 if (pcb->persist_backoff > 0) {
1046 u8_t backoff_cnt = tcp_persist_backoff[pcb->persist_backoff-1];
1047 if (pcb->persist_cnt < backoff_cnt) {
1050 if (pcb->persist_cnt >= backoff_cnt) {
1051 if (tcp_zero_window_probe(pcb) ==
ERR_OK) {
1052 pcb->persist_cnt = 0;
1053 if (pcb->persist_backoff <
sizeof(tcp_persist_backoff)) {
1054 pcb->persist_backoff++;
1060 if (pcb->rtime >= 0) {
1064 if (pcb->unacked != NULL && pcb->rtime >= pcb->rto) {
1067 " pcb->rto %"S16_F
"\n",
1068 pcb->rtime, pcb->rto));
1072 if (pcb->state != SYN_SENT) {
1073 u8_t backoff_idx = LWIP_MIN(pcb->nrtx,
sizeof(tcp_backoff)-1);
1074 pcb->rto = ((pcb->sa >> 3) + pcb->sv) << tcp_backoff[backoff_idx];
1081 eff_wnd = LWIP_MIN(pcb->cwnd, pcb->snd_wnd);
1082 pcb->ssthresh = eff_wnd >> 1;
1083 if (pcb->ssthresh < (tcpwnd_size_t)(pcb->mss << 1)) {
1084 pcb->ssthresh = (pcb->mss << 1);
1086 pcb->cwnd = pcb->mss;
1088 " ssthresh %"TCPWNDSIZE_F
"\n",
1089 pcb->cwnd, pcb->ssthresh));
1093 tcp_rexmit_rto(pcb);
1098 if (pcb->state == FIN_WAIT_2) {
1100 if (pcb->flags & TF_RXCLOSED) {
1103 if ((u32_t)(tcp_ticks - pcb->tmr) >
1104 TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) {
1113 ((pcb->state == ESTABLISHED) ||
1114 (pcb->state == CLOSE_WAIT))) {
1115 if ((u32_t)(tcp_ticks - pcb->tmr) >
1116 (pcb->keep_idle + TCP_KEEP_DUR(pcb)) / TCP_SLOW_INTERVAL)
1119 ip_addr_debug_print(
TCP_DEBUG, &pcb->remote_ip);
1124 }
else if ((u32_t)(tcp_ticks - pcb->tmr) >
1125 (pcb->keep_idle + pcb->keep_cnt_sent * TCP_KEEP_INTVL(pcb))
1126 / TCP_SLOW_INTERVAL)
1128 err = tcp_keepalive(pcb);
1130 pcb->keep_cnt_sent++;
1139 if (pcb->ooseq != NULL &&
1140 (u32_t)tcp_ticks - pcb->tmr >= pcb->rto * TCP_OOSEQ_TIMEOUT) {
1141 tcp_segs_free(pcb->ooseq);
1148 if (pcb->state == SYN_RCVD) {
1149 if ((u32_t)(tcp_ticks - pcb->tmr) >
1150 TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) {
1157 if (pcb->state == LAST_ACK) {
1158 if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
1166 struct tcp_pcb *pcb2;
1167 #if LWIP_CALLBACK_API 1168 tcp_err_fn err_fn = pcb->errf;
1171 enum tcp_state last_state;
1175 LWIP_ASSERT(
"tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs);
1176 prev->next = pcb->next;
1179 LWIP_ASSERT(
"tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb);
1180 tcp_active_pcbs = pcb->next;
1184 tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
1185 pcb->local_port, pcb->remote_port);
1188 err_arg = pcb->callback_arg;
1189 last_state = pcb->state;
1194 tcp_active_pcbs_changed = 0;
1195 TCP_EVENT_ERR(last_state, err_fn, err_arg,
ERR_ABRT);
1196 if (tcp_active_pcbs_changed) {
1197 goto tcp_slowtmr_start;
1206 if (prev->polltmr >= prev->pollinterval) {
1209 tcp_active_pcbs_changed = 0;
1210 TCP_EVENT_POLL(prev, err);
1211 if (tcp_active_pcbs_changed) {
1212 goto tcp_slowtmr_start;
1226 while (pcb != NULL) {
1227 LWIP_ASSERT(
"tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
1231 if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
1237 struct tcp_pcb *pcb2;
1241 LWIP_ASSERT(
"tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs);
1242 prev->next = pcb->next;
1245 LWIP_ASSERT(
"tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb);
1246 tcp_tw_pcbs = pcb->next;
1267 struct tcp_pcb *pcb;
1272 pcb = tcp_active_pcbs;
1274 while (pcb != NULL) {
1275 if (pcb->last_timer != tcp_timer_ctr) {
1276 struct tcp_pcb *next;
1277 pcb->last_timer = tcp_timer_ctr;
1279 if (pcb->flags & TF_ACK_DELAY) {
1283 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
1286 if (pcb->flags & TF_CLOSEPEND) {
1288 pcb->flags &= ~(TF_CLOSEPEND);
1289 tcp_close_shutdown_fin(pcb);
1295 if (pcb->refused_data != NULL) {
1296 tcp_active_pcbs_changed = 0;
1297 tcp_process_refused_data(pcb);
1298 if (tcp_active_pcbs_changed) {
1300 goto tcp_fasttmr_start;
1314 struct tcp_pcb *pcb;
1316 for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1317 if (pcb->flags & TF_NAGLEMEMERR) {
1325 tcp_process_refused_data(
struct tcp_pcb *pcb)
1327 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE 1329 while (pcb->refused_data != NULL)
1333 u8_t refused_flags = pcb->refused_data->
flags;
1336 struct pbuf *refused_data = pcb->refused_data;
1337 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE 1338 pbuf_split_64k(refused_data, &rest);
1339 pcb->refused_data = rest;
1341 pcb->refused_data = NULL;
1345 TCP_EVENT_RECV(pcb, refused_data,
ERR_OK, err);
1355 if (pcb->rcv_wnd != TCP_WND_MAX(pcb)) {
1358 TCP_EVENT_CLOSED(pcb, err);
1371 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE 1376 pcb->refused_data = refused_data;
1389 tcp_segs_free(
struct tcp_seg *seg)
1391 while (seg != NULL) {
1392 struct tcp_seg *next = seg->next;
1404 tcp_seg_free(
struct tcp_seg *seg)
1407 if (seg->p != NULL) {
1424 tcp_setprio(
struct tcp_pcb *pcb, u8_t prio)
1438 tcp_seg_copy(
struct tcp_seg *seg)
1440 struct tcp_seg *cseg;
1442 cseg = (
struct tcp_seg *)
memp_malloc(MEMP_TCP_SEG);
1446 SMEMCPY((u8_t *)cseg, (
const u8_t *)seg,
sizeof(
struct tcp_seg));
1452 #if LWIP_CALLBACK_API 1458 tcp_recv_null(
void *arg,
struct tcp_pcb *pcb,
struct pbuf *p,
err_t err)
1464 }
else if (err ==
ERR_OK) {
1465 return tcp_close(pcb);
1478 tcp_kill_prio(u8_t prio)
1480 struct tcp_pcb *pcb, *inactive;
1484 mprio = LWIP_MIN(TCP_PRIO_MAX, prio);
1489 for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1490 if (pcb->prio <= mprio &&
1491 (u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1492 inactivity = tcp_ticks - pcb->tmr;
1497 if (inactive != NULL) {
1499 (
void *)inactive, inactivity));
1500 tcp_abort(inactive);
1509 tcp_kill_state(
enum tcp_state state)
1511 struct tcp_pcb *pcb, *inactive;
1514 LWIP_ASSERT(
"invalid state", (state == CLOSING) || (state == LAST_ACK));
1520 for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1521 if (pcb->state == state) {
1522 if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1523 inactivity = tcp_ticks - pcb->tmr;
1528 if (inactive != NULL) {
1530 tcp_state_str[state], (
void *)inactive, inactivity));
1532 tcp_abandon(inactive, 0);
1541 tcp_kill_timewait(
void)
1543 struct tcp_pcb *pcb, *inactive;
1549 for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1550 if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1551 inactivity = tcp_ticks - pcb->tmr;
1555 if (inactive != NULL) {
1557 (
void *)inactive, inactivity));
1558 tcp_abort(inactive);
1569 tcp_alloc(u8_t prio)
1571 struct tcp_pcb *pcb;
1573 pcb = (
struct tcp_pcb *)
memp_malloc(MEMP_TCP_PCB);
1577 tcp_kill_timewait();
1579 pcb = (
struct tcp_pcb *)
memp_malloc(MEMP_TCP_PCB);
1583 tcp_kill_state(LAST_ACK);
1585 pcb = (
struct tcp_pcb *)
memp_malloc(MEMP_TCP_PCB);
1589 tcp_kill_state(CLOSING);
1591 pcb = (
struct tcp_pcb *)
memp_malloc(MEMP_TCP_PCB);
1595 tcp_kill_prio(prio);
1597 pcb = (
struct tcp_pcb *)
memp_malloc(MEMP_TCP_PCB);
1600 MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1605 MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1610 MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1615 MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1620 memset(pcb, 0,
sizeof(
struct tcp_pcb));
1622 pcb->snd_buf = TCP_SND_BUF;
1625 pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_WND);
1629 pcb->mss = INITIAL_MSS;
1630 pcb->rto = 3000 / TCP_SLOW_INTERVAL;
1631 pcb->sv = 3000 / TCP_SLOW_INTERVAL;
1634 pcb->tmr = tcp_ticks;
1635 pcb->last_timer = tcp_timer_ctr;
1643 pcb->ssthresh = TCP_SND_BUF;
1645 #if LWIP_CALLBACK_API 1646 pcb->recv = tcp_recv_null;
1650 pcb->keep_idle = TCP_KEEPIDLE_DEFAULT;
1652 #if LWIP_TCP_KEEPALIVE 1653 pcb->keep_intvl = TCP_KEEPINTVL_DEFAULT;
1654 pcb->keep_cnt = TCP_KEEPCNT_DEFAULT;
1676 return tcp_alloc(TCP_PRIO_NORMAL);
1691 tcp_new_ip_type(u8_t type)
1693 struct tcp_pcb * pcb;
1694 pcb = tcp_alloc(TCP_PRIO_NORMAL);
1695 #if LWIP_IPV4 && LWIP_IPV6 1697 IP_SET_TYPE_VAL(pcb->local_ip, type);
1698 IP_SET_TYPE_VAL(pcb->remote_ip, type);
1715 tcp_arg(
struct tcp_pcb *pcb,
void *arg)
1720 pcb->callback_arg = arg;
1723 #if LWIP_CALLBACK_API 1734 tcp_recv(
struct tcp_pcb *pcb, tcp_recv_fn recv)
1737 LWIP_ASSERT(
"invalid socket state for recv callback", pcb->state != LISTEN);
1751 tcp_sent(
struct tcp_pcb *pcb, tcp_sent_fn sent)
1754 LWIP_ASSERT(
"invalid socket state for sent callback", pcb->state != LISTEN);
1771 tcp_err(
struct tcp_pcb *pcb, tcp_err_fn err)
1774 LWIP_ASSERT(
"invalid socket state for err callback", pcb->state != LISTEN);
1789 tcp_accept(
struct tcp_pcb *pcb, tcp_accept_fn accept)
1791 if ((pcb != NULL) && (pcb->state == LISTEN)) {
1792 struct tcp_pcb_listen *lpcb = (
struct tcp_pcb_listen*)pcb;
1793 lpcb->accept = accept;
1807 tcp_poll(
struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval)
1809 LWIP_ASSERT(
"invalid socket state for poll", pcb->state != LISTEN);
1810 #if LWIP_CALLBACK_API 1815 pcb->pollinterval = interval;
1825 tcp_pcb_purge(
struct tcp_pcb *pcb)
1827 if (pcb->state != CLOSED &&
1828 pcb->state != TIME_WAIT &&
1829 pcb->state != LISTEN) {
1833 tcp_backlog_accepted(pcb);
1835 if (pcb->refused_data != NULL) {
1838 pcb->refused_data = NULL;
1840 if (pcb->unsent != NULL) {
1843 if (pcb->unacked != NULL) {
1847 if (pcb->ooseq != NULL) {
1850 tcp_segs_free(pcb->ooseq);
1858 tcp_segs_free(pcb->unsent);
1859 tcp_segs_free(pcb->unacked);
1860 pcb->unacked = pcb->unsent = NULL;
1862 pcb->unsent_oversize = 0;
1874 tcp_pcb_remove(
struct tcp_pcb **pcblist,
struct tcp_pcb *pcb)
1876 TCP_RMV(pcblist, pcb);
1881 if (pcb->state != TIME_WAIT &&
1882 pcb->state != LISTEN &&
1883 pcb->flags & TF_ACK_DELAY) {
1884 pcb->flags |= TF_ACK_NOW;
1888 if (pcb->state != LISTEN) {
1889 LWIP_ASSERT(
"unsent segments leaking", pcb->unsent == NULL);
1890 LWIP_ASSERT(
"unacked segments leaking", pcb->unacked == NULL);
1892 LWIP_ASSERT(
"ooseq segments leaking", pcb->ooseq == NULL);
1896 pcb->state = CLOSED;
1898 pcb->local_port = 0;
1900 LWIP_ASSERT(
"tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane());
1909 tcp_next_iss(
struct tcp_pcb *pcb)
1911 #ifdef LWIP_HOOK_TCP_ISN 1912 return LWIP_HOOK_TCP_ISN(&pcb->local_ip, pcb->local_port, &pcb->remote_ip, pcb->remote_port);
1914 static u32_t iss = 6510;
1923 #if TCP_CALCULATE_EFF_SEND_MSS 1930 tcp_eff_send_mss_impl(u16_t sendmss,
const ip_addr_t *dest
1931 #
if LWIP_IPV6 || LWIP_IPV4_SRC_ROUTING
1932 ,
const ip_addr_t *src
1937 struct netif *outif;
1940 outif = ip_route(src, dest);
1947 mtu = nd6_get_destination_mtu(ip_2_ip6(dest), outif);
1955 if (outif == NULL) {
1968 mss_s = mtu - IP6_HLEN - TCP_HLEN;
1976 mss_s = mtu - IP_HLEN - TCP_HLEN;
1983 sendmss = LWIP_MIN(sendmss, mss_s);
1991 tcp_netif_ip_addr_changed_pcblist(
const ip_addr_t* old_addr,
struct tcp_pcb* pcb_list)
1993 struct tcp_pcb *pcb;
1995 while (pcb != NULL) {
1997 if (ip_addr_cmp(&pcb->local_ip, old_addr)
2000 && (!IP_IS_V4_VAL(pcb->local_ip) || !ip4_addr_islinklocal(ip_2_ip4(&pcb->local_ip)))
2004 struct tcp_pcb *next = pcb->next;
2020 tcp_netif_ip_addr_changed(
const ip_addr_t* old_addr,
const ip_addr_t* new_addr)
2022 struct tcp_pcb_listen *lpcb, *next;
2024 if (!ip_addr_isany(old_addr)) {
2025 tcp_netif_ip_addr_changed_pcblist(old_addr, tcp_active_pcbs);
2026 tcp_netif_ip_addr_changed_pcblist(old_addr, tcp_bound_pcbs);
2028 if (!ip_addr_isany(new_addr)) {
2030 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = next) {
2033 if (ip_addr_cmp(&lpcb->local_ip, old_addr)) {
2036 ip_addr_copy(lpcb->local_ip, *new_addr);
2044 tcp_debug_state_str(
enum tcp_state s)
2046 return tcp_state_str[s];
2049 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG 2056 tcp_debug_print(
struct tcp_hdr *tcphdr)
2061 lwip_ntohs(tcphdr->src), lwip_ntohs(tcphdr->dest)));
2064 lwip_ntohl(tcphdr->seqno)));
2067 lwip_ntohl(tcphdr->ackno)));
2069 LWIP_DEBUGF(
TCP_DEBUG, (
"| %2"U16_F
" | |%"U16_F
"%"U16_F
"%"U16_F
"%"U16_F
"%"U16_F
"%"U16_F
"| %5"U16_F
" | (hdrlen, flags (",
2070 TCPH_HDRLEN(tcphdr),
2071 (u16_t)(TCPH_FLAGS(tcphdr) >> 5 & 1),
2072 (u16_t)(TCPH_FLAGS(tcphdr) >> 4 & 1),
2073 (u16_t)(TCPH_FLAGS(tcphdr) >> 3 & 1),
2074 (u16_t)(TCPH_FLAGS(tcphdr) >> 2 & 1),
2075 (u16_t)(TCPH_FLAGS(tcphdr) >> 1 & 1),
2076 (u16_t)(TCPH_FLAGS(tcphdr) & 1),
2077 lwip_ntohs(tcphdr->wnd)));
2078 tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
2082 lwip_ntohs(tcphdr->chksum), lwip_ntohs(tcphdr->urgp)));
2092 tcp_debug_print_state(
enum tcp_state s)
2103 tcp_debug_print_flags(u8_t flags)
2105 if (flags & TCP_FIN) {
2108 if (flags & TCP_SYN) {
2111 if (flags & TCP_RST) {
2114 if (flags & TCP_PSH) {
2117 if (flags & TCP_ACK) {
2120 if (flags & TCP_URG) {
2123 if (flags & TCP_ECE) {
2126 if (flags & TCP_CWR) {
2136 tcp_debug_print_pcbs(
void)
2138 struct tcp_pcb *pcb;
2139 struct tcp_pcb_listen *pcbl;
2142 for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
2143 LWIP_DEBUGF(
TCP_DEBUG, (
"Local port %"U16_F
", foreign port %"U16_F
" snd_nxt %"U32_F
" rcv_nxt %"U32_F
" ",
2144 pcb->local_port, pcb->remote_port,
2145 pcb->snd_nxt, pcb->rcv_nxt));
2146 tcp_debug_print_state(pcb->state);
2150 for (pcbl = tcp_listen_pcbs.listen_pcbs; pcbl != NULL; pcbl = pcbl->next) {
2152 tcp_debug_print_state(pcbl->state);
2156 for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
2157 LWIP_DEBUGF(
TCP_DEBUG, (
"Local port %"U16_F
", foreign port %"U16_F
" snd_nxt %"U32_F
" rcv_nxt %"U32_F
" ",
2158 pcb->local_port, pcb->remote_port,
2159 pcb->snd_nxt, pcb->rcv_nxt));
2160 tcp_debug_print_state(pcb->state);
2170 struct tcp_pcb *pcb;
2171 for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
2172 LWIP_ASSERT(
"tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED);
2173 LWIP_ASSERT(
"tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN);
2174 LWIP_ASSERT(
"tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
2176 for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
2177 LWIP_ASSERT(
"tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
void pbuf_ref(struct pbuf *p)
void memp_free(memp_t type, void *mem)
void pbuf_cat(struct pbuf *h, struct pbuf *t)
#define LWIP_DEBUGF(debug, message)
#define PBUF_FLAG_TCP_FIN
#define LWIP_UNUSED_ARG(x)
#define TCP_WND_UPDATE_THRESHOLD
u8_t pbuf_free(struct pbuf *p)
void * memp_malloc(memp_t type)
#define ip_get_option(pcb, opt)