tcp.c 54 KB

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