tcp_out.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. /**
  2. * @file
  3. * Transmission Control Protocol, outgoing traffic
  4. *
  5. * The output functions of TCP.
  6. *
  7. */
  8. /*
  9. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. The name of the author may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  26. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  28. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  31. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  32. * OF SUCH DAMAGE.
  33. *
  34. * This file is part of the lwIP TCP/IP stack.
  35. *
  36. * Author: Adam Dunkels <adam@sics.se>
  37. *
  38. */
  39. #include "lwip/opt.h"
  40. #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
  41. #include "lwip/tcp_impl.h"
  42. #include "lwip/def.h"
  43. #include "lwip/mem.h"
  44. #include "lwip/memp.h"
  45. #include "lwip/sys.h"
  46. #include "lwip/ip_addr.h"
  47. #include "lwip/netif.h"
  48. #include "lwip/inet_chksum.h"
  49. #include "lwip/stats.h"
  50. #include "lwip/snmp.h"
  51. #include "netif/etharp.h"
  52. #include <string.h>
  53. #ifdef MEMLEAK_DEBUG
  54. static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
  55. #endif
  56. /* Define some copy-macros for checksum-on-copy so that the code looks
  57. nicer by preventing too many ifdef's. */
  58. #if TCP_CHECKSUM_ON_COPY
  59. #define TCP_DATA_COPY(dst, src, len, seg) do { \
  60. tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), \
  61. len, &seg->chksum, &seg->chksum_swapped); \
  62. seg->flags |= TF_SEG_DATA_CHECKSUMMED; } while(0)
  63. #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped) \
  64. tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), len, chksum, chksum_swapped);
  65. #else /* TCP_CHECKSUM_ON_COPY*/
  66. #define TCP_DATA_COPY(dst, src, len, seg) MEMCPY(dst, src, len)
  67. #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped) MEMCPY(dst, src, len)
  68. #endif /* TCP_CHECKSUM_ON_COPY*/
  69. /** Define this to 1 for an extra check that the output checksum is valid
  70. * (usefule when the checksum is generated by the application, not the stack) */
  71. #ifndef TCP_CHECKSUM_ON_COPY_SANITY_CHECK
  72. #define TCP_CHECKSUM_ON_COPY_SANITY_CHECK 0
  73. #endif
  74. /* Forward declarations.*/
  75. static void tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb);
  76. /** Allocate a pbuf and create a tcphdr at p->payload, used for output
  77. * functions other than the default tcp_output -> tcp_output_segment
  78. * (e.g. tcp_send_empty_ack, etc.)
  79. *
  80. * @param pcb tcp pcb for which to send a packet (used to initialize tcp_hdr)
  81. * @param optlen length of header-options
  82. * @param datalen length of tcp data to reserve in pbuf
  83. * @param seqno_be seqno in network byte order (big-endian)
  84. * @return pbuf with p->payload being the tcp_hdr
  85. */
  86. static struct pbuf *ICACHE_FLASH_ATTR
  87. tcp_output_alloc_header(struct tcp_pcb *pcb, u16_t optlen, u16_t datalen,
  88. u32_t seqno_be /* already in network byte order */)
  89. {
  90. struct tcp_hdr *tcphdr;
  91. struct pbuf *p = pbuf_alloc(PBUF_IP, TCP_HLEN + optlen + datalen, PBUF_RAM);
  92. if (p != NULL) {
  93. LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
  94. (p->len >= TCP_HLEN + optlen));
  95. tcphdr = (struct tcp_hdr *)p->payload;
  96. tcphdr->src = htons(pcb->local_port);
  97. tcphdr->dest = htons(pcb->remote_port);
  98. tcphdr->seqno = seqno_be;
  99. tcphdr->ackno = htonl(pcb->rcv_nxt);
  100. TCPH_HDRLEN_FLAGS_SET(tcphdr, (5 + optlen / 4), TCP_ACK);
  101. tcphdr->wnd = htons(pcb->rcv_ann_wnd);
  102. tcphdr->chksum = 0;
  103. tcphdr->urgp = 0;
  104. /* If we're sending a packet, update the announced right window edge */
  105. pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
  106. }
  107. return p;
  108. }
  109. /**
  110. * Called by tcp_close() to send a segment including FIN flag but not data.
  111. *
  112. * @param pcb the tcp_pcb over which to send a segment
  113. * @return ERR_OK if sent, another err_t otherwise
  114. */
  115. err_t
  116. tcp_send_fin(struct tcp_pcb *pcb)
  117. {
  118. /* first, try to add the fin to the last unsent segment */
  119. if (pcb->unsent != NULL) {
  120. struct tcp_seg *last_unsent;
  121. for (last_unsent = pcb->unsent; last_unsent->next != NULL;
  122. last_unsent = last_unsent->next);
  123. if ((TCPH_FLAGS(last_unsent->tcphdr) & (TCP_SYN | TCP_FIN | TCP_RST)) == 0) {
  124. /* no SYN/FIN/RST flag in the header, we can add the FIN flag */
  125. TCPH_SET_FLAG(last_unsent->tcphdr, TCP_FIN);
  126. return ERR_OK;
  127. }
  128. }
  129. /* no data, no length, flags, copy=1, no optdata */
  130. return tcp_enqueue_flags(pcb, TCP_FIN);
  131. }
  132. /**
  133. * Create a TCP segment with prefilled header.
  134. *
  135. * Called by tcp_write and tcp_enqueue_flags.
  136. *
  137. * @param pcb Protocol control block for the TCP connection.
  138. * @param p pbuf that is used to hold the TCP header.
  139. * @param flags TCP flags for header.
  140. * @param seqno TCP sequence number of this packet
  141. * @param optflags options to include in TCP header
  142. * @return a new tcp_seg pointing to p, or NULL.
  143. * The TCP header is filled in except ackno and wnd.
  144. * p is freed on failure.
  145. */
  146. static struct tcp_seg *ICACHE_FLASH_ATTR
  147. tcp_create_segment(struct tcp_pcb *pcb, struct pbuf *p, u8_t flags, u32_t seqno, u8_t optflags)
  148. {
  149. struct tcp_seg *seg;
  150. u8_t optlen = LWIP_TCP_OPT_LENGTH(optflags);
  151. if ((seg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG)) == NULL) {
  152. LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_create_segment: no memory.\n"));
  153. pbuf_free(p);
  154. return NULL;
  155. }
  156. seg->flags = optflags;
  157. seg->next = NULL;
  158. seg->p = p;
  159. seg->len = p->tot_len - optlen;
  160. #if TCP_OVERSIZE_DBGCHECK
  161. seg->oversize_left = 0;
  162. #endif /* TCP_OVERSIZE_DBGCHECK */
  163. #if TCP_CHECKSUM_ON_COPY
  164. seg->chksum = 0;
  165. seg->chksum_swapped = 0;
  166. /* check optflags */
  167. LWIP_ASSERT("invalid optflags passed: TF_SEG_DATA_CHECKSUMMED",
  168. (optflags & TF_SEG_DATA_CHECKSUMMED) == 0);
  169. #endif /* TCP_CHECKSUM_ON_COPY */
  170. /* build TCP header */
  171. if (pbuf_header(p, TCP_HLEN)) {
  172. LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_create_segment: no room for TCP header in pbuf.\n"));
  173. TCP_STATS_INC(tcp.err);
  174. tcp_seg_free(seg);
  175. return NULL;
  176. }
  177. seg->tcphdr = (struct tcp_hdr *)seg->p->payload;
  178. seg->tcphdr->src = htons(pcb->local_port);
  179. seg->tcphdr->dest = htons(pcb->remote_port);
  180. seg->tcphdr->seqno = htonl(seqno);
  181. /* ackno is set in tcp_output */
  182. TCPH_HDRLEN_FLAGS_SET(seg->tcphdr, (5 + optlen / 4), flags);
  183. /* wnd and chksum are set in tcp_output */
  184. seg->tcphdr->urgp = 0;
  185. return seg;
  186. }
  187. /**
  188. * Allocate a PBUF_RAM pbuf, perhaps with extra space at the end.
  189. *
  190. * This function is like pbuf_alloc(layer, length, PBUF_RAM) except
  191. * there may be extra bytes available at the end.
  192. *
  193. * @param layer flag to define header size.
  194. * @param length size of the pbuf's payload.
  195. * @param max_length maximum usable size of payload+oversize.
  196. * @param oversize pointer to a u16_t that will receive the number of usable tail bytes.
  197. * @param pcb The TCP connection that willo enqueue the pbuf.
  198. * @param apiflags API flags given to tcp_write.
  199. * @param first_seg true when this pbuf will be used in the first enqueued segment.
  200. * @param
  201. */
  202. #if TCP_OVERSIZE
  203. static struct pbuf *ICACHE_FLASH_ATTR
  204. tcp_pbuf_prealloc(pbuf_layer layer, u16_t length, u16_t max_length,
  205. u16_t *oversize, struct tcp_pcb *pcb, u8_t apiflags,
  206. u8_t first_seg)
  207. {
  208. struct pbuf *p;
  209. u16_t alloc = length;
  210. #if LWIP_NETIF_TX_SINGLE_PBUF
  211. LWIP_UNUSED_ARG(max_length);
  212. LWIP_UNUSED_ARG(pcb);
  213. LWIP_UNUSED_ARG(apiflags);
  214. LWIP_UNUSED_ARG(first_seg);
  215. /* always create MSS-sized pbufs */
  216. alloc = pcb->mss; //TCP_MSS;
  217. #else /* LWIP_NETIF_TX_SINGLE_PBUF */
  218. if (length < max_length) {
  219. /* Should we allocate an oversized pbuf, or just the minimum
  220. * length required? If tcp_write is going to be called again
  221. * before this segment is transmitted, we want the oversized
  222. * buffer. If the segment will be transmitted immediately, we can
  223. * save memory by allocating only length. We use a simple
  224. * heuristic based on the following information:
  225. *
  226. * Did the user set TCP_WRITE_FLAG_MORE?
  227. *
  228. * Will the Nagle algorithm defer transmission of this segment?
  229. */
  230. if ((apiflags & TCP_WRITE_FLAG_MORE) ||
  231. (!(pcb->flags & TF_NODELAY) &&
  232. (!first_seg ||
  233. pcb->unsent != NULL ||
  234. pcb->unacked != NULL))) {
  235. alloc = LWIP_MIN(max_length, LWIP_MEM_ALIGN_SIZE(length + TCP_OVERSIZE));
  236. }
  237. }
  238. #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
  239. p = pbuf_alloc(layer, alloc, PBUF_RAM);
  240. if (p == NULL) {
  241. return NULL;
  242. }
  243. LWIP_ASSERT("need unchained pbuf", p->next == NULL);
  244. *oversize = p->len - length;
  245. /* trim p->len to the currently used size */
  246. p->len = p->tot_len = length;
  247. return p;
  248. }
  249. #else /* TCP_OVERSIZE */
  250. #define tcp_pbuf_prealloc(layer, length, mx, os, pcb, api, fst) pbuf_alloc((layer), (length), PBUF_RAM)
  251. #endif /* TCP_OVERSIZE */
  252. #if TCP_CHECKSUM_ON_COPY
  253. /** Add a checksum of newly added data to the segment */
  254. static void ICACHE_FLASH_ATTR
  255. tcp_seg_add_chksum(u16_t chksum, u16_t len, u16_t *seg_chksum,
  256. u8_t *seg_chksum_swapped)
  257. {
  258. u32_t helper;
  259. /* add chksum to old chksum and fold to u16_t */
  260. helper = chksum + *seg_chksum;
  261. chksum = FOLD_U32T(helper);
  262. if ((len & 1) != 0) {
  263. *seg_chksum_swapped = 1 - *seg_chksum_swapped;
  264. chksum = SWAP_BYTES_IN_WORD(chksum);
  265. }
  266. *seg_chksum = chksum;
  267. }
  268. #endif /* TCP_CHECKSUM_ON_COPY */
  269. /** Checks if tcp_write is allowed or not (checks state, snd_buf and snd_queuelen).
  270. *
  271. * @param pcb the tcp pcb to check for
  272. * @param len length of data to send (checked agains snd_buf)
  273. * @return ERR_OK if tcp_write is allowed to proceed, another err_t otherwise
  274. */
  275. static err_t ICACHE_FLASH_ATTR
  276. tcp_write_checks(struct tcp_pcb *pcb, u16_t len)
  277. {
  278. /* connection is in invalid state for data transmission? */
  279. if ((pcb->state != ESTABLISHED) &&
  280. (pcb->state != CLOSE_WAIT) &&
  281. (pcb->state != SYN_SENT) &&
  282. (pcb->state != SYN_RCVD)) {
  283. LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_STATE | LWIP_DBG_LEVEL_SEVERE, ("tcp_write() called in invalid state\n"));
  284. return ERR_CONN;
  285. } else if (len == 0) {
  286. return ERR_OK;
  287. }
  288. /* fail on too much data */
  289. if (len > pcb->snd_buf) {
  290. LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_write: too much data (len=%"U16_F" > snd_buf=%"U16_F")\n",
  291. len, pcb->snd_buf));
  292. pcb->flags |= TF_NAGLEMEMERR;
  293. return ERR_MEM;
  294. }
  295. LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));
  296. /* If total number of pbufs on the unsent/unacked queues exceeds the
  297. * configured maximum, return an error */
  298. /* check for configured max queuelen and possible overflow */
  299. if ((pcb->snd_queuelen >= TCP_SND_QUEUELEN) || (pcb->snd_queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
  300. LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_write: too long queue %"U16_F" (max %"U16_F")\n",
  301. pcb->snd_queuelen, TCP_SND_QUEUELEN));
  302. TCP_STATS_INC(tcp.memerr);
  303. pcb->flags |= TF_NAGLEMEMERR;
  304. return ERR_MEM;
  305. }
  306. if (pcb->snd_queuelen != 0) {
  307. LWIP_ASSERT("tcp_write: pbufs on queue => at least one queue non-empty",
  308. pcb->unacked != NULL || pcb->unsent != NULL);
  309. } else {
  310. LWIP_ASSERT("tcp_write: no pbufs on queue => both queues empty",
  311. pcb->unacked == NULL && pcb->unsent == NULL);
  312. }
  313. return ERR_OK;
  314. }
  315. /**
  316. * Write data for sending (but does not send it immediately).
  317. *��������һ��������ݣ��ú�����һ�����Ķβ����ڿ��ƿ黺�������
  318. * It waits in the expectation of more data being sent soon (as
  319. * it can send them more efficiently by combining them together).
  320. * To prompt the system to send data now, call tcp_output() after
  321. * calling tcp_write().
  322. *
  323. * @param pcb Protocol control block for the TCP connection to enqueue data for.��Ӧ���ӿ��ƿ�
  324. * @param arg Pointer to the data to be enqueued for sending.���������ʼ��ַ
  325. * @param len Data length in bytes������ݳ���
  326. * @param apiflags combination of following flags :����Ƿ���п������Լ����Ķ��ײ��Ƿ������PSH��־
  327. * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack
  328. * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent,
  329. * @return ERR_OK if enqueued, another err_t on error
  330. */
  331. err_t
  332. tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags)
  333. {
  334. struct pbuf *concat_p = NULL;
  335. struct tcp_seg *last_unsent = NULL, *seg = NULL, *prev_seg = NULL, *queue = NULL;
  336. u16_t pos = 0; /* position in 'arg' data */
  337. u16_t queuelen;
  338. u8_t optlen = 0;
  339. u8_t optflags = 0;
  340. #if TCP_OVERSIZE
  341. u16_t oversize = 0;
  342. u16_t oversize_used = 0;
  343. #endif /* TCP_OVERSIZE */
  344. #if TCP_CHECKSUM_ON_COPY
  345. u16_t concat_chksum = 0;
  346. u8_t concat_chksum_swapped = 0;
  347. u16_t concat_chksummed = 0;
  348. #endif /* TCP_CHECKSUM_ON_COPY */
  349. err_t err;
  350. #if LWIP_NETIF_TX_SINGLE_PBUF
  351. /* Always copy to try to create single pbufs for TX */
  352. apiflags |= TCP_WRITE_FLAG_COPY;
  353. #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
  354. LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_write(pcb=%p, data=%p, len=%"U16_F", apiflags=%"U16_F")\n",
  355. (void *)pcb, arg, len, (u16_t)apiflags));
  356. LWIP_ERROR("tcp_write: arg == NULL (programmer violates API)",
  357. arg != NULL, return ERR_ARG;);
  358. err = tcp_write_checks(pcb, len);
  359. if (err != ERR_OK) {
  360. return err;
  361. }
  362. queuelen = pcb->snd_queuelen;
  363. #if LWIP_TCP_TIMESTAMPS
  364. if ((pcb->flags & TF_TIMESTAMP)) {
  365. optflags = TF_SEG_OPTS_TS;
  366. optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS);
  367. }
  368. #endif /* LWIP_TCP_TIMESTAMPS */
  369. /*
  370. * TCP segmentation is done in three phases with increasing complexity:
  371. *
  372. * 1. Copy data directly into an oversized pbuf.
  373. * 2. Chain a new pbuf to the end of pcb->unsent.
  374. * 3. Create new segments.
  375. *
  376. * We may run out of memory at any point. In that case we must
  377. * return ERR_MEM and not change anything in pcb. Therefore, all
  378. * changes are recorded in local variables and committed at the end
  379. * of the function. Some pcb fields are maintained in local copies:
  380. *
  381. * queuelen = pcb->snd_queuelen
  382. * oversize = pcb->unsent_oversize
  383. *
  384. * These variables are set consistently by the phases:
  385. *
  386. * seg points to the last segment tampered with.
  387. *
  388. * pos records progress as data is segmented.
  389. */
  390. /* Find the tail of the unsent queue. */
  391. if (pcb->unsent != NULL) {
  392. u16_t space = 0;
  393. u16_t unsent_optlen = 0;
  394. /* @todo: this could be sped up by keeping last_unsent in the pcb */
  395. for (last_unsent = pcb->unsent; last_unsent->next != NULL;
  396. last_unsent = last_unsent->next);
  397. /* Usable space at the end of the last unsent segment */
  398. unsent_optlen = LWIP_TCP_OPT_LENGTH(last_unsent->flags);
  399. space = pcb->mss - (last_unsent->len + unsent_optlen);
  400. /*
  401. * Phase 1: Copy data directly into an oversized pbuf.
  402. *
  403. * The number of bytes copied is recorded in the oversize_used
  404. * variable. The actual copying is done at the bottom of the
  405. * function.
  406. */
  407. #if TCP_OVERSIZE
  408. #if TCP_OVERSIZE_DBGCHECK
  409. /* check that pcb->unsent_oversize matches last_unsent->unsent_oversize */
  410. LWIP_ASSERT("unsent_oversize mismatch (pcb vs. last_unsent)",
  411. pcb->unsent_oversize == last_unsent->oversize_left);
  412. #endif /* TCP_OVERSIZE_DBGCHECK */
  413. oversize = pcb->unsent_oversize;
  414. if (oversize > 0) {
  415. LWIP_ASSERT("inconsistent oversize vs. space", oversize_used <= space);
  416. seg = last_unsent;
  417. oversize_used = oversize < len ? oversize : len;
  418. pos += oversize_used;
  419. oversize -= oversize_used;
  420. space -= oversize_used;
  421. }
  422. /* now we are either finished or oversize is zero */
  423. LWIP_ASSERT("inconsistend oversize vs. len", (oversize == 0) || (pos == len));
  424. #endif /* TCP_OVERSIZE */
  425. if (pos > len) {
  426. return ERR_MEM;
  427. }
  428. #if !LWIP_NETIF_TX_SINGLE_PBUF
  429. /*
  430. * Phase 2: Chain a new pbuf to the end of pcb->unsent.
  431. *
  432. * We don't extend segments containing SYN/FIN flags or options
  433. * (len==0). The new pbuf is kept in concat_p and pbuf_cat'ed at
  434. * the end.
  435. */
  436. if ((pos < len) && (space > 0) && (last_unsent->len > 0)) {
  437. u16_t seglen = space < len - pos ? space : len - pos;
  438. seg = last_unsent;
  439. /* Create a pbuf with a copy or reference to seglen bytes. We
  440. * can use PBUF_RAW here since the data appears in the middle of
  441. * a segment. A header will never be prepended. */
  442. if (apiflags & TCP_WRITE_FLAG_COPY) {
  443. /* Data is copied */
  444. if ((concat_p = tcp_pbuf_prealloc(PBUF_RAW, seglen, space, &oversize, pcb, apiflags, 1)) == NULL) {
  445. LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
  446. ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n",
  447. seglen));
  448. goto memerr;
  449. }
  450. #if TCP_OVERSIZE_DBGCHECK
  451. last_unsent->oversize_left = oversize;
  452. #endif /* TCP_OVERSIZE_DBGCHECK */
  453. TCP_DATA_COPY2(concat_p->payload, (u8_t*)arg + pos, seglen, &concat_chksum, &concat_chksum_swapped);
  454. #if TCP_CHECKSUM_ON_COPY
  455. concat_chksummed += seglen;
  456. #endif /* TCP_CHECKSUM_ON_COPY */
  457. } else {
  458. /* Data is not copied */
  459. if ((concat_p = pbuf_alloc(PBUF_RAW, seglen, PBUF_ROM)) == NULL) {
  460. LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
  461. ("tcp_write: could not allocate memory for zero-copy pbuf\n"));
  462. goto memerr;
  463. }
  464. #if TCP_CHECKSUM_ON_COPY
  465. /* calculate the checksum of nocopy-data */
  466. tcp_seg_add_chksum(~inet_chksum((u8_t*)arg + pos, seglen), seglen,
  467. &concat_chksum, &concat_chksum_swapped);
  468. concat_chksummed += seglen;
  469. #endif /* TCP_CHECKSUM_ON_COPY */
  470. /* reference the non-volatile payload data */
  471. concat_p->payload = (u8_t*)arg + pos;
  472. }
  473. pos += seglen;
  474. queuelen += pbuf_clen(concat_p);
  475. }
  476. #endif /* !LWIP_NETIF_TX_SINGLE_PBUF */
  477. } else {
  478. #if TCP_OVERSIZE
  479. LWIP_ASSERT("unsent_oversize mismatch (pcb->unsent is NULL)",
  480. pcb->unsent_oversize == 0);
  481. #endif /* TCP_OVERSIZE */
  482. }
  483. /*
  484. * Phase 3: Create new segments.
  485. *
  486. * The new segments are chained together in the local 'queue'
  487. * variable, ready to be appended to pcb->unsent.
  488. */
  489. while (pos < len) {
  490. struct pbuf *p;
  491. u16_t left = len - pos;
  492. u16_t max_len = pcb->mss - optlen;
  493. u16_t seglen = left > max_len ? max_len : left;
  494. #if TCP_CHECKSUM_ON_COPY
  495. u16_t chksum = 0;
  496. u8_t chksum_swapped = 0;
  497. #endif /* TCP_CHECKSUM_ON_COPY */
  498. if (apiflags & TCP_WRITE_FLAG_COPY) {
  499. /* If copy is set, memory should be allocated and data copied
  500. * into pbuf */
  501. if ((p = tcp_pbuf_prealloc(PBUF_TRANSPORT, seglen + optlen, pcb->mss, &oversize, pcb, apiflags, queue == NULL)) == NULL) {
  502. LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n", seglen));
  503. goto memerr;
  504. }
  505. LWIP_ASSERT("tcp_write: check that first pbuf can hold the complete seglen",
  506. (p->len >= seglen));
  507. TCP_DATA_COPY2((char *)p->payload + optlen, (u8_t*)arg + pos, seglen, &chksum, &chksum_swapped);
  508. } else {
  509. /* Copy is not set: First allocate a pbuf for holding the data.
  510. * Since the referenced data is available at least until it is
  511. * sent out on the link (as it has to be ACKed by the remote
  512. * party) we can safely use PBUF_ROM instead of PBUF_REF here.
  513. */
  514. struct pbuf *p2;
  515. #if TCP_OVERSIZE
  516. LWIP_ASSERT("oversize == 0", oversize == 0);
  517. #endif /* TCP_OVERSIZE */
  518. if ((p2 = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_ROM)) == NULL) {
  519. LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: could not allocate memory for zero-copy pbuf\n"));
  520. goto memerr;
  521. }
  522. #if TCP_CHECKSUM_ON_COPY
  523. /* calculate the checksum of nocopy-data */
  524. chksum = ~inet_chksum((u8_t*)arg + pos, seglen);
  525. #endif /* TCP_CHECKSUM_ON_COPY */
  526. /* reference the non-volatile payload data */
  527. p2->payload = (u8_t*)arg + pos;
  528. /* Second, allocate a pbuf for the headers. */
  529. if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
  530. /* If allocation fails, we have to deallocate the data pbuf as
  531. * well. */
  532. pbuf_free(p2);
  533. LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: could not allocate memory for header pbuf\n"));
  534. goto memerr;
  535. }
  536. /* Concatenate the headers and data pbufs together. */
  537. pbuf_cat(p/*header*/, p2/*data*/);
  538. }
  539. queuelen += pbuf_clen(p);
  540. /* Now that there are more segments queued, we check again if the
  541. * length of the queue exceeds the configured maximum or
  542. * overflows. */
  543. if ((queuelen > TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
  544. LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: queue too long %"U16_F" (%"U16_F")\n", queuelen, TCP_SND_QUEUELEN));
  545. pbuf_free(p);
  546. goto memerr;
  547. }
  548. if ((seg = tcp_create_segment(pcb, p, 0, pcb->snd_lbb + pos, optflags)) == NULL) {
  549. goto memerr;
  550. }
  551. #if TCP_OVERSIZE_DBGCHECK
  552. seg->oversize_left = oversize;
  553. #endif /* TCP_OVERSIZE_DBGCHECK */
  554. #if TCP_CHECKSUM_ON_COPY
  555. seg->chksum = chksum;
  556. seg->chksum_swapped = chksum_swapped;
  557. seg->flags |= TF_SEG_DATA_CHECKSUMMED;
  558. #endif /* TCP_CHECKSUM_ON_COPY */
  559. /* first segment of to-be-queued data? */
  560. if (queue == NULL) {
  561. queue = seg;
  562. } else {
  563. /* Attach the segment to the end of the queued segments */
  564. LWIP_ASSERT("prev_seg != NULL", prev_seg != NULL);
  565. prev_seg->next = seg;
  566. }
  567. /* remember last segment of to-be-queued data for next iteration */
  568. prev_seg = seg;
  569. LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE, ("tcp_write: queueing %"U32_F":%"U32_F"\n",
  570. ntohl(seg->tcphdr->seqno),
  571. ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg)));
  572. pos += seglen;
  573. }
  574. /*
  575. * All three segmentation phases were successful. We can commit the
  576. * transaction.
  577. */
  578. /*
  579. * Phase 1: If data has been added to the preallocated tail of
  580. * last_unsent, we update the length fields of the pbuf chain.
  581. */
  582. #if TCP_OVERSIZE
  583. if (oversize_used > 0) {
  584. struct pbuf *p;
  585. /* Bump tot_len of whole chain, len of tail */
  586. for (p = last_unsent->p; p; p = p->next) {
  587. p->tot_len += oversize_used;
  588. if (p->next == NULL) {
  589. TCP_DATA_COPY((char *)p->payload + p->len, arg, oversize_used, last_unsent);
  590. p->len += oversize_used;
  591. }
  592. }
  593. last_unsent->len += oversize_used;
  594. #if TCP_OVERSIZE_DBGCHECK
  595. last_unsent->oversize_left -= oversize_used;
  596. #endif /* TCP_OVERSIZE_DBGCHECK */
  597. }
  598. pcb->unsent_oversize = oversize;
  599. #endif /* TCP_OVERSIZE */
  600. /*
  601. * Phase 2: concat_p can be concatenated onto last_unsent->p
  602. */
  603. if (concat_p != NULL) {
  604. LWIP_ASSERT("tcp_write: cannot concatenate when pcb->unsent is empty",
  605. (last_unsent != NULL));
  606. pbuf_cat(last_unsent->p, concat_p);
  607. last_unsent->len += concat_p->tot_len;
  608. #if TCP_CHECKSUM_ON_COPY
  609. if (concat_chksummed) {
  610. tcp_seg_add_chksum(concat_chksum, concat_chksummed, &last_unsent->chksum,
  611. &last_unsent->chksum_swapped);
  612. last_unsent->flags |= TF_SEG_DATA_CHECKSUMMED;
  613. }
  614. #endif /* TCP_CHECKSUM_ON_COPY */
  615. }
  616. /*
  617. * Phase 3: Append queue to pcb->unsent. Queue may be NULL, but that
  618. * is harmless
  619. */
  620. if (last_unsent == NULL) {
  621. pcb->unsent = queue;
  622. } else {
  623. last_unsent->next = queue;
  624. }
  625. /*
  626. * Finally update the pcb state.
  627. */
  628. pcb->snd_lbb += len;
  629. pcb->snd_buf -= len;
  630. pcb->snd_queuelen = queuelen;
  631. LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: %"S16_F" (after enqueued)\n",
  632. pcb->snd_queuelen));
  633. if (pcb->snd_queuelen != 0) {
  634. LWIP_ASSERT("tcp_write: valid queue length",
  635. pcb->unacked != NULL || pcb->unsent != NULL);
  636. }
  637. /* Set the PSH flag in the last segment that we enqueued. */
  638. if (seg != NULL && seg->tcphdr != NULL && ((apiflags & TCP_WRITE_FLAG_MORE)==0)) {
  639. TCPH_SET_FLAG(seg->tcphdr, TCP_PSH);
  640. }
  641. return ERR_OK;
  642. memerr:
  643. pcb->flags |= TF_NAGLEMEMERR;
  644. TCP_STATS_INC(tcp.memerr);
  645. if (concat_p != NULL) {
  646. pbuf_free(concat_p);
  647. }
  648. if (queue != NULL) {
  649. tcp_segs_free(queue);
  650. }
  651. if (pcb->snd_queuelen != 0) {
  652. LWIP_ASSERT("tcp_write: valid queue length", pcb->unacked != NULL ||
  653. pcb->unsent != NULL);
  654. }
  655. LWIP_DEBUGF(TCP_QLEN_DEBUG | LWIP_DBG_STATE, ("tcp_write: %"S16_F" (with mem err)\n", pcb->snd_queuelen));
  656. return ERR_MEM;
  657. }
  658. /**
  659. * Enqueue TCP options for transmission.
  660. *
  661. * Called by tcp_connect(), tcp_listen_input(), and tcp_send_ctrl().
  662. *
  663. * @param pcb Protocol control block for the TCP connection.
  664. * @param flags TCP header flags to set in the outgoing segment.
  665. * @param optdata pointer to TCP options, or NULL.
  666. * @param optlen length of TCP options in bytes.
  667. */
  668. err_t
  669. tcp_enqueue_flags(struct tcp_pcb *pcb, u8_t flags)
  670. {
  671. struct pbuf *p;
  672. struct tcp_seg *seg;
  673. u8_t optflags = 0;
  674. u8_t optlen = 0;
  675. LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));
  676. LWIP_ASSERT("tcp_enqueue_flags: need either TCP_SYN or TCP_FIN in flags (programmer violates API)",
  677. (flags & (TCP_SYN | TCP_FIN)) != 0);
  678. /* check for configured max queuelen and possible overflow */
  679. if ((pcb->snd_queuelen >= TCP_SND_QUEUELEN) || (pcb->snd_queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
  680. LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue_flags: too long queue %"U16_F" (max %"U16_F")\n",
  681. pcb->snd_queuelen, TCP_SND_QUEUELEN));
  682. TCP_STATS_INC(tcp.memerr);
  683. pcb->flags |= TF_NAGLEMEMERR;
  684. return ERR_MEM;
  685. }
  686. if (flags & TCP_SYN) {
  687. optflags = TF_SEG_OPTS_MSS;
  688. }
  689. #if LWIP_TCP_TIMESTAMPS
  690. if ((pcb->flags & TF_TIMESTAMP)) {
  691. optflags |= TF_SEG_OPTS_TS;
  692. }
  693. #endif /* LWIP_TCP_TIMESTAMPS */
  694. optlen = LWIP_TCP_OPT_LENGTH(optflags);
  695. /* tcp_enqueue_flags is always called with either SYN or FIN in flags.
  696. * We need one available snd_buf byte to do that.
  697. * This means we can't send FIN while snd_buf==0. A better fix would be to
  698. * not include SYN and FIN sequence numbers in the snd_buf count. */
  699. if (pcb->snd_buf == 0) {
  700. LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue_flags: no send buffer available\n"));
  701. TCP_STATS_INC(tcp.memerr);
  702. return ERR_MEM;
  703. }
  704. /* Allocate pbuf with room for TCP header + options */
  705. if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
  706. pcb->flags |= TF_NAGLEMEMERR;
  707. TCP_STATS_INC(tcp.memerr);
  708. return ERR_MEM;
  709. }
  710. LWIP_ASSERT("tcp_enqueue_flags: check that first pbuf can hold optlen",
  711. (p->len >= optlen));
  712. /* Allocate memory for tcp_seg, and fill in fields. */
  713. if ((seg = tcp_create_segment(pcb, p, flags, pcb->snd_lbb, optflags)) == NULL) {
  714. pcb->flags |= TF_NAGLEMEMERR;
  715. TCP_STATS_INC(tcp.memerr);
  716. return ERR_MEM;
  717. }
  718. LWIP_ASSERT("seg->tcphdr not aligned", ((mem_ptr_t)seg->tcphdr % MEM_ALIGNMENT) == 0);
  719. LWIP_ASSERT("tcp_enqueue_flags: invalid segment length", seg->len == 0);
  720. LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE,
  721. ("tcp_enqueue_flags: queueing %"U32_F":%"U32_F" (0x%"X16_F")\n",
  722. ntohl(seg->tcphdr->seqno),
  723. ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg),
  724. (u16_t)flags));
  725. /* Now append seg to pcb->unsent queue */
  726. if (pcb->unsent == NULL) {
  727. pcb->unsent = seg;
  728. } else {
  729. struct tcp_seg *useg;
  730. for (useg = pcb->unsent; useg->next != NULL; useg = useg->next);
  731. useg->next = seg;
  732. }
  733. #if TCP_OVERSIZE
  734. /* The new unsent tail has no space */
  735. pcb->unsent_oversize = 0;
  736. #endif /* TCP_OVERSIZE */
  737. /* SYN and FIN bump the sequence number */
  738. if ((flags & TCP_SYN) || (flags & TCP_FIN)) {
  739. pcb->snd_lbb++;
  740. /* optlen does not influence snd_buf */
  741. pcb->snd_buf--;
  742. }
  743. if (flags & TCP_FIN) {
  744. pcb->flags |= TF_FIN;
  745. }
  746. /* update number of segments on the queues */
  747. pcb->snd_queuelen += pbuf_clen(seg->p);
  748. LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: %"S16_F" (after enqueued)\n", pcb->snd_queuelen));
  749. if (pcb->snd_queuelen != 0) {
  750. LWIP_ASSERT("tcp_enqueue_flags: invalid queue length",
  751. pcb->unacked != NULL || pcb->unsent != NULL);
  752. }
  753. return ERR_OK;
  754. }
  755. #if LWIP_TCP_TIMESTAMPS
  756. /* Build a timestamp option (12 bytes long) at the specified options pointer)
  757. *
  758. * @param pcb tcp_pcb
  759. * @param opts option pointer where to store the timestamp option
  760. */
  761. static void ICACHE_FLASH_ATTR
  762. tcp_build_timestamp_option(struct tcp_pcb *pcb, u32_t *opts)
  763. {
  764. /* Pad with two NOP options to make everything nicely aligned */
  765. opts[0] = PP_HTONL(0x0101080A);
  766. opts[1] = htonl(sys_now());
  767. opts[2] = htonl(pcb->ts_recent);
  768. }
  769. #endif
  770. /** Send an ACK without data.
  771. *
  772. * @param pcb Protocol control block for the TCP connection to send the ACK
  773. */
  774. err_t
  775. tcp_send_empty_ack(struct tcp_pcb *pcb)
  776. {
  777. struct pbuf *p;
  778. struct tcp_hdr *tcphdr;
  779. u8_t optlen = 0;
  780. #if LWIP_TCP_TIMESTAMPS
  781. if (pcb->flags & TF_TIMESTAMP) {
  782. optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS);
  783. }
  784. #endif
  785. p = tcp_output_alloc_header(pcb, optlen, 0, htonl(pcb->snd_nxt));
  786. if (p == NULL) {
  787. LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: (ACK) could not allocate pbuf\n"));
  788. return ERR_BUF;
  789. }
  790. tcphdr = (struct tcp_hdr *)p->payload;
  791. LWIP_DEBUGF(TCP_OUTPUT_DEBUG,
  792. ("tcp_output: sending ACK for %"U32_F"\n", pcb->rcv_nxt));
  793. /* remove ACK flags from the PCB, as we send an empty ACK now */
  794. pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
  795. /* NB. MSS option is only sent on SYNs, so ignore it here */
  796. #if LWIP_TCP_TIMESTAMPS
  797. pcb->ts_lastacksent = pcb->rcv_nxt;
  798. if (pcb->flags & TF_TIMESTAMP) {
  799. tcp_build_timestamp_option(pcb, (u32_t *)(tcphdr + 1));
  800. }
  801. #endif
  802. #if CHECKSUM_GEN_TCP
  803. tcphdr->chksum = inet_chksum_pseudo(p, &(pcb->local_ip), &(pcb->remote_ip),
  804. IP_PROTO_TCP, p->tot_len);
  805. #endif
  806. #if LWIP_NETIF_HWADDRHINT
  807. ip_output_hinted(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
  808. IP_PROTO_TCP, &(pcb->addr_hint));
  809. #else /* LWIP_NETIF_HWADDRHINT*/
  810. ip_output(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
  811. IP_PROTO_TCP);
  812. #endif /* LWIP_NETIF_HWADDRHINT*/
  813. pbuf_free(p);
  814. return ERR_OK;
  815. }
  816. /**
  817. * Find out what we can send and send it
  818. *���Ϳ��ƿ黺����еı��Ķ�
  819. * @param pcb Protocol control block for the TCP connection to send data
  820. * @return ERR_OK if data has been sent or nothing to send
  821. * another err_t on error
  822. */
  823. err_t ICACHE_FLASH_ATTR
  824. tcp_output(struct tcp_pcb *pcb)
  825. {
  826. struct tcp_seg *seg, *useg;
  827. u32_t wnd, snd_nxt;
  828. #if TCP_CWND_DEBUG
  829. s16_t i = 0;
  830. #endif /* TCP_CWND_DEBUG */
  831. /* First, check if we are invoked by the TCP input processing
  832. code. If so, we do not output anything. Instead, we rely on the
  833. input processing code to call us when input processing is done
  834. with. �����ƿ鵱ǰ������ݱ����?ֱ�ӷ���*/
  835. if (tcp_input_pcb == pcb) {
  836. return ERR_OK;
  837. }
  838. wnd = LWIP_MIN(pcb->snd_wnd, pcb->cwnd);//�ӷ��ʹ��ں������ȡС�ߵõ���Ч���ʹ���
  839. seg = pcb->unsent;
  840. /* If the TF_ACK_NOW flag is set and no data will be sent (either
  841. * because the ->unsent queue is empty or because the window does
  842. * not allow it), construct an empty ACK segment and send it.
  843. *
  844. * If data is to be sent, we will just piggyback the ACK (see below).
  845. */
  846. if (pcb->flags & TF_ACK_NOW &&
  847. (seg == NULL ||
  848. ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd)) {
  849. return tcp_send_empty_ack(pcb);//����ֻ��ACK�ı��Ķ�
  850. }
  851. /* useg should point to last segment on unacked queue */
  852. useg = pcb->unacked;
  853. if (useg != NULL) {
  854. for (; useg->next != NULL; useg = useg->next);//�õ�β��
  855. }
  856. #if TCP_OUTPUT_DEBUG
  857. if (seg == NULL) {
  858. LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: nothing to send (%p)\n",
  859. (void*)pcb->unsent));
  860. }
  861. #endif /* TCP_OUTPUT_DEBUG */
  862. #if TCP_CWND_DEBUG
  863. if (seg == NULL) {
  864. LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"U16_F
  865. ", cwnd %"U16_F", wnd %"U32_F
  866. ", seg == NULL, ack %"U32_F"\n",
  867. pcb->snd_wnd, pcb->cwnd, wnd, pcb->lastack));
  868. } else {
  869. LWIP_DEBUGF(TCP_CWND_DEBUG,
  870. ("tcp_output: snd_wnd %"U16_F", cwnd %"U16_F", wnd %"U32_F
  871. ", effwnd %"U32_F", seq %"U32_F", ack %"U32_F"\n",
  872. pcb->snd_wnd, pcb->cwnd, wnd,
  873. ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len,
  874. ntohl(seg->tcphdr->seqno), pcb->lastack));
  875. }
  876. #endif /* TCP_CWND_DEBUG */
  877. /* data available and window allows it to be sent?
  878. *��ǰ��Ч�������?�ķ��ͣ�ѭ�����ͱ��ģ�ֱ�������*/
  879. while (seg != NULL &&
  880. ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd
  881. && (seg->p->ref<2) ) {
  882. LWIP_ASSERT("RST not expected here!",
  883. (TCPH_FLAGS(seg->tcphdr) & TCP_RST) == 0);
  884. /* Stop sending if the nagle algorithm would prevent it
  885. * Don't stop:
  886. * - if tcp_write had a memory error before (prevent delayed ACK timeout) or
  887. * - if FIN was already enqueued for this PCB (SYN is always alone in a segment -
  888. * either seg->next != NULL or pcb->unacked == NULL;
  889. * RST is no sent using tcp_write/tcp_output.
  890. */
  891. if((tcp_do_output_nagle(pcb) == 0) &&
  892. ((pcb->flags & (TF_NAGLEMEMERR | TF_FIN)) == 0)){
  893. break;
  894. }
  895. #if TCP_CWND_DEBUG
  896. LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"U16_F", cwnd %"U16_F", wnd %"U32_F", effwnd %"U32_F", seq %"U32_F", ack %"U32_F", i %"S16_F"\n",
  897. pcb->snd_wnd, pcb->cwnd, wnd,
  898. ntohl(seg->tcphdr->seqno) + seg->len -
  899. pcb->lastack,
  900. ntohl(seg->tcphdr->seqno), pcb->lastack, i));
  901. ++i;
  902. #endif /* TCP_CWND_DEBUG */
  903. pcb->unsent = seg->next;
  904. if (pcb->state != SYN_SENT) {
  905. TCPH_SET_FLAG(seg->tcphdr, TCP_ACK);//��д�ײ�ACK��־
  906. pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);//����־λ
  907. }
  908. tcp_output_segment(seg, pcb);//���ú����ͱ��Ķ�
  909. snd_nxt = ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg);//����snd_nxt
  910. if (TCP_SEQ_LT(pcb->snd_nxt, snd_nxt)) {
  911. pcb->snd_nxt = snd_nxt;//����Ҫ���͵���ݱ��
  912. }
  913. /* put segment on unacknowledged list if length > 0
  914. */
  915. if (TCP_TCPLEN(seg) > 0) {
  916. seg->next = NULL;
  917. /* unacked list is empty? ֱ�ӹҽ�*/
  918. if (pcb->unacked == NULL) {
  919. pcb->unacked = seg;
  920. useg = seg;
  921. /* unacked list is not empty?����ǰ���İ�˳����֯�ڶ����� */
  922. } else {
  923. /* In the case of fast retransmit, the packet should not go to the tail
  924. * of the unacked queue, but rather somewhere before it. We need to check for
  925. * this case. -STJ Jul 27, 2004 */ //���ǰ���ĵ����кŵ��ڶ���β���������кţ�
  926. //�Ӷ����ײ���ʼ
  927. if (TCP_SEQ_LT(ntohl(seg->tcphdr->seqno), ntohl(useg->tcphdr->seqno))) {
  928. /* add segment to before tail of unacked list, keeping the list sorted */
  929. struct tcp_seg **cur_seg = &(pcb->unacked);
  930. while (*cur_seg &&
  931. TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno), ntohl(seg->tcphdr->seqno))) {
  932. cur_seg = &((*cur_seg)->next );
  933. }
  934. seg->next = (*cur_seg);
  935. (*cur_seg) = seg;
  936. } else {//���������ߣ������δȷ�϶���ĩβ
  937. /* add segment to tail of unacked list */
  938. useg->next = seg;
  939. useg = useg->next;
  940. }
  941. }
  942. /* do not queue empty segments on the unacked list */
  943. } else {//���Ķγ���Ϊ0��ֱ��ɾ�������ش�
  944. tcp_seg_free(seg);
  945. }
  946. seg = pcb->unsent;//������һ�����Ķ�
  947. }
  948. #if TCP_OVERSIZE
  949. if (pcb->unsent == NULL) {
  950. /* last unsent has been removed, reset unsent_oversize */
  951. pcb->unsent_oversize = 0;
  952. }
  953. #endif /* TCP_OVERSIZE */
  954. //���ʹ��������±��IJ��ܷ��ͣ��������㴰��̽�⡣
  955. if (seg != NULL && pcb->persist_backoff == 0 &&
  956. ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > pcb->snd_wnd) {
  957. /* prepare for persist timer */
  958. pcb->persist_cnt = 0;
  959. pcb->persist_backoff = 1;
  960. }
  961. pcb->flags &= ~TF_NAGLEMEMERR;//���ڴ�����־
  962. return ERR_OK;
  963. }
  964. /**
  965. * Called by tcp_output() to actually send a TCP segment over IP.
  966. *
  967. * @param seg the tcp_seg to send
  968. * @param pcb the tcp_pcb for the TCP connection used to send the segment
  969. */
  970. static void
  971. tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb)
  972. {
  973. u16_t len;
  974. struct netif *netif;
  975. u32_t *opts;
  976. /** @bug Exclude retransmitted segments from this count. */
  977. snmp_inc_tcpoutsegs();
  978. /* The TCP header has already been constructed, but the ackno and
  979. wnd fields remain. */
  980. seg->tcphdr->ackno = htonl(pcb->rcv_nxt);
  981. /* advertise our receive window size in this TCP segment */
  982. seg->tcphdr->wnd = htons(pcb->rcv_ann_wnd);
  983. pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
  984. /* Add any requested options. NB MSS option is only set on SYN
  985. packets, so ignore it here */
  986. LWIP_ASSERT("seg->tcphdr not aligned", ((mem_ptr_t)seg->tcphdr % MEM_ALIGNMENT) == 0);
  987. opts = (u32_t *)(void *)(seg->tcphdr + 1);
  988. if (seg->flags & TF_SEG_OPTS_MSS) {
  989. TCP_BUILD_MSS_OPTION(*opts);
  990. opts += 1;
  991. }
  992. #if LWIP_TCP_TIMESTAMPS
  993. pcb->ts_lastacksent = pcb->rcv_nxt;
  994. if (seg->flags & TF_SEG_OPTS_TS) {
  995. tcp_build_timestamp_option(pcb, opts);
  996. opts += 3;
  997. }
  998. #endif
  999. /* Set retransmission timer running if it is not currently enabled
  1000. This must be set before checking the route. modify by ives at 2014.4.24*/
  1001. if (pcb->rtime == -1) {
  1002. pcb->rtime = 0;
  1003. }
  1004. /* If we don't have a local IP address, we get one by
  1005. calling ip_route(). */
  1006. if (ip_addr_isany(&(pcb->local_ip))) {
  1007. netif = ip_route(&(pcb->remote_ip));
  1008. if (netif == NULL) {
  1009. return;
  1010. }
  1011. ip_addr_copy(pcb->local_ip, netif->ip_addr);
  1012. }
  1013. if (pcb->rttest == 0) {
  1014. pcb->rttest = tcp_ticks;
  1015. pcb->rtseq = ntohl(seg->tcphdr->seqno);
  1016. LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_output_segment: rtseq %"U32_F"\n", pcb->rtseq));
  1017. }
  1018. LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: %"U32_F":%"U32_F"\n",
  1019. htonl(seg->tcphdr->seqno), htonl(seg->tcphdr->seqno) +
  1020. seg->len));
  1021. len = (u16_t)((u8_t *)seg->tcphdr - (u8_t *)seg->p->payload);
  1022. seg->p->len -= len;
  1023. seg->p->tot_len -= len;
  1024. seg->p->payload = seg->tcphdr;
  1025. seg->tcphdr->chksum = 0;
  1026. #if CHECKSUM_GEN_TCP
  1027. #if TCP_CHECKSUM_ON_COPY
  1028. {
  1029. u32_t acc;
  1030. #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
  1031. u16_t chksum_slow = inet_chksum_pseudo(seg->p, &(pcb->local_ip),
  1032. &(pcb->remote_ip),
  1033. IP_PROTO_TCP, seg->p->tot_len);
  1034. #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */
  1035. if ((seg->flags & TF_SEG_DATA_CHECKSUMMED) == 0) {
  1036. LWIP_ASSERT("data included but not checksummed",
  1037. seg->p->tot_len == (TCPH_HDRLEN(seg->tcphdr) * 4));
  1038. }
  1039. /* rebuild TCP header checksum (TCP header changes for retransmissions!) */
  1040. acc = inet_chksum_pseudo_partial(seg->p, &(pcb->local_ip),
  1041. &(pcb->remote_ip),
  1042. IP_PROTO_TCP, seg->p->tot_len, TCPH_HDRLEN(seg->tcphdr) * 4);
  1043. /* add payload checksum */
  1044. if (seg->chksum_swapped) {
  1045. seg->chksum = SWAP_BYTES_IN_WORD(seg->chksum);
  1046. seg->chksum_swapped = 0;
  1047. }
  1048. acc += (u16_t)~(seg->chksum);
  1049. seg->tcphdr->chksum = FOLD_U32T(acc);
  1050. #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
  1051. if (chksum_slow != seg->tcphdr->chksum) {
  1052. LWIP_DEBUGF(TCP_DEBUG | LWIP_DBG_LEVEL_WARNING,
  1053. ("tcp_output_segment: calculated checksum is %"X16_F" instead of %"X16_F"\n",
  1054. seg->tcphdr->chksum, chksum_slow));
  1055. seg->tcphdr->chksum = chksum_slow;
  1056. }
  1057. #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */
  1058. }
  1059. #else /* TCP_CHECKSUM_ON_COPY */
  1060. seg->tcphdr->chksum = inet_chksum_pseudo(seg->p, &(pcb->local_ip),
  1061. &(pcb->remote_ip),
  1062. IP_PROTO_TCP, seg->p->tot_len);
  1063. #endif /* TCP_CHECKSUM_ON_COPY */
  1064. #endif /* CHECKSUM_GEN_TCP */
  1065. TCP_STATS_INC(tcp.xmit);
  1066. #if LWIP_NETIF_HWADDRHINT
  1067. ip_output_hinted(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
  1068. IP_PROTO_TCP, &(pcb->addr_hint));
  1069. #else /* LWIP_NETIF_HWADDRHINT*/
  1070. ip_output(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
  1071. IP_PROTO_TCP);
  1072. #endif /* LWIP_NETIF_HWADDRHINT*/
  1073. }
  1074. /**
  1075. * Send a TCP RESET packet (empty segment with RST flag set) either to
  1076. * abort a connection or to show that there is no matching local connection
  1077. * for a received segment.
  1078. *
  1079. * Called by tcp_abort() (to abort a local connection), tcp_input() (if no
  1080. * matching local pcb was found), tcp_listen_input() (if incoming segment
  1081. * has ACK flag set) and tcp_process() (received segment in the wrong state)
  1082. *
  1083. * Since a RST segment is in most cases not sent for an active connection,
  1084. * tcp_rst() has a number of arguments that are taken from a tcp_pcb for
  1085. * most other segment output functions.
  1086. *
  1087. * @param seqno the sequence number to use for the outgoing segment
  1088. * @param ackno the acknowledge number to use for the outgoing segment
  1089. * @param local_ip the local IP address to send the segment from
  1090. * @param remote_ip the remote IP address to send the segment to
  1091. * @param local_port the local TCP port to send the segment from
  1092. * @param remote_port the remote TCP port to send the segment to
  1093. */
  1094. void
  1095. tcp_rst(u32_t seqno, u32_t ackno,
  1096. ip_addr_t *local_ip, ip_addr_t *remote_ip,
  1097. u16_t local_port, u16_t remote_port)
  1098. {
  1099. struct pbuf *p;
  1100. struct tcp_hdr *tcphdr;
  1101. p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
  1102. if (p == NULL) {
  1103. LWIP_DEBUGF(TCP_DEBUG, ("tcp_rst: could not allocate memory for pbuf\n"));
  1104. return;
  1105. }
  1106. LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
  1107. (p->len >= sizeof(struct tcp_hdr)));
  1108. tcphdr = (struct tcp_hdr *)p->payload;
  1109. tcphdr->src = htons(local_port);
  1110. tcphdr->dest = htons(remote_port);
  1111. tcphdr->seqno = htonl(seqno);
  1112. tcphdr->ackno = htonl(ackno);
  1113. TCPH_HDRLEN_FLAGS_SET(tcphdr, TCP_HLEN/4, TCP_RST | TCP_ACK);
  1114. tcphdr->wnd = PP_HTONS(TCP_WND);
  1115. tcphdr->chksum = 0;
  1116. tcphdr->urgp = 0;
  1117. #if CHECKSUM_GEN_TCP
  1118. tcphdr->chksum = inet_chksum_pseudo(p, local_ip, remote_ip,
  1119. IP_PROTO_TCP, p->tot_len);
  1120. #endif
  1121. TCP_STATS_INC(tcp.xmit);
  1122. snmp_inc_tcpoutrsts();
  1123. /* Send output with hardcoded TTL since we have no access to the pcb */
  1124. ip_output(p, local_ip, remote_ip, TCP_TTL, 0, IP_PROTO_TCP);
  1125. pbuf_free(p);
  1126. LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_rst: seqno %"U32_F" ackno %"U32_F".\n", seqno, ackno));
  1127. }
  1128. /**
  1129. * Requeue all unacked segments for retransmission
  1130. *
  1131. * Called by tcp_slowtmr() for slow retransmission.
  1132. *
  1133. * @param pcb the tcp_pcb for which to re-enqueue all unacked segments
  1134. */
  1135. void
  1136. tcp_rexmit_rto(struct tcp_pcb *pcb)
  1137. {
  1138. struct tcp_seg *seg;
  1139. struct tcp_seg *t0_head = NULL, *t0_tail = NULL; /* keep in unacked */
  1140. struct tcp_seg *t1_head = NULL, *t1_tail = NULL; /* link to unsent */
  1141. bool t0_1st = true, t1_1st = true;
  1142. if (pcb->unacked == NULL) {
  1143. return;
  1144. }
  1145. #if 1 /* by Snake: resolve the bug of pbuf reuse */
  1146. seg = pcb->unacked;
  1147. while (seg != NULL) {
  1148. if (seg->p->eb) {
  1149. if (t0_1st) {
  1150. t0_head = t0_tail = seg;
  1151. t0_1st = false;
  1152. } else {
  1153. t0_tail->next = seg;
  1154. t0_tail = seg;
  1155. }
  1156. seg = seg->next;
  1157. t0_tail->next = NULL;
  1158. } else {
  1159. if (t1_1st) {
  1160. t1_head = t1_tail = seg;
  1161. t1_1st = false;
  1162. } else {
  1163. t1_tail->next = seg;
  1164. t1_tail = seg;
  1165. }
  1166. seg = seg->next;
  1167. t1_tail->next = NULL;
  1168. }
  1169. }
  1170. if (t1_head && t1_tail) {
  1171. t1_tail->next = pcb->unsent;
  1172. pcb->unsent = t1_head;
  1173. }
  1174. pcb->unacked = t0_head;
  1175. #else
  1176. /* Move all unacked segments to the head of the unsent queue */
  1177. for (seg = pcb->unacked; seg->next != NULL; seg = seg->next);
  1178. /* concatenate unsent queue after unacked queue */
  1179. seg->next = pcb->unsent;
  1180. /* unsent queue is the concatenated queue (of unacked, unsent) */
  1181. pcb->unsent = pcb->unacked;
  1182. /* unacked queue is now empty */
  1183. pcb->unacked = NULL;
  1184. #endif
  1185. /* last unsent hasn't changed, no need to reset unsent_oversize */
  1186. /* increment number of retransmissions */
  1187. ++pcb->nrtx;
  1188. /* Don't take any RTT measurements after retransmitting. */
  1189. pcb->rttest = 0;
  1190. /* Do the actual retransmission */
  1191. tcp_output(pcb);
  1192. }
  1193. /**
  1194. * Requeue the first unacked segment for retransmission
  1195. *
  1196. * Called by tcp_receive() for fast retramsmit.
  1197. *
  1198. * @param pcb the tcp_pcb for which to retransmit the first unacked segment
  1199. */
  1200. void
  1201. tcp_rexmit(struct tcp_pcb *pcb)
  1202. {
  1203. struct tcp_seg *seg;
  1204. struct tcp_seg **cur_seg;
  1205. if (pcb->unacked == NULL) {
  1206. return;
  1207. }
  1208. /* Move the first unacked segment to the unsent queue */
  1209. /* Keep the unsent queue sorted. */
  1210. seg = pcb->unacked;
  1211. pcb->unacked = seg->next;
  1212. cur_seg = &(pcb->unsent);
  1213. while (*cur_seg &&
  1214. TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno), ntohl(seg->tcphdr->seqno))) {
  1215. cur_seg = &((*cur_seg)->next );
  1216. }
  1217. seg->next = *cur_seg;
  1218. *cur_seg = seg;
  1219. #if TCP_OVERSIZE
  1220. if (seg->next == NULL) {
  1221. /* the retransmitted segment is last in unsent, so reset unsent_oversize */
  1222. pcb->unsent_oversize = 0;
  1223. }
  1224. #endif /* TCP_OVERSIZE */
  1225. ++pcb->nrtx;
  1226. /* Don't take any rtt measurements after retransmitting. */
  1227. pcb->rttest = 0;
  1228. /* Do the actual retransmission. */
  1229. snmp_inc_tcpretranssegs();
  1230. /* No need to call tcp_output: we are always called from tcp_input()
  1231. and thus tcp_output directly returns. */
  1232. }
  1233. /**
  1234. * Handle retransmission after three dupacks received
  1235. *
  1236. * @param pcb the tcp_pcb for which to retransmit the first unacked segment
  1237. */
  1238. void
  1239. tcp_rexmit_fast(struct tcp_pcb *pcb)
  1240. {
  1241. if (pcb->unacked != NULL && !(pcb->flags & TF_INFR)) {
  1242. /* This is fast retransmit. Retransmit the first unacked segment. */
  1243. LWIP_DEBUGF(TCP_FR_DEBUG,
  1244. ("tcp_receive: dupacks %"U16_F" (%"U32_F
  1245. "), fast retransmit %"U32_F"\n",
  1246. (u16_t)pcb->dupacks, pcb->lastack,
  1247. ntohl(pcb->unacked->tcphdr->seqno)));
  1248. tcp_rexmit(pcb);
  1249. /* Set ssthresh to half of the minimum of the current
  1250. * cwnd and the advertised window */
  1251. if (pcb->cwnd > pcb->snd_wnd) {
  1252. pcb->ssthresh = pcb->snd_wnd / 2;
  1253. } else {
  1254. pcb->ssthresh = pcb->cwnd / 2;
  1255. }
  1256. /* The minimum value for ssthresh should be 2 MSS */
  1257. if (pcb->ssthresh < 2*pcb->mss) {
  1258. LWIP_DEBUGF(TCP_FR_DEBUG,
  1259. ("tcp_receive: The minimum value for ssthresh %"U16_F
  1260. " should be min 2 mss %"U16_F"...\n",
  1261. pcb->ssthresh, 2*pcb->mss));
  1262. pcb->ssthresh = 2*pcb->mss;
  1263. }
  1264. pcb->cwnd = pcb->ssthresh + 3 * pcb->mss;
  1265. pcb->flags |= TF_INFR;
  1266. }
  1267. }
  1268. /**
  1269. * Send keepalive packets to keep a connection active although
  1270. * no data is sent over it.
  1271. *
  1272. * Called by tcp_slowtmr()
  1273. *
  1274. * @param pcb the tcp_pcb for which to send a keepalive packet
  1275. */
  1276. void
  1277. tcp_keepalive(struct tcp_pcb *pcb)
  1278. {
  1279. struct pbuf *p;
  1280. struct tcp_hdr *tcphdr;
  1281. LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: sending KEEPALIVE probe to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
  1282. ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip),
  1283. ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip)));
  1284. LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: tcp_ticks %"U32_F" pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",
  1285. tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));
  1286. p = tcp_output_alloc_header(pcb, 0, 0, htonl(pcb->snd_nxt - 1));
  1287. if(p == NULL) {
  1288. LWIP_DEBUGF(TCP_DEBUG,
  1289. ("tcp_keepalive: could not allocate memory for pbuf\n"));
  1290. return;
  1291. }
  1292. tcphdr = (struct tcp_hdr *)p->payload;
  1293. #if CHECKSUM_GEN_TCP
  1294. tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip,
  1295. IP_PROTO_TCP, p->tot_len);
  1296. #endif
  1297. TCP_STATS_INC(tcp.xmit);
  1298. /* Send output to IP */
  1299. #if LWIP_NETIF_HWADDRHINT
  1300. ip_output_hinted(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP,
  1301. &(pcb->addr_hint));
  1302. #else /* LWIP_NETIF_HWADDRHINT*/
  1303. ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);
  1304. #endif /* LWIP_NETIF_HWADDRHINT*/
  1305. pbuf_free(p);
  1306. LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: seqno %"U32_F" ackno %"U32_F".\n",
  1307. pcb->snd_nxt - 1, pcb->rcv_nxt));
  1308. }
  1309. /**
  1310. * Send persist timer zero-window probes to keep a connection active
  1311. * when a window update is lost.
  1312. *
  1313. * Called by tcp_slowtmr()
  1314. *
  1315. * @param pcb the tcp_pcb for which to send a zero-window probe packet
  1316. */
  1317. void
  1318. tcp_zero_window_probe(struct tcp_pcb *pcb)
  1319. {
  1320. struct pbuf *p;
  1321. struct tcp_hdr *tcphdr;
  1322. struct tcp_seg *seg;
  1323. u16_t offset = 0;
  1324. u16_t len;
  1325. u8_t is_fin;
  1326. LWIP_DEBUGF(TCP_DEBUG,
  1327. ("tcp_zero_window_probe: sending ZERO WINDOW probe to %"
  1328. U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
  1329. ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip),
  1330. ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip)));
  1331. LWIP_DEBUGF(TCP_DEBUG,
  1332. ("tcp_zero_window_probe: tcp_ticks %"U32_F
  1333. " pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",
  1334. tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));
  1335. seg = pcb->unacked;
  1336. if(seg == NULL) {
  1337. seg = pcb->unsent;
  1338. } else {
  1339. struct ip_hdr *iphdr = NULL;
  1340. iphdr = (struct ip_hdr *)((char*)seg->p->payload + SIZEOF_ETH_HDR);
  1341. offset = IPH_HL(iphdr)*4;
  1342. offset += SIZEOF_ETH_HDR;
  1343. }
  1344. if(seg == NULL) {
  1345. return;
  1346. }
  1347. is_fin = ((TCPH_FLAGS(seg->tcphdr) & TCP_FIN) != 0) && (seg->len == 0);
  1348. /* we want to send one seqno: either FIN or data (no options) */
  1349. len = is_fin ? 0 : 1;
  1350. p = tcp_output_alloc_header(pcb, 0, len, seg->tcphdr->seqno);
  1351. if(p == NULL) {
  1352. LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: no memory for pbuf\n"));
  1353. return;
  1354. }
  1355. tcphdr = (struct tcp_hdr *)p->payload;
  1356. if (is_fin) {
  1357. /* FIN segment, no data */
  1358. TCPH_FLAGS_SET(tcphdr, TCP_ACK | TCP_FIN);
  1359. } else {
  1360. /* Data segment, copy in one byte from the head of the unacked queue */
  1361. struct tcp_hdr *thdr = (struct tcp_hdr *)seg->p->payload;
  1362. char *d = ((char *)p->payload + TCP_HLEN);
  1363. if (pcb->unacked == NULL)
  1364. pbuf_copy_partial(seg->p, d, 1, TCPH_HDRLEN(thdr) * 4);
  1365. else {
  1366. thdr = (struct tcp_hdr *)((char*)seg->p->payload + offset);
  1367. pbuf_copy_partial(seg->p, d, 1, TCPH_HDRLEN(thdr) * 4 + offset);
  1368. }
  1369. }
  1370. #if CHECKSUM_GEN_TCP
  1371. tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip,
  1372. IP_PROTO_TCP, p->tot_len);
  1373. #endif
  1374. TCP_STATS_INC(tcp.xmit);
  1375. /* Send output to IP */
  1376. #if LWIP_NETIF_HWADDRHINT
  1377. ip_output_hinted(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP,
  1378. &(pcb->addr_hint));
  1379. #else /* LWIP_NETIF_HWADDRHINT*/
  1380. ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);
  1381. #endif /* LWIP_NETIF_HWADDRHINT*/
  1382. pbuf_free(p);
  1383. LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: seqno %"U32_F
  1384. " ackno %"U32_F".\n",
  1385. pcb->snd_nxt - 1, pcb->rcv_nxt));
  1386. }
  1387. #endif /* LWIP_TCP */