MeterLogger
tcp.c
Go to the documentation of this file.
1 /**
2  * @file
3  * Transmission Control Protocol for IP
4  *
5  * This file contains common functions for the TCP implementation, such as functinos
6  * for manipulating the data structures and the TCP timer functions. TCP functions
7  * related to input and output is found in tcp_in.c and tcp_out.c respectively.
8  *
9  */
10 
11 /*
12  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
13  * All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without modification,
16  * are permitted provided that the following conditions are met:
17  *
18  * 1. Redistributions of source code must retain the above copyright notice,
19  * this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright notice,
21  * this list of conditions and the following disclaimer in the documentation
22  * and/or other materials provided with the distribution.
23  * 3. The name of the author may not be used to endorse or promote products
24  * derived from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
29  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
31  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35  * OF SUCH DAMAGE.
36  *
37  * This file is part of the lwIP TCP/IP stack.
38  *
39  * Author: Adam Dunkels <adam@sics.se>
40  *
41  */
42 
43 #include "lwip/opt.h"
44 
45 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
46 
47 #include "lwip/def.h"
48 #include "lwip/mem.h"
49 #include "lwip/memp.h"
50 #include "lwip/snmp.h"
51 #include "lwip/tcp.h"
52 #include "lwip/tcp_impl.h"
53 #include "lwip/debug.h"
54 #include "lwip/stats.h"
55 
56 #include <string.h>
57 
58 #ifdef MEMLEAK_DEBUG
59 static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
60 #endif
61 
62 #if 1 || TCP_DEBUG
63 const char tcp_state_str[][12] ICACHE_RODATA_ATTR = {
64  "CLOSED",
65  "LISTEN",
66  "SYN_SENT",
67  "SYN_RCVD",
68  "ESTABLISHED",
69  "FIN_WAIT_1",
70  "FIN_WAIT_2",
71  "CLOSE_WAIT",
72  "CLOSING",
73  "LAST_ACK",
74  "TIME_WAIT"
75 };
76 
77 //char tcp_state_str[12];
78 #endif
79 
80 /* Incremented every coarse grained timer shot (typically every 500 ms). */
81 u32_t tcp_ticks;
82 const u8_t tcp_backoff[13] ICACHE_RODATA_ATTR =
83  { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7};
84  /* Times per slowtmr hits */
85 const u8_t tcp_persist_backoff[7] ICACHE_RODATA_ATTR = { 3, 6, 12, 24, 48, 96, 120 };
86 
87 /* The TCP PCB lists. */
88 
89 /** List of all TCP PCBs bound but not yet (connected || listening) */
90 struct tcp_pcb *tcp_bound_pcbs;
91 /** List of all TCP PCBs in LISTEN state */
92 union tcp_listen_pcbs_t tcp_listen_pcbs;
93 /** List of all TCP PCBs that are in a state in which
94  * they accept or send data. */
95 struct tcp_pcb *tcp_active_pcbs;
96 /** List of all TCP PCBs in TIME-WAIT state */
97 struct tcp_pcb *tcp_tw_pcbs;
98 
99 #define NUM_TCP_PCB_LISTS 4
100 #define NUM_TCP_PCB_LISTS_NO_TIME_WAIT 3
101 /** An array with all (non-temporary) PCB lists, mainly used for smaller code size */
102 struct tcp_pcb ** const tcp_pcb_lists[] ICACHE_RODATA_ATTR = {&tcp_listen_pcbs.pcbs, &tcp_bound_pcbs,
103  &tcp_active_pcbs, &tcp_tw_pcbs};
104 
105 /** Only used for temporary storage. */
106 struct tcp_pcb *tcp_tmp_pcb;
107 
108 /** Timer counter to handle calling slow-timer from tcp_tmr() */
109 static u8_t tcp_timer;
110 static u16_t tcp_new_port(void);//����µ�tcp���ض˿�
111 
112 /**
113  * Called periodically to dispatch TCP timers.
114  *
115  */
116 void
117 tcp_tmr(void)
118 {
119  /* Call tcp_fasttmr() every 250 ms */
120  tcp_fasttmr();
121 
122  if (++tcp_timer & 1) {
123  /* Call tcp_tmr() every 500 ms, i.e., every other timer
124  tcp_tmr() is called. */
125  tcp_slowtmr();
126  }
127 }
128 
129 /**
130  * Closes the TX side of a connection held by the PCB.
131  * For tcp_close(), a RST is sent if the application didn't receive all data
132  * (tcp_recved() not called for all data passed to recv callback).
133  *
134  * Listening pcbs are freed and may not be referenced any more.
135  * Connection pcbs are freed if not yet connected and may not be referenced
136  * any more. If a connection is established (at least SYN received or in
137  * a closing state), the connection is closed, and put in a closing state.
138  * The pcb is then automatically freed in tcp_slowtmr(). It is therefore
139  * unsafe to reference it.
140  *
141  * @param pcb the tcp_pcb to close
142  * @return ERR_OK if connection has been closed
143  * another err_t if closing failed and pcb is not freed
144  */
145 static err_t
146 tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data)
147 {
148  err_t err;
149 
150  if (rst_on_unacked_data && (pcb->state != LISTEN)) {
151  if ((pcb->refused_data != NULL) || (pcb->rcv_wnd != TCP_WND)) {
152  /* Not all data received by application, send RST to tell the remote
153  side about this. */
154  LWIP_ASSERT("pcb->flags & TF_RXCLOSED", pcb->flags & TF_RXCLOSED);
155 
156  /* don't call tcp_abort here: we must not deallocate the pcb since
157  that might not be expected when calling tcp_close */
158  tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
159  pcb->local_port, pcb->remote_port);
160 
161  tcp_pcb_purge(pcb);
162 
163  /* TODO: to which state do we move now? */
164 
165  /* move to TIME_WAIT since we close actively */
166  TCP_RMV(&tcp_active_pcbs, pcb);
167  pcb->state = TIME_WAIT;
168  TCP_REG(&tcp_tw_pcbs, pcb);
169 
170  return ERR_OK;
171  }
172  }
173 
174  switch (pcb->state) {
175  case CLOSED:
176  /* Closing a pcb in the CLOSED state might seem erroneous,
177  * however, it is in this state once allocated and as yet unused
178  * and the user needs some way to free it should the need arise.
179  * Calling tcp_close() with a pcb that has already been closed, (i.e. twice)
180  * or for a pcb that has been used and then entered the CLOSED state
181  * is erroneous, but this should never happen as the pcb has in those cases
182  * been freed, and so any remaining handles are bogus. */
183  /*��CLOSED״̬�¹ر�һ��pcb�ƺ��Ǵ���ģ�
184  *������ˣ�һ�������״̬�·����˶��һ�û��ʹ��,�û���ҪһЩ�취���ͷ���
185  *����һ���Ѿ����رյ�pcb��tcp_close(),(��2��)����һ���Ѿ���ʹ����֮�󣬽���CLOSE״̬�Ǵ����
186  *������Щ����±��ͷŵ�pcb�Dz�����ڵ�,��ˣ��κ�ʣ��ľ���Ǽٵ�
187  */
188  err = ERR_OK;//�趨����ֵ
189  if (pcb->local_port != 0) {
190  TCP_RMV(&tcp_bound_pcbs, pcb);
191  }
192  memp_free(MEMP_TCP_PCB, pcb);//��MEMP_TCP_PCB�ڴ���趨�ͷŵ���pcb��Ӧ�ĵ�Ԫֵ,�ͷ��ڴ�
193  pcb = NULL;
194  break;
195  case LISTEN:
196  err = ERR_OK;
197  tcp_pcb_remove(&tcp_listen_pcbs.pcbs, pcb);//�Ӽ����PCB�б���ɾ���Ӧ��pcb
198  memp_free(MEMP_TCP_PCB_LISTEN, pcb);//��MEMP_TCP_PCB_LISTEN�ڴ�����趨�ͷŵ�pcb��Ԫֵ ,�ͷ��ڴ�
199  pcb = NULL;
200  break;
201  case SYN_SENT:
202  err = ERR_OK;
203  tcp_pcb_remove(&tcp_active_pcbs, pcb);
204  memp_free(MEMP_TCP_PCB, pcb);
205  pcb = NULL;
207  break;
208  case SYN_RCVD:
209  err = tcp_send_fin(pcb);//���������ر�FIN���ֱ���
210  if (err == ERR_OK) {
212  pcb->state = FIN_WAIT_1;//ת��FIN_WAIT_1״̬
213  }
214  break;
215  case ESTABLISHED:
216  err = tcp_send_fin(pcb);
217  if (err == ERR_OK) {
219  pcb->state = FIN_WAIT_1;
220  }
221  break;
222  case CLOSE_WAIT:
223  err = tcp_send_fin(pcb);
224  if (err == ERR_OK) {
226  pcb->state = LAST_ACK;//����LAST_ACK�ȴ�ACK��ʱ
227  }
228  break;
229  default:
230  /* Has already been closed, do nothing. */
231  err = ERR_OK;
232  pcb = NULL;
233  break;
234  }
235 
236  if (pcb != NULL && err == ERR_OK) {
237  /* To ensure all data has been sent when tcp_close returns, we have
238  to make sure tcp_output doesn't fail.
239  Since we don't really have to ensure all data has been sent when tcp_close
240  returns (unsent data is sent from tcp timer functions, also), we don't care
241  for the return value of tcp_output for now. */
242  /* @todo: When implementing SO_LINGER, this must be changed somehow:
243  If SOF_LINGER is set, the data should be sent and acked before close returns.
244  This can only be valid for sequential APIs, not for the raw API. */
245  tcp_output(pcb);//���ú����Ϳ��ƿ������ʣ��ı��ģ�����FIN���ֱ��Ķ�
246  }
247  return err;
248 }
249 
250 /**
251  * Closes the connection held by the PCB.
252  *
253  * Listening pcbs are freed and may not be referenced any more.
254  * Connection pcbs are freed if not yet connected and may not be referenced
255  * any more. If a connection is established (at least SYN received or in
256  * a closing state), the connection is closed, and put in a closing state.
257  * The pcb is then automatically freed in tcp_slowtmr(). It is therefore
258  * unsafe to reference it (unless an error is returned).
259  *
260  * @param pcb the tcp_pcb to close
261  * @return ERR_OK if connection has been closed
262  * another err_t if closing failed and pcb is not freed
263  */
264  /*
265  *ͨ��PCB�ر���������
266  *�����е�pcbӦ�ñ��ͷŵģ�Ҳ����ԶҲ���ᱻʹ����
267  *���û�����ӻ�����Ҳû�б�����,���ӵ�pcbӦ�ñ��ͷŵ�
268  *���һ�����ӱ�����(����SYN�Ѿ������ջ�����һ���ر��е�״̬)
269  *���ӱ��ر��ˣ�����������һ�����ڹرյ�״̬
270  *pcb�Զ���tcp_slowtmr()�ͷ�,�����������Dz���ȫ��
271  */
272 err_t
273 tcp_close(struct tcp_pcb *pcb)
274 {
275 #if TCP_DEBUG //TCP debug��Ϣ����ӡpcb��״̬
276  LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in "));
277  tcp_debug_print_state(pcb->state);
278 #endif /* TCP_DEBUG */
279 
280  if (pcb->state != LISTEN) {
281  /* Set a flag not to receive any more data... */
282  pcb->flags |= TF_RXCLOSED;
283  }
284  /* ... and close */
285  return tcp_close_shutdown(pcb, 1);
286 }
287 
288 /**
289  * Causes all or part of a full-duplex connection of this PCB to be shut down.
290  * This doesn't deallocate the PCB!
291  *
292  * @param pcb PCB to shutdown
293  * @param shut_rx shut down receive side if this is != 0
294  * @param shut_tx shut down send side if this is != 0
295  * @return ERR_OK if shutdown succeeded (or the PCB has already been shut down)
296  * another err_t on error.
297  */
298 err_t
299 tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx)
300 {
301  if (pcb->state == LISTEN) {
302  return ERR_CONN;
303  }
304  if (shut_rx) {
305  /* shut down the receive side: free buffered data... */
306  if (pcb->refused_data != NULL) {
307  pbuf_free(pcb->refused_data);
308  pcb->refused_data = NULL;
309  }
310  /* ... and set a flag not to receive any more data */
311  pcb->flags |= TF_RXCLOSED;
312  }
313  if (shut_tx) {
314  /* This can't happen twice since if it succeeds, the pcb's state is changed.
315  Only close in these states as the others directly deallocate the PCB */
316  switch (pcb->state) {
317  case SYN_RCVD:
318  case ESTABLISHED:
319  case CLOSE_WAIT:
320  return tcp_close_shutdown(pcb, 0);
321  default:
322  /* don't shut down other states */
323  break;
324  }
325  }
326  /* @todo: return another err_t if not in correct state or already shut? */
327  return ERR_OK;
328 }
329 
330 /**
331  * Abandons a connection and optionally sends a RST to the remote
332  * host. Deletes the local protocol control block. This is done when
333  * a connection is killed because of shortage of memory.
334  *
335  * @param pcb the tcp_pcb to abort
336  * @param reset boolean to indicate whether a reset should be sent
337  */
338 void
339 tcp_abandon(struct tcp_pcb *pcb, int reset)
340 {
341  u32_t seqno, ackno;
342  u16_t remote_port, local_port;
343  ip_addr_t remote_ip, local_ip;
344 #if LWIP_CALLBACK_API
345  tcp_err_fn errf;
346 #endif /* LWIP_CALLBACK_API */
347  void *errf_arg;
348 
349  /* pcb->state LISTEN not allowed here */
350  LWIP_ASSERT("don't call tcp_abort/tcp_abandon for listen-pcbs",
351  pcb->state != LISTEN);
352  /* Figure out on which TCP PCB list we are, and remove us. If we
353  are in an active state, call the receive function associated with
354  the PCB with a NULL argument, and send an RST to the remote end. */
355  if (pcb->state == TIME_WAIT) {
356  tcp_pcb_remove(&tcp_tw_pcbs, pcb);
357  memp_free(MEMP_TCP_PCB, pcb);
358  } else {
359  seqno = pcb->snd_nxt;
360  ackno = pcb->rcv_nxt;
361  ip_addr_copy(local_ip, pcb->local_ip);
362  ip_addr_copy(remote_ip, pcb->remote_ip);
363  local_port = pcb->local_port;
364  remote_port = pcb->remote_port;
365 #if LWIP_CALLBACK_API
366  errf = pcb->errf;
367 #endif /* LWIP_CALLBACK_API */
368  errf_arg = pcb->callback_arg;
369  tcp_pcb_remove(&tcp_active_pcbs, pcb);
370  if (pcb->unacked != NULL) {
371  tcp_segs_free(pcb->unacked);
372  }
373  if (pcb->unsent != NULL) {
374  tcp_segs_free(pcb->unsent);
375  }
376 #if TCP_QUEUE_OOSEQ
377  if (pcb->ooseq != NULL) {
378  tcp_segs_free(pcb->ooseq);
379  }
380 #endif /* TCP_QUEUE_OOSEQ */
381  if (reset) {
382  LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abandon: sending RST\n"));
383  tcp_rst(seqno, ackno, &local_ip, &remote_ip, local_port, remote_port);
384  }
385  TCP_EVENT_ERR(errf, errf_arg, ERR_ABRT);
386  memp_free(MEMP_TCP_PCB, pcb);
387  }
388 }
389 
390 /**
391  * Aborts the connection by sending a RST (reset) segment to the remote
392  * host. The pcb is deallocated. This function never fails.
393  *
394  * ATTENTION: When calling this from one of the TCP callbacks, make
395  * sure you always return ERR_ABRT (and never return ERR_ABRT otherwise
396  * or you will risk accessing deallocated memory or memory leaks!
397  *
398  * @param pcb the tcp pcb to abort
399  */
400 void
401 tcp_abort(struct tcp_pcb *pcb)
402 {
403  tcp_abandon(pcb, 1);
404 }
405 
406 /**
407  * Binds the connection to a local portnumber and IP address. If the
408  * IP address is not given (i.e., ipaddr == NULL), the IP address of
409  * the outgoing network interface is used instead.
410  *
411  * @param pcb the tcp_pcb to bind (no check is done whether this pcb is
412  * already bound!)
413  * @param ipaddr the local ip address to bind to (use IP_ADDR_ANY to bind
414  * to any local address
415  * @param port the local port to bind to
416  * @return ERR_USE if the port is already in use
417  * ERR_OK if bound
418  */
419 err_t
420 tcp_bind(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port)
421 {
422  int i;
423  int max_pcb_list = NUM_TCP_PCB_LISTS;
424  struct tcp_pcb *cpcb;
425 
426  LWIP_ERROR("tcp_bind: can only bind in state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
427 
428 #if SO_REUSE
429  /* Unless the REUSEADDR flag is set,
430  we have to check the pcbs in TIME-WAIT state, also.
431  We do not dump TIME_WAIT pcb's; they can still be matched by incoming
432  packets using both local and remote IP addresses and ports to distinguish.
433  */
434  if ((pcb->so_options & SOF_REUSEADDR) != 0) {
435  max_pcb_list = NUM_TCP_PCB_LISTS_NO_TIME_WAIT;
436  }
437 #endif /* SO_REUSE */
438 
439  if (port == 0) {
440  port = tcp_new_port();
441  }
442 
443  /* Check if the address already is in use (on all lists) */
444  for (i = 0; i < max_pcb_list; i++) {
445  for(cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
446  if (cpcb->local_port == port) {
447 #if SO_REUSE
448  /* Omit checking for the same port if both pcbs have REUSEADDR set.
449  For SO_REUSEADDR, the duplicate-check for a 5-tuple is done in
450  tcp_connect. */
451  if (((pcb->so_options & SOF_REUSEADDR) == 0) ||
452  ((cpcb->so_options & SOF_REUSEADDR) == 0))
453 #endif /* SO_REUSE */
454  {
455  if (ip_addr_isany(&(cpcb->local_ip)) ||
456  ip_addr_isany(ipaddr) ||
457  ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {
458  //os_printf("Address in use\n");
459  return ERR_USE;
460  }
461  }
462  }
463  }
464  }
465 
466  if (!ip_addr_isany(ipaddr)) {
467  pcb->local_ip = *ipaddr;
468  }
469  pcb->local_port = port;
470  TCP_REG(&tcp_bound_pcbs, pcb);
471  LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %"U16_F"\n", port));
472  return ERR_OK;
473 }
474 #if LWIP_CALLBACK_API
475 /**
476  * Default accept callback if no accept callback is specified by the user.
477  */
478 static err_t
479 tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err)
480 {
481  LWIP_UNUSED_ARG(arg);
482  LWIP_UNUSED_ARG(pcb);
483  LWIP_UNUSED_ARG(err);
484 
485  return ERR_ABRT;
486 }
487 #endif /* LWIP_CALLBACK_API */
488 
489 /**
490  * Set the state of the connection to be LISTEN, which means that it
491  * is able to accept incoming connections. The protocol control block
492  * is reallocated in order to consume less memory. Setting the
493  * connection to LISTEN is an irreversible process.
494  *��ij���󶨵Ŀ��ƿ���Ϊ����״̬
495  * @param pcb the original tcp_pcb �����Ŀ��ƿ����
496  * @param backlog the incoming connections queue limit
497  * @return tcp_pcb used for listening, consumes less memory.ָ������״̬�Ŀ��ƿ�
498  *
499  * @note The original tcp_pcb is freed. This function therefore has to be
500  * called like this:
501  * tpcb = tcp_listen(tpcb);
502  */
503 struct tcp_pcb *
504 tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
505 {
506  struct tcp_pcb_listen *lpcb;
507 
508  LWIP_UNUSED_ARG(backlog);
509  LWIP_ERROR("tcp_listen: pcb already connected", pcb->state == CLOSED, return NULL);
510 
511  /* already listening? */
512  if (pcb->state == LISTEN) {
513  return pcb;
514  }
515 #if SO_REUSE
516  if ((pcb->so_options & SOF_REUSEADDR) != 0) {
517  /* Since SOF_REUSEADDR allows reusing a local address before the pcb's usage
518  is declared (listen-/connection-pcb), we have to make sure now that
519  this port is only used once for every local IP. */
520  for(lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
521  if (lpcb->local_port == pcb->local_port) {
522  if (ip_addr_cmp(&lpcb->local_ip, &pcb->local_ip)) {
523  /* this address/port is already used */
524  return NULL;
525  }
526  }
527  }
528  }
529 #endif /* SO_REUSE */
530  lpcb = (struct tcp_pcb_listen *)memp_malloc(MEMP_TCP_PCB_LISTEN);//�����ڴ�ؿռ�
531  if (lpcb == NULL) {
532  return NULL;
533  }
534  lpcb->callback_arg = pcb->callback_arg;
535  lpcb->local_port = pcb->local_port;
536  lpcb->state = LISTEN;
537  lpcb->prio = pcb->prio;
538  lpcb->so_options = pcb->so_options;
539  lpcb->so_options |= SOF_ACCEPTCONN;
540  lpcb->ttl = pcb->ttl;
541  lpcb->tos = pcb->tos;
542  ip_addr_copy(lpcb->local_ip, pcb->local_ip);
543  if (pcb->local_port != 0) {
544  TCP_RMV(&tcp_bound_pcbs, pcb);
545  }
546  memp_free(MEMP_TCP_PCB, pcb);
547 #if LWIP_CALLBACK_API
548  lpcb->accept = tcp_accept_null;//���ܿͻ������ӵ�Ĭ�ϻص�����
549 #endif /* LWIP_CALLBACK_API */
550 #if TCP_LISTEN_BACKLOG
551  lpcb->accepts_pending = 0;
552  lpcb->backlog = (backlog ? backlog : 1);
553 #endif /* TCP_LISTEN_BACKLOG */
554  TCP_REG(&tcp_listen_pcbs.pcbs, (struct tcp_pcb *)lpcb);//���ƿ����tcp_listen_pcbs�����ײ�
555  return (struct tcp_pcb *)lpcb;
556 }
557 
558 /**
559  * Update the state that tracks the available window space to advertise.
560  *
561  * Returns how much extra window would be advertised if we sent an
562  * update now.
563  */
564 u32_t tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb)
565 {
566  u32_t new_right_edge = pcb->rcv_nxt + pcb->rcv_wnd;
567 
568  if (TCP_SEQ_GEQ(new_right_edge, pcb->rcv_ann_right_edge + LWIP_MIN((TCP_WND / 2), pcb->mss))) {
569  /* we can advertise more window */
570  pcb->rcv_ann_wnd = pcb->rcv_wnd;
571  return new_right_edge - pcb->rcv_ann_right_edge;
572  } else {
573  if (TCP_SEQ_GT(pcb->rcv_nxt, pcb->rcv_ann_right_edge)) {
574  /* Can happen due to other end sending out of advertised window,
575  * but within actual available (but not yet advertised) window */
576  pcb->rcv_ann_wnd = 0;
577  } else {
578  /* keep the right edge of window constant */
579  u32_t new_rcv_ann_wnd = pcb->rcv_ann_right_edge - pcb->rcv_nxt;
580  LWIP_ASSERT("new_rcv_ann_wnd <= 0xffff", new_rcv_ann_wnd <= 0xffff);
581  pcb->rcv_ann_wnd = (u16_t)new_rcv_ann_wnd;
582  }
583  return 0;
584  }
585 }
586 
587 /**
588  * This function should be called by the application when it has
589  * processed the data. The purpose is to advertise a larger window
590  * when the data has been processed.
591  *Ӧ�ó�����ݴ�����Ϻ�֪ͨ�ں˸��½��մ���
592  * @param pcb the tcp_pcb for which data is read
593  * @param len the amount of bytes that have been read by the application
594  */
595 void
596 tcp_recved(struct tcp_pcb *pcb, u16_t len)
597 {
598  int wnd_inflation;
599 
600  LWIP_ASSERT("tcp_recved: len would wrap rcv_wnd\n",
601  len <= 0xffff - pcb->rcv_wnd );
602 
603  pcb->rcv_wnd += len;
604  if (pcb->rcv_wnd > TCP_WND) {
605  pcb->rcv_wnd = TCP_WND;
606  }
607 
608  wnd_inflation = tcp_update_rcv_ann_wnd(pcb);
609 
610  /* If the change in the right edge of window is significant (default
611  * watermark is TCP_WND/4), then send an explicit update now.
612  * Otherwise wait for a packet to be sent in the normal course of
613  * events (or more window to be available later) */
614  if (wnd_inflation >= TCP_WND_UPDATE_THRESHOLD) {
615  tcp_ack_now(pcb);
616  tcp_output(pcb);
617  }
618 
619  LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: recveived %"U16_F" bytes, wnd %"U16_F" (%"U16_F").\n",
620  len, pcb->rcv_wnd, TCP_WND - pcb->rcv_wnd));
621 }
622 
623 /**
624  * A nastly hack featuring 'goto' statements that allocates a
625  * new TCP local port.
626  *
627  * @return a new (free) local TCP port number
628  */
629 static u16_t
630 tcp_new_port(void)
631 {
632  int i;
633  struct tcp_pcb *pcb;
634 #ifndef TCP_LOCAL_PORT_RANGE_START
635 #define TCP_LOCAL_PORT_RANGE_START 1024
636 #define TCP_LOCAL_PORT_RANGE_END 0x7fff
637 #endif
638  static u16_t port = TCP_LOCAL_PORT_RANGE_START;
639 
640  again:
641 // if (++port >= TCP_LOCAL_PORT_RANGE_END) {
642 // port = TCP_LOCAL_PORT_RANGE_START;
643 // }
644  port = os_random();
645  port %= TCP_LOCAL_PORT_RANGE_END;
646  if (port < TCP_LOCAL_PORT_RANGE_START)
647  port += TCP_LOCAL_PORT_RANGE_START;
648  /* Check all PCB lists. */
649  for (i = 0; i < NUM_TCP_PCB_LISTS; i++) {
650  for(pcb = *tcp_pcb_lists[i]; pcb != NULL; pcb = pcb->next) {
651  if (pcb->local_port == port) {
652  goto again;
653  }
654  }
655  }
656  return port;
657 }
658 
659 /**
660  * Connects to another host. The function given as the "connected"
661  * argument will be called when the connection has been established.
662  *�����������һ��SYN���ֱ���
663  * @param pcb the tcp_pcb used to establish the connection �����Ŀ��ƿ����
664  * @param ipaddr the remote ip address to connect to ������IP��ַ
665  * @param port the remote tcp port to connect to �������˿ں�
666  * @param connected callback function to call when connected (or on error)
667  * @return ERR_VAL if invalid arguments are given
668  * ERR_OK if connect request has been sent
669  * other err_t values if connect request couldn't be sent
670  */
671 err_t
672 tcp_connect(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port,
673  tcp_connected_fn connected)
674 {
675  err_t ret;
676  u32_t iss;
677  u16_t old_local_port;
678 
679  LWIP_ERROR("tcp_connect: can only connected from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
680 
681  LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port));
682  if (ipaddr != NULL) {
683  pcb->remote_ip = *ipaddr;//������IP��ַ��Ч�������Ӽ�¼�м�¼��IP��ַ�����򷵻ش���
684  } else {
685  return ERR_VAL;
686  }
687  pcb->remote_port = port;//��¼�������˿�(Ŀ�Ķ˿�)
688 
689  /* check if we have a route to the remote host */
690  if (ip_addr_isany(&(pcb->local_ip))) {
691  /* no local IP address set, yet. */
692  struct netif *netif = ip_route(&(pcb->remote_ip));
693  if (netif == NULL) {
694  /* Don't even try to send a SYN packet if we have no route
695  since that will fail. */
696  return ERR_RTE;
697  }
698  /* Use the netif's IP address as local address. */
699  ip_addr_copy(pcb->local_ip, netif->ip_addr);
700  }
701 
702  old_local_port = pcb->local_port;
703  if (pcb->local_port == 0) {
704  pcb->local_port = tcp_new_port();
705 
706  }
707 #if SO_REUSE
708  if ((pcb->so_options & SOF_REUSEADDR) != 0) {
709  /* Since SOF_REUSEADDR allows reusing a local address, we have to make sure
710  now that the 5-tuple is unique. */
711  struct tcp_pcb *cpcb;
712  int i;
713  /* Don't check listen- and bound-PCBs, check active- and TIME-WAIT PCBs. */
714  for (i = 2; i < NUM_TCP_PCB_LISTS; i++) {
715  for(cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
716  if ((cpcb->local_port == pcb->local_port) &&
717  (cpcb->remote_port == port) &&
718  ip_addr_cmp(&cpcb->local_ip, &pcb->local_ip) &&
719  ip_addr_cmp(&cpcb->remote_ip, ipaddr)) {
720  /* linux returns EISCONN here, but ERR_USE should be OK for us */
721  return ERR_USE;
722  }
723  }
724  }
725  }
726 #endif /* SO_REUSE */
727  iss = tcp_next_iss();//��ʼ�����
728  pcb->rcv_nxt = 0;//���÷��ʹ��ڵĸ����ֶ�
729  pcb->snd_nxt = iss;
730  pcb->lastack = iss - 1;
731  pcb->snd_lbb = iss - 1;
732  pcb->rcv_wnd = TCP_WND;//����Ĭ�Ͻ��մ��ڸ����ֶ�ֵ
733  pcb->rcv_ann_wnd = TCP_WND;
734  pcb->rcv_ann_right_edge = pcb->rcv_nxt;
735  pcb->snd_wnd = TCP_WND;
736  /* As initial send MSS, we use TCP_MSS but limit it to 536.
737  The send MSS is updated when an MSS option is received. */
738  pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS;//��ʼ������Ķδ�С
739 #if TCP_CALCULATE_EFF_SEND_MSS
740  pcb->mss = tcp_eff_send_mss(pcb->mss, ipaddr);
741 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
742  pcb->cwnd = 1;//��ʼ�������
743  pcb->ssthresh = pcb->mss * 10;
744 #if LWIP_CALLBACK_API
745  pcb->connected = connected;//ע��connected�ص�����
746 #else /* LWIP_CALLBACK_API */
747  LWIP_UNUSED_ARG(connected);
748 #endif /* LWIP_CALLBACK_API */
749 
750  /* Send a SYN together with the MSS option. */
751  ret = tcp_enqueue_flags(pcb, TCP_SYN);
752  if (ret == ERR_OK) {
753  /* SYN segment was enqueued, changed the pcbs state now ���ƿ�����ΪSYN_SENT ״̬*/
754  pcb->state = SYN_SENT;
755  if (old_local_port != 0) {
756  TCP_RMV(&tcp_bound_pcbs, pcb);
757  }
758  TCP_REG(&tcp_active_pcbs, pcb);
760 
761  tcp_output(pcb);//�����ƿ������ӵı��ķ��ͳ�ȥ
762  }
763  return ret;
764 }
765 
766 /**
767  * Called every 500 ms and implements the retransmission timer and the timer that
768  * removes PCBs that have been in TIME-WAIT for enough time. It also increments
769  * various timers such as the inactivity timer in each PCB.
770  *
771  * Automatically called from tcp_tmr().
772  */
773 void
774 tcp_slowtmr(void)
775 {
776  struct tcp_pcb *pcb, *prev;
777  u16_t eff_wnd;
778  u8_t pcb_remove; /* flag if a PCB should be removed */
779  u8_t pcb_reset; /* flag if a RST should be sent when removing */
780  err_t err;
781 
782  err = ERR_OK;
783 
784  ++tcp_ticks;
785 
786  /* Steps through all of the active PCBs. */
787  prev = NULL;
788  pcb = tcp_active_pcbs;
789  if (pcb == NULL) {
790  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: no active pcbs\n"));
791  }
792  while (pcb != NULL) {
793  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: processing active pcb\n"));
794  LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED);
795  LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN);
796  LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT);
797 
798  pcb_remove = 0;
799  pcb_reset = 0;
800 
801  if (pcb->state == SYN_SENT && pcb->nrtx == TCP_SYNMAXRTX) {
802  ++pcb_remove;
803  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max SYN retries reached\n"));
804  }
805  else if (pcb->nrtx == TCP_MAXRTX) {
806  ++pcb_remove;
807  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max DATA retries reached\n"));
808  } else {
809  if (pcb->persist_backoff > 0) {
810  /* If snd_wnd is zero, use persist timer to send 1 byte probes
811  * instead of using the standard retransmission mechanism. */
812  pcb->persist_cnt++;
813  if (pcb->persist_cnt >= system_get_data_of_array_8(tcp_persist_backoff, pcb->persist_backoff-1)) {
814  pcb->persist_cnt = 0;
815  if (pcb->persist_backoff < sizeof(tcp_persist_backoff)) {
816  pcb->persist_backoff++;
817  }
818  tcp_zero_window_probe(pcb);
819  }
820  } else {
821  /* Increase the retransmission timer if it is running */
822  if(pcb->rtime >= 0)
823  ++pcb->rtime;
824 
825  if (pcb->unacked != NULL && pcb->rtime >= pcb->rto) {
826  /* Time for a retransmission. */
827  LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_slowtmr: rtime %"S16_F
828  " pcb->rto %"S16_F"\n",
829  pcb->rtime, pcb->rto));
830 
831  /* Double retransmission time-out unless we are trying to
832  * connect to somebody (i.e., we are in SYN_SENT). */
833  if (pcb->state != SYN_SENT) {
834  pcb->rto = ((pcb->sa >> 3) + pcb->sv) << system_get_data_of_array_8(tcp_backoff, pcb->nrtx);
835 // if (pcb->rto >= TCP_MAXRTO)
836 // pcb->rto >>= 1;
837  }
838 
839  /* Reset the retransmission timer. */
840  pcb->rtime = 0;
841 
842  /* Reduce congestion window and ssthresh. */
843  eff_wnd = LWIP_MIN(pcb->cwnd, pcb->snd_wnd);
844  pcb->ssthresh = eff_wnd >> 1;
845  if (pcb->ssthresh < (pcb->mss << 1)) {
846  pcb->ssthresh = (pcb->mss << 1);
847  }
848  pcb->cwnd = pcb->mss;
849  LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: cwnd %"U16_F
850  " ssthresh %"U16_F"\n",
851  pcb->cwnd, pcb->ssthresh));
852 
853  /* The following needs to be called AFTER cwnd is set to one
854  mss - STJ */
855  tcp_rexmit_rto(pcb);
856  }
857  }
858  }
859  /* Check if this PCB has stayed too long in FIN-WAIT-2 */
860  if (pcb->state == FIN_WAIT_2) {
861  if ((u32_t)(tcp_ticks - pcb->tmr) >
862  TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) {
863  ++pcb_remove;
864  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2\n"));
865  }
866  }
867 
868  /* Check if KEEPALIVE should be sent */
869  if((pcb->so_options & SOF_KEEPALIVE) &&
870  ((pcb->state == ESTABLISHED) ||
871  (pcb->state == CLOSE_WAIT))) {
872 #if LWIP_TCP_KEEPALIVE
873  if((u32_t)(tcp_ticks - pcb->tmr) >
874  (pcb->keep_idle + (pcb->keep_cnt*pcb->keep_intvl))
875  / TCP_SLOW_INTERVAL)
876 #else
877  if((u32_t)(tcp_ticks - pcb->tmr) >
878  (pcb->keep_idle + TCP_MAXIDLE) / TCP_SLOW_INTERVAL)
879 #endif /* LWIP_TCP_KEEPALIVE */
880  {
881  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: KEEPALIVE timeout. Aborting connection to %"U16_F".%"U16_F".%"U16_F".%"U16_F".\n",
882  ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip),
883  ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip)));
884 
885  ++pcb_remove;
886  ++pcb_reset;
887  }
888 #if LWIP_TCP_KEEPALIVE
889  else if((u32_t)(tcp_ticks - pcb->tmr) >
890  (pcb->keep_idle + pcb->keep_cnt_sent * pcb->keep_intvl)
891  / TCP_SLOW_INTERVAL)
892 #else
893  else if((u32_t)(tcp_ticks - pcb->tmr) >
894  (pcb->keep_idle + pcb->keep_cnt_sent * TCP_KEEPINTVL_DEFAULT)
895  / TCP_SLOW_INTERVAL)
896 #endif /* LWIP_TCP_KEEPALIVE */
897  {
898  tcp_keepalive(pcb);
899  pcb->keep_cnt_sent++;
900  }
901  }
902 
903  /* If this PCB has queued out of sequence data, but has been
904  inactive for too long, will drop the data (it will eventually
905  be retransmitted). */
906 #if TCP_QUEUE_OOSEQ
907  if (pcb->ooseq != NULL &&
908  (u32_t)tcp_ticks - pcb->tmr >= pcb->rto * TCP_OOSEQ_TIMEOUT) {
909  tcp_segs_free(pcb->ooseq);
910  pcb->ooseq = NULL;
911  LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: dropping OOSEQ queued data\n"));
912  }
913 #endif /* TCP_QUEUE_OOSEQ */
914 
915  /* Check if this PCB has stayed too long in SYN-RCVD */
916  if (pcb->state == SYN_RCVD) {
917  if ((u32_t)(tcp_ticks - pcb->tmr) >
918  TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) {
919  ++pcb_remove;
920  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD\n"));
921  }
922  }
923 
924  /* Check if this PCB has stayed too long in LAST-ACK */
925  if (pcb->state == LAST_ACK) {
926  if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
927  ++pcb_remove;
928  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in LAST-ACK\n"));
929  }
930  }
931 
932  /* If the PCB should be removed, do it. */
933  if (pcb_remove) {
934  struct tcp_pcb *pcb2;
935  tcp_pcb_purge(pcb);
936  /* Remove PCB from tcp_active_pcbs list. */
937  if (prev != NULL) {
938  LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs);
939  prev->next = pcb->next;
940  } else {
941  /* This PCB was the first. */
942  LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb);
943  tcp_active_pcbs = pcb->next;
944  }
945 
946  if (pcb_reset) {
947  tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
948  pcb->local_port, pcb->remote_port);
949  }
950 
951  TCP_EVENT_ERR(pcb->errf, pcb->callback_arg, ERR_ABRT);
952  pcb2 = pcb;
953  pcb = pcb->next;
954  memp_free(MEMP_TCP_PCB, pcb2);
955  } else {
956  /* get the 'next' element now and work with 'prev' below (in case of abort) */
957  prev = pcb;
958  pcb = pcb->next;
959 
960  /* We check if we should poll the connection. */
961  ++prev->polltmr;
962  if (prev->polltmr >= prev->pollinterval) {
963  prev->polltmr = 0;
964  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: polling application\n"));
965  TCP_EVENT_POLL(prev, err);
966  /* if err == ERR_ABRT, 'prev' is already deallocated */
967  if (err == ERR_OK) {
968  tcp_output(prev);
969  }
970  }
971  }
972  }
973 
974 
975  /* Steps through all of the TIME-WAIT PCBs. */
976  prev = NULL;
977  pcb = tcp_tw_pcbs;
978  while (pcb != NULL) {
979  LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
980  pcb_remove = 0;
981 
982  /* Check if this PCB has stayed long enough in TIME-WAIT */
983  if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
984  ++pcb_remove;
985  }
986 
987 
988 
989  /* If the PCB should be removed, do it. */
990  if (pcb_remove) {
991  struct tcp_pcb *pcb2;
992  tcp_pcb_purge(pcb);
993  /* Remove PCB from tcp_tw_pcbs list. */
994  if (prev != NULL) {
995  LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs);
996  prev->next = pcb->next;
997  } else {
998  /* This PCB was the first. */
999  LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb);
1000  tcp_tw_pcbs = pcb->next;
1001  }
1002  pcb2 = pcb;
1003  pcb = pcb->next;
1004  memp_free(MEMP_TCP_PCB, pcb2);
1005  } else {
1006  prev = pcb;
1007  pcb = pcb->next;
1008  }
1009  }
1010 }
1011 
1012 /**
1013  * Is called every TCP_FAST_INTERVAL (250 ms) and process data previously
1014  * "refused" by upper layer (application) and sends delayed ACKs.
1015  *
1016  * Automatically called from tcp_tmr().
1017  */
1018 void
1019 tcp_fasttmr(void)
1020 {
1021  struct tcp_pcb *pcb = tcp_active_pcbs;
1022 
1023  while(pcb != NULL) {
1024  struct tcp_pcb *next = pcb->next;
1025  /* If there is data which was previously "refused" by upper layer */
1026  if (pcb->refused_data != NULL) {
1027  /* Notify again application with data previously received. */
1028  err_t err;
1029  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_fasttmr: notify kept packet\n"));
1030  TCP_EVENT_RECV(pcb, pcb->refused_data, ERR_OK, err);
1031  if (err == ERR_OK) {
1032  pcb->refused_data = NULL;
1033  } else if (err == ERR_ABRT) {
1034  /* if err == ERR_ABRT, 'pcb' is already deallocated */
1035  pcb = NULL;
1036  }
1037  }
1038 
1039  /* send delayed ACKs */
1040  if (pcb && (pcb->flags & TF_ACK_DELAY)) {
1041  LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n"));
1042  tcp_ack_now(pcb);
1043  tcp_output(pcb);
1044  pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
1045  }
1046 
1047  pcb = next;
1048  }
1049 }
1050 
1051 /**
1052  * Deallocates a list of TCP segments (tcp_seg structures).
1053  *
1054  * @param seg tcp_seg list of TCP segments to free
1055  */
1056 void
1057 tcp_segs_free(struct tcp_seg *seg)
1058 {
1059  while (seg != NULL) {
1060  struct tcp_seg *next = seg->next;
1061  tcp_seg_free(seg);
1062  seg = next;
1063  }
1064 }
1065 
1066 /**
1067  * Frees a TCP segment (tcp_seg structure).
1068  *
1069  * @param seg single tcp_seg to free
1070  */
1071 void
1072 tcp_seg_free(struct tcp_seg *seg)
1073 {
1074  if (seg != NULL) {
1075  if (seg->p != NULL) {
1076  pbuf_free(seg->p);
1077 #if TCP_DEBUG
1078  seg->p = NULL;
1079 #endif /* TCP_DEBUG */
1080  }
1081  memp_free(MEMP_TCP_SEG, seg);
1082  }
1083 }
1084 
1085 /**
1086  * Sets the priority of a connection.
1087  *
1088  * @param pcb the tcp_pcb to manipulate
1089  * @param prio new priority
1090  */
1091 void
1092 tcp_setprio(struct tcp_pcb *pcb, u8_t prio)
1093 {
1094  pcb->prio = prio;
1095 }
1096 
1097 #if TCP_QUEUE_OOSEQ
1098 /**
1099  * Returns a copy of the given TCP segment.
1100  * The pbuf and data are not copied, only the pointers
1101  *
1102  * @param seg the old tcp_seg
1103  * @return a copy of seg
1104  */
1105 struct tcp_seg *
1106 tcp_seg_copy(struct tcp_seg *seg)
1107 {
1108  struct tcp_seg *cseg;
1109 
1110  cseg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG);
1111  if (cseg == NULL) {
1112  return NULL;
1113  }
1114  SMEMCPY((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg));
1115  pbuf_ref(cseg->p);
1116  return cseg;
1117 }
1118 #endif /* TCP_QUEUE_OOSEQ */
1119 
1120 #if LWIP_CALLBACK_API
1121 /**
1122  * Default receive callback that is called if the user didn't register
1123  * a recv callback for the pcb.
1124  */
1125 err_t
1126 tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
1127 {
1128  LWIP_UNUSED_ARG(arg);
1129  if (p != NULL) {
1130  tcp_recved(pcb, p->tot_len);
1131  pbuf_free(p);
1132  } else if (err == ERR_OK) {
1133  return tcp_close(pcb);
1134  }
1135  return ERR_OK;
1136 }
1137 #endif /* LWIP_CALLBACK_API */
1138 
1139 /**
1140  * Kills the oldest active connection that has lower priority than prio.
1141  *
1142  * @param prio minimum priority
1143  */
1144 static void ICACHE_FLASH_ATTR
1145 tcp_kill_prio(u8_t prio)
1146 {
1147  struct tcp_pcb *pcb, *inactive;
1148  u32_t inactivity;
1149  u8_t mprio;
1150 
1151 
1152  mprio = TCP_PRIO_MAX;
1153 
1154  /* We kill the oldest active connection that has lower priority than prio. */
1155  inactivity = 0;
1156  inactive = NULL;
1157  for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1158  if (pcb->prio <= prio &&
1159  pcb->prio <= mprio &&
1160  (u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1161  inactivity = tcp_ticks - pcb->tmr;
1162  inactive = pcb;
1163  mprio = pcb->prio;
1164  }
1165  }
1166  if (inactive != NULL) {
1167  LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F")\n",
1168  (void *)inactive, inactivity));
1169  tcp_abort(inactive);
1170  }
1171 }
1172 
1173 /**
1174  * Kills the oldest connection that is in TIME_WAIT state.
1175  * Called from tcp_alloc() if no more connections are available.
1176  */
1177 static void ICACHE_FLASH_ATTR
1178 tcp_kill_timewait(void)
1179 {
1180  struct tcp_pcb *pcb, *inactive;
1181  u32_t inactivity;
1182 
1183  inactivity = 0;
1184  inactive = NULL;
1185  /* Go through the list of TIME_WAIT pcbs and get the oldest pcb. */
1186  for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1187  if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1188  inactivity = tcp_ticks - pcb->tmr;
1189  inactive = pcb;
1190  }
1191  }
1192  if (inactive != NULL) {
1193  LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F")\n",
1194  (void *)inactive, inactivity));
1195  tcp_abort(inactive);
1196  }
1197 }
1198 
1199 /**
1200  * Allocate a new tcp_pcb structure.
1201  *����һ��TCP���ƿ�ṹ������ʼ������ֶ�
1202  * @param prio priority for the new pcb �¿��ƿ�����ȼ�
1203  * @return a new tcp_pcb that initially is in state CLOSED ָ���¿��ƿ��ָ��
1204  */
1205 struct tcp_pcb *
1206 tcp_alloc(u8_t prio)
1207 {
1208  struct tcp_pcb *pcb;
1209  u32_t iss;
1210 
1211  pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);//�����ڴ�ؿռ�
1212  if (pcb == NULL) {
1213  //os_printf("tcp_pcb memory is fail\n");
1214  /* Try killing oldest connection in TIME-WAIT. */
1215  LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest TIME-WAIT connection\n"));
1216  tcp_kill_timewait();
1217  /* Try to allocate a tcp_pcb again. */
1218  pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1219  if (pcb == NULL) {
1220  /* Try killing active connections with lower priority than the new one. */
1221  LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing connection with prio lower than %d\n", prio));
1222  tcp_kill_prio(prio);
1223  /* Try to allocate a tcp_pcb again. */
1224  pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1225  if (pcb != NULL) {
1226  /* adjust err stats: memp_malloc failed twice before */
1227  MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1228  }
1229  }
1230  if (pcb != NULL) {
1231  /* adjust err stats: timewait PCB was freed above */
1232  MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1233  }
1234  }
1235  if (pcb != NULL) {
1236  os_memset(pcb, 0, sizeof(struct tcp_pcb)); //��0
1237  pcb->prio = prio; //�������ȼ�
1238  pcb->snd_buf = TCP_SND_BUF; //��ʹ�õķ��ͻ������С
1239  pcb->snd_queuelen = 0; //��������ռ�õ�pbuf����
1240  pcb->rcv_wnd = TCP_WND; //���մ���
1241  pcb->rcv_ann_wnd = TCP_WND; //ͨ����մ���
1242  pcb->tos = 0; //��������
1243  pcb->ttl = TCP_TTL; //ttl�ֶ�
1244  /* As initial send MSS, we use TCP_MSS but limit it to 536.
1245  The send MSS is updated when an MSS option is received. */
1246  pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS; //��ʼ������Ķ�
1247  pcb->rto = 1000 / TCP_SLOW_INTERVAL; //��ʼ����ʱʱ��
1248  pcb->sa = 0; //��ʼ����RTT��صIJ���
1249  pcb->sv = 1000 / TCP_SLOW_INTERVAL;
1250  pcb->rtime = -1;
1251  pcb->cwnd = 1; //��ʼ�������
1252  iss = tcp_next_iss(); //��ó�ʼ���к�
1253  pcb->snd_wl2 = iss; //��ʼ�����ʹ��ڸ����ֶ�
1254  pcb->snd_nxt = iss;
1255  pcb->lastack = iss;
1256  pcb->snd_lbb = iss;
1257  pcb->tmr = tcp_ticks; //��¼���ƿ鴴��ϵͳʱ��
1258 
1259  pcb->polltmr = 0; //����������¼���ʱ��
1260 
1261 #if LWIP_CALLBACK_API
1262  pcb->recv = tcp_recv_null; //ע�������ݵ�Ĭ���ϲ㺯��
1263 #endif /* LWIP_CALLBACK_API */
1264 
1265  /* Init KEEPALIVE timer */
1266  pcb->keep_idle = TCP_KEEPIDLE_DEFAULT;
1267 
1268 #if LWIP_TCP_KEEPALIVE
1269  pcb->keep_intvl = TCP_KEEPINTVL_DEFAULT;
1270  pcb->keep_cnt = TCP_KEEPCNT_DEFAULT;
1271 #endif /* LWIP_TCP_KEEPALIVE */
1272 
1273  pcb->keep_cnt_sent = 0; //���ķ��ʹ���
1274  }
1275  return pcb;
1276 }
1277 
1278 /**
1279  * Creates a new TCP protocol control block but doesn't place it on
1280  * any of the TCP PCB lists.
1281  * The pcb is not put on any list until binding using tcp_bind().
1282  *
1283  * @internal: Maybe there should be a idle TCP PCB list where these
1284  * PCBs are put on. Port reservation using tcp_bind() is implemented but
1285  * allocated pcbs that are not bound can't be killed automatically if wanting
1286  * to allocate a pcb with higher prio (@see tcp_kill_prio())
1287  *
1288  * @return a new tcp_pcb that initially is in state CLOSED
1289  */
1290 struct tcp_pcb *
1291 tcp_new(void)
1292 {
1293  return tcp_alloc(TCP_PRIO_NORMAL);
1294 }
1295 
1296 /**
1297  * Used to specify the argument that should be passed callback
1298  * functions.
1299  *����ƿ��callback_arg�ֶ�ע���û���ݣ���tcp_recv�Ⱥ���ص�ʱ��
1300 * ���ֶν���Ϊ����ݸ��������
1301  * @param pcb tcp_pcb to set the callback argument
1302  * @param arg void pointer argument to pass to callback functions
1303  */
1304 void
1305 tcp_arg(struct tcp_pcb *pcb, void *arg)
1306 {
1307  pcb->callback_arg = arg;
1308 }
1309 #if LWIP_CALLBACK_API
1310 
1311 /**
1312  * Used to specify the function that should be called when a TCP
1313  * connection receives data.
1314  *����ƿ��recv�ֶ�ע�ắ���յ����ʱ�ص�
1315  * @param pcb tcp_pcb to set the recv callback
1316  * @param recv callback function to call for this pcb when data is received
1317  */
1318 void
1319 tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv)
1320 {
1321  pcb->recv = recv;
1322 }
1323 
1324 /**
1325  * Used to specify the function that should be called when TCP data
1326  * has been successfully delivered to the remote host.
1327  *����ƿ�send �ֶ�ע�ắ����ݷ��ͳɹ���ص�
1328  * @param pcb tcp_pcb to set the sent callback
1329  * @param sent callback function to call for this pcb when data is successfully sent
1330  */
1331 void
1332 tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent)
1333 {
1334  pcb->sent = sent;
1335 }
1336 
1337 /**
1338  * Used to specify the function that should be called when a fatal error
1339  * has occured on the connection.
1340  *����ƿ�err �ֶ�ע�ắ�����������ص�
1341  * @param pcb tcp_pcb to set the err callback
1342  * @param err callback function to call for this pcb when a fatal error
1343  * has occured on the connection
1344  */
1345 void
1346 tcp_err(struct tcp_pcb *pcb, tcp_err_fn err)
1347 {
1348  pcb->errf = err;
1349 }
1350 
1351 /**
1352  * Used for specifying the function that should be called when a
1353  * LISTENing connection has been connected to another host.
1354  *����ƿ��accept�ֶ�ע�ắ����������ʱ�ص�
1355  * @param pcb tcp_pcb to set the accept callback
1356  * @param accept callback function to call for this pcb when LISTENing
1357  * connection has been connected to another host
1358  */
1359 void
1360 tcp_accept(struct tcp_pcb *pcb, tcp_accept_fn accept)
1361 {
1362  pcb->accept = accept;
1363 }
1364 #endif /* LWIP_CALLBACK_API */
1365 
1366 
1367 /**
1368  * Used to specify the function that should be called periodically
1369  * from TCP. The interval is specified in terms of the TCP coarse
1370  * timer interval, which is called twice a second.
1371  *����ƿ��POLL�ֶ�ע�ắ��ú��������Ա�����
1372  */
1373 void
1374 tcp_poll(struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval)
1375 {
1376 #if LWIP_CALLBACK_API
1377  pcb->poll = poll;
1378 #else /* LWIP_CALLBACK_API */
1379  LWIP_UNUSED_ARG(poll);
1380 #endif /* LWIP_CALLBACK_API */
1381  pcb->pollinterval = interval;
1382 }
1383 
1384 /**
1385  * Purges a TCP PCB. Removes any buffered data and frees the buffer memory
1386  * (pcb->ooseq, pcb->unsent and pcb->unacked are freed).
1387  *
1388  * @param pcb tcp_pcb to purge. The pcb itself is not deallocated!
1389  */
1390 void
1391 tcp_pcb_purge(struct tcp_pcb *pcb)
1392 {
1393  if (pcb->state != CLOSED &&
1394  pcb->state != TIME_WAIT &&
1395  pcb->state != LISTEN) {
1396 
1397  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge\n"));
1398 
1399 #if TCP_LISTEN_BACKLOG
1400  if (pcb->state == SYN_RCVD) {
1401  /* Need to find the corresponding listen_pcb and decrease its accepts_pending */
1402  struct tcp_pcb_listen *lpcb;
1403  LWIP_ASSERT("tcp_pcb_purge: pcb->state == SYN_RCVD but tcp_listen_pcbs is NULL",
1404  tcp_listen_pcbs.listen_pcbs != NULL);
1405  for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
1406  if ((lpcb->local_port == pcb->local_port) &&
1407  (ip_addr_isany(&lpcb->local_ip) ||
1408  ip_addr_cmp(&pcb->local_ip, &lpcb->local_ip))) {
1409  /* port and address of the listen pcb match the timed-out pcb */
1410  LWIP_ASSERT("tcp_pcb_purge: listen pcb does not have accepts pending",
1411  lpcb->accepts_pending > 0);
1412  lpcb->accepts_pending--;
1413  break;
1414  }
1415  }
1416  }
1417 #endif /* TCP_LISTEN_BACKLOG */
1418 
1419 
1420  if (pcb->refused_data != NULL) {
1421  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->refused_data\n"));
1422  pbuf_free(pcb->refused_data);
1423  pcb->refused_data = NULL;
1424  }
1425  if (pcb->unsent != NULL) {
1426  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: not all data sent\n"));
1427  }
1428  if (pcb->unacked != NULL) {
1429  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->unacked\n"));
1430  }
1431 #if TCP_QUEUE_OOSEQ
1432  if (pcb->ooseq != NULL) {
1433  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->ooseq\n"));
1434  }
1435  tcp_segs_free(pcb->ooseq);
1436  pcb->ooseq = NULL;
1437 #endif /* TCP_QUEUE_OOSEQ */
1438 
1439  /* Stop the retransmission timer as it will expect data on unacked
1440  queue if it fires */
1441  pcb->rtime = -1;
1442 
1443  tcp_segs_free(pcb->unsent);
1444  tcp_segs_free(pcb->unacked);
1445  pcb->unacked = pcb->unsent = NULL;
1446 #if TCP_OVERSIZE
1447  pcb->unsent_oversize = 0;
1448 #endif /* TCP_OVERSIZE */
1449  }
1450 }
1451 
1452 /**
1453  * Purges the PCB and removes it from a PCB list. Any delayed ACKs are sent first.
1454  *
1455  * @param pcblist PCB list to purge.
1456  * @param pcb tcp_pcb to purge. The pcb itself is NOT deallocated!
1457  */
1458 void
1459 tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb)
1460 {
1461  TCP_RMV(pcblist, pcb);
1462 
1463  tcp_pcb_purge(pcb);
1464 
1465  /* if there is an outstanding delayed ACKs, send it */
1466  if (pcb->state != TIME_WAIT &&
1467  pcb->state != LISTEN &&
1468  pcb->flags & TF_ACK_DELAY) {
1469  pcb->flags |= TF_ACK_NOW;
1470  tcp_output(pcb);
1471  }
1472 
1473  if (pcb->state != LISTEN) {
1474  LWIP_ASSERT("unsent segments leaking", pcb->unsent == NULL);
1475  LWIP_ASSERT("unacked segments leaking", pcb->unacked == NULL);
1476 #if TCP_QUEUE_OOSEQ
1477  LWIP_ASSERT("ooseq segments leaking", pcb->ooseq == NULL);
1478 #endif /* TCP_QUEUE_OOSEQ */
1479  }
1480 
1481  pcb->state = CLOSED;
1482 
1483  LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane());
1484 }
1485 
1486 /**
1487  * Calculates a new initial sequence number for new connections.
1488  *
1489  * @return u32_t pseudo random sequence number
1490  */
1491 u32_t
1492 tcp_next_iss(void)
1493 {
1494  static u32_t iss = 6510;
1495 
1496  again:
1497  iss += tcp_ticks; /* XXX */
1498  if (iss == 0)
1499  goto again;
1500 
1501  return iss;
1502 }
1503 
1504 #if TCP_CALCULATE_EFF_SEND_MSS
1505 /**
1506  * Calcluates the effective send mss that can be used for a specific IP address
1507  * by using ip_route to determin the netif used to send to the address and
1508  * calculating the minimum of TCP_MSS and that netif's mtu (if set).
1509  */
1510 u16_t
1511 tcp_eff_send_mss(u16_t sendmss, ip_addr_t *addr)
1512 {
1513  u16_t mss_s;
1514  struct netif *outif;
1515 
1516  outif = ip_route(addr);
1517  if ((outif != NULL) && (outif->mtu != 0)) {
1518  mss_s = outif->mtu - IP_HLEN - TCP_HLEN;
1519  /* RFC 1122, chap 4.2.2.6:
1520  * Eff.snd.MSS = min(SendMSS+20, MMS_S) - TCPhdrsize - IPoptionsize
1521  * We correct for TCP options in tcp_write(), and don't support IP options.
1522  */
1523  sendmss = LWIP_MIN(sendmss, mss_s);
1524  }
1525  return sendmss;
1526 }
1527 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
1528 
1529 #if 0 && TCP_DEBUG
1530 const char*
1531 tcp_debug_state_str(enum tcp_state s)
1532 {
1533  system_get_string_from_flash(tcp_state_str_rodata[s], tcp_state_str, 12);
1534 
1535  return tcp_state_str;
1536 }
1537 #endif
1538 
1539 #undef TCP_DEBUG
1540 //#define TCP_DEBUG LWIP_DBG_ON
1541 #undef LWIP_DBG_TYPES_ON
1542 #define LWIP_DBG_TYPES_ON TCP_DEBUG
1543 #undef LWIP_DEBUGF
1544 #define LWIP_DEBUGF(debug, message) DEBUG_printf message
1545 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
1546 /**
1547  * Print a tcp header for debugging purposes.
1548  *
1549  * @param tcphdr pointer to a struct tcp_hdr
1550  */
1551 void
1552 tcp_debug_print(struct tcp_hdr *tcphdr)
1553 {
1554  LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n"));
1555  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1556  LWIP_DEBUGF(TCP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
1557  ntohs(tcphdr->src), ntohs(tcphdr->dest)));
1558  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1559  LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (seq no)\n",
1560  ntohl(tcphdr->seqno)));
1561  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1562  LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (ack no)\n",
1563  ntohl(tcphdr->ackno)));
1564  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1565  LWIP_DEBUGF(TCP_DEBUG, ("| %2"U16_F" | |%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"| %5"U16_F" | (hdrlen, flags (",
1566  TCPH_HDRLEN(tcphdr),
1567  TCPH_FLAGS(tcphdr) >> 5 & 1,
1568  TCPH_FLAGS(tcphdr) >> 4 & 1,
1569  TCPH_FLAGS(tcphdr) >> 3 & 1,
1570  TCPH_FLAGS(tcphdr) >> 2 & 1,
1571  TCPH_FLAGS(tcphdr) >> 1 & 1,
1572  TCPH_FLAGS(tcphdr) & 1,
1573  ntohs(tcphdr->wnd)));
1574  tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
1575  LWIP_DEBUGF(TCP_DEBUG, ("), win)\n"));
1576  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1577  LWIP_DEBUGF(TCP_DEBUG, ("| 0x%04"X16_F" | %5"U16_F" | (chksum, urgp)\n",
1578  ntohs(tcphdr->chksum), ntohs(tcphdr->urgp)));
1579  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1580 }
1581 
1582 /**
1583  * Print a tcp state for debugging purposes.
1584  *
1585  * @param s enum tcp_state to print
1586  */
1587 void
1588 tcp_debug_print_state(enum tcp_state s)
1589 {
1590  LWIP_DEBUGF(TCP_DEBUG, ("State: %s\n", tcp_state_str[s]));
1591 }
1592 
1593 /**
1594  * Print tcp flags for debugging purposes.
1595  *
1596  * @param flags tcp flags, all active flags are printed
1597  */
1598 void
1599 tcp_debug_print_flags(u8_t flags)
1600 {
1601  if (flags & TCP_FIN) {
1602  LWIP_DEBUGF(TCP_DEBUG, ("FIN "));
1603  }
1604  if (flags & TCP_SYN) {
1605  LWIP_DEBUGF(TCP_DEBUG, ("SYN "));
1606  }
1607  if (flags & TCP_RST) {
1608  LWIP_DEBUGF(TCP_DEBUG, ("RST "));
1609  }
1610  if (flags & TCP_PSH) {
1611  LWIP_DEBUGF(TCP_DEBUG, ("PSH "));
1612  }
1613  if (flags & TCP_ACK) {
1614  LWIP_DEBUGF(TCP_DEBUG, ("ACK "));
1615  }
1616  if (flags & TCP_URG) {
1617  LWIP_DEBUGF(TCP_DEBUG, ("URG "));
1618  }
1619  if (flags & TCP_ECE) {
1620  LWIP_DEBUGF(TCP_DEBUG, ("ECE "));
1621  }
1622  if (flags & TCP_CWR) {
1623  LWIP_DEBUGF(TCP_DEBUG, ("CWR "));
1624  }
1625  LWIP_DEBUGF(TCP_DEBUG, ("\n"));
1626 }
1627 
1628 /**
1629  * Print all tcp_pcbs in every list for debugging purposes.
1630  */
1631 void
1632 tcp_debug_print_pcbs(void)
1633 {
1634  struct tcp_pcb *pcb;
1635  LWIP_DEBUGF(TCP_DEBUG, ("Active PCB states:\n"));
1636  for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1637  LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
1638  pcb->local_port, pcb->remote_port,
1639  pcb->snd_nxt, pcb->rcv_nxt));
1640  tcp_debug_print_state(pcb->state);
1641  }
1642  LWIP_DEBUGF(TCP_DEBUG, ("Listen PCB states:\n"));
1643  for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next) {
1644  LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
1645  pcb->local_port, pcb->remote_port,
1646  pcb->snd_nxt, pcb->rcv_nxt));
1647  tcp_debug_print_state(pcb->state);
1648  }
1649  LWIP_DEBUGF(TCP_DEBUG, ("TIME-WAIT PCB states:\n"));
1650  for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1651  LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
1652  pcb->local_port, pcb->remote_port,
1653  pcb->snd_nxt, pcb->rcv_nxt));
1654  tcp_debug_print_state(pcb->state);
1655  }
1656 }
1657 
1658 /**
1659  * Check state consistency of the tcp_pcb lists.
1660  */
1661 s16_t
1662 tcp_pcbs_sane(void)
1663 {
1664  struct tcp_pcb *pcb;
1665  for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1666  LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED);
1667  LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN);
1668  LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
1669  }
1670  for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1671  LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
1672  }
1673  return 1;
1674 }
1675 #endif /* TCP_DEBUG */
1676 
1677 #endif /* LWIP_TCP */
#define ERR_ISCONN
Definition: err.h:73
#define TCP_MAXRTX
Definition: opt.h:910
#define TCP_INPUT_DEBUG
Definition: opt.h:1931
u16_t tot_len
Definition: pbuf.h:90
#define ERR_CONN
Definition: err.h:66
#define ip4_addr3_16(ipaddr)
Definition: ip_addr.h:228
void memp_free(memp_t type, void *mem) ICACHE_FLASH_ATTR
Definition: memp.c:438
#define SMEMCPY(dst, src, len)
Definition: opt.h:92
#define ERR_USE
Definition: err.h:70
#define SOF_REUSEADDR
Definition: ip.h:99
#define U16_F
Definition: cc.h:61
signed short s16_t
Definition: cc.h:55
#define ERR_VAL
Definition: err.h:58
#define NULL
Definition: def.h:47
#define TCP_CWND_DEBUG
Definition: opt.h:1953
#define snmp_inc_tcpestabresets()
Definition: snmp.h:314
const ip_addr_t ip_addr_any ICACHE_RODATA_ATTR
Definition: ip_addr.c:44
#define ICACHE_FLASH_ATTR
Definition: c_types.h:99
#define TCP_RTO_DEBUG
Definition: opt.h:1946
#define SOF_ACCEPTCONN
Definition: ip.h:98
void * memp_malloc(memp_t type) ICACHE_FLASH_ATTR
Definition: memp.c:393
#define TCP_SYNMAXRTX
Definition: opt.h:917
#define SOF_KEEPALIVE
Definition: ip.h:100
#define ERR_ABRT
Definition: err.h:63
#define LWIP_ERROR(message, expression, handler)
Definition: debug.h:73
#define S32_F
Definition: cc.h:64
#define LWIP_MIN(x, y)
Definition: def.h:44
#define TCP_WND
Definition: opt.h:903
struct netif * ip_route(ip_addr_t *dest) ICACHE_FLASH_ATTR
Definition: ip.c:125
#define TCP_TTL
Definition: opt.h:895
#define U32_F
Definition: cc.h:65
#define ntohl(x)
Definition: def.h:84
#define snmp_inc_tcpactiveopens()
Definition: snmp.h:311
unsigned long u32_t
Definition: cc.h:56
struct tcp_pcb **const tcp_pcb_lists[]
#define ERR_RTE
Definition: err.h:56
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:94
#define os_memset
Definition: osapi.h:38
#define ip_addr_cmp(addr1, addr2)
Definition: ip_addr.h:198
#define ip_addr_copy(dest, src)
Definition: ip_addr.h:162
#define ERR_OK
Definition: err.h:52
unsigned long os_random(void)
Definition: pbuf.h:76
u8_t flags
Definition: netif.h:193
s8_t err_t
Definition: err.h:47
typedefPACK_STRUCT_END struct ip_addr ip_addr_t
Definition: ip_addr.h:64
Definition: netif.h:139
#define ip4_addr4_16(ipaddr)
Definition: ip_addr.h:229
ip_addr_t ip_addr
Definition: netif.h:144
#define TCP_DEBUG
Definition: opt.h:1924
#define TCP_SND_BUF
Definition: opt.h:956
#define ip_addr_isany(addr1)
Definition: ip_addr.h:200
u16_t mtu
Definition: netif.h:187
#define S16_F
Definition: cc.h:60
u8_t pbuf_free(struct pbuf *p) ICACHE_FLASH_ATTR
Definition: pbuf.c:685
void pbuf_ref(struct pbuf *p) ICACHE_FLASH_ATTR
Definition: pbuf.c:801
#define IP_HLEN
Definition: ip.h:50
#define ip4_addr1_16(ipaddr)
Definition: ip_addr.h:226
unsigned char u8_t
Definition: cc.h:52
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:65
#define TCP_MSS
Definition: opt.h:936
#define TCP_WND_UPDATE_THRESHOLD
Definition: opt.h:1031
#define MEMP_STATS_DEC(x, i)
Definition: stats.h:254
#define TCP_RST_DEBUG
Definition: opt.h:1974
#define ip4_addr2_16(ipaddr)
Definition: ip_addr.h:227
#define TCP_MSL
Definition: lwipopts.h:1074
#define snmp_inc_tcpattemptfails()
Definition: snmp.h:313
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:73
#define ntohs(x)
Definition: def.h:82
unsigned short u16_t
Definition: cc.h:54
#define X16_F
Definition: cc.h:62