tcp_in.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  1. /**
  2. * @file
  3. * Transmission Control Protocol, incoming traffic
  4. *
  5. * The input processing functions of the TCP layer.
  6. *
  7. * These functions are generally called in the order (ip_input() ->)
  8. * tcp_input() -> * tcp_process() -> tcp_receive() (-> application).
  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. #include "lwip/opt.h"
  43. #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
  44. #include "lwip/tcp_impl.h"
  45. #include "lwip/def.h"
  46. #include "lwip/ip_addr.h"
  47. #include "lwip/netif.h"
  48. #include "lwip/mem.h"
  49. #include "lwip/memp.h"
  50. #include "lwip/inet_chksum.h"
  51. #include "lwip/stats.h"
  52. #include "lwip/snmp.h"
  53. #include "arch/perf.h"
  54. #ifdef MEMLEAK_DEBUG
  55. static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
  56. #endif
  57. /* These variables are global to all functions involved in the input
  58. processing of TCP segments. They are set by the tcp_input()
  59. function. */
  60. static struct tcp_seg inseg; //tcp_seg�ṹ����������ı��Ķ�
  61. static struct tcp_hdr *tcphdr; //���Ķ���TCP�ײ�
  62. static struct ip_hdr *iphdr; //IP��ݰ��ײ�
  63. static u32_t seqno, ackno; //TCP�ײ�������ֶ���ȷ�Ϻ��ֶ�
  64. static u8_t flags; //�ײ���־�ֶ�
  65. static u16_t tcplen; //TCP���ij���
  66. static u8_t recv_flags; //��ǰ���Ĵ�����
  67. static struct pbuf *recv_data; //���Ķ����pbuf
  68. struct tcp_pcb *tcp_input_pcb; //��ǰ���Ŀ��ƿ�
  69. /* Forward declarations. */
  70. static err_t tcp_process(struct tcp_pcb *pcb)ICACHE_FLASH_ATTR;
  71. static void tcp_receive(struct tcp_pcb *pcb)ICACHE_FLASH_ATTR;
  72. static void tcp_parseopt(struct tcp_pcb *pcb)ICACHE_FLASH_ATTR;
  73. static err_t tcp_listen_input(struct tcp_pcb_listen *pcb)ICACHE_FLASH_ATTR;
  74. static err_t tcp_timewait_input(struct tcp_pcb *pcb)ICACHE_FLASH_ATTR;
  75. /**
  76. * The initial input processing of TCP. It verifies the TCP header, demultiplexes
  77. * the segment between the PCBs and passes it on to tcp_process(), which implements
  78. * the TCP finite state machine. This function is called by the IP layer (in
  79. * ip_input()).
  80. *
  81. * @param p received TCP segment to process (p->payload pointing to the IP header)
  82. * @param inp network interface on which this segment was received
  83. */
  84. /**
  85. * TCP��ʼ�����봦�?��֤��TCPͷ���������IP�����
  86. * @����p:������յ�TCP��(ָ��IPͷ�ĸ���)
  87. * @����inp:���նε�����ӿ�
  88. */
  89. void
  90. tcp_input(struct pbuf *p, struct netif *inp)
  91. {
  92. struct tcp_pcb *pcb, *prev;
  93. struct tcp_pcb_listen *lpcb;
  94. #if SO_REUSE
  95. struct tcp_pcb *lpcb_prev = NULL;
  96. struct tcp_pcb_listen *lpcb_any = NULL;
  97. #endif /* SO_REUSE */
  98. u8_t hdrlen;
  99. err_t err;
  100. PERF_START;
  101. TCP_STATS_INC(tcp.recv); //״̬��1
  102. snmp_inc_tcpinsegs(); //tcp����μ�1
  103. iphdr = (struct ip_hdr *)p->payload;// pointer to the actual data in the buffer
  104. /*
  105. *��ͷ����(IHL)��4λ��IPЭ���ͷ�ij��ȣ�ָ��IPv4Э���ͷ���ȵ��ֽ������ٸ�32λ��
  106. *����IPv4�İ�ͷ���ܰ�ɱ������Ŀ�ѡ ���������ֶο�������ȷ��IPv4��ݱ�����ݲ��ֵ�ƫ������
  107. *IPv4��ͷ����С������20���ֽڣ����IHL����ֶε���Сֵ��ʮ���Ʊ�ʾ����5 (5x4 = 20�ֽ�)��
  108. *����˵�����ʾ�İ�ͷ�����ֽ�����4�ֽڵı���
  109. */
  110. tcphdr = (struct tcp_hdr *)((u8_t *)p->payload + IPH_HL(iphdr) * 4);
  111. #if TCP_INPUT_DEBUG
  112. tcp_debug_print(tcphdr);
  113. #endif
  114. /* remove header from payload */
  115. if (pbuf_header(p, -((s16_t)(IPH_HL(iphdr) * 4))) || (p->tot_len < sizeof(struct tcp_hdr))) {
  116. /* drop short packets */
  117. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: short packet (%"U16_F" bytes) discarded\n", p->tot_len));
  118. TCP_STATS_INC(tcp.lenerr);//���󳤶ȼ���
  119. TCP_STATS_INC(tcp.drop);//��ֹ����
  120. snmp_inc_tcpinerrs();
  121. pbuf_free(p);//�ͷ�buffer
  122. return;
  123. }
  124. /* Don't even process incoming broadcasts/multicasts. */
  125. if (ip_addr_isbroadcast(&current_iphdr_dest, inp) ||
  126. ip_addr_ismulticast(&current_iphdr_dest)) {
  127. TCP_STATS_INC(tcp.proterr);//��������
  128. TCP_STATS_INC(tcp.drop);
  129. snmp_inc_tcpinerrs();
  130. pbuf_free(p);
  131. return;
  132. }
  133. #if CHECKSUM_CHECK_TCP
  134. /* Verify TCP checksum. */
  135. if (inet_chksum_pseudo(p, ip_current_src_addr(), ip_current_dest_addr(),
  136. IP_PROTO_TCP, p->tot_len) != 0) {
  137. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: packet discarded due to failing checksum 0x%04"X16_F"\n",
  138. inet_chksum_pseudo(p, ip_current_src_addr(), ip_current_dest_addr(),
  139. IP_PROTO_TCP, p->tot_len)));
  140. #if TCP_DEBUG
  141. tcp_debug_print(tcphdr);
  142. #endif /* TCP_DEBUG */
  143. TCP_STATS_INC(tcp.chkerr);//��������
  144. TCP_STATS_INC(tcp.drop);
  145. snmp_inc_tcpinerrs();
  146. pbuf_free(p);
  147. return;
  148. }
  149. #endif
  150. /* Move the payload pointer in the pbuf so that it points to the
  151. TCP data instead of the TCP header. */
  152. hdrlen = TCPH_HDRLEN(tcphdr);//����ͷ�ij���
  153. if(pbuf_header(p, -(hdrlen * 4))){//���TCPͷ������0Ϊ�ɹ�������
  154. /* drop short packets */
  155. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: short packet\n"));
  156. TCP_STATS_INC(tcp.lenerr);//tcp���ȴ������
  157. TCP_STATS_INC(tcp.drop);
  158. snmp_inc_tcpinerrs();
  159. pbuf_free(p);
  160. return;
  161. }
  162. /* Convert fields in TCP header to host byte order. */
  163. tcphdr->src = ntohs(tcphdr->src); //ת��Դ��ַ
  164. tcphdr->dest = ntohs(tcphdr->dest); //ת��Ŀ�ĵ�ַ
  165. seqno = tcphdr->seqno = ntohl(tcphdr->seqno); //ת�����к�
  166. ackno = tcphdr->ackno = ntohl(tcphdr->ackno); //ת��Ӧ���
  167. tcphdr->wnd = ntohs(tcphdr->wnd); //ת��tcp����
  168. flags = TCPH_FLAGS(tcphdr);//�õ�tcp header�ı�־
  169. /*
  170. *��־��3λ�����ֶΣ���
  171. * ����λ��1λ
  172. * ���ֶ�λ��1λ��ȡֵ��0��������ݱ��ֶΣ���1����ݱ����ֶܷΣ�
  173. * ����λ��1λ��ȡֵ��0����ݰ����û�а�1����ݰ�����и��İ�
  174. */
  175. tcplen = p->tot_len + ((flags & (TCP_FIN | TCP_SYN)) ? 1 : 0);//TCP_FIN �� TCP_SYN ���1�������0
  176. /* Demultiplex an incoming segment. First, we check if it is destined
  177. for an active connection. ���ȣ�����Ƿ�һ��Ҫ����һ������*/
  178. ////////////////////////////////////////////////////////////////////////////////////////
  179. prev = NULL;
  180. for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {//������б�
  181. LWIP_ASSERT("tcp_input: active pcb->state != CLOSED", pcb->state != CLOSED);
  182. LWIP_ASSERT("tcp_input: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
  183. LWIP_ASSERT("tcp_input: active pcb->state != LISTEN", pcb->state != LISTEN);
  184. if (pcb->remote_port == tcphdr->src &&
  185. pcb->local_port == tcphdr->dest &&
  186. ip_addr_cmp(&(pcb->remote_ip), &current_iphdr_src) &&
  187. ip_addr_cmp(&(pcb->local_ip), &current_iphdr_dest)) {//�����صĵ�ַ
  188. /* Move this PCB to the front of the list so that subsequent
  189. lookups will be faster (we exploit locality in TCP segment
  190. arrivals). */
  191. LWIP_ASSERT("tcp_input: pcb->next != pcb (before cache)", pcb->next != pcb);
  192. if (prev != NULL) {//���ǰһ���ڵ㲻Ϊ��
  193. prev->next = pcb->next;
  194. pcb->next = tcp_active_pcbs;
  195. tcp_active_pcbs = pcb;//pcb������ǰ��
  196. }
  197. LWIP_ASSERT("tcp_input: pcb->next != pcb (after cache)", pcb->next != pcb);
  198. break;
  199. }
  200. prev = pcb;//prevָ��pcb
  201. }
  202. if (pcb == NULL) {
  203. /* If it did not go to an active connection, we check the connections
  204. in the TIME-WAIT state. */
  205. for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {//����ȴ�״̬�µ�pcb
  206. LWIP_ASSERT("tcp_input: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
  207. if (pcb->remote_port == tcphdr->src &&
  208. pcb->local_port == tcphdr->dest &&
  209. ip_addr_cmp(&(pcb->remote_ip), &current_iphdr_src) &&
  210. ip_addr_cmp(&(pcb->local_ip), &current_iphdr_dest)) {
  211. /* We don't really care enough to move this PCB to the front
  212. of the list since we are not very likely to receive that
  213. many segments for connections in TIME-WAIT. */
  214. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: packed for TIME_WAITing connection.\n"));
  215. tcp_timewait_input(pcb);//����tcp timewait ��
  216. pbuf_free(p);
  217. return;
  218. }
  219. }
  220. /* Finally, if we still did not get a match, we check all PCBs that
  221. are LISTENing for incoming connections. */
  222. prev = NULL;
  223. for(lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {//�������״̬�����е�pcb
  224. if (lpcb->local_port == tcphdr->dest) {
  225. #if SO_REUSE
  226. if (ip_addr_cmp(&(lpcb->local_ip), &current_iphdr_dest)) {
  227. /* found an exact match */
  228. break;
  229. } else if(ip_addr_isany(&(lpcb->local_ip))) {
  230. /* found an ANY-match */
  231. lpcb_any = lpcb;
  232. lpcb_prev = prev;
  233. }
  234. #else /* SO_REUSE */
  235. if (ip_addr_cmp(&(lpcb->local_ip), &current_iphdr_dest) ||
  236. ip_addr_isany(&(lpcb->local_ip))) {
  237. /* found a match */
  238. break;
  239. }
  240. #endif /* SO_REUSE */
  241. }
  242. prev = (struct tcp_pcb *)lpcb;
  243. }
  244. #if SO_REUSE
  245. /* first try specific local IP */
  246. if (lpcb == NULL) {
  247. /* only pass to ANY if no specific local IP has been found */
  248. lpcb = lpcb_any;
  249. prev = lpcb_prev;
  250. }
  251. #endif /* SO_REUSE */
  252. if (lpcb != NULL) {
  253. /* Move this PCB to the front of the list so that subsequent
  254. lookups will be faster (we exploit locality in TCP segment
  255. arrivals). */
  256. if (prev != NULL) {
  257. ((struct tcp_pcb_listen *)prev)->next = lpcb->next;
  258. /* our successor is the remainder of the listening list */
  259. lpcb->next = tcp_listen_pcbs.listen_pcbs;
  260. /* put this listening pcb at the head of the listening list */
  261. tcp_listen_pcbs.listen_pcbs = lpcb;
  262. }
  263. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: packed for LISTENing connection.\n"));
  264. tcp_listen_input(lpcb);//����tcp������ݰ�
  265. pbuf_free(p);
  266. return;
  267. }
  268. }
  269. #if TCP_INPUT_DEBUG
  270. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("+-+-+-+-+-+-+-+-+-+-+-+-+-+- tcp_input: flags "));
  271. tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
  272. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n"));
  273. #endif /* TCP_INPUT_DEBUG */
  274. if (pcb != NULL) {
  275. /* The incoming segment belongs to a connection. */
  276. #if TCP_INPUT_DEBUG
  277. #if TCP_DEBUG
  278. tcp_debug_print_state(pcb->state);
  279. #endif /* TCP_DEBUG */
  280. #endif /* TCP_INPUT_DEBUG */
  281. /* Set up a tcp_seg structure. */
  282. inseg.next = NULL;
  283. inseg.len = p->tot_len;
  284. inseg.p = p;
  285. inseg.tcphdr = tcphdr;
  286. recv_data = NULL;
  287. recv_flags = 0;
  288. /* If there is data which was previously "refused" by upper layer */
  289. if (pcb->refused_data != NULL) {
  290. /* Notify again application with data previously received. */
  291. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: notify kept packet\n"));
  292. TCP_EVENT_RECV(pcb, pcb->refused_data, ERR_OK, err);//pcb�������
  293. if (err == ERR_OK) {
  294. pcb->refused_data = NULL;
  295. } else if ((err == ERR_ABRT) || (tcplen > 0)) {
  296. /* if err == ERR_ABRT, 'pcb' is already deallocated */
  297. /* Drop incoming packets because pcb is "full" (only if the incoming
  298. segment contains data). */
  299. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: drop incoming packets, because pcb is \"full\"\n"));
  300. TCP_STATS_INC(tcp.drop);//tcp�������
  301. snmp_inc_tcpinerrs();
  302. pbuf_free(p);
  303. return;
  304. }
  305. }
  306. tcp_input_pcb = pcb;//��¼��ǰ���Ĵ���Ŀ��ƿ�
  307. err = tcp_process(pcb);//���?��
  308. /* A return value of ERR_ABRT means that tcp_abort() was called
  309. and that the pcb has been freed. If so, we don't do anything. */
  310. if (err != ERR_ABRT) {
  311. if (recv_flags & TF_RESET) {
  312. /* TF_RESET means that the connection was reset by the other
  313. end. We then call the error callback to inform the
  314. application that the connection is dead before we
  315. deallocate the PCB. */
  316. TCP_EVENT_ERR(pcb->errf, pcb->callback_arg, ERR_RST);
  317. tcp_pcb_remove(&tcp_active_pcbs, pcb);//ɾ���pcb�б��е�pcb
  318. memp_free(MEMP_TCP_PCB, pcb);
  319. } else if (recv_flags & TF_CLOSED) {
  320. /* The connection has been closed and we will deallocate the
  321. PCB. */
  322. if (!(pcb->flags & TF_RXCLOSED)) {
  323. /* Connection closed although the application has only shut down the
  324. tx side: call the PCB's err callback and indicate the closure to
  325. ensure the application doesn't continue using the PCB. */
  326. TCP_EVENT_ERR(pcb->errf, pcb->callback_arg, ERR_CLSD);
  327. }
  328. tcp_pcb_remove(&tcp_active_pcbs, pcb);
  329. memp_free(MEMP_TCP_PCB, pcb);
  330. } else {
  331. err = ERR_OK;
  332. /* If the application has registered a "sent" function to be
  333. called when new send buffer space is available, we call it
  334. now. */
  335. if (pcb->acked > 0) {
  336. TCP_EVENT_SENT(pcb, pcb->acked, err);//����ݱ�ȷ�ϣ��ص��û���send����
  337. if (err == ERR_ABRT) {
  338. goto aborted;
  339. }
  340. }
  341. if (recv_data != NULL) {//����ݽ��յ�
  342. LWIP_ASSERT("pcb->refused_data == NULL", pcb->refused_data == NULL);
  343. if (pcb->flags & TF_RXCLOSED) {
  344. /* received data although already closed -> abort (send RST) to
  345. notify the remote host that not all data has been processed */
  346. pbuf_free(recv_data);
  347. tcp_abort(pcb);
  348. goto aborted;
  349. }
  350. //PSH��־ PSH ����λ��
  351. //��PSH=1ʱ��Ҫ���ͷ����Ϸ��͸÷ֶΣ�
  352. //����շ�����Ľ����Ľ���Ӧ�ò㣬�������д��?
  353. if (flags & TCP_PSH) {
  354. recv_data->flags |= PBUF_FLAG_PUSH;//���bufferӦ������������
  355. }
  356. /* Notify application that data has been received. */
  357. TCP_EVENT_RECV(pcb, recv_data, ERR_OK, err);
  358. if (err == ERR_ABRT) {
  359. goto aborted;
  360. }
  361. /* If the upper layer can't receive this data, store it */
  362. if (err != ERR_OK) {
  363. pcb->refused_data = recv_data;
  364. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: keep incoming packet, because pcb is \"full\"\n"));
  365. }
  366. }
  367. /* If a FIN segment was received, we call the callback
  368. function with a NULL buffer to indicate EOF. */
  369. if (recv_flags & TF_GOT_FIN) {
  370. /* correct rcv_wnd as the application won't call tcp_recved()
  371. for the FIN's seqno */
  372. if (pcb->rcv_wnd != TCP_WND) {
  373. pcb->rcv_wnd++;
  374. }
  375. TCP_EVENT_CLOSED(pcb, err);
  376. if (err == ERR_ABRT) {
  377. goto aborted;
  378. }
  379. }
  380. tcp_input_pcb = NULL;//���ȫ�ֱ���
  381. /* Try to send something out. */
  382. tcp_output(pcb);//�����������
  383. #if TCP_INPUT_DEBUG
  384. #if TCP_DEBUG
  385. tcp_debug_print_state(pcb->state);
  386. #endif /* TCP_DEBUG */
  387. #endif /* TCP_INPUT_DEBUG */
  388. }
  389. }
  390. /* Jump target if pcb has been aborted in a callback (by calling tcp_abort()).
  391. Below this line, 'pcb' may not be dereferenced! */
  392. aborted:
  393. tcp_input_pcb = NULL;
  394. recv_data = NULL;
  395. /* give up our reference to inseg.p */
  396. if (inseg.p != NULL)
  397. {
  398. pbuf_free(inseg.p);//�ͷ�buffer
  399. inseg.p = NULL;
  400. }
  401. /*add processing queue segments that arrive out of order by LiuHan*/
  402. #if TCP_QUEUE_OOSEQ
  403. extern char RxNodeNum(void);
  404. if (RxNodeNum() < 2){
  405. extern void pbuf_free_ooseq_new(void* arg);
  406. // os_printf("reclaim some memory from queued\n");
  407. pbuf_free_ooseq_new(NULL);
  408. }
  409. #endif
  410. } else {
  411. /* If no matching PCB was found, send a TCP RST (reset) to the
  412. sender. */
  413. LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_input: no PCB match found, resetting.\n"));
  414. if (!(TCPH_FLAGS(tcphdr) & TCP_RST)) {
  415. TCP_STATS_INC(tcp.proterr);//��������
  416. TCP_STATS_INC(tcp.drop);//tcp�������
  417. tcp_rst(ackno, seqno + tcplen,
  418. ip_current_dest_addr(), ip_current_src_addr(),
  419. tcphdr->dest, tcphdr->src);//����TCP��λ
  420. }
  421. pbuf_free(p);
  422. }
  423. LWIP_ASSERT("tcp_input: tcp_pcbs_sane()", tcp_pcbs_sane());
  424. PERF_STOP("tcp_input");
  425. }
  426. /**
  427. * Called by tcp_input() when a segment arrives for a listening
  428. * connection (from tcp_input()).
  429. *
  430. * @param pcb the tcp_pcb_listen for which a segment arrived
  431. * @return ERR_OK if the segment was processed
  432. * another err_t on error
  433. *
  434. * @note the return value is not (yet?) used in tcp_input()
  435. * @note the segment which arrived is saved in global variables, therefore only the pcb
  436. * involved is passed as a parameter to this function
  437. */
  438. /*
  439. *����LISTEN״̬�Ŀ��ƿ���øú���
  440. *ͨ���Ƿ�������������һ���˿ڲ�����ͻ���SYN��������
  441. *
  442. */
  443. static err_t
  444. tcp_listen_input(struct tcp_pcb_listen *pcb)
  445. {
  446. struct tcp_pcb *npcb;
  447. struct tcp_pcb *pactive_pcb;
  448. u8_t active_pcb_num = 0;
  449. err_t rc;
  450. /* In the LISTEN state, we check for incoming SYN segments,
  451. creates a new PCB, and responds with a SYN|ACK. */
  452. if (flags & TCP_ACK) {
  453. /* For incoming segments with the ACK flag set, respond with a
  454. RST. */
  455. LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_listen_input: ACK in LISTEN, sending reset\n"));
  456. tcp_rst(ackno + 1, seqno + tcplen,
  457. ip_current_dest_addr(), ip_current_src_addr(),
  458. tcphdr->dest, tcphdr->src);
  459. } else if (flags & TCP_SYN) {//�յ�SYN����
  460. LWIP_DEBUGF(TCP_DEBUG, ("TCP connection request %"U16_F" -> %"U16_F".\n", tcphdr->src, tcphdr->dest));
  461. #if TCP_LISTEN_BACKLOG
  462. if (pcb->accepts_pending >= pcb->backlog) {
  463. LWIP_DEBUGF(TCP_DEBUG, ("tcp_listen_input: listen backlog exceeded for port %"U16_F"\n", tcphdr->dest));
  464. return ERR_ABRT;
  465. }
  466. #endif /* TCP_LISTEN_BACKLOG */
  467. for(pactive_pcb = tcp_active_pcbs; pactive_pcb != NULL; pactive_pcb = pactive_pcb->next){
  468. if (pactive_pcb->state == ESTABLISHED){
  469. active_pcb_num ++;
  470. }
  471. }
  472. if (active_pcb_num == MEMP_NUM_TCP_PCB){
  473. LWIP_DEBUGF(TCP_DEBUG, ("tcp_listen_input: exceed the number of active TCP connections\n"));
  474. TCP_STATS_INC(tcp.memerr);
  475. return ERR_MEM;
  476. }
  477. npcb = tcp_alloc(pcb->prio);//�������ƿ�
  478. /* If a new PCB could not be created (probably due to lack of memory),
  479. we don't do anything, but rely on the sender will retransmit the
  480. SYN at a time when we have more memory available. */
  481. if (npcb == NULL) {
  482. LWIP_DEBUGF(TCP_DEBUG, ("tcp_listen_input: could not allocate PCB\n"));
  483. TCP_STATS_INC(tcp.memerr);//TCP�ڴ�������
  484. return ERR_MEM;
  485. }
  486. #if TCP_LISTEN_BACKLOG
  487. pcb->accepts_pending++;
  488. #endif /* TCP_LISTEN_BACKLOG */
  489. /* Set up the new PCB. */
  490. //���ƿ���������ص�4���ֶ�
  491. ip_addr_copy(npcb->local_ip, current_iphdr_dest);
  492. npcb->local_port = pcb->local_port;
  493. ip_addr_copy(npcb->remote_ip, current_iphdr_src);
  494. npcb->remote_port = tcphdr->src;
  495. //���ƿ��������ֶ�
  496. npcb->state = SYN_RCVD;//��������״̬
  497. npcb->rcv_nxt = seqno + 1;//������һ������������
  498. npcb->rcv_ann_right_edge = npcb->rcv_nxt;
  499. npcb->snd_wnd = tcphdr->wnd;//���÷��ʹ���
  500. npcb->ssthresh = npcb->snd_wnd;
  501. npcb->snd_wl1 = seqno - 1;/* initialise to seqno-1 to force window update */
  502. npcb->callback_arg = pcb->callback_arg;
  503. #if LWIP_CALLBACK_API
  504. npcb->accept = pcb->accept;
  505. #endif /* LWIP_CALLBACK_API */
  506. /* inherit socket options */
  507. npcb->so_options = pcb->so_options & SOF_INHERITED;
  508. /* Register the new PCB so that we can begin receiving segments
  509. for it. */
  510. TCP_REG(&tcp_active_pcbs, npcb);
  511. /* Parse any options in the SYN. */
  512. tcp_parseopt(npcb);
  513. #if TCP_CALCULATE_EFF_SEND_MSS
  514. npcb->mss = tcp_eff_send_mss(npcb->mss, &(npcb->remote_ip));
  515. #endif /* TCP_CALCULATE_EFF_SEND_MSS */
  516. snmp_inc_tcppassiveopens();
  517. /* Send a SYN|ACK together with the MSS option. */
  518. rc = tcp_enqueue_flags(npcb, TCP_SYN | TCP_ACK);
  519. if (rc != ERR_OK) {//��������ͷ��¿��ƿ�
  520. tcp_abandon(npcb, 0);
  521. return rc;
  522. }
  523. return tcp_output(npcb);//���ͱ���
  524. }
  525. return ERR_OK;
  526. }
  527. /**
  528. * Called by tcp_input() when a segment arrives for a connection in
  529. * TIME_WAIT.
  530. *
  531. * @param pcb the tcp_pcb for which a segment arrived
  532. *
  533. * @note the segment which arrived is saved in global variables, therefore only the pcb
  534. * involved is passed as a parameter to this function
  535. */
  536. /*
  537. *����TIME_WAIT״̬�Ŀ��ƿ���øú������յ��ı��ĶΣ�
  538. *��״̬�£��ر����ӵ����ֹ���Ѿ��������ڵȴ�2MSL��ʱ��
  539. *��״̬�µı��Ķ����������еľ���ݣ�ֱ��ɾ��ɡ�
  540. *����Ҫ���ͷ�����ACK����
  541. */
  542. static err_t
  543. tcp_timewait_input(struct tcp_pcb *pcb)
  544. {
  545. if (flags & TCP_RST) { //RST��λ��ֱ�ӷ���
  546. return ERR_OK;
  547. }
  548. if (flags & TCP_SYN) { //��SYN������Ϣ����������ݱ���ڽ��մ����ڣ����ͷ�����RST����
  549. if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt, pcb->rcv_nxt+pcb->rcv_wnd)) {
  550. tcp_rst(ackno, seqno + tcplen, ip_current_dest_addr(), ip_current_src_addr(),
  551. tcphdr->dest, tcphdr->src);
  552. return ERR_OK;
  553. }
  554. } else if (flags & TCP_FIN) { //���İ�FIN������Ϣ
  555. pcb->tmr = tcp_ticks; //��λ�ȴ�2MSLʱ�䣬���ƿ����µȴ�2MSL
  556. }
  557. if ((tcplen > 0)) { //��������ݵı��Ļ����ڽ��մ������SYN����
  558. pcb->flags |= TF_ACK_NOW;//����һ��ACK����
  559. return tcp_output(pcb);
  560. }
  561. return ERR_OK;
  562. }
  563. /**
  564. * Implements the TCP state machine. Called by tcp_input. In some
  565. * states tcp_receive() is called to receive data. The tcp_seg
  566. * argument will be freed by the caller (tcp_input()) unless the
  567. * recv_data pointer in the pcb is set.
  568. *
  569. * @param pcb the tcp_pcb for which a segment arrived
  570. *
  571. * @note the segment which arrived is saved in global variables, therefore only the pcb
  572. * involved is passed as a parameter to this function
  573. */
  574. static err_t
  575. tcp_process(struct tcp_pcb *pcb)
  576. {
  577. struct tcp_seg *rseg;
  578. u8_t acceptable = 0;
  579. err_t err;
  580. err = ERR_OK;
  581. /* Process incoming RST segments. */
  582. if (flags & TCP_RST) {
  583. /* First, determine if the reset is acceptable. */
  584. if (pcb->state == SYN_SENT) {
  585. if (ackno == pcb->snd_nxt) {
  586. acceptable = 1;
  587. }
  588. } else {
  589. if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt,
  590. pcb->rcv_nxt+pcb->rcv_wnd)) {
  591. acceptable = 1;
  592. }
  593. }
  594. if (acceptable) {
  595. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_process: Connection RESET\n"));
  596. LWIP_ASSERT("tcp_input: pcb->state != CLOSED", pcb->state != CLOSED);
  597. recv_flags |= TF_RESET;
  598. pcb->flags &= ~TF_ACK_DELAY;
  599. return ERR_RST;
  600. } else {
  601. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_process: unacceptable reset seqno %"U32_F" rcv_nxt %"U32_F"\n",
  602. seqno, pcb->rcv_nxt));
  603. LWIP_DEBUGF(TCP_DEBUG, ("tcp_process: unacceptable reset seqno %"U32_F" rcv_nxt %"U32_F"\n",
  604. seqno, pcb->rcv_nxt));
  605. return ERR_OK;
  606. }
  607. }
  608. if ((flags & TCP_SYN) && (pcb->state != SYN_SENT && pcb->state != SYN_RCVD)) {
  609. /* Cope with new connection attempt after remote end crashed */
  610. tcp_ack_now(pcb);
  611. return ERR_OK;
  612. }
  613. if ((pcb->flags & TF_RXCLOSED) == 0) {
  614. /* Update the PCB (in)activity timer unless rx is closed (see tcp_shutdown) */
  615. pcb->tmr = tcp_ticks;
  616. }
  617. pcb->keep_cnt_sent = 0;
  618. tcp_parseopt(pcb);
  619. /* Do different things depending on the TCP state. */
  620. switch (pcb->state) {
  621. case SYN_SENT:
  622. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("SYN-SENT: ackno %"U32_F" pcb->snd_nxt %"U32_F" unacked %"U32_F"\n", ackno,
  623. pcb->snd_nxt, ntohl(pcb->unacked->tcphdr->seqno)));
  624. /* received SYN ACK with expected sequence number? */
  625. if ((flags & TCP_ACK) && (flags & TCP_SYN)
  626. && ackno == ntohl(pcb->unacked->tcphdr->seqno) + 1) {
  627. pcb->snd_buf++;
  628. pcb->rcv_nxt = seqno + 1;
  629. pcb->rcv_ann_right_edge = pcb->rcv_nxt;
  630. pcb->lastack = ackno;
  631. pcb->snd_wnd = tcphdr->wnd;
  632. pcb->snd_wl1 = seqno - 1; /* initialise to seqno - 1 to force window update */
  633. pcb->state = ESTABLISHED;
  634. #if TCP_CALCULATE_EFF_SEND_MSS
  635. pcb->mss = tcp_eff_send_mss(pcb->mss, &(pcb->remote_ip));
  636. #endif /* TCP_CALCULATE_EFF_SEND_MSS */
  637. /* Set ssthresh again after changing pcb->mss (already set in tcp_connect
  638. * but for the default value of pcb->mss) */
  639. pcb->ssthresh = pcb->mss * 10;
  640. pcb->cwnd = ((pcb->cwnd == 1) ? (pcb->mss * 2) : pcb->mss);
  641. LWIP_ASSERT("pcb->snd_queuelen > 0", (pcb->snd_queuelen > 0));
  642. --pcb->snd_queuelen;
  643. LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_process: SYN-SENT --queuelen %"U16_F"\n", (u16_t)pcb->snd_queuelen));
  644. rseg = pcb->unacked;
  645. pcb->unacked = rseg->next;
  646. /* If there's nothing left to acknowledge, stop the retransmit
  647. timer, otherwise reset it to start again */
  648. if(pcb->unacked == NULL)
  649. pcb->rtime = -1;
  650. else {
  651. pcb->rtime = 0;
  652. // pcb->nrtx = 0;
  653. }
  654. pcb->nrtx = 0;
  655. tcp_seg_free(rseg);
  656. /* Call the user specified function to call when sucessfully
  657. * connected. */
  658. TCP_EVENT_CONNECTED(pcb, ERR_OK, err);
  659. if (err == ERR_ABRT) {
  660. return ERR_ABRT;
  661. }
  662. tcp_ack_now(pcb);
  663. }
  664. /* received ACK? possibly a half-open connection */
  665. else if (flags & TCP_ACK) {
  666. /* send a RST to bring the other side in a non-synchronized state. */
  667. tcp_rst(ackno, seqno + tcplen, ip_current_dest_addr(), ip_current_src_addr(),
  668. tcphdr->dest, tcphdr->src);
  669. }
  670. break;
  671. case SYN_RCVD:
  672. if (flags & TCP_ACK) {
  673. /* expected ACK number? */
  674. if (TCP_SEQ_BETWEEN(ackno, pcb->lastack+1, pcb->snd_nxt)) {
  675. u16_t old_cwnd;
  676. pcb->state = ESTABLISHED;
  677. LWIP_DEBUGF(TCP_DEBUG, ("TCP connection established %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
  678. #if LWIP_CALLBACK_API
  679. LWIP_ASSERT("pcb->accept != NULL", pcb->accept != NULL);
  680. #endif
  681. /* Call the accept function. */
  682. TCP_EVENT_ACCEPT(pcb, ERR_OK, err);
  683. if (err != ERR_OK) {
  684. /* If the accept function returns with an error, we abort
  685. * the connection. */
  686. /* Already aborted? */
  687. if (err != ERR_ABRT) {
  688. tcp_abort(pcb);
  689. }
  690. return ERR_ABRT;
  691. }
  692. old_cwnd = pcb->cwnd;
  693. /* If there was any data contained within this ACK,
  694. * we'd better pass it on to the application as well. */
  695. tcp_receive(pcb);
  696. /* Prevent ACK for SYN to generate a sent event */
  697. if (pcb->acked != 0) {
  698. pcb->acked--;
  699. }
  700. pcb->cwnd = ((old_cwnd == 1) ? (pcb->mss * 2) : pcb->mss);
  701. if (recv_flags & TF_GOT_FIN) {
  702. tcp_ack_now(pcb);
  703. pcb->state = CLOSE_WAIT;
  704. }
  705. } else {
  706. /* incorrect ACK number, send RST */
  707. tcp_rst(ackno, seqno + tcplen, ip_current_dest_addr(), ip_current_src_addr(),
  708. tcphdr->dest, tcphdr->src);
  709. }
  710. } else if ((flags & TCP_SYN) && (seqno == pcb->rcv_nxt - 1)) {
  711. /* Looks like another copy of the SYN - retransmit our SYN-ACK */
  712. tcp_rexmit(pcb);
  713. }
  714. break;
  715. case CLOSE_WAIT:
  716. /* FALLTHROUGH */
  717. case ESTABLISHED:
  718. tcp_receive(pcb);
  719. if (recv_flags & TF_GOT_FIN) { /* passive close */
  720. tcp_ack_now(pcb);
  721. pcb->state = CLOSE_WAIT;
  722. }
  723. break;
  724. case FIN_WAIT_1:
  725. tcp_receive(pcb);
  726. if (recv_flags & TF_GOT_FIN) {
  727. if ((flags & TCP_ACK) && (ackno == pcb->snd_nxt)) {
  728. LWIP_DEBUGF(TCP_DEBUG,
  729. ("TCP connection closed: FIN_WAIT_1 %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
  730. tcp_ack_now(pcb);
  731. tcp_pcb_purge(pcb);
  732. TCP_RMV(&tcp_active_pcbs, pcb);
  733. pcb->state = TIME_WAIT;
  734. TCP_REG(&tcp_tw_pcbs, pcb);
  735. } else {
  736. tcp_ack_now(pcb);
  737. pcb->state = CLOSING;
  738. }
  739. } else if ((flags & TCP_ACK) && (ackno == pcb->snd_nxt)) {
  740. pcb->state = FIN_WAIT_2;
  741. }
  742. break;
  743. case FIN_WAIT_2:
  744. tcp_receive(pcb);
  745. if (recv_flags & TF_GOT_FIN) {
  746. LWIP_DEBUGF(TCP_DEBUG, ("TCP connection closed: FIN_WAIT_2 %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
  747. tcp_ack_now(pcb);
  748. tcp_pcb_purge(pcb);
  749. TCP_RMV(&tcp_active_pcbs, pcb);
  750. pcb->state = TIME_WAIT;
  751. TCP_REG(&tcp_tw_pcbs, pcb);
  752. }
  753. break;
  754. case CLOSING:
  755. tcp_receive(pcb);
  756. if (flags & TCP_ACK && ackno == pcb->snd_nxt) {
  757. LWIP_DEBUGF(TCP_DEBUG, ("TCP connection closed: CLOSING %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
  758. tcp_pcb_purge(pcb);
  759. TCP_RMV(&tcp_active_pcbs, pcb);
  760. pcb->state = TIME_WAIT;
  761. TCP_REG(&tcp_tw_pcbs, pcb);
  762. }
  763. break;
  764. case LAST_ACK:
  765. tcp_receive(pcb);
  766. if (flags & TCP_ACK && ackno == pcb->snd_nxt) {
  767. LWIP_DEBUGF(TCP_DEBUG, ("TCP connection closed: LAST_ACK %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
  768. /* bugfix #21699: don't set pcb->state to CLOSED here or we risk leaking segments */
  769. recv_flags |= TF_CLOSED;
  770. }
  771. break;
  772. default:
  773. break;
  774. }
  775. return ERR_OK;
  776. }
  777. #if TCP_QUEUE_OOSEQ
  778. /**
  779. * Insert segment into the list (segments covered with new one will be deleted)
  780. *
  781. * Called from tcp_receive()
  782. */
  783. static void ICACHE_FLASH_ATTR
  784. tcp_oos_insert_segment(struct tcp_seg *cseg, struct tcp_seg *next)
  785. {
  786. struct tcp_seg *old_seg;
  787. if (TCPH_FLAGS(cseg->tcphdr) & TCP_FIN) {
  788. /* received segment overlaps all following segments */
  789. tcp_segs_free(next);
  790. next = NULL;
  791. }
  792. else {
  793. /* delete some following segments
  794. oos queue may have segments with FIN flag */
  795. while (next &&
  796. TCP_SEQ_GEQ((seqno + cseg->len),
  797. (next->tcphdr->seqno + next->len))) {
  798. /* cseg with FIN already processed */
  799. if (TCPH_FLAGS(next->tcphdr) & TCP_FIN) {
  800. TCPH_SET_FLAG(cseg->tcphdr, TCP_FIN);
  801. }
  802. old_seg = next;
  803. next = next->next;
  804. tcp_seg_free(old_seg);
  805. }
  806. if (next &&
  807. TCP_SEQ_GT(seqno + cseg->len, next->tcphdr->seqno)) {
  808. /* We need to trim the incoming segment. */
  809. cseg->len = (u16_t)(next->tcphdr->seqno - seqno);
  810. pbuf_realloc(cseg->p, cseg->len);
  811. }
  812. }
  813. cseg->next = next;
  814. }
  815. #endif /* TCP_QUEUE_OOSEQ */
  816. /**
  817. * Called by tcp_process. Checks if the given segment is an ACK for outstanding
  818. * data, and if so frees the memory of the buffered data. Next, is places the
  819. * segment on any of the receive queues (pcb->recved or pcb->ooseq). If the segment
  820. * is buffered, the pbuf is referenced by pbuf_ref so that it will not be freed until
  821. * i it has been removed from the buffer.
  822. *
  823. * If the incoming segment constitutes an ACK for a segment that was used for RTT
  824. * estimation, the RTT is estimated here as well.
  825. *
  826. * Called from tcp_process().
  827. */
  828. static void
  829. tcp_receive(struct tcp_pcb *pcb)
  830. {
  831. struct tcp_seg *next;
  832. #if TCP_QUEUE_OOSEQ
  833. struct tcp_seg *prev, *cseg;
  834. #endif /* TCP_QUEUE_OOSEQ */
  835. struct pbuf *p;
  836. s32_t off;
  837. s16_t m;
  838. u32_t right_wnd_edge;
  839. u16_t new_tot_len;
  840. int found_dupack = 0;
  841. if (flags & TCP_ACK) {//����ACK
  842. right_wnd_edge = pcb->snd_wnd + pcb->snd_wl2;//���ʹ��� + ����Ӧ����󴰿ڸ���
  843. // first /* Update window. */
  844. /*seqno > snd_wl1���������ֹ�̲��ô��ָ���;
  845. *seqno = snd_wl1����ackno > snd_wl2;��ʱ���Է�û�з�����ݣ�ֻ���յ���ݵ�ȷ��;
  846. *ackno = snd_wl2�ұ����ײ��б�snd_wnd���Ĵ���.����������Ӧֵ
  847. */
  848. if (TCP_SEQ_LT(pcb->snd_wl1, seqno) ||
  849. (pcb->snd_wl1 == seqno && TCP_SEQ_LT(pcb->snd_wl2, ackno)) ||
  850. (pcb->snd_wl2 == ackno && tcphdr->wnd > pcb->snd_wnd)) {
  851. pcb->snd_wnd = tcphdr->wnd;
  852. pcb->snd_wl1 = seqno;
  853. pcb->snd_wl2 = ackno;
  854. if (pcb->snd_wnd > 0 && pcb->persist_backoff > 0) {
  855. pcb->persist_backoff = 0;//�����ʱ���˳�
  856. }
  857. LWIP_DEBUGF(TCP_WND_DEBUG, ("tcp_receive: window update %"U16_F"\n", pcb->snd_wnd));
  858. #if TCP_WND_DEBUG
  859. } else {
  860. if (pcb->snd_wnd != tcphdr->wnd) {
  861. LWIP_DEBUGF(TCP_WND_DEBUG,
  862. ("tcp_receive: no window update lastack %"U32_F" ackno %"
  863. U32_F" wl1 %"U32_F" seqno %"U32_F" wl2 %"U32_F"\n",
  864. pcb->lastack, ackno, pcb->snd_wl1, seqno, pcb->snd_wl2));
  865. }
  866. #endif /* TCP_WND_DEBUG */
  867. }
  868. /* (From Stevens TCP/IP Illustrated Vol II, p970.) Its only a
  869. * duplicate ack if:
  870. * 1) It doesn't ACK new data û��ȷ�������
  871. * 2) length of received packet is zero (i.e. no payload) ���Ķ����κ����
  872. * 3) the advertised window hasn't changed ���ش���û�и���
  873. * 4) There is outstanding unacknowledged data (retransmission timer running)������ݵȴ�ȷ��
  874. * 5) The ACK is == biggest ACK sequence number so far seen (snd_una) ackno = lastack
  875. *
  876. * If it passes all five, should process as a dupack:
  877. * a) dupacks < 3: do nothing
  878. * b) dupacks == 3: fast retransmit
  879. * c) dupacks > 3: increase cwnd
  880. *
  881. * If it only passes 1-3, should reset dupack counter (and add to
  882. * stats, which we don't do in lwIP)
  883. *
  884. * If it only passes 1, should reset dupack counter
  885. *
  886. */
  887. /* Clause 1 */
  888. if (TCP_SEQ_LEQ(ackno, pcb->lastack)) {//���ظ�ACK?
  889. pcb->acked = 0;
  890. /* Clause 2 */
  891. if (tcplen == 0) {
  892. /* Clause 3 */
  893. if (pcb->snd_wl2 + pcb->snd_wnd == right_wnd_edge){
  894. /* Clause 4 */
  895. if (pcb->rtime >= 0) {
  896. /* Clause 5 */
  897. if (pcb->lastack == ackno) {
  898. found_dupack = 1;
  899. if (pcb->dupacks + 1 > pcb->dupacks)
  900. ++pcb->dupacks;
  901. if (pcb->dupacks > 3) {
  902. /* Inflate the congestion window, but not if it means that
  903. the value overflows. */
  904. if ((u16_t)(pcb->cwnd + pcb->mss) > pcb->cwnd) {
  905. pcb->cwnd += pcb->mss;
  906. }
  907. } else if (pcb->dupacks == 3) {//���ظ�ACK
  908. /* Do fast retransmit */
  909. tcp_rexmit_fast(pcb);
  910. }
  911. }
  912. }
  913. }
  914. }
  915. /* If Clause (1) or more is true, but not a duplicate ack, reset
  916. * count of consecutive duplicate acks */
  917. if (!found_dupack) {
  918. pcb->dupacks = 0;
  919. }
  920. } else if (TCP_SEQ_BETWEEN(ackno, pcb->lastack+1, pcb->snd_nxt)){//ackno��lastack+1��snd_nxt֮�䣬�жϷ��ʹ��������
  921. /* We come here when the ACK acknowledges new data. */
  922. if (pcb->flags & TF_INFR) {
  923. pcb->flags &= ~TF_INFR;// Reset the "IN Fast Retransmit" flag,since we are no longer in fast retransmit
  924. pcb->cwnd = pcb->ssthresh;//Reset the congestion window to the "slow start threshold".
  925. }
  926. /* Reset the number of retransmissions. */
  927. pcb->nrtx = 0;
  928. /* Reset the retransmission time-out. */
  929. pcb->rto = (pcb->sa >> 3) + pcb->sv;
  930. /* Update the send buffer space. Diff between the two can never exceed 64K? */
  931. pcb->acked = (u16_t)(ackno - pcb->lastack);
  932. pcb->snd_buf += pcb->acked;
  933. /* Reset the fast retransmit variables. */
  934. pcb->dupacks = 0;
  935. pcb->lastack = ackno;
  936. /* Update the congestion control variables (cwnd and
  937. ssthresh). */
  938. if (pcb->state >= ESTABLISHED) {//״̬Ϊ�������ӱ�־
  939. if (pcb->cwnd < pcb->ssthresh) {
  940. if ((u16_t)(pcb->cwnd + pcb->mss) > pcb->cwnd) {
  941. pcb->cwnd += pcb->mss;
  942. }
  943. LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_receive: slow start cwnd %"U16_F"\n", pcb->cwnd));
  944. } else {
  945. u16_t new_cwnd = (pcb->cwnd + pcb->mss * pcb->mss / pcb->cwnd);
  946. if (new_cwnd > pcb->cwnd) {
  947. pcb->cwnd = new_cwnd;
  948. }
  949. LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_receive: congestion avoidance cwnd %"U16_F"\n", pcb->cwnd));
  950. }
  951. }
  952. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: ACK for %"U32_F", unacked->seqno %"U32_F":%"U32_F"\n",
  953. ackno,
  954. pcb->unacked != NULL?
  955. ntohl(pcb->unacked->tcphdr->seqno): 0,
  956. pcb->unacked != NULL?
  957. ntohl(pcb->unacked->tcphdr->seqno) + TCP_TCPLEN(pcb->unacked): 0));
  958. /* Remove segment from the unacknowledged list if the incoming
  959. ACK acknowlegdes them.
  960. *�ͷ�unacked�����ϱ�ȷ�ϵı��ĶΣ�
  961. *ֱ��unacked����Ϊ��ֹͣ*/
  962. while (pcb->unacked != NULL &&
  963. TCP_SEQ_LEQ(ntohl(pcb->unacked->tcphdr->seqno) +
  964. TCP_TCPLEN(pcb->unacked), ackno)) {
  965. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: removing %"U32_F":%"U32_F" from pcb->unacked\n",
  966. ntohl(pcb->unacked->tcphdr->seqno),
  967. ntohl(pcb->unacked->tcphdr->seqno) +
  968. TCP_TCPLEN(pcb->unacked)));
  969. next = pcb->unacked;//pcb unacked��־
  970. pcb->unacked = pcb->unacked->next;//pcb unacked ��һ����־
  971. LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_receive: queuelen %"U16_F" ... ", (u16_t)pcb->snd_queuelen));
  972. LWIP_ASSERT("pcb->snd_queuelen >= pbuf_clen(next->p)", (pcb->snd_queuelen >= pbuf_clen(next->p)));
  973. /* Prevent ACK for FIN to generate a sent event */
  974. if ((pcb->acked != 0) && ((TCPH_FLAGS(next->tcphdr) & TCP_FIN) != 0)) {
  975. pcb->acked--;
  976. }
  977. pcb->snd_queuelen -= pbuf_clen(next->p);//�������������pbufs����
  978. tcp_seg_free(next);//�ͷ�tcp��
  979. LWIP_DEBUGF(TCP_QLEN_DEBUG, ("%"U16_F" (after freeing unacked)\n", (u16_t)pcb->snd_queuelen));
  980. if (pcb->snd_queuelen != 0) {
  981. LWIP_ASSERT("tcp_receive: valid queue length", pcb->unacked != NULL ||
  982. pcb->unsent != NULL);
  983. }
  984. }
  985. /* If there's nothing left to acknowledge, stop the retransmit
  986. timer, otherwise reset it to start again */
  987. if(pcb->unacked == NULL) //����ݵȴ�ȷ��
  988. pcb->rtime = -1; //ֹͣ�ش���ʱ��
  989. else
  990. pcb->rtime = 0; //��λ�ش���ʱ��
  991. pcb->polltmr = 0;
  992. } else {
  993. /* Fix bug bug #21582: out of sequence ACK, didn't really ack anything */
  994. pcb->acked = 0;
  995. }
  996. /* We go through the ->unsent list to see if any of the segments
  997. on the list are acknowledged by the ACK. This may seem
  998. strange since an "unsent" segment shouldn't be acked. The
  999. rationale is that lwIP puts all outstanding segments on the
  1000. ->unsent list after a retransmission, so these segments may
  1001. in fact have been sent once. */
  1002. /** unsent�������Ƿ��ܱ�acknoȷ�ϵı��ĶΣ������ͷ�**/
  1003. while (pcb->unsent != NULL &&
  1004. TCP_SEQ_BETWEEN(ackno, ntohl(pcb->unsent->tcphdr->seqno) +
  1005. TCP_TCPLEN(pcb->unsent), pcb->snd_nxt)) {
  1006. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: removing %"U32_F":%"U32_F" from pcb->unsent\n",
  1007. ntohl(pcb->unsent->tcphdr->seqno), ntohl(pcb->unsent->tcphdr->seqno) +
  1008. TCP_TCPLEN(pcb->unsent)));
  1009. next = pcb->unsent;//pcbδ���ͱ�־
  1010. pcb->unsent = pcb->unsent->next;//δ���͵���һ��
  1011. LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_receive: queuelen %"U16_F" ... ", (u16_t)pcb->snd_queuelen));
  1012. LWIP_ASSERT("pcb->snd_queuelen >= pbuf_clen(next->p)", (pcb->snd_queuelen >= pbuf_clen(next->p)));
  1013. /* Prevent ACK for FIN to generate a sent event */
  1014. if ((pcb->acked != 0) && ((TCPH_FLAGS(next->tcphdr) & TCP_FIN) != 0)) {
  1015. pcb->acked--;
  1016. }
  1017. pcb->snd_queuelen -= pbuf_clen(next->p);//������pbuf�ĸ���
  1018. tcp_seg_free(next);//�ͷŶ�
  1019. LWIP_DEBUGF(TCP_QLEN_DEBUG, ("%"U16_F" (after freeing unsent)\n", (u16_t)pcb->snd_queuelen));
  1020. if (pcb->snd_queuelen != 0) {//���������
  1021. LWIP_ASSERT("tcp_receive: valid queue length",
  1022. pcb->unacked != NULL || pcb->unsent != NULL);
  1023. }
  1024. }
  1025. /* End of ACK for new data processing. */
  1026. LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_receive: pcb->rttest %"U32_F" rtseq %"U32_F" ackno %"U32_F"\n",
  1027. pcb->rttest, pcb->rtseq, ackno));
  1028. /* RTT estimation calculations. This is done by checking if the
  1029. incoming segment acknowledges the segment we use to take a
  1030. round-trip time measurement. */
  1031. if (pcb->rttest && TCP_SEQ_LT(pcb->rtseq, ackno)) {//RTT���ڽ����Ҹñ��Ķα�ȷ��
  1032. /* diff between this shouldn't exceed 32K since this are tcp timer ticks
  1033. and a round-trip shouldn't be that long... */
  1034. m = (s16_t)(tcp_ticks - pcb->rttest);//����Mֵ
  1035. LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_receive: experienced rtt %"U16_F" ticks (%"U16_F" msec).\n",
  1036. m, m * TCP_SLOW_INTERVAL));
  1037. /* This is taken directly from VJs original code in his paper �����RTT���㹫ʽ*/
  1038. m = m - (pcb->sa >> 3);
  1039. pcb->sa += m;
  1040. if (m < 0) {
  1041. m = -m;
  1042. }
  1043. m = m - (pcb->sv >> 2);
  1044. pcb->sv += m;
  1045. pcb->rto = (pcb->sa >> 3) + pcb->sv;
  1046. LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_receive: RTO %"U16_F" (%"U16_F" milliseconds)\n",
  1047. pcb->rto, pcb->rto * TCP_SLOW_INTERVAL));
  1048. pcb->rttest = 0;
  1049. }
  1050. }
  1051. /* If the incoming segment contains data, we must process it
  1052. further. */
  1053. if (tcplen > 0) {
  1054. /* This code basically does three things:
  1055. +) If the incoming segment contains data that is the next
  1056. in-sequence data, this data is passed to the application. This
  1057. might involve trimming the first edge of the data. The rcv_nxt
  1058. variable and the advertised window are adjusted.
  1059. +) If the incoming segment has data that is above the next
  1060. sequence number expected (->rcv_nxt), the segment is placed on
  1061. the ->ooseq queue. This is done by finding the appropriate
  1062. place in the ->ooseq queue (which is ordered by sequence
  1063. number) and trim the segment in both ends if needed. An
  1064. immediate ACK is sent to indicate that we received an
  1065. out-of-sequence segment.
  1066. +) Finally, we check if the first segment on the ->ooseq queue
  1067. now is in sequence (i.e., if rcv_nxt >= ooseq->seqno). If
  1068. rcv_nxt > ooseq->seqno, we must trim the first edge of the
  1069. segment on ->ooseq before we adjust rcv_nxt. The data in the
  1070. segments that are now on sequence are chained onto the
  1071. incoming segment so that we only need to call the application
  1072. once.
  1073. */
  1074. /* First, we check if we must trim the first edge. We have to do
  1075. this if the sequence number of the incoming segment is less
  1076. than rcv_nxt, and the sequence number plus the length of the
  1077. segment is larger than rcv_nxt. */
  1078. /* if (TCP_SEQ_LT(seqno, pcb->rcv_nxt)){
  1079. if (TCP_SEQ_LT(pcb->rcv_nxt, seqno + tcplen)) {*/
  1080. if (TCP_SEQ_BETWEEN(pcb->rcv_nxt, seqno + 1, seqno + tcplen - 1)){// seqno < rcv_nxt < seqno + tcplen
  1081. /* Trimming the first edge is done by pushing the payload
  1082. pointer in the pbuf downwards. This is somewhat tricky since
  1083. we do not want to discard the full contents of the pbuf up to
  1084. the new starting point of the data since we have to keep the
  1085. TCP header which is present in the first pbuf in the chain.
  1086. What is done is really quite a nasty hack: the first pbuf in
  1087. the pbuf chain is pointed to by inseg.p. Since we need to be
  1088. able to deallocate the whole pbuf, we cannot change this
  1089. inseg.p pointer to point to any of the later pbufs in the
  1090. chain. Instead, we point the ->payload pointer in the first
  1091. pbuf to data in one of the later pbufs. We also set the
  1092. inseg.data pointer to point to the right place. This way, the
  1093. ->p pointer will still point to the first pbuf, but the
  1094. ->p->payload pointer will point to data in another pbuf.
  1095. After we are done with adjusting the pbuf pointers we must
  1096. adjust the ->data pointer in the seg and the segment
  1097. length.*/
  1098. //ȥ�����Ķ�����ݱ�ŵ���rcv_nxt�����
  1099. off = pcb->rcv_nxt - seqno;
  1100. p = inseg.p;
  1101. LWIP_ASSERT("inseg.p != NULL", inseg.p);
  1102. LWIP_ASSERT("insane offset!", (off < 0x7fff));
  1103. if (inseg.p->len < off) {
  1104. LWIP_ASSERT("pbuf too short!", (((s32_t)inseg.p->tot_len) >= off));
  1105. new_tot_len = (u16_t)(inseg.p->tot_len - off);
  1106. while (p->len < off) {
  1107. off -= p->len;
  1108. /* KJM following line changed (with addition of new_tot_len var)
  1109. to fix bug #9076
  1110. inseg.p->tot_len -= p->len; */
  1111. p->tot_len = new_tot_len;
  1112. p->len = 0;
  1113. p = p->next;
  1114. }
  1115. if(pbuf_header(p, (s16_t)-off)) {
  1116. /* Do we need to cope with this failing? Assert for now */
  1117. LWIP_ASSERT("pbuf_header failed", 0);
  1118. }
  1119. } else {
  1120. if(pbuf_header(inseg.p, (s16_t)-off)) {
  1121. /* Do we need to cope with this failing? Assert for now */
  1122. LWIP_ASSERT("pbuf_header failed", 0);
  1123. }
  1124. }
  1125. inseg.len -= (u16_t)(pcb->rcv_nxt - seqno);
  1126. inseg.tcphdr->seqno = seqno = pcb->rcv_nxt;
  1127. }
  1128. else {
  1129. if (TCP_SEQ_LT(seqno, pcb->rcv_nxt)){//seqno < rcv_nxt
  1130. /* the whole segment is < rcv_nxt */
  1131. /* must be a duplicate of a packet that has already been correctly handled */
  1132. //���Ķ���������ݱ�ž�С��rcv_nxt����˱������ظ����ģ�
  1133. //ֱ����Դ����ӦACK���Ĵ���
  1134. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: duplicate seqno %"U32_F"\n", seqno));
  1135. tcp_ack_now(pcb);
  1136. }
  1137. }
  1138. /* The sequence number must be within the window (above rcv_nxt
  1139. and below rcv_nxt + rcv_wnd) in order to be further
  1140. processed. */
  1141. if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt,
  1142. pcb->rcv_nxt + pcb->rcv_wnd - 1)){//rcv_nxt < seqno < rcv_nxt + rcv_wnd - 1,������ڽ��շ�Χ��
  1143. if (pcb->rcv_nxt == seqno) {
  1144. /* The incoming segment is the next in sequence. We check if
  1145. we have to trim the end of the segment and update rcv_nxt
  1146. and pass the data to the application. */
  1147. tcplen = TCP_TCPLEN(&inseg);//���㱨�Ķγ���
  1148. if (tcplen > pcb->rcv_wnd) {//������մ��ڴ�С��������β���ض�
  1149. LWIP_DEBUGF(TCP_INPUT_DEBUG,
  1150. ("tcp_receive: other end overran receive window"
  1151. "seqno %"U32_F" len %"U16_F" right edge %"U32_F"\n",
  1152. seqno, tcplen, pcb->rcv_nxt + pcb->rcv_wnd));
  1153. if (TCPH_FLAGS(inseg.tcphdr) & TCP_FIN) {
  1154. /* Must remove the FIN from the header as we're trimming
  1155. * that byte of sequence-space from the packet */
  1156. TCPH_FLAGS_SET(inseg.tcphdr, TCPH_FLAGS(inseg.tcphdr) &~ TCP_FIN);
  1157. }
  1158. /* Adjust length of segment to fit in the window. */
  1159. inseg.len = pcb->rcv_wnd;
  1160. if (TCPH_FLAGS(inseg.tcphdr) & TCP_SYN) {
  1161. inseg.len -= 1;
  1162. }
  1163. pbuf_realloc(inseg.p, inseg.len);
  1164. tcplen = TCP_TCPLEN(&inseg);
  1165. LWIP_ASSERT("tcp_receive: segment not trimmed correctly to rcv_wnd\n",
  1166. (seqno + tcplen) == (pcb->rcv_nxt + pcb->rcv_wnd));
  1167. }
  1168. #if TCP_QUEUE_OOSEQ
  1169. /* Received in-sequence data, adjust ooseq data if:
  1170. - FIN has been received or
  1171. - inseq overlaps with ooseq */
  1172. if (pcb->ooseq != NULL) {
  1173. if (TCPH_FLAGS(inseg.tcphdr) & TCP_FIN) {
  1174. LWIP_DEBUGF(TCP_INPUT_DEBUG,
  1175. ("tcp_receive: received in-order FIN, binning ooseq queue\n"));
  1176. /* Received in-order FIN means anything that was received
  1177. * out of order must now have been received in-order, so
  1178. * bin the ooseq queue */
  1179. while (pcb->ooseq != NULL) {
  1180. struct tcp_seg *old_ooseq = pcb->ooseq;
  1181. pcb->ooseq = pcb->ooseq->next;
  1182. tcp_seg_free(old_ooseq);
  1183. }
  1184. }
  1185. else {
  1186. next = pcb->ooseq;
  1187. /* Remove all segments on ooseq that are covered by inseg already.
  1188. * FIN is copied from ooseq to inseg if present. */
  1189. while (next &&
  1190. TCP_SEQ_GEQ(seqno + tcplen,
  1191. next->tcphdr->seqno + next->len)) {
  1192. /* inseg cannot have FIN here (already processed above) */
  1193. if (TCPH_FLAGS(next->tcphdr) & TCP_FIN &&
  1194. (TCPH_FLAGS(inseg.tcphdr) & TCP_SYN) == 0) {
  1195. TCPH_SET_FLAG(inseg.tcphdr, TCP_FIN);
  1196. tcplen = TCP_TCPLEN(&inseg);
  1197. }
  1198. prev = next;
  1199. next = next->next;
  1200. tcp_seg_free(prev);
  1201. }
  1202. /* Now trim right side of inseg if it overlaps with the first
  1203. * segment on ooseq */
  1204. if (next &&
  1205. TCP_SEQ_GT(seqno + tcplen,
  1206. next->tcphdr->seqno)) {
  1207. /* inseg cannot have FIN here (already processed above) */
  1208. inseg.len = (u16_t)(next->tcphdr->seqno - seqno);
  1209. if (TCPH_FLAGS(inseg.tcphdr) & TCP_SYN) {
  1210. inseg.len -= 1;
  1211. }
  1212. pbuf_realloc(inseg.p, inseg.len);
  1213. tcplen = TCP_TCPLEN(&inseg);
  1214. LWIP_ASSERT("tcp_receive: segment not trimmed correctly to ooseq queue\n",
  1215. (seqno + tcplen) == next->tcphdr->seqno);
  1216. }
  1217. pcb->ooseq = next;
  1218. }
  1219. }
  1220. #endif /* TCP_QUEUE_OOSEQ */
  1221. pcb->rcv_nxt = seqno + tcplen;
  1222. /* Update the receiver's (our) window. */
  1223. LWIP_ASSERT("tcp_receive: tcplen > rcv_wnd\n", pcb->rcv_wnd >= tcplen);
  1224. pcb->rcv_wnd -= tcplen;
  1225. tcp_update_rcv_ann_wnd(pcb);
  1226. /* If there is data in the segment, we make preparations to
  1227. pass this up to the application. The ->recv_data variable
  1228. is used for holding the pbuf that goes to the
  1229. application. The code for reassembling out-of-sequence data
  1230. chains its data on this pbuf as well.
  1231. If the segment was a FIN, we set the TF_GOT_FIN flag that will
  1232. be used to indicate to the application that the remote side has
  1233. closed its end of the connection. */
  1234. if (inseg.p->tot_len > 0) {
  1235. recv_data = inseg.p;
  1236. /* Since this pbuf now is the responsibility of the
  1237. application, we delete our reference to it so that we won't
  1238. (mistakingly) deallocate it. */
  1239. inseg.p = NULL;
  1240. }
  1241. if (TCPH_FLAGS(inseg.tcphdr) & TCP_FIN) {
  1242. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: received FIN.\n"));
  1243. recv_flags |= TF_GOT_FIN;
  1244. }
  1245. #if TCP_QUEUE_OOSEQ
  1246. /* We now check if we have segments on the ->ooseq queue that
  1247. are now in sequence. */
  1248. while (pcb->ooseq != NULL &&
  1249. pcb->ooseq->tcphdr->seqno == pcb->rcv_nxt) {
  1250. cseg = pcb->ooseq;
  1251. seqno = pcb->ooseq->tcphdr->seqno;
  1252. pcb->rcv_nxt += TCP_TCPLEN(cseg);
  1253. LWIP_ASSERT("tcp_receive: ooseq tcplen > rcv_wnd\n",
  1254. pcb->rcv_wnd >= TCP_TCPLEN(cseg));
  1255. pcb->rcv_wnd -= TCP_TCPLEN(cseg);
  1256. tcp_update_rcv_ann_wnd(pcb);
  1257. if (cseg->p->tot_len > 0) {
  1258. /* Chain this pbuf onto the pbuf that we will pass to
  1259. the application. */
  1260. if (recv_data) {
  1261. pbuf_cat(recv_data, cseg->p);
  1262. } else {
  1263. recv_data = cseg->p;
  1264. }
  1265. cseg->p = NULL;
  1266. }
  1267. if (TCPH_FLAGS(cseg->tcphdr) & TCP_FIN) {
  1268. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: dequeued FIN.\n"));
  1269. recv_flags |= TF_GOT_FIN;
  1270. if (pcb->state == ESTABLISHED) { /* force passive close or we can move to active close */
  1271. pcb->state = CLOSE_WAIT;
  1272. }
  1273. }
  1274. pcb->ooseq = cseg->next;
  1275. tcp_seg_free(cseg);
  1276. }
  1277. #endif /* TCP_QUEUE_OOSEQ */
  1278. /* Acknowledge the segment(s). */
  1279. tcp_ack(pcb);
  1280. } else {
  1281. /* We get here if the incoming segment is out-of-sequence. */
  1282. tcp_send_empty_ack(pcb);
  1283. #if TCP_QUEUE_OOSEQ
  1284. /* We queue the segment on the ->ooseq queue. */
  1285. if (pcb->ooseq == NULL) {
  1286. pcb->ooseq = tcp_seg_copy(&inseg);
  1287. } else {
  1288. /* If the queue is not empty, we walk through the queue and
  1289. try to find a place where the sequence number of the
  1290. incoming segment is between the sequence numbers of the
  1291. previous and the next segment on the ->ooseq queue. That is
  1292. the place where we put the incoming segment. If needed, we
  1293. trim the second edges of the previous and the incoming
  1294. segment so that it will fit into the sequence.
  1295. If the incoming segment has the same sequence number as a
  1296. segment on the ->ooseq queue, we discard the segment that
  1297. contains less data. */
  1298. prev = NULL;
  1299. for(next = pcb->ooseq; next != NULL; next = next->next) {//��ooseqȡ�µ�M�����ĶΣ��ñ��Ķηǿգ�M++
  1300. if (seqno == next->tcphdr->seqno) {//�ñ��Ķ���ʼ���== Ҫ����ı��Ķα��
  1301. /* The sequence number of the incoming segment is the
  1302. same as the sequence number of the segment on
  1303. ->ooseq. We check the lengths to see which one to
  1304. discard. */
  1305. if (inseg.len > next->len) {//Ҫ����ı��Ķα�Ÿ�
  1306. /* The incoming segment is larger than the old
  1307. segment. We replace some segments with the new
  1308. one. */
  1309. cseg = tcp_seg_copy(&inseg);//Ҫ����ı��Ķδ����M�����Ķ�
  1310. if (cseg != NULL) {
  1311. if (prev != NULL) {
  1312. prev->next = cseg;
  1313. } else {
  1314. pcb->ooseq = cseg;
  1315. }
  1316. tcp_oos_insert_segment(cseg, next);
  1317. }
  1318. break;
  1319. } else {
  1320. /* Either the lenghts are the same or the incoming
  1321. segment was smaller than the old one; in either
  1322. case, we ditch the incoming segment. */
  1323. break;
  1324. }
  1325. } else {
  1326. if (prev == NULL) {
  1327. if (TCP_SEQ_LT(seqno, next->tcphdr->seqno)) {
  1328. /* The sequence number of the incoming segment is lower
  1329. than the sequence number of the first segment on the
  1330. queue. We put the incoming segment first on the
  1331. queue. */
  1332. cseg = tcp_seg_copy(&inseg);
  1333. if (cseg != NULL) {
  1334. pcb->ooseq = cseg;
  1335. tcp_oos_insert_segment(cseg, next);
  1336. }
  1337. break;
  1338. }
  1339. } else {
  1340. /*if (TCP_SEQ_LT(prev->tcphdr->seqno, seqno) &&
  1341. TCP_SEQ_LT(seqno, next->tcphdr->seqno)) {*/
  1342. if (TCP_SEQ_BETWEEN(seqno, prev->tcphdr->seqno+1, next->tcphdr->seqno-1)) {
  1343. /* The sequence number of the incoming segment is in
  1344. between the sequence numbers of the previous and
  1345. the next segment on ->ooseq. We trim trim the previous
  1346. segment, delete next segments that included in received segment
  1347. and trim received, if needed. */
  1348. cseg = tcp_seg_copy(&inseg);
  1349. if (cseg != NULL) {
  1350. if (TCP_SEQ_GT(prev->tcphdr->seqno + prev->len, seqno)) {
  1351. /* We need to trim the prev segment. */
  1352. prev->len = (u16_t)(seqno - prev->tcphdr->seqno);
  1353. pbuf_realloc(prev->p, prev->len);
  1354. }
  1355. prev->next = cseg;
  1356. tcp_oos_insert_segment(cseg, next);
  1357. }
  1358. break;
  1359. }
  1360. }
  1361. /* If the "next" segment is the last segment on the
  1362. ooseq queue, we add the incoming segment to the end
  1363. of the list. */
  1364. if (next->next == NULL &&
  1365. TCP_SEQ_GT(seqno, next->tcphdr->seqno)) {
  1366. if (TCPH_FLAGS(next->tcphdr) & TCP_FIN) {
  1367. /* segment "next" already contains all data */
  1368. break;
  1369. }
  1370. next->next = tcp_seg_copy(&inseg);
  1371. if (next->next != NULL) {
  1372. if (TCP_SEQ_GT(next->tcphdr->seqno + next->len, seqno)) {
  1373. /* We need to trim the last segment. */
  1374. next->len = (u16_t)(seqno - next->tcphdr->seqno);
  1375. pbuf_realloc(next->p, next->len);
  1376. }
  1377. /* check if the remote side overruns our receive window */
  1378. if ((u32_t)tcplen + seqno > pcb->rcv_nxt + (u32_t)pcb->rcv_wnd) {
  1379. LWIP_DEBUGF(TCP_INPUT_DEBUG,
  1380. ("tcp_receive: other end overran receive window"
  1381. "seqno %"U32_F" len %"U16_F" right edge %"U32_F"\n",
  1382. seqno, tcplen, pcb->rcv_nxt + pcb->rcv_wnd));
  1383. if (TCPH_FLAGS(next->next->tcphdr) & TCP_FIN) {
  1384. /* Must remove the FIN from the header as we're trimming
  1385. * that byte of sequence-space from the packet */
  1386. TCPH_FLAGS_SET(next->next->tcphdr, TCPH_FLAGS(next->next->tcphdr) &~ TCP_FIN);
  1387. }
  1388. /* Adjust length of segment to fit in the window. */
  1389. next->next->len = pcb->rcv_nxt + pcb->rcv_wnd - seqno;
  1390. pbuf_realloc(next->next->p, next->next->len);
  1391. tcplen = TCP_TCPLEN(next->next);
  1392. LWIP_ASSERT("tcp_receive: segment not trimmed correctly to rcv_wnd\n",
  1393. (seqno + tcplen) == (pcb->rcv_nxt + pcb->rcv_wnd));
  1394. }
  1395. }
  1396. break;
  1397. }
  1398. }
  1399. prev = next;
  1400. }
  1401. }
  1402. #endif /* TCP_QUEUE_OOSEQ */
  1403. }
  1404. } else {
  1405. /* The incoming segment is not withing the window. */
  1406. tcp_send_empty_ack(pcb);
  1407. }
  1408. } else {
  1409. /* Segments with length 0 is taken care of here. Segments that
  1410. fall out of the window are ACKed. */
  1411. /*if (TCP_SEQ_GT(pcb->rcv_nxt, seqno) ||
  1412. TCP_SEQ_GEQ(seqno, pcb->rcv_nxt + pcb->rcv_wnd)) {*/
  1413. if(!TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt, pcb->rcv_nxt + pcb->rcv_wnd-1)){
  1414. tcp_ack_now(pcb);//��Դ�˷���һ������ȷ�ϱ���
  1415. }
  1416. }
  1417. }
  1418. /**
  1419. * Parses the options contained in the incoming segment.
  1420. *
  1421. * Called from tcp_listen_input() and tcp_process().
  1422. * Currently, only the MSS option is supported!
  1423. *
  1424. * @param pcb the tcp_pcb for which a segment arrived
  1425. */
  1426. static void
  1427. tcp_parseopt(struct tcp_pcb *pcb)
  1428. {
  1429. u16_t c, max_c;
  1430. u16_t mss;
  1431. u8_t *opts, opt;
  1432. #if LWIP_TCP_TIMESTAMPS
  1433. u32_t tsval;
  1434. #endif
  1435. opts = (u8_t *)tcphdr + TCP_HLEN;
  1436. /* Parse the TCP MSS option, if present. */
  1437. if(TCPH_HDRLEN(tcphdr) > 0x5) {
  1438. max_c = (TCPH_HDRLEN(tcphdr) - 5) << 2;
  1439. for (c = 0; c < max_c; ) {
  1440. opt = opts[c];
  1441. switch (opt) {
  1442. case 0x00:
  1443. /* End of options. */
  1444. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: EOL\n"));
  1445. return;
  1446. case 0x01:
  1447. /* NOP option. */
  1448. ++c;
  1449. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: NOP\n"));
  1450. break;
  1451. case 0x02:
  1452. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: MSS\n"));
  1453. if (opts[c + 1] != 0x04 || c + 0x04 > max_c) {
  1454. /* Bad length */
  1455. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
  1456. return;
  1457. }
  1458. /* An MSS option with the right option length. */
  1459. mss = (opts[c + 2] << 8) | opts[c + 3];
  1460. /* Limit the mss to the configured TCP_MSS and prevent division by zero */
  1461. pcb->mss = ((mss > TCP_MSS) || (mss == 0)) ? TCP_MSS : mss;
  1462. /* Advance to next option */
  1463. c += 0x04;
  1464. break;
  1465. #if LWIP_TCP_TIMESTAMPS
  1466. case 0x08:
  1467. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: TS\n"));
  1468. if (opts[c + 1] != 0x0A || c + 0x0A > max_c) {
  1469. /* Bad length */
  1470. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
  1471. return;
  1472. }
  1473. /* TCP timestamp option with valid length */
  1474. tsval = (opts[c+2]) | (opts[c+3] << 8) |
  1475. (opts[c+4] << 16) | (opts[c+5] << 24);
  1476. if (flags & TCP_SYN) {
  1477. pcb->ts_recent = ntohl(tsval);
  1478. pcb->flags |= TF_TIMESTAMP;
  1479. } else if (TCP_SEQ_BETWEEN(pcb->ts_lastacksent, seqno, seqno+tcplen)) {
  1480. pcb->ts_recent = ntohl(tsval);
  1481. }
  1482. /* Advance to next option */
  1483. c += 0x0A;
  1484. break;
  1485. #endif
  1486. default:
  1487. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: other\n"));
  1488. if (opts[c + 1] == 0) {
  1489. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
  1490. /* If the length field is zero, the options are malformed
  1491. and we don't process them further. */
  1492. return;
  1493. }
  1494. /* All other options have a length field, so that we easily
  1495. can skip past them. */
  1496. c += opts[c + 1];
  1497. }
  1498. }
  1499. }
  1500. }
  1501. #endif /* LWIP_TCP */